source: trunk/UTIL/PYTHON/mcd/proto/cgi-bin/mcdcgi.py @ 773

Last change on this file since 773 was 761, checked in by aslmd, 13 years ago

UTIL PYTHON : a more definitive version of what could make a new and simple MCD web interface. added a lot of capabilities: variable setting, beginner mode, wind vector, fixed or not local time, etc etc etc. some modifications were made to the python MCD interface too (this makes the basis for the web interface).

  • Property svn:executable set to *
File size: 5.0 KB
Line 
1#!/home/aymeric/Software/epd-7.0-2-rh5-x86/bin/python
2### here the version used to f2py the MCD Fortran routines
3
4##################################################
5### A Python CGI for the Mars Climate Database ###
6### ------------------------------------------ ###
7### Aymeric SPIGA 18-19/04/2012 ~ 11/08/2012   ###
8### ------------------------------------------ ###
9### (see mcdtest.py for examples of use)       ###
10##################################################
11
12import cgi, cgitb 
13import numpy as np
14from mcd import mcd
15import cStringIO
16import os as daos
17import matplotlib.pyplot as mpl
18import Image
19
20# for debugging in web browser
21cgitb.enable()
22
23# Create instance of FieldStorage
24form = cgi.FieldStorage() 
25
26# create a MCD object
27query = mcd()
28
29# Get data from user-defined fields and define free dimensions
30getlat = form.getvalue("latitude")
31if getlat == "all":  islatfree = 1 ; query.lat = -9999.
32else:                islatfree = 0 ; query.lat = float(getlat)
33getlon = form.getvalue("longitude")
34if getlon == "all":  islonfree = 1 ; query.lon = -9999.
35else:                islonfree = 0 ; query.lon = float(getlon)
36getloct = form.getvalue("localtime")
37if getloct == "all": isloctfree = 1 ; query.loct = -9999.
38else:                isloctfree = 0 ; query.loct = float(getloct)
39getalt = form.getvalue("altitude")
40if getalt == "all":  isaltfree = 1 ; query.xz = -9999.
41else:                isaltfree = 0 ; query.xz = float(getalt)
42sumfree = islatfree + islonfree + isloctfree + isaltfree
43if sumfree > 2: exit() ## only 1D or 2D plots for the moment
44query.xdate = float(form.getvalue("ls"))
45query.hrkey = int(form.getvalue("hrkey"))
46query.dust = int(form.getvalue("dust"))
47#        self.zkey      = 3  # specify that xz is the altitude above surface (m)
48#        self.perturkey = 0  #integer perturkey ! perturbation type (0: none)
49#        self.seedin    = 1  #random number generator seed (unused if perturkey=0)
50#        self.gwlength  = 0. #gravity Wave wavelength (unused if perturkey=0)
51
52# Get variables to plot
53var1 = form.getvalue("var1")
54var2 = form.getvalue("var2")
55var3 = form.getvalue("var3")
56var4 = form.getvalue("var4")
57vartoplot = [var1]
58if var2 != "none": vartoplot = np.append(vartoplot,var2)
59if var3 != "none": vartoplot = np.append(vartoplot,var3)
60if var4 != "none": vartoplot = np.append(vartoplot,var4)
61iswind = form.getvalue("iswind")
62if iswind == "on": iswindlog = True
63else:              iswindlog = False
64isfixedlt = form.getvalue("isfixedlt")
65if isfixedlt == "on": input_fixedlt=True
66else:                 input_fixedlt=False 
67
68# reference name (to test which figures are already in the database)
69reference = str(islatfree)+str(islonfree)+str(isloctfree)+str(isaltfree)+query.getnameset()+str(var1)+str(var2)+str(var3)+str(var4)+str(iswind)+str(isfixedlt)
70figname = 'img/'+reference+'.jpg'
71testexist = daos.path.isfile(figname)
72
73# extract data from MCD if needed
74if not testexist:
75
76  ### 1D plots
77  if sumfree == 1:
78
79    ### getting data
80    if isloctfree == 1:         query.diurnal(nd=24)
81    elif islonfree == 1:        query.zonal()
82    elif islatfree == 1:        query.meridional()
83    elif isaltfree == 1:        query.profile()   
84    else:                       exit() 
85
86    ### generic building of figure
87    #query.plot1d(["t","p","u","v"],vertplot=isaltfree)
88    query.plot1d(vartoplot,vertplot=isaltfree)
89    mpl.savefig("img/temp.png",dpi=85,bbox_inches='tight',pad_inches=0.25)
90    Image.open("img/temp.png").save(figname,'JPEG')
91
92  ### 2D plots
93  elif sumfree == 2:
94
95    ### getting data
96    if islatfree == 1 and islonfree == 1:       query.latlon()   
97    else:                                       exit() 
98
99    ### figure   
100    query.map2d(vartoplot,incwind=iswindlog,fixedlt=input_fixedlt) 
101    mpl.savefig("img/temp.png",dpi=110,bbox_inches='tight',pad_inches=0.4)
102    Image.open("img/temp.png").save(figname,'JPEG') ##lighter images   
103    ## http://www.pythonware.com/library/pil/handbook/introduction.htm
104
105## This is quite common
106print "Content-type:text/html\r\n\r\n"
107print "<html>"
108print "<head>"
109print "<title>MCD. Simple Python interface</title>"
110print "</head>"
111print "<body>"
112
113## Now the part which differs
114if sumfree == 0:        query.update() ; query.htmlprinttabextvar(vartoplot)  #query.printmeanvar()
115elif sumfree >= 1:      print "<img src='../"+figname+"'><br />"
116else:                   exit()
117
118## This is quite common
119#print "Based on the <a href='http://www-mars.lmd.jussieu.fr'>Mars Climate Database</a> (c) LMD/OU/IAA/ESA/CNES.<br />"
120print "<hr>"
121print "<a href='../index.html'>Click here to start a new query</a>."
122#query.printset()
123print "<hr>"
124print "</body>"
125print "</html>"
126
127##write to file object
128#f = cStringIO.StringIO()
129#mpl.savefig(f)
130#f.seek(0)
131
132##output to browser
133#print "Content-type: image/png\n"
134#print f.read()
135#exit()
136
137#print "Content-type:text/html\r\n\r\n"
138#print "<html>"
139#print "<head>"
140#print "<title>MCD. Simple Python interface</title>"
141#print "</head>"
142#print "<body>"
143
144
145
146
147
148
149## HTTP Header
150#print "Content-Type:application/octet-stream; name=\"FileName\"\r\n";
151#print "Content-Disposition: attachment; filename=\"FileName\"\r\n\n";
152## Actual File Content will go hear.
153#fo = open("foo.txt", "rb")
154#str = fo.read();
155#print str
156## Close opend file
157#fo.close()
Note: See TracBrowser for help on using the repository browser.