[639] | 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 ### |
---|
| 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 |
---|
| 30 | query.lat = float(form.getvalue("latitude")) |
---|
| 31 | query.lon = float(form.getvalue("longitude")) |
---|
| 32 | query.loct = float(form.getvalue("localtime")) |
---|
| 33 | query.xdate = float(form.getvalue("ls")) |
---|
| 34 | query.xz = float(form.getvalue("altitude")) |
---|
| 35 | query.hrkey = int(form.getvalue("hrkey")) |
---|
| 36 | query.dust = int(form.getvalue("dust")) |
---|
| 37 | # self.zkey = 3 # specify that xz is the altitude above surface (m) |
---|
| 38 | # self.perturkey = 0 #integer perturkey ! perturbation type (0: none) |
---|
| 39 | # self.seedin = 1 #random number generator seed (unused if perturkey=0) |
---|
| 40 | # self.gwlength = 0. #gravity Wave wavelength (unused if perturkey=0) |
---|
| 41 | |
---|
| 42 | # Get free dimensions |
---|
| 43 | islatfree = float(form.getvalue("islatfree")) |
---|
| 44 | islonfree = float(form.getvalue("islonfree")) |
---|
| 45 | isloctfree = float(form.getvalue("isloctfree")) |
---|
| 46 | isaltfree = float(form.getvalue("isaltfree")) |
---|
| 47 | sumfree = islatfree + islonfree + isloctfree + isaltfree |
---|
| 48 | if sumfree > 2: exit() ## only 1D or 2D plots for the moment |
---|
| 49 | |
---|
| 50 | # reference name (to test which figures are already in the database) |
---|
| 51 | reference = str(islatfree)+str(islonfree)+str(isloctfree)+str(isaltfree)+query.getnameset() |
---|
| 52 | figname = 'img/'+reference+'.jpg' |
---|
| 53 | testexist = daos.path.isfile(figname) |
---|
| 54 | |
---|
| 55 | # extract data from MCD if needed |
---|
| 56 | if not testexist: |
---|
| 57 | |
---|
| 58 | ### 1D plots |
---|
| 59 | if sumfree == 1: |
---|
| 60 | |
---|
| 61 | ### getting data |
---|
| 62 | if isloctfree == 1: query.diurnal(nd=24) |
---|
| 63 | elif islonfree == 1: query.zonal() |
---|
| 64 | elif islatfree == 1: query.meridional() |
---|
| 65 | elif isaltfree == 1: query.profile() |
---|
| 66 | else: exit() |
---|
| 67 | |
---|
| 68 | ### generic building of figure |
---|
| 69 | query.plot1d(["t","p","u","v"],vertplot=isaltfree) |
---|
| 70 | mpl.savefig("img/temp.png",dpi=85,bbox_inches='tight',pad_inches=0.25) |
---|
| 71 | Image.open("img/temp.png").save(figname,'JPEG') |
---|
| 72 | |
---|
| 73 | ### 2D plots |
---|
| 74 | elif sumfree == 2: |
---|
| 75 | |
---|
| 76 | ### getting data |
---|
| 77 | if islatfree == 1 and islonfree == 1: query.latlon() |
---|
| 78 | else: exit() |
---|
| 79 | |
---|
| 80 | query.map2d(["t","u"]) |
---|
| 81 | mpl.savefig("img/temp.png",dpi=110,bbox_inches='tight',pad_inches=0.4) |
---|
| 82 | Image.open("img/temp.png").save(figname,'JPEG') ##lighter images |
---|
| 83 | ## http://www.pythonware.com/library/pil/handbook/introduction.htm |
---|
| 84 | |
---|
| 85 | ## This is quite common |
---|
| 86 | print "Content-type:text/html\r\n\r\n" |
---|
| 87 | print "<html>" |
---|
| 88 | print "<head>" |
---|
| 89 | print "<title>MCD. Simple Python interface</title>" |
---|
| 90 | print "</head>" |
---|
| 91 | print "<body>" |
---|
| 92 | |
---|
| 93 | ## Now the part which differs |
---|
| 94 | if sumfree == 0: query.update() ; query.printmeanvar() |
---|
| 95 | elif sumfree >= 1: print "<img src='../"+figname+"'><br />" |
---|
| 96 | else: exit() |
---|
| 97 | |
---|
| 98 | ## This is quite common |
---|
| 99 | print "Based on the <a href='http://www-mars.lmd.jussieu.fr'>Mars Climate Database</a> (c) LMD/OU/IAA/ESA/CNES.<br />" |
---|
| 100 | print "<hr>" |
---|
| 101 | query.printset() |
---|
| 102 | print "</body>" |
---|
| 103 | print "</html>" |
---|
| 104 | |
---|
| 105 | ##write to file object |
---|
| 106 | #f = cStringIO.StringIO() |
---|
| 107 | #mpl.savefig(f) |
---|
| 108 | #f.seek(0) |
---|
| 109 | |
---|
| 110 | ##output to browser |
---|
| 111 | #print "Content-type: image/png\n" |
---|
| 112 | #print f.read() |
---|
| 113 | #exit() |
---|
| 114 | |
---|
| 115 | #print "Content-type:text/html\r\n\r\n" |
---|
| 116 | #print "<html>" |
---|
| 117 | #print "<head>" |
---|
| 118 | #print "<title>MCD. Simple Python interface</title>" |
---|
| 119 | #print "</head>" |
---|
| 120 | #print "<body>" |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | ## HTTP Header |
---|
| 128 | #print "Content-Type:application/octet-stream; name=\"FileName\"\r\n"; |
---|
| 129 | #print "Content-Disposition: attachment; filename=\"FileName\"\r\n\n"; |
---|
| 130 | ## Actual File Content will go hear. |
---|
| 131 | #fo = open("foo.txt", "rb") |
---|
| 132 | #str = fo.read(); |
---|
| 133 | #print str |
---|
| 134 | ## Close opend file |
---|
| 135 | #fo.close() |
---|