[807] | 1 | #!/usr/bin/python |
---|
[796] | 2 | ###!/usr/bin/env python |
---|
[793] | 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 |
---|
[639] | 5 | ### here the version used to f2py the MCD Fortran routines |
---|
| 6 | |
---|
| 7 | ################################################## |
---|
| 8 | ### A Python CGI for the Mars Climate Database ### |
---|
[761] | 9 | ### ------------------------------------------ ### |
---|
| 10 | ### Aymeric SPIGA 18-19/04/2012 ~ 11/08/2012 ### |
---|
| 11 | ### ------------------------------------------ ### |
---|
[639] | 12 | ### (see mcdtest.py for examples of use) ### |
---|
| 13 | ################################################## |
---|
[793] | 14 | ### ajouts et corrections par Franck Guyon 09/2012 |
---|
[796] | 15 | ### ajouts suite a brainstorm equipe AS 10/2012 |
---|
[639] | 16 | |
---|
| 17 | import cgi, cgitb |
---|
| 18 | import numpy as np |
---|
[793] | 19 | #from mcd import mcd |
---|
| 20 | from modules import * |
---|
| 21 | from modules import mcd |
---|
| 22 | |
---|
[639] | 23 | import cStringIO |
---|
| 24 | import os as daos |
---|
| 25 | import matplotlib.pyplot as mpl |
---|
[793] | 26 | from PIL import Image |
---|
[639] | 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 |
---|
[793] | 35 | #query = mcd() |
---|
| 36 | query=mcd.mcd() #FG: import from module mcd |
---|
[639] | 37 | |
---|
[761] | 38 | # Get data from user-defined fields and define free dimensions |
---|
[793] | 39 | # FG: add tests if var==None to have values in local without forms ones |
---|
[796] | 40 | query.lat = -9999. |
---|
[761] | 41 | getlat = form.getvalue("latitude") |
---|
[796] | 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:]) |
---|
[811] | 45 | elif "," in getlat: islatfree = 1 ; ind = getlat.find(",") ; query.lats = float(getlat[:ind]) ; query.late = float(getlat[ind+1:]) |
---|
| 46 | elif "/" in getlat: islatfree = 1 ; ind = getlat.find("/") ; query.lats = float(getlat[:ind]) ; query.late = float(getlat[ind+1:]) |
---|
[761] | 47 | else: islatfree = 0 ; query.lat = float(getlat) |
---|
[794] | 48 | |
---|
[796] | 49 | query.lon = -9999. |
---|
[761] | 50 | getlon = form.getvalue("longitude") |
---|
[796] | 51 | if getlon == None: getlon = "1" |
---|
| 52 | if getlon == "all": islonfree = 1 ; query.lons = -180. ; query.lone = 180. |
---|
| 53 | elif ";" in getlon: islonfree = 1 ; ind = getlon.find(";") ; query.lons = float(getlon[:ind]) ; query.lone = float(getlon[ind+1:]) |
---|
[811] | 54 | elif "," in getlon: islonfree = 1 ; ind = getlon.find(",") ; query.lons = float(getlon[:ind]) ; query.lone = float(getlon[ind+1:]) |
---|
| 55 | elif "/" in getlon: islonfree = 1 ; ind = getlon.find("/") ; query.lons = float(getlon[:ind]) ; query.lone = float(getlon[ind+1:]) |
---|
[761] | 56 | else: islonfree = 0 ; query.lon = float(getlon) |
---|
[793] | 57 | |
---|
[796] | 58 | query.loct = -9999. |
---|
[761] | 59 | getloct = form.getvalue("localtime") |
---|
[796] | 60 | if getloct == None: getloct = "1" |
---|
| 61 | if getloct == "all": isloctfree = 1 ; query.locts = 0. ; query.locte = 24. |
---|
| 62 | elif ";" in getloct: isloctfree = 1 ; ind = getloct.find(";") ; query.locts = float(getloct[:ind]) ; query.locte = float(getloct[ind+1:]) |
---|
[811] | 63 | elif "," in getloct: isloctfree = 1 ; ind = getloct.find(",") ; query.locts = float(getloct[:ind]) ; query.locte = float(getloct[ind+1:]) |
---|
| 64 | elif "/" in getloct: isloctfree = 1 ; ind = getloct.find("/") ; query.locts = float(getloct[:ind]) ; query.locte = float(getloct[ind+1:]) |
---|
[761] | 65 | else: isloctfree = 0 ; query.loct = float(getloct) |
---|
[793] | 66 | |
---|
[806] | 67 | try: query.zkey = int(form.getvalue("zkey")) |
---|
| 68 | except: query.zkey = int(3) |
---|
| 69 | |
---|
[796] | 70 | query.xz = -9999. |
---|
[761] | 71 | getalt = form.getvalue("altitude") |
---|
[796] | 72 | if getalt == None: getalt = "1" |
---|
[806] | 73 | if getalt == "all": |
---|
| 74 | isaltfree = 1 |
---|
| 75 | if query.zkey == 2: query.xzs = -5000. ; query.xze = 100000. |
---|
| 76 | elif query.zkey == 3: query.xzs = 0. ; query.xze = 120000. |
---|
| 77 | elif query.zkey == 5: query.xzs = -5000. ; query.xze = 100000. |
---|
| 78 | elif query.zkey == 4: query.xzs = 1000. ; query.xze = 0.001 |
---|
| 79 | elif query.zkey == 1: query.xzs = 3396000. ; query.xze = 3596000. |
---|
[796] | 80 | elif ";" in getalt: isaltfree = 1 ; ind = getalt.find(";") ; query.xzs = float(getalt[:ind]) ; query.xze = float(getalt[ind+1:]) |
---|
[811] | 81 | elif "," in getalt: isaltfree = 1 ; ind = getalt.find(",") ; query.xzs = float(getalt[:ind]) ; query.xze = float(getalt[ind+1:]) |
---|
| 82 | elif "/" in getalt: isaltfree = 1 ; ind = getalt.find("/") ; query.xzs = float(getalt[:ind]) ; query.xze = float(getalt[ind+1:]) |
---|
[761] | 83 | else: isaltfree = 0 ; query.xz = float(getalt) |
---|
[793] | 84 | |
---|
[761] | 85 | sumfree = islatfree + islonfree + isloctfree + isaltfree |
---|
| 86 | if sumfree > 2: exit() ## only 1D or 2D plots for the moment |
---|
[793] | 87 | |
---|
[805] | 88 | try: query.datekey = int(form.getvalue("datekeyhtml")) |
---|
| 89 | except: query.datekey = float(1) |
---|
| 90 | if query.datekey == 1: |
---|
| 91 | try: query.xdate = float(form.getvalue("ls")) |
---|
| 92 | except: query.xdate = float(1) |
---|
| 93 | else: |
---|
| 94 | try: query.xdate = float(form.getvalue("julian")) |
---|
| 95 | except: query.xdate = float(1) |
---|
| 96 | query.loct = 0. |
---|
| 97 | |
---|
[793] | 98 | try: query.hrkey = int(form.getvalue("hrkey")) |
---|
| 99 | except: query.hrkey = int(1) |
---|
| 100 | try: query.dust = int(form.getvalue("dust")) |
---|
| 101 | except: query.dust = int(1) |
---|
[639] | 102 | # self.perturkey = 0 #integer perturkey ! perturbation type (0: none) |
---|
| 103 | # self.seedin = 1 #random number generator seed (unused if perturkey=0) |
---|
| 104 | # self.gwlength = 0. #gravity Wave wavelength (unused if perturkey=0) |
---|
| 105 | |
---|
[761] | 106 | # Get variables to plot |
---|
| 107 | var1 = form.getvalue("var1") |
---|
| 108 | var2 = form.getvalue("var2") |
---|
| 109 | var3 = form.getvalue("var3") |
---|
| 110 | var4 = form.getvalue("var4") |
---|
[793] | 111 | |
---|
| 112 | # fg: vartoplot is not None without form values |
---|
| 113 | # vartoplot = [var1] |
---|
| 114 | # fg: init var as with form values |
---|
| 115 | if var1 == None: var1="t" |
---|
[794] | 116 | #if var2 == None: var2="p" |
---|
[793] | 117 | |
---|
| 118 | vartoplot = [] |
---|
| 119 | if var1 != "none": vartoplot = np.append(vartoplot,var1) |
---|
[794] | 120 | if var2 != "none" and var2 != None: vartoplot = np.append(vartoplot,var2) |
---|
[793] | 121 | if var3 != "none" and var3 != None: vartoplot = np.append(vartoplot,var3) |
---|
| 122 | if var4 != "none" and var4 != None: vartoplot = np.append(vartoplot,var4) |
---|
| 123 | |
---|
[761] | 124 | iswind = form.getvalue("iswind") |
---|
| 125 | if iswind == "on": iswindlog = True |
---|
| 126 | else: iswindlog = False |
---|
| 127 | isfixedlt = form.getvalue("isfixedlt") |
---|
| 128 | if isfixedlt == "on": input_fixedlt=True |
---|
| 129 | else: input_fixedlt=False |
---|
[639] | 130 | |
---|
| 131 | # reference name (to test which figures are already in the database) |
---|
[796] | 132 | reference = query.getnameset()+str(var1)+str(var2)+str(var3)+str(var4)+str(iswind)+str(isfixedlt) |
---|
[793] | 133 | figname = '../img/'+reference+'.png' |
---|
[811] | 134 | txtname = '../txt/'+reference |
---|
[639] | 135 | testexist = daos.path.isfile(figname) |
---|
| 136 | |
---|
| 137 | # extract data from MCD if needed |
---|
| 138 | if not testexist: |
---|
| 139 | |
---|
| 140 | ### 1D plots |
---|
| 141 | if sumfree == 1: |
---|
| 142 | |
---|
| 143 | ### getting data |
---|
[811] | 144 | if isloctfree == 1: query.diurnal(nd=24) |
---|
[639] | 145 | elif islonfree == 1: query.zonal() |
---|
| 146 | elif islatfree == 1: query.meridional() |
---|
| 147 | elif isaltfree == 1: query.profile() |
---|
| 148 | else: exit() |
---|
| 149 | |
---|
| 150 | ### generic building of figure |
---|
[811] | 151 | query.getascii(vartoplot,filename=txtname) |
---|
[800] | 152 | query.htmlplot1d(vartoplot,figname=figname) |
---|
[793] | 153 | #mpl.savefig("img/temp.png",dpi=85,bbox_inches='tight',pad_inches=0.25) |
---|
| 154 | #Image.open("../img/temp.png").save(figname,'JPEG') |
---|
[639] | 155 | |
---|
| 156 | ### 2D plots |
---|
| 157 | elif sumfree == 2: |
---|
| 158 | |
---|
| 159 | ### getting data |
---|
[806] | 160 | if islatfree == 1 and islonfree == 1: |
---|
| 161 | query.htmlmap2d(vartoplot,incwind=iswindlog,fixedlt=input_fixedlt,figname=figname) |
---|
| 162 | #mpl.savefig("img/temp.png",dpi=110,bbox_inches='tight',pad_inches=0.4) |
---|
| 163 | #Image.open("img/temp.png").save(figname,'JPEG') ##lighter images |
---|
| 164 | ### http://www.pythonware.com/library/pil/handbook/introduction.htm |
---|
| 165 | elif isaltfree == 1 and islonfree == 1: |
---|
| 166 | query.htmlplot2d(vartoplot,fixedlt=input_fixedlt,figname=figname) |
---|
| 167 | elif isaltfree == 1 and islatfree == 1: |
---|
| 168 | query.htmlplot2d(vartoplot,fixedlt=input_fixedlt,figname=figname) |
---|
| 169 | else: |
---|
| 170 | exit() |
---|
[761] | 171 | |
---|
[639] | 172 | ## This is quite common |
---|
[793] | 173 | print "Content-type:text/html\n" |
---|
| 174 | print " " #Apache needs a space after content-type |
---|
[639] | 175 | |
---|
[793] | 176 | #entete="""<?xml version="1.0" encoding="UTF-8"?> |
---|
| 177 | #<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd"> |
---|
| 178 | #<html xmlns="http://www.w3.org/1999/xhtml"> """ |
---|
| 179 | |
---|
| 180 | header="""<html><head><title>Mars Climate Database: The Web Interface</title></head><body>""" |
---|
| 181 | |
---|
| 182 | print header |
---|
[797] | 183 | #print query.printset() |
---|
| 184 | #print "<br />" |
---|
[793] | 185 | |
---|
[805] | 186 | print "<a href='../index.html'>Click here to start a new query</a><br />" |
---|
| 187 | |
---|
[639] | 188 | ## Now the part which differs |
---|
[761] | 189 | if sumfree == 0: query.update() ; query.htmlprinttabextvar(vartoplot) #query.printmeanvar() |
---|
[811] | 190 | elif sumfree == 2: print "<img src='"+figname+"'><br />" |
---|
| 191 | elif sumfree == 1: |
---|
| 192 | print "<a href='"+txtname+"'>Click here to download an ASCII file containing data</a><br />" |
---|
| 193 | print "<img src='"+figname+"'><br />" |
---|
[793] | 194 | else: print "<h1>ERROR : sumfree is not or badly defined ...</h1></body></html>" |
---|
[639] | 195 | |
---|
[793] | 196 | |
---|
[639] | 197 | ## This is quite common |
---|
[793] | 198 | bottom = "<hr><a href='../index.html'>Click here to start a new query</a>.<hr></body></html>" |
---|
[794] | 199 | #print bottom |
---|
[639] | 200 | |
---|
| 201 | ##write to file object |
---|
| 202 | #f = cStringIO.StringIO() |
---|
| 203 | #mpl.savefig(f) |
---|
| 204 | #f.seek(0) |
---|
| 205 | |
---|
| 206 | ##output to browser |
---|
| 207 | #print "Content-type: image/png\n" |
---|
| 208 | #print f.read() |
---|
| 209 | #exit() |
---|
| 210 | |
---|
| 211 | #print "Content-type:text/html\r\n\r\n" |
---|
| 212 | #print "<html>" |
---|
| 213 | #print "<head>" |
---|
| 214 | #print "<title>MCD. Simple Python interface</title>" |
---|
| 215 | #print "</head>" |
---|
| 216 | #print "<body>" |
---|
| 217 | |
---|
| 218 | |
---|
| 219 | |
---|
| 220 | |
---|
| 221 | |
---|
| 222 | |
---|
| 223 | ## HTTP Header |
---|
| 224 | #print "Content-Type:application/octet-stream; name=\"FileName\"\r\n"; |
---|
| 225 | #print "Content-Disposition: attachment; filename=\"FileName\"\r\n\n"; |
---|
| 226 | ## Actual File Content will go hear. |
---|
| 227 | #fo = open("foo.txt", "rb") |
---|
| 228 | #str = fo.read(); |
---|
| 229 | #print str |
---|
| 230 | ## Close opend file |
---|
| 231 | #fo.close() |
---|