source: trunk/UTIL/PYTHON/mcd/mcd.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: 26.3 KB
Line 
1####################################################
2### A Python Class for the Mars Climate Database ###
3### ---------------------------------------------###
4### Aymeric SPIGA 17-21/04/2012                  ###
5### ---------------------------------------------###
6### (see mcdtest.py for examples of use)         ###
7####################################################
8
9import numpy as np
10import fmcd
11import matplotlib.pyplot as mpl
12import myplot
13
14
15class mcd():
16 
17    def __repr__(self):
18    # print out a help string when help is invoked on the object
19        whatprint = 'MCD object. \"help(mcd)\" for more information\n'
20        return whatprint
21
22########################
23### Default settings ###
24########################
25
26    def __init__(self):
27    # default settings
28        ## 0. general stuff
29        self.name      = "MCD v4.3"
30        self.ack       = "Mars Climate Database (c) LMD/OU/IAA/ESA/CNES"
31        #self.dset      = '/home/aymeric/Science/MCD_v4.3/data/'
32        self.dset      = '/home/marshttp/MCD_v4.3/data/'
33        ## 1. spatio-temporal coordinates
34        self.lat       = 0.
35        self.lats      = None
36        self.late      = None
37        self.lon       = 0.
38        self.lons      = None
39        self.lone      = None
40        self.loct      = 0.
41        self.locts     = None
42        self.locte     = None
43        self.xdate     = 0.  # see datekey
44        self.xdates    = None
45        self.xdatee    = None
46        self.xz        = 10. # see zkey
47        self.xzs       = None
48        self.xze       = None
49        ## 1bis. related settings
50        self.zkey      = 3  # specify that xz is the altitude above surface (m)
51                            # zkey  : <integer>   type of vertical coordinate xz
52                            # 1 = radius from centre of planet (m)
53                            # 2 = height above areoid (m) (MOLA zero datum)
54                            # 3 = height above surface (m)
55                            # 4 = pressure level (Pa)
56                            # 5 = altitude above mean Mars Radius(=3396000m) (m)
57        self.datekey   = 1  # 0 = "Earth time": xdate is given in Julian days (localtime must be set to zero)
58                            # 1 = "Mars date": xdate is the value of Ls
59        ## 2. climatological options
60        self.dust      = 2  #our best guess MY24 scenario, with solar average conditions
61        self.hrkey     = 1  #set high resolution mode on (hrkey=0 to set high resolution off)
62        ## 3. additional settings for advanced use
63        self.extvarkey = 1  #extra output variables (1: yes, 0: no)
64        self.perturkey = 0  #integer perturkey ! perturbation type (0: none)
65        self.seedin    = 1  #random number generator seed (unused if perturkey=0)
66        self.gwlength  = 0. #gravity Wave wavelength (unused if perturkey=0)
67        ## outputs. just to define attributes.
68        ## --> in update
69        self.pres = None ; self.dens = None ; self.temp = None ; self.zonwind = None ; self.merwind = None ; self.meanvar = None ; self.extvar = None
70        self.seedout = None ; self.ierr = None
71        ## --> in prepare
72        self.xcoord = None ; self.ycoord = None
73        self.prestab = None ; self.denstab = None ; self.temptab = None 
74        self.zonwindtab = None ; self.merwindtab = None ; self.meanvartab = None ; self.extvartab = None
75        ## plot stuff
76        self.xlabel = None ; self.ylabel = None
77        self.vertplot = False
78
79    def viking1(self): self.name = "Viking 1 site. MCD v4.3 output" ; self.lat = 22.48 ; self.lon = -49.97 ; self.xdate = 97.
80    def viking2(self): self.name = "Viking 2 site. MCD v4.3 output" ; self.lat = 47.97 ; self.lon = -225.74 ; self.xdate = 117.6
81
82    def getdustlabel(self):
83        if self.dust == 1: self.dustlabel = "MY24 minimum solar scenario"
84        elif self.dust == 2: self.dustlabel = "MY24 average solar scenario"
85        elif self.dust == 3: self.dustlabel = "MY24 maximum solar scenario"
86        elif self.dust == 4: self.dustlabel = "dust storm minimum solar scenario"
87        elif self.dust == 5: self.dustlabel = "dust storm average solar scenario"
88        elif self.dust == 6: self.dustlabel = "dust storm maximum solar scenario"
89        elif self.dust == 7: self.dustlabel = "warm scenario (dusty, maximum solar)"
90        elif self.dust == 8: self.dustlabel = "cold scenario (low dust, minimum solar)"
91
92    def gettitle(self):
93        self.getdustlabel()
94        self.title = self.name + " with " + self.dustlabel + "."
95        if self.lats is None:  self.title = self.title + " Latitude " + str(self.lat) + "E"
96        if self.lons is None:  self.title = self.title + " Longitude " + str(self.lon) + "N"
97        if self.xzs is None:   
98            self.vertunits()
99            self.title = self.title + " Altitude " + str(self.xz) + " " + self.vunits
100        if self.locts is None: self.title = self.title + " Local time " + str(self.loct) + "h"
101
102    def getextvarlab(self,num):
103        whichfield = { \
104        91: "Pressure (Pa)", \
105        92: "Density (kg/m3)", \
106        93: "Temperature (K)", \
107        94: "W-E wind component (m/s)", \
108        95: "S-N wind component (m/s)", \
109        1: "Radial distance from planet center (m)",\
110        2: "Altitude above areoid (Mars geoid) (m)",\
111        3: "Altitude above local surface (m)",\
112        4: "orographic height (m) (surface altitude above areoid)",\
113        5: "Ls, solar longitude of Mars (deg)",\
114        6: "LST local true solar time (hrs)",\
115        7: "Universal solar time (LST at lon=0) (hrs)",\
116        8: "Air heat capacity Cp (J kg-1 K-1)",\
117        9: "gamma=Cp/Cv Ratio of specific heats",\
118        10: "density RMS day to day variations (kg/m^3)",\
119        11: "[not defined]",\
120        12: "[not defined]",\
121        13: "scale height H(p) (m)",\
122        14: "GCM orography (m)",\
123        15: "surface temperature (K)",\
124        16: "daily maximum mean surface temperature (K)",\
125        17: "daily minimum mean surface temperature (K)",\
126        18: "surf. temperature RMS day to day variations (K)",\
127        19: "surface pressure (high resolution if hireskey=1)",\
128        20: "GCM surface pressure (Pa)",\
129        21: "atmospheric pressure RMS day to day variations (Pa)",\
130        22: "surface pressure RMS day to day variations (Pa)",\
131        23: "temperature RMS day to day variations (K)",\
132        24: "zonal wind RMS day to day variations (m/s)",\
133        25: "meridional wind RMS day to day variations (m/s)",\
134        26: "vertical wind component (m/s) >0 when downwards!",\
135        27: "vertical wind RMS day to day variations (m/s)",\
136        28: "small scale perturbation (gravity wave) (kg/m^3)",\
137        29: "q2: turbulent kinetic energy (m2/s2)",\
138        30: "[not defined]",\
139        31: "thermal IR flux to surface (W/m2)",\
140        32: "solar flux to surface (W/m2)",\
141        33: "thermal IR flux to space (W/m2)",\
142        34: "solar flux reflected to space (W/m2)",\
143        35: "surface CO2 ice layer (kg/m2)",\
144        36: "DOD: Dust column visible optical depth",\
145        37: "Dust mass mixing ratio (kg/kg)",\
146        38: "DOD RMS day to day variations",\
147        39: "DOD total standard deviation over season",\
148        40: "Water vapor column (kg/m2)",\
149        41: "Water vapor vol. mixing ratio (mol/mol)",\
150        42: "Water ice column (kg/m2)",\
151        43: "Water ice mixing ratio (mol/mol)",\
152        44: "O3 ozone vol. mixing ratio (mol/mol)",\
153        45: "[CO2] vol. mixing ratio (mol/mol)",\
154        46: "[O] vol. mixing ratio (mol/mol)",\
155        47: "[N2] vol. mixing ratio (mol/mol)",\
156        48: "[CO] vol. mixing ratio (mol/mol)",\
157        49: "R: Molecular gas constant (J K-1 kg-1)",\
158        50: "Air viscosity estimation (N s m-2)"
159        }
160        if num not in whichfield: myplot.errormess("Incorrect subscript in extvar.")
161        return whichfield[num]
162
163    def convertlab(self,num):       
164        ## a conversion from text inquiries to extvar numbers. to be completed.
165        if num == "p": num = 91
166        elif num == "rho": num = 92
167        elif num == "t": num = 93
168        elif num == "u": num = 94
169        elif num == "v": num = 95
170        elif num == "tsurf": num = 15
171        elif num == "topo": num = 4
172        elif num == "h": num = 13
173        elif num == "ps": num = 19
174        elif num == "tau": num = 36
175        elif num == "mtot": num = 40
176        elif num == "icetot": num = 42
177        elif num == "ps_ddv": num = 22
178        elif num == "h2ovap": num = 41
179        elif num == "h2oice": num = 43
180        elif num == "cp": num = 8
181        elif num == "rho_ddv": num = 10
182        elif num == "tsurfmx": num = 16
183        elif num == "tsurfmn": num = 17
184        elif num == "lwdown": num = 31
185        elif num == "swdown": num = 32
186        elif num == "lwup": num = 33
187        elif num == "swup": num = 34
188        elif num == "o3": num = 44
189        elif num == "o": num = 46
190        elif num == "co": num = 48
191        elif num == "visc": num = 50
192        elif num == "co2ice": num = 35
193        elif not isinstance(num, np.int): myplot.errormess("field reference not found.")
194        return num
195
196###################
197### One request ###
198###################
199
200    def update(self):
201    # retrieve fields from MCD (call_mcd). more info in fmcd.call_mcd.__doc__
202        (self.pres, self.dens, self.temp, self.zonwind, self.merwind, \
203         self.meanvar, self.extvar, self.seedout, self.ierr) \
204         = \
205         fmcd.call_mcd(self.zkey,self.xz,self.lon,self.lat,self.hrkey, \
206             self.datekey,self.xdate,self.loct,self.dset,self.dust, \
207             self.perturkey,self.seedin,self.gwlength,self.extvarkey )
208        ## we use the end of extvar (unused) to store meanvar. this is convenient for getextvar(lab)
209        self.extvar[90] = self.pres ; self.extvar[91] = self.dens
210        self.extvar[92] = self.temp ; self.extvar[93] = self.zonwind ; self.extvar[94] = self.merwind
211        ## treat missing values
212        if self.temp == -999: self.extvar[:] = np.NaN ; self.meanvar[:] = np.NaN
213
214    def printset(self):
215    # print main settings
216        print "zkey",self.zkey,"xz",self.xz,"lon",self.lon,"lat",self.lat,"hrkey",self.hrkey, \
217              "xdate",self.xdate,"loct",self.loct,"dust",self.dust
218
219    def getnameset(self):
220    # set a name referring to settings [convenient for databases]
221        strlat = str(self.lat)+str(self.lats)+str(self.late)
222        strlon = str(self.lon)+str(self.lons)+str(self.lone)
223        strxz = str(self.xz)+str(self.xzs)+str(self.xze)
224        strloct = str(self.loct)+str(self.locts)+str(self.locte)
225        name = str(self.zkey)+strxz+strlon+strlat+str(self.hrkey)+str(self.datekey)+str(self.xdate)+strloct+str(self.dust)
226        return name
227
228    def printcoord(self):
229    # print requested space-time coordinates
230        print "LAT",self.lat,"LON",self.lon,"LOCT",self.loct,"XDATE",self.xdate
231
232    def printmeanvar(self):
233    # print mean MCD variables
234        print "Pressure = %5.3f pascals. " % (self.pres)
235        print "Density = %5.3f kilograms per cubic meter. " % (self.dens)
236        print "Temperature = %3.0f kelvins (%4.0f degrees celsius)." % (self.temp,self.temp-273.15)
237        print "Zonal wind = %5.3f meters per second." % (self.zonwind)
238        print "Meridional wind = %5.3f meters per second." % (self.merwind)
239        print "Total horizontal wind = %5.3f meters per second." % ( np.sqrt(self.zonwind**2 + self.merwind**2) )
240
241    def printextvar(self,num):
242    # print extra MCD variables
243        num = self.convertlab(num)
244        print self.getextvarlab(num) + " ..... " + str(self.extvar[num-1])
245
246    def printallextvar(self):
247    # print all extra MCD variables   
248        for i in range(50): self.printextvar(i+1)
249
250    def htmlprinttabextvar(self,tabtodo):
251        print "Results from the Mars Climate Database"
252        print "<ul>"
253        for i in range(len(tabtodo)): print "<li>" ; self.printextvar(tabtodo[i]) ; print "</li>"
254        print "</ul>"
255        print "<hr>"
256        print "SETTINGS<br />"
257        self.printcoord()
258        self.printset()
259
260    def printmcd(self):
261    # 1. call MCD 2. print settings 3. print mean vars
262        self.update()
263        self.printcoord()
264        print "-------------------------------------------"
265        self.printmeanvar()
266
267########################
268### Several requests ###
269########################
270
271    def prepare(self,ndx=None,ndy=None):
272    ### prepare I/O arrays for 1d slices
273      if ndx is None:  print "No dimension in prepare. Exit. Set at least ndx." ; exit()
274      else:            self.xcoord = np.ones(ndx)
275      if ndy is None:  dashape = (ndx)     ; dashapemean = (ndx,6)     ; dashapeext = (ndx,101)     ; self.ycoord = None
276      else:            dashape = (ndx,ndy) ; dashapemean = (ndx,ndy,6) ; dashapeext = (ndx,ndy,101) ; self.ycoord = np.ones(ndy)
277      self.prestab = np.ones(dashape) ; self.denstab = np.ones(dashape) ; self.temptab = np.ones(dashape)
278      self.zonwindtab = np.ones(dashape) ; self.merwindtab = np.ones(dashape) 
279      self.meanvartab = np.ones(dashapemean) ; self.extvartab = np.ones(dashapeext)
280
281    def getextvar(self,num):
282    ### get a given var in extvartab
283      try: field=self.extvartab[:,:,num] 
284      except: field=self.extvartab[:,num]
285      return field
286
287    def definefield(self,choice):
288    ### for analysis or plot purposes, set field and field label from user-defined choice
289      choice = self.convertlab(choice)
290      field = self.getextvar(choice); fieldlab = self.getextvarlab(choice)
291      return field,fieldlab
292
293    def ininterv(self,dstart,dend,nd,start=None,end=None,yaxis=False):
294    ### user-defined start and end are used to create xcoord (or ycoord) vector
295      if start is not None and end is not None:  first, second = self.correctbounds(start,end)
296      else:                                      first, second = self.correctbounds(dstart,dend) 
297      if self.zkey != 4: tabtab = np.linspace(first,second,nd)
298      else:              tabtab = np.logspace(first,second,nd)
299      if not yaxis:      self.xcoord = tabtab
300      else:              self.ycoord = tabtab
301
302    def correctbounds(self,start,end):
303      if self.zkey != 4:
304        # regular altitudes
305        if start > end: first = end ; second = start
306        else:           first = start ; second = end
307      else:
308        # pressure: reversed avis
309        if start < end: first = np.log10(end) ; second = np.log10(start)
310        else:           first = np.log10(start) ; second = np.log10(end)
311      return first, second
312
313    def vertlabel(self):
314      if self.zkey == 1:   self.xlabel = "radius from centre of planet (m)"
315      elif self.zkey == 2: self.xlabel = "height above areoid (m) (MOLA zero datum)"
316      elif self.zkey == 3: self.xlabel = "height above surface (m)"
317      elif self.zkey == 4: self.xlabel = "pressure level (Pa)"
318      elif self.zkey == 5: self.xlabel = "altitude above mean Mars Radius(=3396000m) (m)"
319
320    def vertunits(self):
321      if self.zkey == 1:   self.vunits = "m CP"
322      elif self.zkey == 2: self.vunits = "m AMR"
323      elif self.zkey == 3: self.vunits = "m ALS"
324      elif self.zkey == 4: self.vunits = "Pa"
325      elif self.zkey == 5: self.vunits = "m AMMRad"
326
327###################
328### 1D analysis ###
329###################
330
331    def put1d(self,i):
332    ## fill in subscript i in output arrays
333    ## (arrays must have been correctly defined through prepare)
334      if self.prestab is None:  myplot.errormess("arrays must be prepared first through self.prepare")
335      self.prestab[i] = self.pres ; self.denstab[i] = self.dens ; self.temptab[i] = self.temp
336      self.zonwindtab[i] = self.zonwind ; self.merwindtab[i] = self.merwind
337      self.meanvartab[i,1:5] = self.meanvar[0:4]  ## note: var numbering according to MCD manual is kept
338      self.extvartab[i,1:100] = self.extvar[0:99] ## note: var numbering according to MCD manual is kept
339
340    def diurnal(self,nd=13):
341    ### retrieve a local time slice
342      save = self.loct
343      self.xlabel = "Local time (Martian hour)"
344      self.prepare(ndx=nd) ; self.ininterv(0.,24.,nd,start=self.locts,end=self.locte) 
345      for i in range(nd): self.loct = self.xcoord[i] ; self.update() ; self.put1d(i)
346      self.loct = save
347
348    def zonal(self,nd=37):
349    ### retrieve a longitude slice
350      save = self.lon
351      self.xlabel = "East longitude (degrees)"
352      self.prepare(ndx=nd) ; self.ininterv(-180.,180.,nd,start=self.lons,end=self.lone)
353      for i in range(nd): self.lon = self.xcoord[i] ; self.update() ; self.put1d(i)
354      self.lon = save
355
356    def meridional(self,nd=19):
357    ### retrieve a latitude slice
358      save = self.lat
359      self.xlabel = "North latitude (degrees)"
360      self.prepare(ndx=nd) ; self.ininterv(-90.,90.,nd,start=self.lats,end=self.late)
361      for i in range(nd): self.lat = self.xcoord[i] ; self.update() ; self.put1d(i)
362      self.lat = save
363
364    def profile(self,nd=20,tabperso=None):
365    ### retrieve an altitude slice (profile)
366      save = self.xz
367      self.vertlabel()
368      self.vertplot = True
369      if tabperso is not None: nd = len(tabperso)
370      correct = False
371      self.prepare(ndx=nd) ; self.ininterv(0.,120000.,nd,start=self.xzs,end=self.xze)
372      if tabperso is not None: self.xcoord = tabperso
373      for i in range(nd): self.xz = self.xcoord[i] ; self.update() ; self.put1d(i)
374      self.xz = save
375
376    def seasonal(self,nd=12):
377    ### retrieve a seasonal slice
378      save = self.xdate
379      self.xlabel = "Areocentric longitude (degrees)"
380      self.prepare(ndx=nd) ; self.ininterv(0.,360.,nd,start=self.xdates,end=self.xdatee)
381      for i in range(nd): self.xdate = self.xcoord[i] ; self.update() ; self.put1d(i)
382      self.xdate = save
383
384    def makeplot1d(self,choice):
385    ### one 1D plot is created for the user-defined variable in choice.
386      (field, fieldlab) = self.definefield(choice)
387      if not self.vertplot:  absc = self.xcoord ; ordo = field ; ordolab = fieldlab ; absclab = self.xlabel
388      else:                  ordo = self.xcoord ; absc = field ; absclab = fieldlab ; ordolab = self.xlabel
389      mpl.plot(absc,ordo,'-bo') ; mpl.ylabel(ordolab) ; mpl.xlabel(absclab) #; mpl.xticks(query.xcoord)
390      if self.zkey == 4: mpl.semilogy() ; ax = mpl.gca() ; ax.set_ylim(ax.get_ylim()[::-1])
391      mpl.figtext(0.5, 0.01, self.ack, ha='center')
392
393    def plot1d(self,tabtodo):
394    ### complete 1D figure with possible multiplots
395      if isinstance(tabtodo,np.str): tabtodo=[tabtodo] ## so that asking one element without [] is possible.
396      if isinstance(tabtodo,np.int): tabtodo=[tabtodo] ## so that asking one element without [] is possible.
397      fig = mpl.figure() ; subv,subh = myplot.definesubplot( len(tabtodo) , fig ) 
398      for i in range(len(tabtodo)): mpl.subplot(subv,subh,i+1).grid(True, linestyle=':', color='grey') ; self.makeplot1d(tabtodo[i])
399
400    def htmlplot1d(self,tabtodo,figname="temp.png",title=""):
401    ### complete 1D figure with possible multiplots
402    ### added in 09/2012 for online MCD
403    ### see http://www.dalkescientific.com/writings/diary/archive/2005/04/23/matplotlib_without_gui.html
404      from matplotlib.figure import Figure
405      from matplotlib.backends.backend_agg import FigureCanvasAgg
406      if isinstance(tabtodo,np.str): tabtodo=[tabtodo] ## so that asking one element without [] is possible.
407      if isinstance(tabtodo,np.int): tabtodo=[tabtodo] ## so that asking one element without [] is possible.
408      fig = Figure(figsize=(8,8)) ; subv,subh = myplot.definesubplot( len(tabtodo) , fig )
409      for i in range(len(tabtodo)):
410        yeah = fig.add_subplot(subv,subh,i+1) #.grid(True, linestyle=':', color='grey')
411        choice = tabtodo[i]
412        (field, fieldlab) = self.definefield(choice)
413        if not self.vertplot:  absc = self.xcoord ; ordo = field ; ordolab = fieldlab ; absclab = self.xlabel
414        else:                  ordo = self.xcoord ; absc = field ; absclab = fieldlab ; ordolab = self.xlabel
415        yeah.plot(absc,ordo,'-bo') #; mpl.xticks(query.xcoord)
416        ax = fig.gca() ; ax.set_ylabel(ordolab) ; ax.set_xlabel(absclab)
417        if self.zkey == 4: ax.set_yscale('log') ; ax.set_ylim(ax.get_ylim()[::-1])
418      self.gettitle()
419      fig.text(0.5, 0.95, self.title, ha='center')
420      fig.text(0.5, 0.01, self.ack, ha='center')
421      canvas = FigureCanvasAgg(fig)
422      # The size * the dpi gives the final image size
423      #   a4"x4" image * 80 dpi ==> 320x320 pixel image
424      canvas.print_figure(figname, dpi=80)
425
426###################
427### 2D analysis ###
428###################
429
430    def latlon(self,ndx=37,ndy=19,fixedlt=False):
431    ### retrieve a latitude/longitude slice
432    ### default is: local time is not fixed. user-defined local time is at longitude 0.
433      save1 = self.lon ; save2 = self.lat ; save3 = self.loct
434      self.xlabel = "East longitude (degrees)" ; self.ylabel = "North latitude (degrees)"
435      self.prepare(ndx=ndx,ndy=ndy)
436      self.ininterv(-180.,180.,ndx,start=self.lons,end=self.lone)
437      self.ininterv(-90.,  90.,ndy,start=self.lats,end=self.late,yaxis=True)
438      if not fixedlt: umst = self.loct
439      for i in range(ndx):
440       for j in range(ndy):
441         self.lon = self.xcoord[i] ; self.lat = self.ycoord[j]
442         if not fixedlt: 
443           if self.lons is not None and self.lone is not None: self.loct = (umst + (self.lons+self.lone)/30.) % 24
444           else:                                               self.loct = (umst + self.lon/15.) % 24
445         self.update() ; self.put2d(i,j)
446      if not fixedlt: self.loct = umst
447      self.lon = save1 ; self.lat = save2 ; self.loct = save3
448
449    def put2d(self,i,j):
450    ## fill in subscript i,j in output arrays
451    ## (arrays must have been correctly defined through prepare)
452      if self.prestab is None:  myplot.errormess("arrays must be prepared first through self.prepare")
453      self.prestab[i,j] = self.pres ; self.denstab[i,j] = self.dens ; self.temptab[i,j] = self.temp
454      self.zonwindtab[i,j] = self.zonwind ; self.merwindtab[i,j] = self.merwind
455      self.meanvartab[i,j,1:5] = self.meanvar[0:4]  ## note: var numbering according to MCD manual is kept
456      self.extvartab[i,j,1:100] = self.extvar[0:99] ## note: var numbering according to MCD manual is kept
457
458    def makemap2d(self,choice,incwind=False,fixedlt=False,proj="cyl"):
459    ### one 2D map is created for the user-defined variable in choice.
460      self.latlon(fixedlt=fixedlt) ## a map is implicitely a lat-lon plot. otherwise it is a plot (cf. makeplot2d)
461      if choice == "wind" or incwind:
462          (windx, fieldlabwx) = self.definefield("u")
463          (windy, fieldlabwy) = self.definefield("v")
464      if choice == "wind":
465          field = np.sqrt(windx*windx + windy*windy)
466          fieldlab = "Horizontal wind speed (m/s)"
467      else:   
468          (field, fieldlab) = self.definefield(choice)
469      if incwind:   myplot.maplatlon(self.xcoord,self.ycoord,field,title=fieldlab,proj=proj,vecx=windx,vecy=windy) #,stride=1)
470      else:         myplot.maplatlon(self.xcoord,self.ycoord,field,title=fieldlab,proj=proj)
471      mpl.figtext(0.5, 0.0, self.ack, ha='center')
472
473    def map2d(self,tabtodo,incwind=False,fixedlt=False,proj="cyl"):
474    ### complete 2D figure with possible multiplots
475      if isinstance(tabtodo,np.str): tabtodo=[tabtodo] ## so that asking one element without [] is possible.
476      if isinstance(tabtodo,np.int): tabtodo=[tabtodo] ## so that asking one element without [] is possible.
477      fig = mpl.figure()
478      subv,subh = myplot.definesubplot( len(tabtodo) , fig ) 
479      for i in range(len(tabtodo)): mpl.subplot(subv,subh,i+1) ; self.makemap2d(tabtodo[i],incwind=incwind,fixedlt=fixedlt,proj=proj)
480
481    def htmlmap2d(self,tabtodo,incwind=False,fixedlt=False,figname="temp.png",title=""):
482    ### complete 2D figure with possible multiplots
483    ### added in 09/2012 for online MCD
484    ### see http://www.dalkescientific.com/writings/diary/archive/2005/04/23/matplotlib_without_gui.html
485      from matplotlib.figure import Figure
486      from matplotlib.backends.backend_agg import FigureCanvasAgg
487      from matplotlib.cm import get_cmap
488      if isinstance(tabtodo,np.str): tabtodo=[tabtodo] ## so that asking one element without [] is possible.
489      if isinstance(tabtodo,np.int): tabtodo=[tabtodo] ## so that asking one element without [] is possible.
490      fig = Figure(figsize=(8,8)) ; subv,subh = myplot.definesubplot( len(tabtodo) , fig )
491
492      ### topocontours
493      fieldc = self.getextvar(self.convertlab("topo"))
494
495      for i in range(len(tabtodo)):
496        yeah = fig.add_subplot(subv,subh,i+1)
497        choice = tabtodo[i]
498        self.latlon(fixedlt=fixedlt) 
499        ## a map is implicitely a lat-lon plot. otherwise it is a plot (cf. makeplot2d)
500        (field, fieldlab) = self.definefield(choice)
501        if incwind: (windx, fieldlabwx) = self.definefield("u") ; (windy, fieldlabwy) = self.definefield("v")
502
503        proj="cyl" ; colorb="jet" ; ndiv=20 ; zeback="molabw" ; trans=1.0 #0.6
504        title="" ; vecx=None ; vecy=None ; stride=2
505        lon = self.xcoord
506        lat = self.ycoord
507
508        ### get lon and lat in 2D version. get lat/lon intervals
509        #numdim = len(np.array(lon).shape)
510        #if numdim == 2:     [lon2d,lat2d] = [lon,lat]
511        #elif numdim == 1:   [lon2d,lat2d] = np.meshgrid(lon,lat)
512        #else:               errormess("lon and lat arrays must be 1D or 2D")
513        #[wlon,wlat] = myplot.latinterv()
514        ### define projection and background. define x and y given the projection
515        #m = basemap.Basemap(projection='moll') marche pas
516        #m = myplot.define_proj(proj,wlon,wlat,back=zeback,blat=None,blon=None)
517        #x, y = m(lon2d, lat2d)
518        ### TEMP
519        x = lon ; y = lat
520        ## define field. bound field.
521        what_I_plot = np.transpose(field)
522        zevmin, zevmax = myplot.calculate_bounds(what_I_plot)  ## vmin=min(what_I_plot_frame), vmax=max(what_I_plot_frame))
523        what_I_plot = myplot.bounds(what_I_plot,zevmin,zevmax)
524        ## define contour field levels. define color palette
525        ticks = ndiv + 1
526        zelevels = np.linspace(zevmin,zevmax,ticks)
527        palette = get_cmap(name=colorb)
528        ## contours topo
529        zelevc = np.linspace(-8000.,20000.,20)
530        yeah.contour( x, y, np.transpose(fieldc), zelevc, colors='black',linewidths = 0.4)
531        # contour field
532        c = yeah.contourf( x, y, what_I_plot, zelevels, cmap = palette, alpha = trans )
533        clb = Figure.colorbar(fig,c,orientation='vertical',format="%.1e")
534        clb.set_label(fieldlab)
535        ax = fig.gca() ; ax.set_title(fieldlab) ; ax.set_ylabel("Latitude") ; ax.set_xlabel("Longitude")
536        ax.set_xticks(np.arange(-180,181,45)) ; ax.set_xbound(lower=self.lons, upper=self.lone)
537        ax.set_yticks(np.arange(-90,91,30)) ; ax.set_ybound(lower=self.lats, upper=self.late)
538        if incwind:
539          [x2d,y2d] = np.meshgrid(x,y)
540          yeah.quiver(x2d,y2d,np.transpose(windx),np.transpose(windy))
541      self.gettitle()
542      fig.text(0.5, 0.95, self.title, ha='center')
543      fig.text(0.5, 0.01, self.ack, ha='center')
544      canvas = FigureCanvasAgg(fig)
545      # The size * the dpi gives the final image size
546      #   a4"x4" image * 80 dpi ==> 320x320 pixel image
547      canvas.print_figure(figname, dpi=80)
548
549
550    ### TODO: makeplot2d, plot2d, passer plot settings
551
Note: See TracBrowser for help on using the repository browser.