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