#!/usr/local/bin/python import os, string, sys, glob, re, time, commands, cgi ######################################################## #This is a cgi script that makes a PNG plot of mesonet data, and #returns a simple web page linking the plot. #The script needs a better user interface to be useful. #Now it only accepts one argument (column number to plot) from the command line, e.g.: # plotmeso.cgi 6 #or from a browser: # plotmeso.cgi?6 #The default is to plot column 5 #The script desperately needs a friendly front end, and the use of CGI.pm #This script demonstrates the use of python to write an input file for gnuplot #and then to call gnuplot to make the appropriate plot. #To help you to understand this script, execute the script from #the command line and/or from the browser. Then look at the latest #three files made in the directory "visit". #Be sure to chmod a=rwx visit, so that httpd can write files there. ########################################################## pathtodata='/data/mesonet/mts/' #use this on Rossby #$pathtodata='datex/'; # use this with the sample data errlog='/usr/local/apache/logs/error_log' #use this on rossby gnuplot='/usr/local/bin/gnuplot' #full path to gnuplot pid=os.getpid() #use process I.D. later to make temp file names verbose=2; #set this to 0, 1 or 2 to see more messages #What follows is a kludge for rossby. When gnuplot is called by #the apache httpd to make a plot for this script, gnuplot cannot find #the libraries to run. On other machines this doesn't happen. So #we set an environment variable containing the path information: os.environ['LD_LIBRARY_PATH']=':/usr/lib:/usr/local/lib:/usr/openwin/lib:/usr/java/lib:/usr/lib/http:/usr/ucblib:${LD_LIBRARY_PATH}' print "Content-type: text/html\n" print "
\n"
if verbose>=2:
print "\nENV variables are now:\n" #print out for debugging purposes
for key in os.environ.keys():
print key, os.environ[key]
#print "****STRING =",string
#line=sys.stdin.readline()
#print "****LINE =", line
print "****QUERY STRING =", os.environ['QUERY_STRING']
The_Form = cgi.FieldStorage()
for name in The_Form.keys():
print "Input: " + name + " value: " + The_Form[name].value + "
"
print "THE VALUE IS", The_Form["var"].value
try:
col=string.atoi(The_Form["var"].value) #get cloumn number to plot from command line
except:
col=5 #the default if the above bombs
print "*****COL =",col
try:
stid=The_Form["id"].value
except:
stid="NORM" #get Norman data
stid=string.lower(stid) #make stid lower case for file name
if verbose>0:
print "lowercase stid is:"+stid
theglob=pathtodata+"*"+stid+".mts" #make filenames you are seeking with "wildcard" *
if verbose>0: print "will look for files: %s \n" % theglob
try:
a=glob.glob(theglob)
except:
print "no files found"
sys.exit()
a.sort()
if verbose>0:
for file in a:
print file
try:
day=string.atoi(The_Form["when"].value) #get cloumn number to plot from command line
except:
day=-1 #the default if the above bombs
file=a[day]; #use last file alphabetically, which is latest
if verbose>0: print "will use",file
meso=open(file,'r') #open mesonet data file for reading
tmpdata='visit/'+`pid`+'tmp.dat' #make tmp date file
tmpgnu= 'visit/'+`pid`+'tmp.gnu' #make tmp file name for gnuplot commands
tmppng= 'visit/'+`pid`+'tmp.png' #make tmp file name for png image
if verbose>0: print "creating tmp file:",tmpdata
tmp=open(tmpdata,'w')
h1=meso.readline() #read first line from mesonet data file, which is a comment
h2=meso.readline() #read second line from mesonet data file, which is a comment
h3=meso.readline() #read third line from mesonet data file, which is a comment
tmp.write("#%s#%s#%s" % (h1, h2,h3)) #print comment lines to tmp data file, but put a leading #
for line in meso.readlines(): # read the rest of lines
line=re.sub('-99\d','-998',line)
line=re.sub('-99\d.0*','-998',line)
tmp.write(line) #print out line to tmp data file
tmp.close()
h2=string.strip(h2) #strip of leading and trail whitespace and trailing \n
h3=string.strip(h3)
labels=string.split(h3)
label=labels[col-1]
stid=string.upper(stid)
if verbose>0: print "got label",label,"from col=",col
if verbose>0: print "will make gnuplot file:",tmpgnu
gnu=open(tmpgnu,'w')
#makeing a temporary gnuplot script:
try:
pltype=The_Form["type"].value
except:
pltype="linespoints"
pltype=string.strip(pltype)
print ("PLTYPE=",pltype)
gnu.write("""
set title '%s'
set missing "-998"
set term png color
set output '%s'
plot [0:1440] '%s' using 3:%s t '%s %s' with %s
set output
""" % (h2,tmppng,tmpdata,`col`,stid,label,pltype))
gnu.close()
if verbose>0 :
print "this is what was written in",tmpgnu
for line in open(tmpgnu,'r').readlines():
print line,
command=gnuplot+" "+tmpgnu #make command line to execute gnuplot
if verbose>0: print "\nwill try command:",command
os.system(command) #executes with unix, in this case it is a call to gnuplot
if verbose>0:
theglob= 'visit/'+`pid`+'*'
print "\nlook for files matching made by this process: "+theglob
nfiles= glob.glob(theglob)
for f in nfiles:
print f
print "\ncheck age of files in directory visit and clean out old files made by others:\n"
visitfiles=glob.glob('visit/*.*') #get a list of files in visit directory
ct=time.time()
for f in visitfiles:
stats=os.stat(f)
ctime=ct-stats[-1]
if verbose>0: print f,ctime
if (ctime>1000.): os.remove(f) #clean files older than 1000 s
if verbose>=2: # execute unix command and put output in errmess
errmess=commands.getoutput("/usr/bin/tail -10 "+errlog)
print "\ntail of messages in error log "+errlog+":\n",errmess
print "end of messages in "+errlog
print "\nYou should see a plot here. Set verbose=1 or 2 to see more messages.\n"
print "If it is soon after 0Z, you might not see much data.\n"
print '