1 | #!/usr/bin/python |
---|
2 | ###!/usr/bin/env python |
---|
3 | ###!/home/aymeric/Software/epd-7.0-2-rh5-x86/bin/python |
---|
4 | ####!/home/marshttp/EPD/epd-7.0-2-rh5-x86_64/bin/python |
---|
5 | ### here the version used to f2py the MCD Fortran routines |
---|
6 | |
---|
7 | ################################################## |
---|
8 | ### A Python CGI for the Mars Climate Database ### |
---|
9 | ### ------------------------------------------ ### |
---|
10 | ### Aymeric SPIGA 18-19/04/2012 ~ 11/08/2012 ### |
---|
11 | ### ------------------------------------------ ### |
---|
12 | ### (see mcdtest.py for examples of use) ### |
---|
13 | ################################################## |
---|
14 | ### ajouts et corrections par Franck Guyon 09/2012 |
---|
15 | ### ajouts suite a brainstorm equipe AS 10/2012 |
---|
16 | |
---|
17 | import cgi, cgitb |
---|
18 | import numpy as np |
---|
19 | from modules import * |
---|
20 | from modules import mcd |
---|
21 | |
---|
22 | import cStringIO |
---|
23 | import os as daos |
---|
24 | import matplotlib.pyplot as mpl |
---|
25 | |
---|
26 | ### a function to read HTML arguments for coordinates |
---|
27 | def gethtmlcoord(userinput,defmin,defmax): |
---|
28 | import string |
---|
29 | # accepted separators. the symbol - should always be last. |
---|
30 | separators = [":",";",",","/","_"] |
---|
31 | # initial values |
---|
32 | val = -9999. ; vals = None ; vale = None ; foundinterv = False |
---|
33 | if userinput == None: userinput = "1" |
---|
34 | # the main work. either all, or an interval, or a single value. |
---|
35 | # ... all |
---|
36 | if userinput == "all": isfree = 1 ; vals = defmin ; vale = defmax ; foundinterv = True |
---|
37 | # ... an interval |
---|
38 | else: |
---|
39 | for sep in separators: |
---|
40 | if not foundinterv: |
---|
41 | isfree = 1 ; ind = userinput.find(sep) |
---|
42 | if ind != -1: vals = float(userinput[:ind]) ; vale = float(userinput[ind+1:]) ; foundinterv = True |
---|
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 |
---|
60 | # return values |
---|
61 | return isfree, val, vals, vale |
---|
62 | |
---|
63 | # set an errormess variable which must stay to None for interface to proceed |
---|
64 | errormess = "" |
---|
65 | |
---|
66 | # for debugging in web browser |
---|
67 | cgitb.enable() |
---|
68 | |
---|
69 | # Create instance of FieldStorage |
---|
70 | form = cgi.FieldStorage() |
---|
71 | |
---|
72 | # create a MCD object |
---|
73 | query=mcd.mcd() |
---|
74 | |
---|
75 | # Get the kind of vertical coordinates and choose default behavior for "all" |
---|
76 | try: query.zkey = int(form.getvalue("zkey")) |
---|
77 | except: query.zkey = int(3) |
---|
78 | if query.zkey == 2: minxz = -5000. ; maxxz = 150000. |
---|
79 | elif query.zkey == 3: minxz = 0. ; maxxz = 250000. |
---|
80 | elif query.zkey == 5: minxz = -5000. ; maxxz = 150000. |
---|
81 | elif query.zkey == 4: minxz = 1.e3 ; maxxz = 1.e-6 |
---|
82 | elif query.zkey == 1: minxz = 3396000. ; maxxz = 3596000. |
---|
83 | |
---|
84 | # Get data from user-defined fields and define free dimensions |
---|
85 | islatfree, query.lat, query.lats, query.late = gethtmlcoord( form.getvalue("latitude"), -90., 90. ) |
---|
86 | islonfree, query.lon, query.lons, query.lone = gethtmlcoord( form.getvalue("longitude"), -180., 180. ) |
---|
87 | isloctfree, query.loct, query.locts, query.locte = gethtmlcoord( form.getvalue("localtime"), 0., 24. ) |
---|
88 | isaltfree, query.xz, query.xzs, query.xze = gethtmlcoord( form.getvalue("altitude"), minxz, maxxz) |
---|
89 | |
---|
90 | try: query.datekey = int(form.getvalue("datekeyhtml")) |
---|
91 | except: query.datekey = float(1) |
---|
92 | badlschar = False |
---|
93 | if query.datekey == 1: |
---|
94 | try: query.xdate = float(form.getvalue("ls")) |
---|
95 | except: query.xdate = float(1) ; badlschar = True |
---|
96 | else: |
---|
97 | try: query.xdate = float(form.getvalue("julian")) |
---|
98 | except: query.xdate = float(1) |
---|
99 | query.loct = 0. |
---|
100 | |
---|
101 | # Prevent the user from doing bad |
---|
102 | badinterv = (islatfree == -1) or (islonfree == -1) or (isloctfree == -1) or (isaltfree == -1) |
---|
103 | if badinterv: |
---|
104 | errormess = errormess+"<li>Bad syntax. Write a value (or) a range val1;val2 (or) 'all'. Separator shall be either ; : , / _ " |
---|
105 | badls = (query.datekey == 1 and (query.xdate < 0. or query.xdate > 360.)) |
---|
106 | if badls: |
---|
107 | errormess = errormess+"<li>Solar longitude must be between 0 and 360." |
---|
108 | if badlschar: |
---|
109 | errormess = errormess+"<li>Solar longitude is in the wrong format. It should be a positive number between 0 and 360. Intervals of solar longitude are not allowed." |
---|
110 | badloct = (isloctfree == 0 and query.loct > 24.) \ |
---|
111 | or (isloctfree == 1 and (query.locts > 24. or query.locte > 24.)) \ |
---|
112 | or (isloctfree == 0 and query.loct < 0.) \ |
---|
113 | or (isloctfree == 1 and (query.locts < 0. or query.locte < 0.)) |
---|
114 | if badloct: |
---|
115 | errormess = errormess+"<li>Local time must be less than 24 martian hours (and not a negative number)." |
---|
116 | badlat = (islatfree == 0 and abs(query.lat) > 90.) \ |
---|
117 | or (islatfree == 1 and (abs(query.lats) > 90. or abs(query.late) > 90.)) |
---|
118 | if badlat: |
---|
119 | errormess = errormess+"<li>Latitude coordinates must be between -90 and 90." |
---|
120 | badlon = (islonfree == 0 and abs(query.lon) > 360.) \ |
---|
121 | or (islonfree == 1 and (abs(query.lons) > 360. or abs(query.lone) > 360.)) |
---|
122 | if badlon: |
---|
123 | errormess = errormess+"<li>Longitude coordinates must be between -360 and 360." |
---|
124 | badalt = (isaltfree == 0 and (query.zkey in [3]) and query.xz < 0.) \ |
---|
125 | or (isaltfree == 1 and (query.zkey in [3]) and (query.xzs < 0. or query.xze < 0.)) |
---|
126 | if badalt: |
---|
127 | errormess = errormess+"<li>Vertical coordinates must be positive when requesting altitude above surface." |
---|
128 | badalt2 = (isaltfree == 0 and (query.zkey in [1,4]) and query.xz <= 0.) \ |
---|
129 | or (isaltfree == 1 and (query.zkey in [1,4]) and (query.xzs <= 0. or query.xze <= 0.)) |
---|
130 | if badalt2: |
---|
131 | errormess = errormess+"<li>Vertical coordinates must be <b>strictly</b> positive when requesting pressure levels or altitude above Mars center." |
---|
132 | badalt3 = (isaltfree == 0 and query.zkey == 4 and query.xz > 1500.) \ |
---|
133 | or (isaltfree == 1 and query.zkey == 4 and min(query.xzs,query.xze) > 1500.) |
---|
134 | if badalt3: |
---|
135 | errormess = errormess+"<li>Pressure values larger than 1500 Pa are unlikely to be encountered in the Martian atmosphere." |
---|
136 | badrange = (isloctfree == 1 and query.locts == query.locte) \ |
---|
137 | or (islatfree == 1 and query.lats == query.late) \ |
---|
138 | or (islonfree == 1 and query.lons == query.lone) \ |
---|
139 | or (isaltfree == 1 and query.xzs == query.xze) |
---|
140 | if badrange: |
---|
141 | errormess = errormess+"<li>One or several coordinate intervals are not... intervals. Set either a real range or an unique value." |
---|
142 | |
---|
143 | # Get how many free dimensions we have |
---|
144 | sumfree = islatfree + islonfree + isloctfree + isaltfree |
---|
145 | if sumfree >= 3: errormess = errormess + "<li>3 or more free dimensions are set... but only 1D and 2D plots are supported!" |
---|
146 | |
---|
147 | # Get additional parameters |
---|
148 | try: query.hrkey = int(form.getvalue("hrkey")) |
---|
149 | except: query.hrkey = int(1) |
---|
150 | try: query.dust = int(form.getvalue("dust")) |
---|
151 | except: query.dust = int(1) |
---|
152 | # self.perturkey = 0 #integer perturkey ! perturbation type (0: none) |
---|
153 | # self.seedin = 1 #random number generator seed (unused if perturkey=0) |
---|
154 | # self.gwlength = 0. #gravity Wave wavelength (unused if perturkey=0) |
---|
155 | try: query.colorm = form.getvalue("colorm") |
---|
156 | except: query.colorm = "jet" |
---|
157 | |
---|
158 | try: query.min2d = float(form.getvalue("minval")) |
---|
159 | except: query.min2d = None |
---|
160 | try: query.max2d = float(form.getvalue("maxval")) |
---|
161 | except: query.max2d = None |
---|
162 | |
---|
163 | try: query.dpi = float(form.getvalue("dpi")) |
---|
164 | except: query.dpi = 80. |
---|
165 | |
---|
166 | |
---|
167 | # Get variables to plot |
---|
168 | var1 = form.getvalue("var1") |
---|
169 | var2 = form.getvalue("var2") |
---|
170 | var3 = form.getvalue("var3") |
---|
171 | var4 = form.getvalue("var4") |
---|
172 | |
---|
173 | # fg: init var as with form values |
---|
174 | if var1 == None: var1="t" |
---|
175 | |
---|
176 | vartoplot = [] |
---|
177 | if var1 != "none": vartoplot = np.append(vartoplot,var1) |
---|
178 | if var2 != "none" and var2 != None: vartoplot = np.append(vartoplot,var2) |
---|
179 | if var3 != "none" and var3 != None: vartoplot = np.append(vartoplot,var3) |
---|
180 | if var4 != "none" and var4 != None: vartoplot = np.append(vartoplot,var4) |
---|
181 | |
---|
182 | iswind = form.getvalue("iswind") |
---|
183 | if iswind == "on": iswindlog = True |
---|
184 | else: iswindlog = False |
---|
185 | isfixedlt = form.getvalue("isfixedlt") |
---|
186 | if isfixedlt == "on": query.fixedlt=True |
---|
187 | else: query.fixedlt=False |
---|
188 | iszonmean = form.getvalue("zonmean") |
---|
189 | if iszonmean == "on": query.zonmean=True |
---|
190 | else: query.zonmean=False |
---|
191 | |
---|
192 | ### now, proceed... |
---|
193 | if errormess == "": |
---|
194 | |
---|
195 | # reference name (to test which figures are already in the database) |
---|
196 | reference = query.getnameset()+str(var1)+str(var2)+str(var3)+str(var4)+str(iswind)+str(isfixedlt)+str(iszonmean)+query.colorm+str(query.min2d)+str(query.max2d)+str(query.dpi) |
---|
197 | figname = '../img/'+reference+'.png' |
---|
198 | txtname = '../txt/'+reference+'.txt' |
---|
199 | testexist = daos.path.isfile(figname) |
---|
200 | |
---|
201 | # extract data from MCD if needed |
---|
202 | if not testexist: |
---|
203 | |
---|
204 | ### 1D plots |
---|
205 | if sumfree == 1: |
---|
206 | |
---|
207 | ### getting data |
---|
208 | if isloctfree == 1: query.diurnal(nd=25) |
---|
209 | elif islonfree == 1: query.zonal(nd=64) |
---|
210 | elif islatfree == 1: query.meridional(nd=48) |
---|
211 | elif isaltfree == 1: query.profile(nd=35) |
---|
212 | else: exit() |
---|
213 | |
---|
214 | ### generic building of figure |
---|
215 | query.getascii(vartoplot,filename=txtname) |
---|
216 | query.htmlplot1d(vartoplot,figname=figname) |
---|
217 | |
---|
218 | ### 2D plots |
---|
219 | elif sumfree == 2: |
---|
220 | |
---|
221 | ### getting data |
---|
222 | if islatfree == 1 and islonfree == 1: query.htmlmap2d(vartoplot,incwind=iswindlog,figname=figname) |
---|
223 | else: query.htmlplot2d(vartoplot,figname=figname) |
---|
224 | |
---|
225 | #### NOW WRITE THE HTML PAGE TO USER |
---|
226 | |
---|
227 | ## This is quite common |
---|
228 | print "Content-type:text/html\n" |
---|
229 | print " " #Apache needs a space after content-type |
---|
230 | |
---|
231 | #entete="""<?xml version="1.0" encoding="UTF-8"?> |
---|
232 | #<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd"> |
---|
233 | #<html xmlns="http://www.w3.org/1999/xhtml"> """ |
---|
234 | |
---|
235 | header="""<html><head><title>Mars Climate Database: The Web Interface</title></head><body>""" |
---|
236 | |
---|
237 | print header |
---|
238 | #print query.printset() |
---|
239 | #print "<br />" |
---|
240 | |
---|
241 | ## Now the part which differs |
---|
242 | if errormess != "": |
---|
243 | print "<h1>Ooops!</h1>" |
---|
244 | print "Please correct the following problems before submitting again." |
---|
245 | print "<ul>" |
---|
246 | print errormess |
---|
247 | print "</ul>" |
---|
248 | else: |
---|
249 | if sumfree == 0: query.update() ; query.htmlprinttabextvar(vartoplot) |
---|
250 | elif sumfree == 2: print "<img src='"+figname+"'><br />" |
---|
251 | elif sumfree == 1: |
---|
252 | print "<a href='"+txtname+"'>Click here to download an ASCII file containing data</a><br />" |
---|
253 | print "<hr>" |
---|
254 | print "<img src='"+figname+"'><br />" |
---|
255 | |
---|
256 | ## This is quite common |
---|
257 | bottom = "</body></html>" |
---|
258 | print bottom |
---|