1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | ### A. Spiga + T. Navarro + A. Colaitis |
---|
4 | |
---|
5 | ########################################################################################### |
---|
6 | ########################################################################################### |
---|
7 | ### What is below relate to running the file as a command line executable (very convenient) |
---|
8 | if __name__ == "__main__": |
---|
9 | import sys |
---|
10 | from optparse import OptionParser ### to be replaced by argparse |
---|
11 | from api_wrapper import api_onelevel |
---|
12 | from gcm_transformations import call_zrecast |
---|
13 | from netCDF4 import Dataset |
---|
14 | from myplot import getlschar, separatenames, readslices, adjust_length, whatkindfile, errormess |
---|
15 | from os import system |
---|
16 | from planetoplot import planetoplot |
---|
17 | from myscript import getparseroptions |
---|
18 | import glob |
---|
19 | import numpy as np |
---|
20 | |
---|
21 | |
---|
22 | ############################# |
---|
23 | ### Get options and variables |
---|
24 | parser = OptionParser() ; getparseroptions(parser) ; (opt,args) = parser.parse_args() |
---|
25 | if opt.file is None: errormess("I want to eat one file at least ! Use pp.py -f name_of_my_file. Or type pp.py -h") |
---|
26 | if opt.var is None and opt.anomaly is True: errormess("Cannot ask to compute anomaly if no variable is set") |
---|
27 | if opt.fref is not None and opt.operat is None: errormess("you must specify an operation when using a reference file") |
---|
28 | if opt.operat in ["+","-"] and opt.fref is None: errormess("you must specifiy a reference file when using inter-file operations") |
---|
29 | if opt.fref is not None and opt.operat is not None and opt.itp is not None: interpref=True |
---|
30 | else: interpref=False |
---|
31 | if opt.rate is not None: opt.save = "avi" |
---|
32 | elif opt.save == "avi": opt.rate = 8 ## this is a default value for -S avi |
---|
33 | if opt.save == "html": opt.rate = -1 ## this is convenient because everything is done in planetoplot with mrate |
---|
34 | |
---|
35 | ############################# |
---|
36 | ### Get infos about slices |
---|
37 | zeslat = readslices(opt.slat) ; zeslon = readslices(opt.slon) ; zesvert = readslices(opt.svert) ; zestime = readslices(opt.stime) |
---|
38 | |
---|
39 | reffile = opt.fref |
---|
40 | zexaxis = [opt.xmin,opt.xmax] ; zeyaxis=[opt.ymin,opt.ymax] |
---|
41 | |
---|
42 | ############################# |
---|
43 | ### Catch multiple files |
---|
44 | if "*" in opt.file[0] or "?" in opt.file[0]: |
---|
45 | yeah = glob.glob(opt.file[0]) ; yeah.sort() |
---|
46 | opt.file[0] = yeah[0] |
---|
47 | for file in yeah[1:]: opt.file[0] = opt.file[0] + "," + file |
---|
48 | |
---|
49 | ############################# |
---|
50 | ### 1. LOOP ON FILE LISTS TO BE PUT IN DIFFERENT FIGURES |
---|
51 | for i in range(len(opt.file)): |
---|
52 | |
---|
53 | zefiles = separatenames(opt.file[i]) |
---|
54 | |
---|
55 | typefile = whatkindfile(Dataset(zefiles[0])) ; stralt = None |
---|
56 | if typefile in ["meso"]: |
---|
57 | [lschar,zehour,zehourin] = getlschar ( zefiles[0] ) |
---|
58 | if opt.var is None: opt.var = ["HGT"] ; opt.nocolorb = True |
---|
59 | elif typefile in ["geo"]: |
---|
60 | lschar="" |
---|
61 | if opt.var is None: opt.var = ["HGT_M"] ; opt.nocolorb = True |
---|
62 | else: |
---|
63 | lschar="" |
---|
64 | if opt.var is None: |
---|
65 | opt.var = ["phisinit"] ; opt.clb = "nobar" |
---|
66 | ### temporaire... en attendant mieux. |
---|
67 | if opt.back == "titan": opt.var = ["phis"] ; opt.nocolorb = True |
---|
68 | |
---|
69 | if opt.vmin is not None : zevmin = opt.vmin[min(i,len(opt.vmin)-1)] |
---|
70 | else: zevmin = None |
---|
71 | if opt.vmax is not None : zevmax = opt.vmax[min(i,len(opt.vmax)-1)] |
---|
72 | else: zevmax = None |
---|
73 | #print "vmin, zevmin", opt.vmin, zevmin ; print "vmax, zevmax", opt.vmax, zevmax |
---|
74 | |
---|
75 | ############################# |
---|
76 | ### 2. LOOP ON VAR LISTS TO BE PUT IN DIFFERENT FIGURES |
---|
77 | for j in range(len(opt.var)): |
---|
78 | |
---|
79 | zevars = separatenames(opt.var[j]) |
---|
80 | |
---|
81 | inputnvert = separatenames(opt.lvl) |
---|
82 | if np.array(inputnvert).size == 1: |
---|
83 | zelevel = float(inputnvert[0]) |
---|
84 | ze_interp_levels = [-9999.] |
---|
85 | elif np.array(inputnvert).size > 2: |
---|
86 | zelevel = -99. |
---|
87 | start = float(inputnvert[0]) |
---|
88 | stop = float(inputnvert[1]) |
---|
89 | if np.array(inputnvert).size == 2: numsample = 20 |
---|
90 | else: numsample = float(inputnvert[2]) |
---|
91 | if stop > start: |
---|
92 | ## altitude coordinates |
---|
93 | ze_interp_levels = np.linspace(start,stop,numsample) |
---|
94 | else: |
---|
95 | ## pressure coordinates |
---|
96 | ze_interp_levels = np.logspace(np.log10(start),np.log10(stop),numsample) |
---|
97 | |
---|
98 | ######################################################### |
---|
99 | if opt.itp is not None: |
---|
100 | if opt.itp > 0: |
---|
101 | ##### |
---|
102 | ##### MESOSCALE : written by AS |
---|
103 | ##### |
---|
104 | if typefile in ["meso"]: |
---|
105 | if zelevel == 0. and opt.itp == 4: zelevel = 0.010 |
---|
106 | if np.array(inputnvert).size == 1: zesvert = readslices([str(zelevel)]) |
---|
107 | ### winds or no winds |
---|
108 | if opt.winds : zefields = 'uvmet' |
---|
109 | elif opt.var[j] in ['deltat','DELTAT'] : zefields = 'tk,TSURF' |
---|
110 | elif opt.var[j] in ['uv','UV','hodograph','hodograph_2'] : zefields = 'U,V' |
---|
111 | else : zefields = '' |
---|
112 | ### var or no var |
---|
113 | if zefields == '' : zefields = opt.var[j] |
---|
114 | else : zefields = zefields + "," + opt.var[j] |
---|
115 | if opt.var2 is not None : zefields = zefields + "," + opt.var2 |
---|
116 | ### call fortran routines |
---|
117 | for fff in range(len(zefiles)): |
---|
118 | newname = api_onelevel ( path_to_input = '', \ |
---|
119 | input_name = zefiles[fff], \ |
---|
120 | fields = zefields, \ |
---|
121 | interp_method = opt.itp, \ |
---|
122 | interp_level = ze_interp_levels, \ |
---|
123 | onelevel = zelevel, \ |
---|
124 | nocall = opt.nocall ) |
---|
125 | if fff == 0: zetab = newname |
---|
126 | else: zetab = np.append(zetab,newname) |
---|
127 | if interpref: |
---|
128 | reffile = api_onelevel ( path_to_input = '', \ |
---|
129 | input_name = opt.fref, \ |
---|
130 | fields = zefields, \ |
---|
131 | interp_method = opt.itp, \ |
---|
132 | interp_level = ze_interp_levels, \ |
---|
133 | onelevel = zelevel, \ |
---|
134 | nocall = opt.nocall ) |
---|
135 | zefiles = zetab #; print zefiles |
---|
136 | zelevel = 0 ## so that zelevel could play again the role of nvert |
---|
137 | ##### |
---|
138 | ##### GCM : written by AC |
---|
139 | ##### |
---|
140 | elif typefile == "gcm": |
---|
141 | inputvar = zevars |
---|
142 | if opt.var2 is not None : inputvar = np.append(inputvar,opt.var2) |
---|
143 | interpolated_files="" |
---|
144 | interpolated_files=call_zrecast(interp_mode=opt.itp,\ |
---|
145 | input_name=zefiles,\ |
---|
146 | fields=inputvar,\ |
---|
147 | limites = ze_interp_levels,\ |
---|
148 | predefined=opt.intas) |
---|
149 | |
---|
150 | zefiles=interpolated_files |
---|
151 | if interpref: |
---|
152 | interpolated_ref="" |
---|
153 | interpolated_ref=call_zrecast(interp_mode=opt.itp,\ |
---|
154 | input_name=[opt.fref],\ |
---|
155 | fields=zevars,\ |
---|
156 | predefined=opt.intas) |
---|
157 | |
---|
158 | reffile=interpolated_ref[0] |
---|
159 | else: |
---|
160 | print "type not supported" |
---|
161 | exit() |
---|
162 | |
---|
163 | ############# |
---|
164 | ### Main call |
---|
165 | name = planetoplot (zefiles,level=int(zelevel),vertmode=opt.itp,\ |
---|
166 | proj=opt.proj,back=opt.back,target=opt.tgt,stride=opt.ste,var=zevars,\ |
---|
167 | clb=separatenames(opt.clb),winds=opt.winds,\ |
---|
168 | addchar=lschar,vmin=zevmin,vmax=zevmax,\ |
---|
169 | tile=opt.tile,zoom=opt.zoom,display=opt.display,\ |
---|
170 | hole=opt.hole,save=opt.save,\ |
---|
171 | anomaly=opt.anomaly,var2=opt.var2,ndiv=opt.ndiv,\ |
---|
172 | mult=opt.mult,zetitle=separatenames(opt.zetitle),\ |
---|
173 | slon=zeslon,slat=zeslat,svert=zesvert,stime=zestime,\ |
---|
174 | outputname=opt.out,resolution=opt.res,\ |
---|
175 | ope=opt.operat,fileref=reffile,minop=opt.vminope,maxop=opt.vmaxope,titleref=opt.titref,\ |
---|
176 | invert_y=opt.inverty,xaxis=zexaxis,yaxis=zeyaxis,ylog=opt.logy,xlog=opt.logx,yintegral=opt.column,\ |
---|
177 | blat=opt.blat,blon=opt.blon,tsat=opt.tsat,flagnolow=opt.nolow,\ |
---|
178 | mrate=opt.rate,mquality=opt.quality,trans=opt.trans,zarea=opt.area,axtime=opt.axtime,\ |
---|
179 | redope=opt.redope,seevar=opt.seevar,xlab=opt.xlab,ylab=opt.ylab,lbls=separatenames(opt.labels),\ |
---|
180 | lstyle=separatenames(opt.linestyle),cross=readslices(opt.mark),markdevil=opt.mdevil,facwind=opt.facwind,\ |
---|
181 | trycol=opt.trycol,streamflag=opt.stream,nocolorb=opt.nocolorb,analysis=opt.analysis) |
---|
182 | print 'DONE: '+name |
---|
183 | system("rm -f to_be_erased") |
---|
184 | |
---|
185 | ######################################################### |
---|
186 | ### Generate a .sh file with the used command saved in it |
---|
187 | command = "" |
---|
188 | for arg in sys.argv: command = command + arg + ' ' |
---|
189 | #if typefile not in ["meso","mesoapi"]: name = 'pycommand' |
---|
190 | if opt.save == "gui": name = 'pycommand' |
---|
191 | elif opt.save == "avi": system("mv -f movie*.avi "+name+".avi") |
---|
192 | elif opt.save == "html": system("cat $PYTHONPATH/header.html > anim.html ; cat zepics >> anim.html ; cat $PYTHONPATH/body.html >> anim.html ; rm -rf zepics "+name+" ; mkdir "+name+" ; mv anim.html image*png "+name) |
---|
193 | f = open(name+'.sh', 'w') |
---|
194 | f.write(command) |
---|
195 | |
---|
196 | #print "********** OPTIONS: ", opt |
---|
197 | print "********************************************************** END" |
---|