#!/usr/bin/python
# Note: the cgi-bin area is at:
# /home/www.gb.nrao.edu/active/cgi-bin/
import cgi
import sys
import string
import math
import os
import MySQLdb
import datetime
global rcvr, beam, polty, rrcvr, ppt, rfreq, datesel, pdate, butval, rcvrlist, fsmooth
# from gbt.ygor.getConfigValue import *
#--------------------------------------------------
# database access parameters -
# dbhost = getConfigValue("localdb", "DatabaseHost")
dbhost = "gbtdata.gbt.nrao.edu"
rcvrdb = "receivers"
dbpassword = "showmedata"
dbuser = "turtle_query"
#--------------------------------------------------
rcvr="unknown"
beam = 0
polty = "unk"
rrcvr = "unk"
ppt = "unk"
rfreq = 0
fsmooth = "10"
datesel=""
pdate=datetime.date(1999,01,01)
butval=""
rcvrlist = ['Rcvr_342 (MHz)', 'Rcvr_450 (MHz)', 'Rcvr_600 (MHz)', 'Rcvr_800 (MHz)', \
'Rcvr_1070 (MHz)', 'Rcvr1_2 (GHz)', 'Rcvr2_3 (GHz)', 'Rcvr4_6 (GHz)', \
'Rcvr8_10 (GHz)', 'Rcvr12_18 (GHz)', 'Rcvr18_26 (GHz)', 'RcvrArray18_26', \
'Rcvr26_40 (GHz)','Rcvr40_52 (GHz)','Rcvr68_92 (GHz)']
#-------------------------------------------------
# get list of channels and list of dates.
#-------------------------------------------------
def getchans( rcvrname, rfreq ) :
chlist = [] # channel list
dlist = [] # date slist
blist = [] # beam list
plist = [] # pol list
if rcvrname == "unk" : return(dlist)
try :
db = MySQLdb.connect(passwd=dbpassword, db=rcvrdb,
host=dbhost, user=dbuser)
cursor = db.cursor()
except :
print "Cannot open the database: ", rcvrdb
return
q = "select id, date, receiver, channel, beam, polarization from measurement where receiver = '%s' " % (rcvrname)
cursor.execute(q)
fieldValues = cursor.fetchall()
nvals = len(fieldValues)
cursor.close()
for fv in fieldValues :
# print fv[0], fv[1], fv[2], fv[3], fv[4], fv[5]
if rfreq == 0 :
if fv[1] not in dlist : dlist.append( fv[1])
else :
if fv[3].find( str(rfreq) ) > 0 :
if fv[1] not in dlist : dlist.append( fv[1])
# if fv[3] not in chlist : chlist.append( fv[3])
# if fv[4] not in blist : blist.append( fv[4])
# if fv[5] not in plist : plist.append( fv[5])
return (dlist) # return date list and channel list
#-------------------------------------------------
#-------------------------------------------------
# smooth data to specified frequency grid
# fsm is new grid size in MHz
def resmooth(fsm, fdat) :
if len(fdat) < 10 : return None
fd1 = fdat[0]
fdn = fdat[-1]
f0 = 1000.0 * fd1[0] # in MHz
fn = 1000.0 * fdn[0]
q, md = divmod(f0,fsm)
f00 = (q+1)*fsm
nng = int(math.floor((fn-f00)/fsm) )
print "
Smoothing to %.2f MHz, start=%.2f, %d points\n" % (fsm, f00,nng)
# create a new array (freq, val, wt)
ndat = []
for i in range(nng+2) :
fxval = f00+i*fsm
ndat.append( (fxval, 0.0, 0.0))
for fd in fdat :
fx = 1000.0 * fd[0]
nx0 = int(math.floor((fx-fsm - f00)/fsm))
if nx0 < 0 : nx0=0
fx1 = fx + fsm
nx1 = int(math.ceil((fx+fsm - f00)/fsm))
if nx1 > nng+1 : nx1=nng+1
tv = fd[2]
# sum into bins, use weight = 1/(freq diff)
for j in range(nx0,nx1) :
nnr = ndat[j]
nnfrq = nnr[0]
ddf = abs(nnfrq - fx)
if ddf <= 0 : wwt = 2.0
else :
wwt = 1.0/ddf
if wwt >= 2.0 : wwt = 2.0
tval = nnr[1] + wwt*tv
wval = nnr[2] + wwt
ndat[j] = (nnfrq,tval,wval)
newdat = []
for nd in ndat :
frq = 0.001*nd[0]
tvv = nd[1]
wtt = nd[2]
if wtt > 0 :
tvavg=tvv/wtt
newdat.append( (frq,1.0, tvavg))
return newdat
#-------------------------------------------------
#-------------------------------------------------
#-------------------------------------------------
# reqdate is a datetime object
# if beamsel is None it uses beam 1
# for VLBI, gets just the R and L channels
#-------------------------------------------------
def writevtcals(rcvrname, reqdate=None, beamsel=None, polmode=None, rfreq=rfreq, \
fsmooth=fsmooth) :
sbeam = 1
if beamsel != None : sbeam = int(beamsel)
pmode = 'Circ'
if polmode == 'Lin' : pmode = polmode
fvsel = []
dslist = []
testpol1 = 'L'
testpol2 = 'R'
if pmode == 'Lin' :
testpol1 = 'X'
testpol2 = 'Y'
print "
Receiver:", rcvrname
print " req date = ", reqdate, " beam=",sbeam, " pol=", polmode, " f_smooth=",fsmooth
try :
db = MySQLdb.connect(passwd=dbpassword, db=rcvrdb,
host=dbhost, user=dbuser)
cursor = db.cursor()
except :
print "
Cannot open the database: ", rcvrdb
return
try:
q = "select id, date, receiver, channel, beam, polarization from measurement where receiver = '%s' and beam = '%d' " % (rcvrname,sbeam)
#print "
q=", q
cursor.execute(q)
fieldValues = cursor.fetchall()
nvals = len(fieldValues)
dslist = [reqdate]
except :
print "
Cannot read from the database: ", rcvrdb, dbhost
return
# print "
using date = ", dslist[0]
rd = dslist[0]
fvsel = []
for fv in fieldValues :
if (fv[1] == rd ) : # get just the entries matching the requested date
# if prime focus, select required frequency
if rfreq == 0 :
bmpol = "%s%d" % (fv[5],fv[4])
fvsel.append((fv[0], bmpol, fv[5], fv[3]))
else :
if fv[3].find(str(rfreq)) > 0 :
bmpol = "%s%d" % (fv[5],fv[4])
fvsel.append((fv[0], bmpol, fv[5], fv[3]))
rrdat = []
lldat = []
for fvs in fvsel :
print "
fvs=",fvs
q2 = "select frequency, temperature, low_cal, high_cal, id from tcal where id = '%d' " % (fvs[0])
cursor.execute(q2)
fvs2 = sorted(cursor.fetchall())
nfvs2 = len(fvs2)
if fvs[2] == testpol2 : rrdat = fvs2
if fvs[2] == testpol1 : lldat = fvs2
if len(rrdat) <= 1 and len(lldat) <= 1 :
print "
Sorry, no data for this rcvr/date/pol"
return
cursor.close()
# get frequency spacing
if len(rrdat)>0 :
xx1 = rrdat[0]
xx2 = rrdat[1]
rrspace = 1000.0*(xx2[0] - xx1[0])
else : rrspace = 0.0
if len(lldat)>0 :
xx1 = lldat[0]
xx2 = lldat[1]
llspace = 1000.0*(xx2[0] - xx1[0])
else : llspace = 0.0
print "
Freq grid(MHz) %8.3f XL, %8.3f YR" % (llspace,rrspace)
fsmooth1 = float(fsmooth)
print "fsmooth1=",fsmooth1
newlldat = None
newrrdat = None
if llspace < 0.6*fsmooth1 : newlldat = resmooth(fsmooth1, lldat)
if rrspace < 0.6*fsmooth1 : newrrdat = resmooth(fsmooth1, rrdat)
if newlldat != None : lldat = newlldat
if newrrdat != None : rrdat = newrrdat
compdate = "%4d%02d%02d" % (rd.year,rd.month,rd.day)
outfilename = "%s_%s%d_%s.txt" % (rcvrname, pmode, sbeam,compdate)
# print "
outdata=",outfilename, len(rrdat), len(lldat)
print "
Writing ", len(lldat), " XL, and ", len(rrdat), " YR cals"
if len(lldat) != len(rrdat) :
print "
Number of RCPs not the same as LCPs: list probably bogus!"
# try dumping the data into the web page
wmode = "w"
# outf = open(outfilename, wmode)
# print header lines
# outf.write( "! Tcals for the %s receiver at GB\n" % (rcvrname))
print "
" print "
' # if len(tcallist) > 0 : # print rcvr, beam, polty, ppt, rrcvr, rfreq, datesel, butval def writepage(tst=None) : global rcvr, beam, polty, rrcvr, ppt, rfreq, datesel, pdate, butval, rcvrlist, fsmooth tcallist = [] dlist = [] print "Content-Type: text/html" print print "\n" print "
"
if tst == None :
try :
form = cgi.FieldStorage()
retrieve1(form)
dlist = getchans(rrcvr, rfreq)
# if form["butpress"].value == "get dates" :
putnewpage(dlist,tcallist)
# if butval == "get dates" :
# print "got ", butval
if butval == "get tcals" :
tcallist = []
# print "got ", butval
writevtcals(rrcvr, reqdate=pdate, beamsel=beam, polmode=ppt,\
rfreq=rfreq,fsmooth=fsmooth )
except :
print "\n
Some HTML Error happened."
else :
# rrcvr='Rcvr8_10'
# rcvr='Rcvr8_10'
rrcvr='Rcvr26_40'
rcvr='Rcvr8_10'
# rrcvr='Rcvr1_2'
# rcvr='Rcvr1_2'
fsmooth = "1"
rfreq=0
dlist = getchans(rrcvr,rfreq)
print dlist
# set test values
beam=1
polty='Circular'
ppt='Circ'
# datesel=dlist[1]
pdate=dlist[-1]
butval='get dates'
print 'pdate=',pdate
putnewpage(dlist,tcallist)
writevtcals(rrcvr, reqdate=pdate, beamsel=beam, polmode=ppt,rfreq=rfreq,fsmooth=fsmooth )
print ""
print ""
#-----------------------------------------------------------
# main program
#-----------------------------------------------------------
sys.stderr = sys.stdout
writepage(None)