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

Last change on this file since 811 was 811, checked in by aslmd, 12 years ago

UTIL PYTHON mcd interface. added option to print ASCII file. added sanity checks. topography contours are all solid lines. html section have cleaner bounds for axis. allowed for more separator namely , and /

  • Property svn:executable set to *
File size: 8.8 KB
Line 
1#!/usr/bin/python
2###!/usr/bin/env python
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
5### here the version used to f2py the MCD Fortran routines
6
7##################################################
8### A Python CGI for the Mars Climate Database ###
9### ------------------------------------------ ###
10### Aymeric SPIGA 18-19/04/2012 ~ 11/08/2012   ###
11### ------------------------------------------ ###
12### (see mcdtest.py for examples of use)       ###
13##################################################
14### ajouts et corrections par Franck Guyon 09/2012
15### ajouts suite a brainstorm equipe AS 10/2012
16
17import cgi, cgitb 
18import numpy as np
19#from mcd import mcd
20from modules import *
21from modules import mcd
22
23import cStringIO
24import os as daos
25import matplotlib.pyplot as mpl
26from PIL import Image
27
28# for debugging in web browser
29cgitb.enable()
30
31# Create instance of FieldStorage
32form = cgi.FieldStorage() 
33
34# create a MCD object
35#query = mcd()
36query=mcd.mcd() #FG: import from module mcd
37
38# Get data from user-defined fields and define free dimensions
39# FG: add tests if var==None to have values in local without forms ones
40query.lat = -9999.
41getlat = form.getvalue("latitude")
42if getlat == None: getlat = "1"
43if getlat == "all":  islatfree = 1 ; query.lats = -90. ; query.late = 90.
44elif ";" in getlat:  islatfree = 1 ; ind = getlat.find(";") ; query.lats = float(getlat[:ind]) ; query.late = float(getlat[ind+1:])
45elif "," in getlat:  islatfree = 1 ; ind = getlat.find(",") ; query.lats = float(getlat[:ind]) ; query.late = float(getlat[ind+1:])
46elif "/" in getlat:  islatfree = 1 ; ind = getlat.find("/") ; query.lats = float(getlat[:ind]) ; query.late = float(getlat[ind+1:])
47else:                islatfree = 0 ; query.lat = float(getlat)
48
49query.lon = -9999.
50getlon = form.getvalue("longitude")
51if getlon == None: getlon = "1"
52if getlon == "all":  islonfree = 1 ; query.lons = -180. ; query.lone = 180.
53elif ";" in getlon:  islonfree = 1 ; ind = getlon.find(";") ; query.lons = float(getlon[:ind]) ; query.lone = float(getlon[ind+1:])
54elif "," in getlon:  islonfree = 1 ; ind = getlon.find(",") ; query.lons = float(getlon[:ind]) ; query.lone = float(getlon[ind+1:])
55elif "/" in getlon:  islonfree = 1 ; ind = getlon.find("/") ; query.lons = float(getlon[:ind]) ; query.lone = float(getlon[ind+1:])
56else:                islonfree = 0 ; query.lon = float(getlon)
57
58query.loct = -9999.
59getloct = form.getvalue("localtime")
60if getloct == None: getloct = "1"
61if getloct == "all": isloctfree = 1 ; query.locts = 0. ; query.locte = 24.
62elif ";" in getloct: isloctfree = 1 ; ind = getloct.find(";") ; query.locts = float(getloct[:ind]) ; query.locte = float(getloct[ind+1:])
63elif "," in getloct: isloctfree = 1 ; ind = getloct.find(",") ; query.locts = float(getloct[:ind]) ; query.locte = float(getloct[ind+1:])
64elif "/" in getloct: isloctfree = 1 ; ind = getloct.find("/") ; query.locts = float(getloct[:ind]) ; query.locte = float(getloct[ind+1:])
65else:                isloctfree = 0 ; query.loct = float(getloct)
66
67try: query.zkey = int(form.getvalue("zkey"))
68except: query.zkey = int(3)
69
70query.xz = -9999.
71getalt = form.getvalue("altitude")
72if getalt == None: getalt = "1"
73if 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.
80elif ";" in getalt:  isaltfree = 1 ; ind = getalt.find(";") ; query.xzs = float(getalt[:ind]) ; query.xze = float(getalt[ind+1:])
81elif "," in getalt:  isaltfree = 1 ; ind = getalt.find(",") ; query.xzs = float(getalt[:ind]) ; query.xze = float(getalt[ind+1:])
82elif "/" in getalt:  isaltfree = 1 ; ind = getalt.find("/") ; query.xzs = float(getalt[:ind]) ; query.xze = float(getalt[ind+1:])
83else:                isaltfree = 0 ; query.xz = float(getalt)
84
85sumfree = islatfree + islonfree + isloctfree + isaltfree
86if sumfree > 2: exit() ## only 1D or 2D plots for the moment
87
88try: query.datekey = int(form.getvalue("datekeyhtml"))
89except: query.datekey = float(1)
90if query.datekey == 1:
91    try: query.xdate = float(form.getvalue("ls"))
92    except: query.xdate = float(1)
93else:
94    try: query.xdate = float(form.getvalue("julian"))
95    except: query.xdate = float(1)
96    query.loct = 0.
97
98try: query.hrkey = int(form.getvalue("hrkey"))
99except: query.hrkey = int(1)
100try: query.dust = int(form.getvalue("dust"))
101except: query.dust  = int(1)
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
106# Get variables to plot
107var1 = form.getvalue("var1")
108var2 = form.getvalue("var2")
109var3 = form.getvalue("var3")
110var4 = form.getvalue("var4")
111
112# fg: vartoplot is not None without form values
113# vartoplot = [var1]
114# fg: init var as with form values
115if var1 == None: var1="t"
116#if var2 == None: var2="p"
117
118vartoplot = []
119if var1 != "none": vartoplot = np.append(vartoplot,var1)
120if var2 != "none" and var2 != None: vartoplot = np.append(vartoplot,var2)
121if var3 != "none" and var3 != None: vartoplot = np.append(vartoplot,var3)
122if var4 != "none" and var4 != None: vartoplot = np.append(vartoplot,var4)
123
124iswind = form.getvalue("iswind")
125if iswind == "on": iswindlog = True
126else:              iswindlog = False
127isfixedlt = form.getvalue("isfixedlt")
128if isfixedlt == "on": input_fixedlt=True
129else:                 input_fixedlt=False 
130
131# reference name (to test which figures are already in the database)
132reference = query.getnameset()+str(var1)+str(var2)+str(var3)+str(var4)+str(iswind)+str(isfixedlt)
133figname = '../img/'+reference+'.png'
134txtname = '../txt/'+reference
135testexist = daos.path.isfile(figname)
136
137# extract data from MCD if needed
138if not testexist:
139
140  ### 1D plots
141  if sumfree == 1:
142
143    ### getting data
144    if isloctfree == 1:         query.diurnal(nd=24) 
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
151    query.getascii(vartoplot,filename=txtname)
152    query.htmlplot1d(vartoplot,figname=figname)
153    #mpl.savefig("img/temp.png",dpi=85,bbox_inches='tight',pad_inches=0.25)
154    #Image.open("../img/temp.png").save(figname,'JPEG')
155
156  ### 2D plots
157  elif sumfree == 2:
158
159    ### getting data
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() 
171
172## This is quite common
173print "Content-type:text/html\n"
174print "  "  #Apache needs a space after content-type
175
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
180header="""<html><head><title>Mars Climate Database: The Web Interface</title></head><body>"""
181
182print header
183#print query.printset()
184#print "<br />"
185
186print "<a href='../index.html'>Click here to start a new query</a><br />"
187
188## Now the part which differs
189if sumfree == 0:        query.update() ; query.htmlprinttabextvar(vartoplot)  #query.printmeanvar()
190elif sumfree == 2:      print "<img src='"+figname+"'><br />"
191elif sumfree == 1:     
192    print "<a href='"+txtname+"'>Click here to download an ASCII file containing data</a><br />"
193    print "<img src='"+figname+"'><br />"
194else:                   print "<h1>ERROR : sumfree is not or badly defined ...</h1></body></html>"
195
196
197## This is quite common
198bottom = "<hr><a href='../index.html'>Click here to start a new query</a>.<hr></body></html>"
199#print bottom
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()
Note: See TracBrowser for help on using the repository browser.