source: trunk/UTIL/PYTHON/mcd/proto/cgi-bin/mcdcgi.py @ 760

Last change on this file since 760 was 639, checked in by aslmd, 13 years ago

UTIL PYTHON : Interfacing MCD with python. (Also a toy web server in proto; for the moment, do not use nor modify).

  • Property svn:executable set to *
File size: 4.0 KB
Line 
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
12import cgi, cgitb 
13import numpy as np
14from mcd import mcd
15import cStringIO
16import os as daos
17import matplotlib.pyplot as mpl
18import Image
19
20# for debugging in web browser
21cgitb.enable()
22
23# Create instance of FieldStorage
24form = cgi.FieldStorage() 
25
26# create a MCD object
27query = mcd()
28
29# Get data from user-defined fields
30query.lat = float(form.getvalue("latitude"))
31query.lon = float(form.getvalue("longitude"))
32query.loct = float(form.getvalue("localtime"))
33query.xdate = float(form.getvalue("ls"))
34query.xz = float(form.getvalue("altitude"))
35query.hrkey = int(form.getvalue("hrkey"))
36query.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
43islatfree  = float(form.getvalue("islatfree"))
44islonfree  = float(form.getvalue("islonfree"))
45isloctfree = float(form.getvalue("isloctfree"))
46isaltfree  = float(form.getvalue("isaltfree"))
47sumfree = islatfree + islonfree + isloctfree + isaltfree
48if sumfree > 2: exit() ## only 1D or 2D plots for the moment
49
50# reference name (to test which figures are already in the database)
51reference = str(islatfree)+str(islonfree)+str(isloctfree)+str(isaltfree)+query.getnameset()
52figname = 'img/'+reference+'.jpg'
53testexist = daos.path.isfile(figname)
54
55# extract data from MCD if needed
56if 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
86print "Content-type:text/html\r\n\r\n"
87print "<html>"
88print "<head>"
89print "<title>MCD. Simple Python interface</title>"
90print "</head>"
91print "<body>"
92
93## Now the part which differs
94if sumfree == 0:        query.update() ; query.printmeanvar() 
95elif sumfree >= 1:      print "<img src='../"+figname+"'><br />"
96else:                   exit()
97
98## This is quite common
99print "Based on the <a href='http://www-mars.lmd.jussieu.fr'>Mars Climate Database</a> (c) LMD/OU/IAA/ESA/CNES.<br />"
100print "<hr>"
101query.printset()
102print "</body>"
103print "</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()
Note: See TracBrowser for help on using the repository browser.