source: trunk/UTIL/PYTHON/gcm.py @ 389

Last change on this file since 389 was 388, checked in by acolaitis, 14 years ago

PYTHON.
#######
~
Added new option and routines that go along.
~
#######

M 387 meso.py
M 387 gcm.py
M 387 myscript.py
-------------------- Added the --tsat option. Allows the computation and plotting of Tsat-T instead of T when the z-axis is a pressure coordinate and the variable to plot is temperature. This is usefull to study saturation. Be carefull not to use the --column option, which uses a z-axis in meters, and would output wrong results with an axis in pressure. This could however be worked out in a future commit.

M 387 mymath.py
-------------------- Added several new functions, that may not be at their best place in mymath...:

  • get_tsat(pressure,temp=None,zlon=None,zlat=None,zalt=None,ztime=None) -->when given pressure only, the routine outputs a vertical profile of condensation temperature. -->when given all the inputs, the routine returns Tcond-T by taking care of all dimension problems, automatically finding the vertical axis in the temperature field, and taking care of eventual missing values by masking the one present in the input by the value 1e20 in the output.
  • get_dim(zlon,zlat,zalt,ztime,zvar): --> returns a dictionnary giving the position of each of the dimensions in zvar in the following form:

{'latitude': 2, 'altitude': 1, 'longitude': 3, 'Time': 0}

--> this works for any shape for zvar between 1 and 4 dimensions, and takes care of tricky cases when variable's dimensions are not in the right order, and when two dimensions have the same number of elements.

  • get_key(self,value) --> this function returns the key(s) associated to a value in a dictionnary. This is used to be able to access dictionnaries in the opposite way than the natural one.

M 387 planetoplot.py
-------------------- Added changes relative to the --tsat option

  • Property svn:executable set to *
File size: 7.2 KB
Line 
1#!/usr/bin/env python
2
3### T. Navarro + A. Spiga + A. Colaitis
4
5###########################################################################################
6###########################################################################################
7### What is below relate to running the file as a command line executable (very convenient)
8if __name__ == "__main__":
9    import sys
10    from optparse import OptionParser    ### to be replaced by argparse
11    from api_wrapper import api_onelevel
12    from zrecast_wrapper import call_zrecast
13    from netCDF4 import Dataset
14    from myplot import getlschar, separatenames, readslices, adjust_length
15    from os import system
16    from planetoplot import planetoplot
17    from myscript import getparseroptions
18    import numpy as np
19
20    #############################
21    ### Get options and variables
22    parser = OptionParser()
23    getparseroptions(parser)
24    (opt,args) = parser.parse_args()
25
26    if opt.file is None: 
27        print "I want to eat one file at least ! Use winds.py -f name_of_my_file. Or type winds.py -h"
28        exit()
29    if opt.var is None and opt.anomaly is True:
30        print "Cannot ask to compute anomaly if no variable is set"
31        exit()
32    print "Options:", opt
33
34#    listvar = ''
35#    if opt.var is None:
36#        zerange = [-999999]
37#    else:
38#        zelen = len(opt.var)
39#        zerange = range(zelen)
40#        #if zelen == 1: listvar = opt.var[0] + ','
41#        #else         :
42#        for jjj in zerange: listvar += opt.var[jjj] + ','
43#        listvar = listvar[0:len(listvar)-1]
44#        vmintab = adjust_length (opt.vmin, zelen)
45#        vmaxtab = adjust_length (opt.vmax, zelen)
46
47
48    ################################
49    ### General check
50 
51    if opt.fref is not None:
52       if opt.operat is None:
53          print "you must specify an operation when using a reference file"
54          exit()
55    if opt.operat in ["+","-"]:
56       if opt.fref is None:
57          print "you must specifiy a reference file when using inter-file operations"
58          exit()
59
60    interpref=False
61    if opt.fref is not None:
62       if opt.operat is not None:
63          if opt.itp is not None:
64             interpref=True
65
66    ################################
67
68
69    print "file, length", opt.file, len(opt.file)
70
71    zeslat  = readslices(opt.slat)
72    zeslon  = readslices(opt.slon)
73    zesvert = readslices(opt.svert)
74    zestime = readslices(opt.stime)
75    print "slat,zeslat", opt.slat, zeslat
76    print "slon,zeslon", opt.slon, zeslon
77    print "svert,zesvert", opt.svert, zesvert
78    print "stime,zestime", opt.stime, zestime
79
80    for i in range(len(opt.file)):
81      for j in range(len(opt.var)):
82
83        zefiles = separatenames(opt.file[i])
84        print "zefiles", zefiles
85       
86        if opt.vmin is not None : zevmin  = opt.vmin[min(i,len(opt.vmin)-1)]
87        else: zevmin = None
88        if opt.vmax is not None : zevmax  = opt.vmax[min(i,len(opt.vmax)-1)]
89        else: zevmax = None
90        print "vmin, zevmin", opt.vmin, zevmin
91        print "vmax, zevmax", opt.vmax, zevmax
92       
93        zevar = separatenames(opt.var[j])
94        zevars = zevar[j]
95        zevar = zevar[0]
96        print "var, zevar", opt.var, zevar
97       
98        #checkcoherence(len(zefiles),len(opt.slat),len(opt.slon),len(opt.stime))
99       
100        zefile = zefiles[0]
101             
102        #zelevel = opt.lvl   
103        stralt = None
104        #[lschar,zehour,zehourin] = getlschar ( zefile )  ## getlschar from wrfout (or simply return "" if another file)
105        [lschar,zehour,zehourin] = ["",0,0]  ## dummy
106   
107        inputnvert = separatenames(opt.lvl)
108        if np.array(inputnvert).size == 1:
109            zelevel = float(inputnvert[0])
110            ze_interp_levels = [-9999.]
111        else:
112            zelevel = -99.
113            ze_interp_levels = np.linspace(float(inputnvert[0]),float(inputnvert[1]),float(inputnvert[2]))
114        print 'level: ', zelevel
115        print 'interp_levels: ',ze_interp_levels
116
117        #####################################################
118        ### Call Fortran routines for vertical interpolations  -->  zrecast for GCM ?       
119#        if opt.itp is not None:
120#            if zelevel == 0. and opt.itp == 4:  zelevel = 0.010
121#            ### winds or no winds
122#            if opt.winds            :  zefields = 'uvmet'
123#            else                    :  zefields = ''
124#            ### var or no var
125#            #if opt.var is None      :  pass
126#            if zefields == ''       :  zefields = listvar
127#            else                    :  zefields = zefields + "," + listvar
128#            if opt.var2 is not None : zefields = zefields + "," + opt.var2 
129#            print zefields
130#            zefile = api_onelevel (  path_to_input   = '', \
131#                                     input_name      = zefile, \
132#                                     fields          = zefields, \
133#                                     interp_method   = opt.itp, \
134#                                     interp_level    = ze_interp_levels, \
135#                                     onelevel        = zelevel, \
136#                                     nocall          = opt.nocall )
137#            print zefile
138#            zelevel = 0 ## so that zelevel could play again the role of nvert
139
140# A.C.  #####################################################
141        ### Call Fortran routines for vertical interpolations  -->  zrecast
142
143        if opt.itp is not None:
144           interpolated_files=""
145           interpolated_files=call_zrecast(interp_mode=opt.itp,\
146                    input_name=zefiles,\
147                    fields=zevars)
148
149           zefiles=interpolated_files
150        if interpref:
151           interpolated_ref=""
152           interpolated_ref=call_zrecast(interp_mode=opt.itp,\
153                    input_name=[opt.fref],\
154                    fields=zevars)
155
156           reffile=interpolated_ref[0]
157        else:
158           reffile=opt.fref
159# Divers ####################################################
160   
161        zexaxis=[opt.xmin,opt.xmax]
162        zeyaxis=[opt.ymin,opt.ymax]
163
164        #############
165        ### Main call
166        name = planetoplot (zefiles,level=int(zelevel),vertmode=opt.itp,\
167                proj=opt.proj,back=opt.back,target=opt.tgt,stride=opt.ste,var=zevar,\
168                numplot=opt.num,colorb=opt.clb,winds=opt.winds,\
169                addchar=lschar,interv=[zehour,zehourin],vmin=zevmin,vmax=zevmax,\
170                tile=opt.tile,zoom=opt.zoom,display=opt.display,\
171                itstep=opt.it,hole=opt.hole,save=opt.save,\
172                anomaly=opt.anomaly,var2=opt.var2,ndiv=opt.ndiv,first=opt.frt,\
173                mult=opt.mult,zetitle=opt.zetitle,\
174                slon=zeslon,slat=zeslat,svert=zesvert,stime=zestime,\
175                outputname=opt.out,resolution=opt.res,\
176                ope=opt.operat,fileref=reffile,minop=opt.vminope,maxop=opt.vmaxope,titleref=opt.titref,\
177                invert_y=opt.inverty,xaxis=zexaxis,yaxis=zeyaxis,ylog=opt.logy,yintegral=opt.column,\
178                blat=opt.blat,tsat=opt.tsat)
179        print 'Done: '+name
180        system("rm -f to_be_erased")
181 
182    #########################################################
183    ### Generate a .sh file with the used command saved in it
184    command = ""
185    for arg in sys.argv: command = command + arg + ' '
186    name = 'pycommand'
187    f = open(name+'.sh', 'w')
188    f.write(command)
Note: See TracBrowser for help on using the repository browser.