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
RevLine 
[807]1#!/usr/bin/python
[796]2###!/usr/bin/env python
[793]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
[639]5### here the version used to f2py the MCD Fortran routines
6
7##################################################
8### A Python CGI for the Mars Climate Database ###
[761]9### ------------------------------------------ ###
10### Aymeric SPIGA 18-19/04/2012 ~ 11/08/2012   ###
11### ------------------------------------------ ###
[639]12### (see mcdtest.py for examples of use)       ###
13##################################################
[793]14### ajouts et corrections par Franck Guyon 09/2012
[796]15### ajouts suite a brainstorm equipe AS 10/2012
[639]16
17import cgi, cgitb 
18import numpy as np
[793]19#from mcd import mcd
20from modules import *
21from modules import mcd
22
[639]23import cStringIO
24import os as daos
25import matplotlib.pyplot as mpl
[793]26from PIL import Image
[639]27
28# for debugging in web browser
29cgitb.enable()
30
31# Create instance of FieldStorage
32form = cgi.FieldStorage() 
33
34# create a MCD object
[793]35#query = mcd()
36query=mcd.mcd() #FG: import from module mcd
[639]37
[761]38# Get data from user-defined fields and define free dimensions
[793]39# FG: add tests if var==None to have values in local without forms ones
[796]40query.lat = -9999.
[761]41getlat = form.getvalue("latitude")
[796]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:])
[811]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:])
[761]47else:                islatfree = 0 ; query.lat = float(getlat)
[794]48
[796]49query.lon = -9999.
[761]50getlon = form.getvalue("longitude")
[796]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:])
[811]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:])
[761]56else:                islonfree = 0 ; query.lon = float(getlon)
[793]57
[796]58query.loct = -9999.
[761]59getloct = form.getvalue("localtime")
[796]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:])
[811]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:])
[761]65else:                isloctfree = 0 ; query.loct = float(getloct)
[793]66
[806]67try: query.zkey = int(form.getvalue("zkey"))
68except: query.zkey = int(3)
69
[796]70query.xz = -9999.
[761]71getalt = form.getvalue("altitude")
[796]72if getalt == None: getalt = "1"
[806]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.
[796]80elif ";" in getalt:  isaltfree = 1 ; ind = getalt.find(";") ; query.xzs = float(getalt[:ind]) ; query.xze = float(getalt[ind+1:])
[811]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:])
[761]83else:                isaltfree = 0 ; query.xz = float(getalt)
[793]84
[761]85sumfree = islatfree + islonfree + isloctfree + isaltfree
86if sumfree > 2: exit() ## only 1D or 2D plots for the moment
[793]87
[805]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
[793]98try: query.hrkey = int(form.getvalue("hrkey"))
99except: query.hrkey = int(1)
100try: query.dust = int(form.getvalue("dust"))
101except: query.dust  = int(1)
[639]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
[761]106# Get variables to plot
107var1 = form.getvalue("var1")
108var2 = form.getvalue("var2")
109var3 = form.getvalue("var3")
110var4 = form.getvalue("var4")
[793]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"
[794]116#if var2 == None: var2="p"
[793]117
118vartoplot = []
119if var1 != "none": vartoplot = np.append(vartoplot,var1)
[794]120if var2 != "none" and var2 != None: vartoplot = np.append(vartoplot,var2)
[793]121if var3 != "none" and var3 != None: vartoplot = np.append(vartoplot,var3)
122if var4 != "none" and var4 != None: vartoplot = np.append(vartoplot,var4)
123
[761]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 
[639]130
131# reference name (to test which figures are already in the database)
[796]132reference = query.getnameset()+str(var1)+str(var2)+str(var3)+str(var4)+str(iswind)+str(isfixedlt)
[793]133figname = '../img/'+reference+'.png'
[811]134txtname = '../txt/'+reference
[639]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
[811]144    if isloctfree == 1:         query.diurnal(nd=24) 
[639]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
[811]151    query.getascii(vartoplot,filename=txtname)
[800]152    query.htmlplot1d(vartoplot,figname=figname)
[793]153    #mpl.savefig("img/temp.png",dpi=85,bbox_inches='tight',pad_inches=0.25)
154    #Image.open("../img/temp.png").save(figname,'JPEG')
[639]155
156  ### 2D plots
157  elif sumfree == 2:
158
159    ### getting data
[806]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() 
[761]171
[639]172## This is quite common
[793]173print "Content-type:text/html\n"
174print "  "  #Apache needs a space after content-type
[639]175
[793]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
[797]183#print query.printset()
184#print "<br />"
[793]185
[805]186print "<a href='../index.html'>Click here to start a new query</a><br />"
187
[639]188## Now the part which differs
[761]189if sumfree == 0:        query.update() ; query.htmlprinttabextvar(vartoplot)  #query.printmeanvar()
[811]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 />"
[793]194else:                   print "<h1>ERROR : sumfree is not or badly defined ...</h1></body></html>"
[639]195
[793]196
[639]197## This is quite common
[793]198bottom = "<hr><a href='../index.html'>Click here to start a new query</a>.<hr></body></html>"
[794]199#print bottom
[639]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.