source: trunk/UTIL/PYTHON/meso.py @ 388

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