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

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

UTIL PYTHON mcd online. improved interface for time (earth and mars). improved titles.

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