1 | ###!/usr/bin/env python |
---|
2 | #!/usr/bin/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 mcd import mcd |
---|
20 | from modules import * |
---|
21 | from modules import mcd |
---|
22 | |
---|
23 | import cStringIO |
---|
24 | import os as daos |
---|
25 | import matplotlib.pyplot as mpl |
---|
26 | from PIL import Image |
---|
27 | |
---|
28 | # for debugging in web browser |
---|
29 | cgitb.enable() |
---|
30 | |
---|
31 | # Create instance of FieldStorage |
---|
32 | form = cgi.FieldStorage() |
---|
33 | |
---|
34 | # create a MCD object |
---|
35 | #query = mcd() |
---|
36 | query=mcd.mcd() #FG: import from module mcd |
---|
37 | |
---|
38 | # Get data from user-defined fields and define free dimensions |
---|
39 | # FG: add tests if var==None to have values in local without forms ones |
---|
40 | query.lat = -9999. |
---|
41 | getlat = form.getvalue("latitude") |
---|
42 | if getlat == None: getlat = "1" |
---|
43 | if getlat == "all": islatfree = 1 ; query.lats = -90. ; query.late = 90. |
---|
44 | elif ";" in getlat: islatfree = 1 ; ind = getlat.find(";") ; query.lats = float(getlat[:ind]) ; query.late = float(getlat[ind+1:]) |
---|
45 | else: islatfree = 0 ; query.lat = float(getlat) |
---|
46 | |
---|
47 | query.lon = -9999. |
---|
48 | getlon = form.getvalue("longitude") |
---|
49 | if getlon == None: getlon = "1" |
---|
50 | if getlon == "all": islonfree = 1 ; query.lons = -180. ; query.lone = 180. |
---|
51 | elif ";" in getlon: islonfree = 1 ; ind = getlon.find(";") ; query.lons = float(getlon[:ind]) ; query.lone = float(getlon[ind+1:]) |
---|
52 | else: islonfree = 0 ; query.lon = float(getlon) |
---|
53 | |
---|
54 | query.loct = -9999. |
---|
55 | getloct = form.getvalue("localtime") |
---|
56 | if getloct == None: getloct = "1" |
---|
57 | if getloct == "all": isloctfree = 1 ; query.locts = 0. ; query.locte = 24. |
---|
58 | elif ";" in getloct: isloctfree = 1 ; ind = getloct.find(";") ; query.locts = float(getloct[:ind]) ; query.locte = float(getloct[ind+1:]) |
---|
59 | else: isloctfree = 0 ; query.loct = float(getloct) |
---|
60 | |
---|
61 | query.xz = -9999. |
---|
62 | getalt = form.getvalue("altitude") |
---|
63 | if getalt == None: getalt = "1" |
---|
64 | if getalt == "all": isaltfree = 1 ; query.xzs = 0. ; query.xze = 120000. |
---|
65 | elif ";" in getalt: isaltfree = 1 ; ind = getalt.find(";") ; query.xzs = float(getalt[:ind]) ; query.xze = float(getalt[ind+1:]) |
---|
66 | else: isaltfree = 0 ; query.xz = float(getalt) |
---|
67 | |
---|
68 | sumfree = islatfree + islonfree + isloctfree + isaltfree |
---|
69 | if sumfree > 2: exit() ## only 1D or 2D plots for the moment |
---|
70 | |
---|
71 | try: query.xdate = float(form.getvalue("ls")) |
---|
72 | except: query.xdate = float(1) |
---|
73 | try: query.hrkey = int(form.getvalue("hrkey")) |
---|
74 | except: query.hrkey = int(1) |
---|
75 | try: query.dust = int(form.getvalue("dust")) |
---|
76 | except: query.dust = int(1) |
---|
77 | try: query.zkey = int(form.getvalue("zkey")) |
---|
78 | except: query.zkey = int(3) |
---|
79 | # self.perturkey = 0 #integer perturkey ! perturbation type (0: none) |
---|
80 | # self.seedin = 1 #random number generator seed (unused if perturkey=0) |
---|
81 | # self.gwlength = 0. #gravity Wave wavelength (unused if perturkey=0) |
---|
82 | |
---|
83 | # Get variables to plot |
---|
84 | var1 = form.getvalue("var1") |
---|
85 | var2 = form.getvalue("var2") |
---|
86 | var3 = form.getvalue("var3") |
---|
87 | var4 = form.getvalue("var4") |
---|
88 | |
---|
89 | # fg: vartoplot is not None without form values |
---|
90 | # vartoplot = [var1] |
---|
91 | # fg: init var as with form values |
---|
92 | if var1 == None: var1="t" |
---|
93 | #if var2 == None: var2="p" |
---|
94 | |
---|
95 | vartoplot = [] |
---|
96 | if var1 != "none": vartoplot = np.append(vartoplot,var1) |
---|
97 | if var2 != "none" and var2 != None: vartoplot = np.append(vartoplot,var2) |
---|
98 | if var3 != "none" and var3 != None: vartoplot = np.append(vartoplot,var3) |
---|
99 | if var4 != "none" and var4 != None: vartoplot = np.append(vartoplot,var4) |
---|
100 | |
---|
101 | iswind = form.getvalue("iswind") |
---|
102 | if iswind == "on": iswindlog = True |
---|
103 | else: iswindlog = False |
---|
104 | isfixedlt = form.getvalue("isfixedlt") |
---|
105 | if isfixedlt == "on": input_fixedlt=True |
---|
106 | else: input_fixedlt=False |
---|
107 | |
---|
108 | # reference name (to test which figures are already in the database) |
---|
109 | reference = query.getnameset()+str(var1)+str(var2)+str(var3)+str(var4)+str(iswind)+str(isfixedlt) |
---|
110 | figname = '../img/'+reference+'.png' |
---|
111 | |
---|
112 | testexist = daos.path.isfile(figname) |
---|
113 | |
---|
114 | # extract data from MCD if needed |
---|
115 | if not testexist: |
---|
116 | |
---|
117 | ### 1D plots |
---|
118 | if sumfree == 1: |
---|
119 | |
---|
120 | ### getting data |
---|
121 | if isloctfree == 1: query.diurnal(nd=24) |
---|
122 | elif islonfree == 1: query.zonal() |
---|
123 | elif islatfree == 1: query.meridional() |
---|
124 | elif isaltfree == 1: query.profile() |
---|
125 | else: exit() |
---|
126 | |
---|
127 | ### generic building of figure |
---|
128 | query.htmlplot1d(vartoplot,vertplot=isaltfree,figname=figname) |
---|
129 | #mpl.savefig("img/temp.png",dpi=85,bbox_inches='tight',pad_inches=0.25) |
---|
130 | #Image.open("../img/temp.png").save(figname,'JPEG') |
---|
131 | |
---|
132 | ### 2D plots |
---|
133 | elif sumfree == 2: |
---|
134 | |
---|
135 | ### getting data |
---|
136 | if islatfree == 1 and islonfree == 1: query.latlon() |
---|
137 | else: exit() |
---|
138 | |
---|
139 | ### figure |
---|
140 | zetitle = "MCD v4.3 - Dust scenario "+str(query.dust)+" - Date is "+str(query.xdate) |
---|
141 | query.htmlmap2d(vartoplot,incwind=iswindlog,fixedlt=input_fixedlt,figname=figname,title=zetitle) |
---|
142 | #mpl.savefig("img/temp.png",dpi=110,bbox_inches='tight',pad_inches=0.4) |
---|
143 | #Image.open("img/temp.png").save(figname,'JPEG') ##lighter images |
---|
144 | ### http://www.pythonware.com/library/pil/handbook/introduction.htm |
---|
145 | |
---|
146 | ## This is quite common |
---|
147 | print "Content-type:text/html\n" |
---|
148 | print " " #Apache needs a space after content-type |
---|
149 | |
---|
150 | #entete="""<?xml version="1.0" encoding="UTF-8"?> |
---|
151 | #<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd"> |
---|
152 | #<html xmlns="http://www.w3.org/1999/xhtml"> """ |
---|
153 | |
---|
154 | header="""<html><head><title>Mars Climate Database: The Web Interface</title></head><body>""" |
---|
155 | |
---|
156 | print header |
---|
157 | #print query.printset() |
---|
158 | #print "<br />" |
---|
159 | |
---|
160 | ## Now the part which differs |
---|
161 | if sumfree == 0: query.update() ; query.htmlprinttabextvar(vartoplot) #query.printmeanvar() |
---|
162 | elif sumfree >= 1: print "<a href='../index.html'>Click here to start a new query</a><br /><img src='"+figname+"'><br />" |
---|
163 | else: print "<h1>ERROR : sumfree is not or badly defined ...</h1></body></html>" |
---|
164 | |
---|
165 | |
---|
166 | ## This is quite common |
---|
167 | bottom = "<hr><a href='../index.html'>Click here to start a new query</a>.<hr></body></html>" |
---|
168 | #print bottom |
---|
169 | |
---|
170 | ##write to file object |
---|
171 | #f = cStringIO.StringIO() |
---|
172 | #mpl.savefig(f) |
---|
173 | #f.seek(0) |
---|
174 | |
---|
175 | ##output to browser |
---|
176 | #print "Content-type: image/png\n" |
---|
177 | #print f.read() |
---|
178 | #exit() |
---|
179 | |
---|
180 | #print "Content-type:text/html\r\n\r\n" |
---|
181 | #print "<html>" |
---|
182 | #print "<head>" |
---|
183 | #print "<title>MCD. Simple Python interface</title>" |
---|
184 | #print "</head>" |
---|
185 | #print "<body>" |
---|
186 | |
---|
187 | |
---|
188 | |
---|
189 | |
---|
190 | |
---|
191 | |
---|
192 | ## HTTP Header |
---|
193 | #print "Content-Type:application/octet-stream; name=\"FileName\"\r\n"; |
---|
194 | #print "Content-Disposition: attachment; filename=\"FileName\"\r\n\n"; |
---|
195 | ## Actual File Content will go hear. |
---|
196 | #fo = open("foo.txt", "rb") |
---|
197 | #str = fo.read(); |
---|
198 | #print str |
---|
199 | ## Close opend file |
---|
200 | #fo.close() |
---|