#!/usr/bin/python import numpy as np import cgi, cgitb cgitb.enable() import gzip, os, sys, datetime, re, time, urllib, collections import MySQLdb as mysqldb ## Set timezone first os.environ['TZ'] = 'Etc/Greenwich' time.tzset() ## Get arguments from storage or use default if any errors are thrown form = cgi.FieldStorage() try: net = int(form["net"].value) except: net = 1 try: zoom = int(form["zoom"].value) except: zoom = 9 try: latcen = float(form["latcen"].value) except: latcen = 40.76623 try: loncen = float(form["loncen"].value) except: loncen = -111.84755 try: limitstring = form["limit"].value except: limitstring = 'None' if(len(limitstring) == 0): limitstring = 'None' if(net > 0): latn = latcen + 3 lats = latcen - 3 lone = loncen + 5 lonw = loncen - 5 else: latn = latcen + 1.25 lats = latcen - 1.25 lone = loncen + 1.5 lonw = loncen - 1.5 templimit_active = 0 templimit_low = -1000.0 templimit_high = 1000.0 relhlimit_active = 0 relhlimit_low = -1000.0 relhlimit_high = 1000.0 skntlimit_active = 0 skntlimit_low = -1000.0 skntlimit_high = 1000.0 gustlimit_active = 0 gustlimit_low = -1000.0 gustlimit_high = 1000.0 if(limitstring != 'None'): if(',' in limitstring): limitlist = limitstring.split(',') else: limitlist = [limitstring] for limittype in limitlist: if(limittype[0] == 'T'): templimit_active = 1 if(limittype[1] == 'G'): try: templimit_low = float(limittype[2:]) except: skip = 1 if(limittype[1] == 'L'): try: templimit_high = float(limittype[2:]) except: skip = 1 if(limittype[0] == 'R'): relhlimit_active = 1 if(limittype[1] == 'G'): try: relhlimit_low = float(limittype[2:]) except: skip = 1 if(limittype[1] == 'L'): try: relhlimit_high = float(limittype[2:]) except: skip = 1 if(limittype[0] == 'S'): skntlimit_active = 1 if(limittype[1] == 'G'): try: skntlimit_low = float(limittype[2:])*0.868976 except: skip = 1 if(limittype[1] == 'L'): try: skntlimit_high = float(limittype[2:])*0.868976 except: skip = 1 if(limittype[0] == 'G'): gustlimit_active = 1 if(limittype[1] == 'G'): try: gustlimit_low = float(limittype[2:])*0.868976 except: skip = 1 if(limittype[1] == 'L'): try: gustlimit_high = float(limittype[2:])*0.868976 except: skip = 1 ## Dictionary of variable options and what what color lists to use... allvarlist = '3,5,6,7,8,14,15,35,45,53,60,71,73'; dbvars = { 3: 'TMPF', 5: 'RELH', 6: 'SKNT', 7: 'DRCT', 8: 'GUST', 14: 'SOLR', 35: 'QFLG', 15: 'TSOI', 45: 'TRD', 53: 'VSBY', 60: 'GUST', 71: 'VOLT', 4000: 'CDRT', 4001: 'DATTIM', 4002: 'SKNTINT', 4003: 'DWPF' } networkoptions = { 0: 'All Networks' } ## Determine time variables! minback = 90 curdattim = datetime.datetime.strftime(datetime.datetime.utcnow(),'%Y%m%d%H%M') nowyr = datetime.datetime.strftime(datetime.datetime.utcnow(),'%Y') olddattim = datetime.datetime.strftime(datetime.datetime.utcnow()-datetime.timedelta(minutes=minback),'%Y%m%d%H%M') curtimstr = datetime.datetime.strftime(datetime.datetime.utcnow(),'%Y-%m-%d %H:%MZ') carddrctlist = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] carddrctmax = [11.25,33.75,56.25,78.75,101.25,123.75,146.25,168.75,191.25,213.75,236.25,258.75,281.25,303.75,326.25,348.75,360.01] carddrctmin = [ 0.00,11.25,33.75,56.25, 78.75,101.25,123.75,146.25,168.75,191.25,213.75,236.25,258.75,281.25,303.75,326.25,348.75] ## Connect to database and get data! dbconnection = None try: stninfoids = [] dbconnection = mysqldb.connect('meso2.chpc.utah.edu','metwww','wind','mesobest') dbcur = dbconnection.cursor() dbquerynets = 'SELECT ID,MNAME FROM MNET WHERE ID<1000' dbcur.execute(dbquerynets) mesonets = dbcur.fetchall() for netrow in mesonets: networkoptions[netrow[0]] = netrow[1] if(net > 0): dbquerymeta = 'SELECT ID,STID,LATITUDE,LONGITUDE,MNET_ID FROM STNINFO WHERE LATITUDE > '+str(lats)+' AND LATITUDE < '+str(latn)+' AND LONGITUDE > '+str(lonw)+' AND LONGITUDE < '+str(lone)+' AND MNET_ID ='+str(net) else: dbquerymeta = 'SELECT ID,STID,LATITUDE,LONGITUDE,MNET_ID FROM STNINFO WHERE LATITUDE > '+str(lats)+' AND LATITUDE < '+str(latn)+' AND LONGITUDE > '+str(lonw)+' AND LONGITUDE < '+str(lone)+' AND MNET_ID < 1000' dbcur.execute(dbquerymeta) metadata = dbcur.fetchall() dbquerydata = 'SELECT STNINFO_ID,DATTIM,VALUE,VARIABLE_ID FROM DATA_LATEST WHERE DATTIM >= '+str(olddattim)+' AND DATTIM <= '+str(curdattim)+' AND VARIABLE_ID in ('+allvarlist+') ORDER BY VARIABLE_ID' dbcur.execute(dbquerydata) obsdata = dbcur.fetchall() dbconnection.close() except mysqldb.Error, e: skip = 1 ## Now organize lists! # print "Content-Type: text/html\n" stninfoidlist = [] latdict = {} londict = {} mnetdict = {} stiddict = {} showstndict = {} allstninfoidlist = [] allstidlist = [] alllatlist = [] alllonlist = [] allmnetlist = [] allshowlist = [] obsdatalist = {} stndattims = {} obsfloattimelist = {} obsdatalist = {} stndattims = {} for metarow in metadata: stninfoidlist.append(metarow[0]) stiddict[metarow[0]] = metarow[1] latdict[metarow[0]] = metarow[2] londict[metarow[0]] = metarow[3] mnetdict[metarow[0]] = metarow[4] if(limitstring != 'None'): showstndict[metarow[0]] = 0 else: showstndict[metarow[0]] = 1 if(len(obsdata) > 0): for ob in obsdata: obval = ob[2] if(ob[3] == 3 and templimit_active == 1): if(obval >= templimit_low and obval <= templimit_high): showstndict[ob[0]] = 1 if(ob[3] == 5 and relhlimit_active == 1): if(obval >= relhlimit_low and obval <= relhlimit_high): showstndict[ob[0]] = 1 if(ob[3] == 6 and skntlimit_active == 1): if(obval >= skntlimit_low and obval <= skntlimit_high): showstndict[ob[0]] = 1 if(ob[3] == 8 and gustlimit_active == 1): if(obval >= gustlimit_low and obval <= gustlimit_high): showstndict[ob[0]] = 1 if(ob[3] == 73): stndattims[ob[0]] = ob[1] if(olddattim >= ob[1] and ob[0] in stninfoidlist): allstninfoidlist.append(ob[0]) allstidlist.append(stiddict[ob[0]]) alllatlist.append(latdict[ob[0]]) alllonlist.append(londict[ob[0]]) allmnetlist.append(mnetdict[ob[0]]) allshowlist.append(showstndict[ob[0]]) for key in dbvars: obsdatalist[dbvars[key]] = ['NA'] * len(allstninfoidlist) for ob in obsdata: if(ob[0] in allstninfoidlist and ob[0] in stndattims and ob[3] in dbvars and ob[1] == stndattims[ob[0]]): mylistindex = allstninfoidlist.index(ob[0]) obval = ob[2] obsdatalist['DATTIM'][mylistindex] = str(ob[1])[0:8]+'/'+str(ob[1])[8:12]+'Z' if(ob[3] == 8): obsfloattimelist[mylistindex] = float(ob[1]) if(ob[3] == 6): if(obval > 0): if(obval >= 5): skntint = ("%d" % ((int(obval)/5)*5)) else: skntint = '1' else: if(obval == 0): skntint = '0' else: skntint = 'v' obsdatalist['SKNTINT'][mylistindex] = skntint if(ob[3] == 6 or ob[3] == 8 or ob[3] == 60): obval = obval * 1.15077945 if(ob[3] == 7): for k in range(0,len(carddrctlist)): if(obval >= carddrctmin[k] and obval < carddrctmax[k]): obdrct = carddrctlist[k] obsdatalist['CDRT'][mylistindex] = obdrct if(ob[3] == 35): if(int(obval) == -1): obval = '#FF0000' elif(int(obval) == 1): obval = '#999900' else: obval = '#000000' if(ob[3] == 60): try: if(float(ob[1]) >= obsfloattimelist[mylistindex]): if(obval > float(obsdatalist[dbvars[ob[3]]][mylistindex])): obsdatalist[dbvars[ob[3]]][mylistindex] = ("%d" % np.around(obval,0)) except: skip = 1 else: try: if(ob[3] == 71): obsdatalist[dbvars[ob[3]]][mylistindex] = ("%.2f" % obval) else: obsdatalist[dbvars[ob[3]]][mylistindex] = ("%d" % np.around(obval,0)) except: obsdatalist[dbvars[ob[3]]][mylistindex] = obval for i in range(0,len(obsdatalist['TMPF']),1): if(obsdatalist['TMPF'][i] != 'NA' and obsdatalist['RELH'][i] != 'NA'): tmpc = (5./9.)*(float(obsdatalist['TMPF'][i]) - 32) es = 6.11*(10**((7.5*tmpc)/(237.7+tmpc))) e = es*(float(obsdatalist['RELH'][i])/100.) if(e != 0): alogs = 0.43429*np.log(e) - 0.43429*np.log(6.11) dwpf = (237.7*alogs)/(7.5-alogs) dwpf = (dwpf*(9./5.))+32 obsdatalist['DWPF'][i] = ("%d" % np.around(dwpf,0)) else: obsdatalist['DWPF'][i] = obsdatalist['TMPF'][i] # height: 900px; print "Content-Type: text/html\n" print'''