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 | |
---|
12 | import cgi, cgitb |
---|
13 | import numpy as np |
---|
14 | from mcd import mcd |
---|
15 | import cStringIO |
---|
16 | import os as daos |
---|
17 | import matplotlib.pyplot as mpl |
---|
18 | import Image |
---|
19 | |
---|
20 | # for debugging in web browser |
---|
21 | cgitb.enable() |
---|
22 | |
---|
23 | # Create instance of FieldStorage |
---|
24 | form = cgi.FieldStorage() |
---|
25 | |
---|
26 | # create a MCD object |
---|
27 | query = mcd() |
---|
28 | |
---|
29 | # Get data from user-defined fields and define free dimensions |
---|
30 | getlat = form.getvalue("latitude") |
---|
31 | if getlat == "all": islatfree = 1 ; query.lat = -9999. |
---|
32 | else: islatfree = 0 ; query.lat = float(getlat) |
---|
33 | getlon = form.getvalue("longitude") |
---|
34 | if getlon == "all": islonfree = 1 ; query.lon = -9999. |
---|
35 | else: islonfree = 0 ; query.lon = float(getlon) |
---|
36 | getloct = form.getvalue("localtime") |
---|
37 | if getloct == "all": isloctfree = 1 ; query.loct = -9999. |
---|
38 | else: isloctfree = 0 ; query.loct = float(getloct) |
---|
39 | getalt = form.getvalue("altitude") |
---|
40 | if getalt == "all": isaltfree = 1 ; query.xz = -9999. |
---|
41 | else: isaltfree = 0 ; query.xz = float(getalt) |
---|
42 | sumfree = islatfree + islonfree + isloctfree + isaltfree |
---|
43 | if sumfree > 2: exit() ## only 1D or 2D plots for the moment |
---|
44 | query.xdate = float(form.getvalue("ls")) |
---|
45 | query.hrkey = int(form.getvalue("hrkey")) |
---|
46 | query.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 |
---|
53 | var1 = form.getvalue("var1") |
---|
54 | var2 = form.getvalue("var2") |
---|
55 | var3 = form.getvalue("var3") |
---|
56 | var4 = form.getvalue("var4") |
---|
57 | vartoplot = [var1] |
---|
58 | if var2 != "none": vartoplot = np.append(vartoplot,var2) |
---|
59 | if var3 != "none": vartoplot = np.append(vartoplot,var3) |
---|
60 | if var4 != "none": vartoplot = np.append(vartoplot,var4) |
---|
61 | iswind = form.getvalue("iswind") |
---|
62 | if iswind == "on": iswindlog = True |
---|
63 | else: iswindlog = False |
---|
64 | isfixedlt = form.getvalue("isfixedlt") |
---|
65 | if isfixedlt == "on": input_fixedlt=True |
---|
66 | else: input_fixedlt=False |
---|
67 | |
---|
68 | # reference name (to test which figures are already in the database) |
---|
69 | reference = str(islatfree)+str(islonfree)+str(isloctfree)+str(isaltfree)+query.getnameset()+str(var1)+str(var2)+str(var3)+str(var4)+str(iswind)+str(isfixedlt) |
---|
70 | figname = 'img/'+reference+'.jpg' |
---|
71 | testexist = daos.path.isfile(figname) |
---|
72 | |
---|
73 | # extract data from MCD if needed |
---|
74 | if 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 |
---|
106 | print "Content-type:text/html\r\n\r\n" |
---|
107 | print "<html>" |
---|
108 | print "<head>" |
---|
109 | print "<title>MCD. Simple Python interface</title>" |
---|
110 | print "</head>" |
---|
111 | print "<body>" |
---|
112 | |
---|
113 | ## Now the part which differs |
---|
114 | if sumfree == 0: query.update() ; query.htmlprinttabextvar(vartoplot) #query.printmeanvar() |
---|
115 | elif sumfree >= 1: print "<img src='../"+figname+"'><br />" |
---|
116 | else: 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 />" |
---|
120 | print "<hr>" |
---|
121 | print "<a href='../index.html'>Click here to start a new query</a>." |
---|
122 | #query.printset() |
---|
123 | print "<hr>" |
---|
124 | print "</body>" |
---|
125 | print "</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() |
---|