Ignore:
Timestamp:
Oct 29, 2012, 11:06:27 AM (12 years ago)
Author:
aslmd
Message:

UTIL PYTHON. MCD online interface. addressed the TODO list of rev 812, plus other small improvements. this version was sent to beta-testers in the euromars list.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UTIL/PYTHON/mcd/proto/cgi-bin/mcdcgi.py

    r812 r821  
    1717import cgi, cgitb
    1818import numpy as np
    19 #from mcd import mcd
    2019from modules import *
    2120from modules import mcd
     
    2423import os as daos
    2524import matplotlib.pyplot as mpl
    26 from PIL import Image
    27 
    28 
    2925
    3026### a function to read HTML arguments for coordinates
    3127def gethtmlcoord(userinput,defmin,defmax):
    32    # accepted separators
    33    separators = [":",";",",","/"]
     28   import string
     29   # accepted separators. the symbol - should always be last.
     30   separators = [":",";",",","/","_"]
    3431   # initial values
    3532   val = -9999. ; vals = None ; vale = None ; foundinterv = False
    3633   if userinput == None:   userinput = "1"
    37    # the main work. either all -- or an interval -- or a single value.
     34   # the main work. either all, or an interval, or a single value.
     35   # ... all
    3836   if userinput == "all":  isfree = 1 ; vals = defmin ; vale = defmax ; foundinterv = True
     37   # ... an interval
    3938   else:
    4039       for sep in separators:
     40         if not foundinterv:
    4141           isfree = 1 ; ind = userinput.find(sep)
    4242           if ind != -1: vals = float(userinput[:ind]) ; vale = float(userinput[ind+1:]) ; foundinterv = True
    43    if not foundinterv: isfree = 0 ; val = float(userinput)
     43   # ... a single value (or an error)
     44   if not foundinterv:
     45       # treat the difficult case of possible - separator
     46       test = userinput[1:].find("-") # at this stage:
     47                                      # * if - is found in the first position, it could not be a separator
     48                                      # * if - found at positions > 0, it must be considered as a separator
     49       if test != -1:
     50         isfree = 1 ; ind=test+1
     51         vals = float(userinput[:ind]) ; vale = float(userinput[ind+1:]) ; foundinterv = True
     52       else:
     53         # check if input is valid (each character is numeric or -)
     54         for char in userinput:
     55            if char not in string.digits:
     56               if char not in ["-","."]: userinput="yorgl"
     57         # either we are OK. if we are not we set isfree to -1.
     58         if userinput != "yorgl":  isfree = 0 ; val = float(userinput)
     59         else:                     isfree = -1
    4460   # return values
    4561   return isfree, val, vals, vale
    4662
     63# set an errormess variable which must stay to None for interface to proceed
     64errormess = ""
    4765
    4866# for debugging in web browser
     
    5371
    5472# create a MCD object
    55 #query = mcd()
    56 query=mcd.mcd() #FG: import from module mcd
     73query=mcd.mcd()
    5774
    5875# Get the kind of vertical coordinates and choose default behavior for "all"
    5976try: query.zkey = int(form.getvalue("zkey"))
    6077except: query.zkey = int(3)
    61 if query.zkey == 2:    minxz = -5000.   ; maxxz = 100000.
    62 elif query.zkey == 3:  minxz = 0.       ; maxxz = 120000.
    63 elif query.zkey == 5:  minxz = -5000.   ; maxxz = 100000.
    64 elif query.zkey == 4:  minxz = 1000.    ; maxxz = 0.001
     78if query.zkey == 2:    minxz = -5000.   ; maxxz = 150000.
     79elif query.zkey == 3:  minxz = 0.       ; maxxz = 250000.
     80elif query.zkey == 5:  minxz = -5000.   ; maxxz = 150000.
     81elif query.zkey == 4:  minxz = 1.e3     ; maxxz = 1.e-6
    6582elif query.zkey == 1:  minxz = 3396000. ; maxxz = 3596000.
    6683
     
    7087isloctfree, query.loct, query.locts, query.locte = gethtmlcoord( form.getvalue("localtime"),    0.,  24. )
    7188isaltfree,  query.xz,   query.xzs,   query.xze   = gethtmlcoord( form.getvalue("altitude"),  minxz, maxxz)
    72 
    73 sumfree = islatfree + islonfree + isloctfree + isaltfree
    74 if sumfree > 2: exit() ## only 1D or 2D plots for the moment
    7589
    7690try: query.datekey = int(form.getvalue("datekeyhtml"))
     
    8498    query.loct = 0.
    8599
     100# Prevent the user from doing bad
     101badinterv = (islatfree == -1) or (islonfree == -1) or (isloctfree == -1) or (isaltfree == -1)
     102if badinterv:
     103    errormess = errormess+"<li>Bad syntax. Write a value (or) a range val1;val2 (or) 'all'. Separator shall be either ; : , / _ "
     104badls = (query.datekey == 1 and (query.xdate < 0. or query.xdate > 360.))
     105if badls:
     106    errormess = errormess+"<li>Solar longitude must be between 0 and 360."
     107badloct = (isloctfree == 0 and query.loct > 24.) \
     108       or (isloctfree == 1 and (query.locts > 24. or query.locte > 24.)) \
     109       or (isloctfree == 0 and query.loct < 0.) \
     110       or (isloctfree == 1 and (query.locts < 0. or query.locte < 0.))
     111if badloct:
     112    errormess = errormess+"<li>Local time must be less than 24 martian hours (and not a negative number)."
     113badlat = (islatfree == 0 and abs(query.lat) > 90.) \
     114      or (islatfree == 1 and (abs(query.lats) > 90. or abs(query.late) > 90.))
     115if badlat:
     116    errormess = errormess+"<li>Latitude coordinates must be between -90 and 90."
     117badlon = (islonfree == 0 and abs(query.lon) > 360.) \
     118      or (islonfree == 1 and (abs(query.lons) > 360. or abs(query.lone) > 360.))
     119if badlon:
     120    errormess = errormess+"<li>Longitude coordinates must be between -360 and 360."
     121badalt = (isaltfree == 0 and (query.zkey in [3]) and query.xz < 0.) \
     122      or (isaltfree == 1 and (query.zkey in [3]) and (query.xzs < 0. or query.xze < 0.))
     123if badalt:
     124    errormess = errormess+"<li>Vertical coordinates must be positive when requesting altitude above surface."
     125badalt2 = (isaltfree == 0 and (query.zkey in [1,4]) and query.xz <= 0.) \
     126      or (isaltfree == 1 and (query.zkey in [1,4]) and (query.xzs <= 0. or query.xze <= 0.))
     127if badalt2:
     128    errormess = errormess+"<li>Vertical coordinates must be <b>strictly</b> positive when requesting pressure levels or altitude above Mars center."
     129badalt3 = (isaltfree == 0 and query.zkey == 4 and query.xz > 1500.) \
     130       or (isaltfree == 1 and query.zkey == 4 and min(query.xzs,query.xze) > 1500.)
     131if badalt3:
     132    errormess = errormess+"<li>Pressure values larger than 1500 Pa are unlikely to be encountered in the Martian atmosphere."
     133badrange = (isloctfree == 1 and query.locts == query.locte) \
     134        or (islatfree == 1 and query.lats == query.late) \
     135        or (islonfree == 1 and query.lons == query.lone) \
     136        or (isaltfree == 1 and query.xzs == query.xze)
     137if badrange:
     138    errormess = errormess+"<li>One or several coordinate intervals are not... intervals. Set either a real range or an unique value."
     139
     140# Get how many free dimensions we have
     141sumfree = islatfree + islonfree + isloctfree + isaltfree
     142if sumfree >= 3: errormess = errormess + "<li>3 or more free dimensions are set... but only 1D and 2D plots are supported!"
     143
     144# Get additional parameters
    86145try: query.hrkey = int(form.getvalue("hrkey"))
    87146except: query.hrkey = int(1)
     
    91150#        self.seedin    = 1  #random number generator seed (unused if perturkey=0)
    92151#        self.gwlength  = 0. #gravity Wave wavelength (unused if perturkey=0)
     152try: query.colorm = form.getvalue("colorm")
     153except: query.colorm = "jet"
    93154
    94155# Get variables to plot
     
    98159var4 = form.getvalue("var4")
    99160
    100 # fg: vartoplot is not None without form values
    101 # vartoplot = [var1]
    102161# fg: init var as with form values
    103162if var1 == None: var1="t"
    104 #if var2 == None: var2="p"
    105163
    106164vartoplot = []
     
    114172else:              iswindlog = False
    115173isfixedlt = form.getvalue("isfixedlt")
    116 if isfixedlt == "on": input_fixedlt=True
    117 else:                 input_fixedlt=False 
    118 
    119 # reference name (to test which figures are already in the database)
    120 reference = query.getnameset()+str(var1)+str(var2)+str(var3)+str(var4)+str(iswind)+str(isfixedlt)
    121 figname = '../img/'+reference+'.png'
    122 txtname = '../txt/'+reference
    123 testexist = daos.path.isfile(figname)
    124 
    125 # extract data from MCD if needed
    126 if not testexist:
     174if isfixedlt == "on": query.fixedlt=True
     175else:                 query.fixedlt=False 
     176iszonmean = form.getvalue("zonmean")
     177if iszonmean  == "on": query.zonmean=True
     178else:                  query.zonmean=False
     179
     180### now, proceed...
     181if errormess == "":
     182
     183 # reference name (to test which figures are already in the database)
     184 reference = query.getnameset()+str(var1)+str(var2)+str(var3)+str(var4)+str(iswind)+str(isfixedlt)+str(iszonmean)+query.colorm
     185 figname = '../img/'+reference+'.png'
     186 txtname = '../txt/'+reference+'.txt'
     187 testexist = daos.path.isfile(figname)
     188
     189 # extract data from MCD if needed
     190 if not testexist:
    127191
    128192  ### 1D plots
     
    130194
    131195    ### getting data
    132     if isloctfree == 1:         query.diurnal(nd=24)
     196    if isloctfree == 1:         query.diurnal(nd=25)
    133197    elif islonfree == 1:        query.zonal(nd=64)
    134198    elif islatfree == 1:        query.meridional(nd=48)
     
    139203    query.getascii(vartoplot,filename=txtname)
    140204    query.htmlplot1d(vartoplot,figname=figname)
    141     #mpl.savefig("img/temp.png",dpi=85,bbox_inches='tight',pad_inches=0.25)
    142     #Image.open("../img/temp.png").save(figname,'JPEG')
    143205
    144206  ### 2D plots
     
    146208
    147209    ### getting data
    148     if islatfree == 1 and islonfree == 1:       
    149         query.htmlmap2d(vartoplot,incwind=iswindlog,fixedlt=input_fixedlt,figname=figname)
    150         #mpl.savefig("img/temp.png",dpi=110,bbox_inches='tight',pad_inches=0.4)
    151         #Image.open("img/temp.png").save(figname,'JPEG') ##lighter images   
    152         ### http://www.pythonware.com/library/pil/handbook/introduction.htm
    153     elif isaltfree == 1 and islonfree == 1:     
    154         query.htmlplot2d(vartoplot,fixedlt=input_fixedlt,figname=figname)
    155     elif isaltfree == 1 and islatfree == 1:     
    156         query.htmlplot2d(vartoplot,fixedlt=input_fixedlt,figname=figname)
    157     else:
    158         exit() 
     210    if islatfree == 1 and islonfree == 1:     query.htmlmap2d(vartoplot,incwind=iswindlog,figname=figname)
     211    else:                                     query.htmlplot2d(vartoplot,figname=figname)
     212
     213#### NOW WRITE THE HTML PAGE TO USER
    159214
    160215## This is quite common
     
    172227#print "<br />"
    173228
    174 
    175 #print "<a href='../index.html'>Click here to start a new query</a><br />"
    176 #print "<hr>"
    177 
    178229## Now the part which differs
    179 if sumfree == 0:        query.update() ; query.htmlprinttabextvar(vartoplot)  #query.printmeanvar()
    180 elif sumfree == 2:      print "<img src='"+figname+"'><br />"
    181 elif sumfree == 1:     
    182     print "<a href='"+txtname+"'>Click here to download an ASCII file containing data</a><br />"
    183     print "<hr>"
    184     print "<img src='"+figname+"'><br />"
    185 else:                   print "<h1>ERROR : sumfree is not or badly defined ...</h1></body></html>"
    186 
     230if errormess != "":
     231                       print "<h1>Ooops!</h1>"
     232                       print "Please correct the following problems before submitting again."
     233                       print "<ul>"
     234                       print errormess
     235                       print "</ul>"
     236else:
     237  if sumfree == 0:      query.update() ; query.htmlprinttabextvar(vartoplot)
     238  elif sumfree == 2:    print "<img src='"+figname+"'><br />"
     239  elif sumfree == 1:     
     240                        print "<a href='"+txtname+"'>Click here to download an ASCII file containing data</a><br />"
     241                        print "<hr>"
     242                        print "<img src='"+figname+"'><br />"
    187243
    188244## This is quite common
    189 bottom = "<hr><a href='../index.html'>Click here to start a new query</a>.<hr></body></html>"
    190 #print bottom
    191 
    192 ##write to file object
    193 #f = cStringIO.StringIO()
    194 #mpl.savefig(f)
    195 #f.seek(0)
    196 
    197 ##output to browser
    198 #print "Content-type: image/png\n"
    199 #print f.read()
    200 #exit()
    201 
    202 #print "Content-type:text/html\r\n\r\n"
    203 #print "<html>"
    204 #print "<head>"
    205 #print "<title>MCD. Simple Python interface</title>"
    206 #print "</head>"
    207 #print "<body>"
    208 
    209 
    210 
    211 
    212 
    213 
    214 ## HTTP Header
    215 #print "Content-Type:application/octet-stream; name=\"FileName\"\r\n";
    216 #print "Content-Disposition: attachment; filename=\"FileName\"\r\n\n";
    217 ## Actual File Content will go hear.
    218 #fo = open("foo.txt", "rb")
    219 #str = fo.read();
    220 #print str
    221 ## Close opend file
    222 #fo.close()
    223 
     245bottom = "</body></html>"
     246print bottom
Note: See TracChangeset for help on using the changeset viewer.