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

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

UTIL PYTHON : a working version of MCD python on an Apache server thanks to F. Guyon and L. Fairhead. plus added specific functions in mcd.py to make plots without using GUI, it was necessary to rewrite some stuff to avoid calling to matplotlib.pyplot.

  • Property svn:executable set to *
File size: 5.9 KB
Line 
1#!/usr/bin/python
2###!/home/aymeric/Software/epd-7.0-2-rh5-x86/bin/python
3####!/home/marshttp/EPD/epd-7.0-2-rh5-x86_64/bin/python
4### here the version used to f2py the MCD Fortran routines
5
6##################################################
7### A Python CGI for the Mars Climate Database ###
8### ------------------------------------------ ###
9### Aymeric SPIGA 18-19/04/2012 ~ 11/08/2012   ###
10### ------------------------------------------ ###
11### (see mcdtest.py for examples of use)       ###
12##################################################
13### ajouts et corrections par Franck Guyon 09/2012
14
15import cgi, cgitb 
16import numpy as np
17#from mcd import mcd
18from modules import *
19from modules import mcd
20
21import cStringIO
22import os as daos
23import matplotlib.pyplot as mpl
24from PIL import Image
25
26# for debugging in web browser
27cgitb.enable()
28
29# Create instance of FieldStorage
30form = cgi.FieldStorage() 
31
32# create a MCD object
33#query = mcd()
34query=mcd.mcd() #FG: import from module mcd
35
36# Get data from user-defined fields and define free dimensions
37# FG: add tests if var==None to have values in local without forms ones
38getlat = form.getvalue("latitude")
39if getlat == None: getlat = 1
40
41if getlat == "all":  islatfree = 1 ; query.lat = -9999.
42else:                islatfree = 0 ; query.lat = float(getlat)
43getlon = form.getvalue("longitude")
44if getlon == None: getlon = 1
45if getlon == "all":  islonfree = 1 ; query.lon = -9999.
46else:                islonfree = 0 ; query.lon = float(getlon)
47
48getloct = form.getvalue("localtime")
49
50if getloct == None: getloct = 1
51if getloct == "all": isloctfree = 1 ; query.loct = -9999.
52else:                isloctfree = 0 ; query.loct = float(getloct)
53
54getalt = form.getvalue("altitude")
55if getalt == None: getalt = 1
56if getalt == "all":  isaltfree = 1 ; query.xz = -9999.
57else:                isaltfree = 0 ; query.xz = float(getalt)
58
59sumfree = islatfree + islonfree + isloctfree + isaltfree
60if sumfree > 2: exit() ## only 1D or 2D plots for the moment
61
62try: query.xdate = float(form.getvalue("ls"))
63except: query.xdate = float(1)
64try: query.hrkey = int(form.getvalue("hrkey"))
65except: query.hrkey = int(1)
66try: query.dust = int(form.getvalue("dust"))
67except: query.dust  = int(1)
68#        self.zkey      = 3  # specify that xz is the altitude above surface (m)
69#        self.perturkey = 0  #integer perturkey ! perturbation type (0: none)
70#        self.seedin    = 1  #random number generator seed (unused if perturkey=0)
71#        self.gwlength  = 0. #gravity Wave wavelength (unused if perturkey=0)
72
73# Get variables to plot
74var1 = form.getvalue("var1")
75var2 = form.getvalue("var2")
76var3 = form.getvalue("var3")
77var4 = form.getvalue("var4")
78
79# fg: vartoplot is not None without form values
80# vartoplot = [var1]
81# fg: init var as with form values
82if var1 == None: var1="t"
83if var2 == None: var2="p"
84
85vartoplot = []
86if var1 != "none": vartoplot = np.append(vartoplot,var1)
87if var2 != "none": vartoplot = np.append(vartoplot,var2)
88if var3 != "none" and var3 != None: vartoplot = np.append(vartoplot,var3)
89if var4 != "none" and var4 != None: vartoplot = np.append(vartoplot,var4)
90
91iswind = form.getvalue("iswind")
92if iswind == "on": iswindlog = True
93else:              iswindlog = False
94isfixedlt = form.getvalue("isfixedlt")
95if isfixedlt == "on": input_fixedlt=True
96else:                 input_fixedlt=False 
97
98# reference name (to test which figures are already in the database)
99reference = str(islatfree)+str(islonfree)+str(isloctfree)+str(isaltfree)+query.getnameset()+str(var1)+str(var2)+str(var3)+str(var4)+str(iswind)+str(isfixedlt)
100figname = '../img/'+reference+'.png'
101
102testexist = daos.path.isfile(figname)
103
104# extract data from MCD if needed
105if not testexist:
106
107  ### 1D plots
108  if sumfree == 1:
109
110    ### getting data
111    if isloctfree == 1:         query.diurnal(nd=24)
112    elif islonfree == 1:        query.zonal()
113    elif islatfree == 1:        query.meridional()
114    elif isaltfree == 1:        query.profile()   
115    else:                       exit() 
116
117    ### generic building of figure
118    query.htmlplot1d(vartoplot,vertplot=isaltfree,figname=figname)
119    #mpl.savefig("img/temp.png",dpi=85,bbox_inches='tight',pad_inches=0.25)
120    #Image.open("../img/temp.png").save(figname,'JPEG')
121
122  ### 2D plots
123  elif sumfree == 2:
124
125    ### getting data
126    if islatfree == 1 and islonfree == 1:       query.latlon()   
127    else:                                       exit() 
128
129    ### figure   
130    query.map2d(vartoplot,incwind=iswindlog,fixedlt=input_fixedlt) 
131    mpl.savefig("img/temp.png",dpi=110,bbox_inches='tight',pad_inches=0.4)
132    Image.open("img/temp.png").save(figname,'JPEG') ##lighter images   
133    ## http://www.pythonware.com/library/pil/handbook/introduction.htm
134
135## This is quite common
136print "Content-type:text/html\n"
137print "  "  #Apache needs a space after content-type
138
139#entete="""<?xml version="1.0" encoding="UTF-8"?>
140#<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd">
141#<html xmlns="http://www.w3.org/1999/xhtml"> """
142
143header="""<html><head><title>Mars Climate Database: The Web Interface</title></head><body>"""
144
145print header
146
147## Now the part which differs
148if sumfree == 0:        query.update() ; query.htmlprinttabextvar(vartoplot)  #query.printmeanvar()
149elif sumfree >= 1:      print "<img src='"+figname+"'><br />"
150else:                   print "<h1>ERROR : sumfree is not or badly defined ...</h1></body></html>"
151
152
153## This is quite common
154bottom = "<hr><a href='../index.html'>Click here to start a new query</a>.<hr></body></html>"
155print bottom
156
157##write to file object
158#f = cStringIO.StringIO()
159#mpl.savefig(f)
160#f.seek(0)
161
162##output to browser
163#print "Content-type: image/png\n"
164#print f.read()
165#exit()
166
167#print "Content-type:text/html\r\n\r\n"
168#print "<html>"
169#print "<head>"
170#print "<title>MCD. Simple Python interface</title>"
171#print "</head>"
172#print "<body>"
173
174
175
176
177
178
179## HTTP Header
180#print "Content-Type:application/octet-stream; name=\"FileName\"\r\n";
181#print "Content-Disposition: attachment; filename=\"FileName\"\r\n\n";
182## Actual File Content will go hear.
183#fo = open("foo.txt", "rb")
184#str = fo.read();
185#print str
186## Close opend file
187#fo.close()
Note: See TracBrowser for help on using the repository browser.