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