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 numpy as np |
---|
19 | |
---|
20 | ############################# |
---|
21 | ### Get options and variables |
---|
22 | parser = OptionParser() ; getparseroptions(parser) ; (opt,args) = parser.parse_args() |
---|
23 | if opt.file is None: errormess("I want to eat one file at least ! Use winds.py -f name_of_my_file. Or type winds.py -h") |
---|
24 | if opt.var is None and opt.anomaly is True: errormess("Cannot ask to compute anomaly if no variable is set") |
---|
25 | if opt.fref is not None and opt.operat is None: errormess("you must specify an operation when using a reference file") |
---|
26 | if opt.operat in ["+","-"] and opt.fref is None: errormess("you must specifiy a reference file when using inter-file operations") |
---|
27 | if opt.fref is not None and opt.operat is not None and opt.itp is not None: interpref=True |
---|
28 | else: interpref=False |
---|
29 | if opt.rate is not None: opt.save = "avi" |
---|
30 | elif opt.save == "avi": opt.rate = 8 ## this is a default value for -S avi |
---|
31 | if opt.save == "html": opt.rate = -1 ## this is convenient because everything is done in planetoplot with mrate |
---|
32 | |
---|
33 | ############################# |
---|
34 | ### Get infos about slices |
---|
35 | zeslat = readslices(opt.slat) ; zeslon = readslices(opt.slon) ; zesvert = readslices(opt.svert) ; zestime = readslices(opt.stime) |
---|
36 | reffile = opt.fref |
---|
37 | zexaxis = [opt.xmin,opt.xmax] ; zeyaxis=[opt.ymin,opt.ymax] |
---|
38 | |
---|
39 | ############################# |
---|
40 | ### 1. LOOP ON FILE LISTS TO BE PUT IN DIFFERENT FIGURES |
---|
41 | for i in range(len(opt.file)): |
---|
42 | |
---|
43 | zefiles = separatenames(opt.file[i]) |
---|
44 | |
---|
45 | typefile = whatkindfile(Dataset(zefiles[0])) ; stralt = None |
---|
46 | if typefile in ["meso","mesoapi","mesoideal"]: |
---|
47 | [lschar,zehour,zehourin] = getlschar ( zefiles[0] ) |
---|
48 | if opt.var is None: opt.var = ["HGT"] ; opt.clb = "nobar" |
---|
49 | elif typefile in ["geo"]: |
---|
50 | [lschar,zehour,zehourin] = ["",0,0] |
---|
51 | if opt.var is None: opt.var = ["HGT_M"] ; opt.clb = "nobar" |
---|
52 | else: |
---|
53 | [lschar,zehour,zehourin] = ["",0,0] |
---|
54 | if opt.var is None: opt.var = ["phisinit"] ; opt.clb = "nobar" |
---|
55 | |
---|
56 | if opt.vmin is not None : zevmin = opt.vmin[min(i,len(opt.vmin)-1)] |
---|
57 | else: zevmin = None |
---|
58 | if opt.vmax is not None : zevmax = opt.vmax[min(i,len(opt.vmax)-1)] |
---|
59 | else: zevmax = None |
---|
60 | #print "vmin, zevmin", opt.vmin, zevmin ; print "vmax, zevmax", opt.vmax, zevmax |
---|
61 | |
---|
62 | ############################# |
---|
63 | ### 2. LOOP ON VAR LISTS TO BE PUT IN DIFFERENT FIGURES |
---|
64 | for j in range(len(opt.var)): |
---|
65 | |
---|
66 | zevars = separatenames(opt.var[j]) |
---|
67 | |
---|
68 | inputnvert = separatenames(opt.lvl) |
---|
69 | if np.array(inputnvert).size == 1: |
---|
70 | zelevel = float(inputnvert[0]) |
---|
71 | ze_interp_levels = [-9999.] |
---|
72 | else: |
---|
73 | zelevel = -99. |
---|
74 | ze_interp_levels = np.linspace(float(inputnvert[0]),float(inputnvert[1]),float(inputnvert[2])) |
---|
75 | |
---|
76 | ######################################################### |
---|
77 | ### Call Fortran routines for vertical interpolations ### |
---|
78 | ######################################################### |
---|
79 | if opt.itp is not None: |
---|
80 | ##### |
---|
81 | ##### MESOSCALE : written by AS |
---|
82 | ##### |
---|
83 | if typefile in ["meso","mesoideal"]: |
---|
84 | if zelevel == 0. and opt.itp == 4: zelevel = 0.010 |
---|
85 | ### winds or no winds |
---|
86 | if opt.winds : zefields = 'uvmet' |
---|
87 | else : zefields = '' |
---|
88 | ### var or no var |
---|
89 | if zefields == '' : zefields = opt.var[j] |
---|
90 | else : zefields = zefields + "," + opt.var[j] |
---|
91 | if opt.var2 is not None : zefields = zefields + "," + opt.var2 |
---|
92 | ### call fortran routines |
---|
93 | for fff in range(len(zefiles)): |
---|
94 | newname = api_onelevel ( path_to_input = '', \ |
---|
95 | input_name = zefiles[fff], \ |
---|
96 | fields = zefields, \ |
---|
97 | interp_method = opt.itp, \ |
---|
98 | interp_level = ze_interp_levels, \ |
---|
99 | onelevel = zelevel, \ |
---|
100 | nocall = opt.nocall ) |
---|
101 | if fff == 0: zetab = newname |
---|
102 | else: zetab = np.append(zetab,newname) |
---|
103 | zefiles = zetab #; print zefiles |
---|
104 | zelevel = 0 ## so that zelevel could play again the role of nvert |
---|
105 | ##### |
---|
106 | ##### GCM : written by AC |
---|
107 | ##### |
---|
108 | elif typefile == "gcm": |
---|
109 | inputvar = zevars |
---|
110 | if opt.var2 is not None : inputvar = np.append(inputvar,opt.var2) |
---|
111 | interpolated_files="" |
---|
112 | interpolated_files=call_zrecast(interp_mode=opt.itp,\ |
---|
113 | input_name=zefiles,\ |
---|
114 | fields=inputvar,\ |
---|
115 | predefined=opt.intas) |
---|
116 | |
---|
117 | zefiles=interpolated_files |
---|
118 | if interpref: |
---|
119 | interpolated_ref="" |
---|
120 | interpolated_ref=call_zrecast(interp_mode=opt.itp,\ |
---|
121 | input_name=[opt.fref],\ |
---|
122 | fields=zevars,\ |
---|
123 | predefined=opt.intas) |
---|
124 | |
---|
125 | reffile=interpolated_ref[0] |
---|
126 | else: |
---|
127 | print "type not supported" |
---|
128 | exit() |
---|
129 | |
---|
130 | ############# |
---|
131 | ### Main call |
---|
132 | name = planetoplot (zefiles,level=int(zelevel),vertmode=opt.itp,\ |
---|
133 | proj=opt.proj,back=opt.back,target=opt.tgt,stride=opt.ste,var=zevars,\ |
---|
134 | colorb=opt.clb,winds=opt.winds,\ |
---|
135 | addchar=lschar,interv=[zehour,zehourin],vmin=zevmin,vmax=zevmax,\ |
---|
136 | tile=opt.tile,zoom=opt.zoom,display=opt.display,\ |
---|
137 | hole=opt.hole,save=opt.save,\ |
---|
138 | anomaly=opt.anomaly,var2=opt.var2,ndiv=opt.ndiv,\ |
---|
139 | mult=opt.mult,zetitle=opt.zetitle,\ |
---|
140 | slon=zeslon,slat=zeslat,svert=zesvert,stime=zestime,\ |
---|
141 | outputname=opt.out,resolution=opt.res,\ |
---|
142 | ope=opt.operat,fileref=reffile,minop=opt.vminope,maxop=opt.vmaxope,titleref=opt.titref,\ |
---|
143 | invert_y=opt.inverty,xaxis=zexaxis,yaxis=zeyaxis,ylog=opt.logy,yintegral=opt.column,\ |
---|
144 | blat=opt.blat,blon=opt.blon,tsat=opt.tsat,flagnolow=opt.nolow,\ |
---|
145 | mrate=opt.rate,mquality=opt.quality,trans=opt.trans,zarea=opt.area,axtime=opt.axtime) |
---|
146 | print 'DONE: '+name |
---|
147 | system("rm -f to_be_erased") |
---|
148 | |
---|
149 | ######################################################### |
---|
150 | ### Generate a .sh file with the used command saved in it |
---|
151 | command = "" |
---|
152 | for arg in sys.argv: command = command + arg + ' ' |
---|
153 | #if typefile not in ["meso","mesoapi"]: name = 'pycommand' |
---|
154 | if opt.save == "gui": name = 'pycommand' |
---|
155 | elif opt.save == "avi": system("mv -f movie*.avi "+name+".avi") |
---|
156 | 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) |
---|
157 | f = open(name+'.sh', 'w') |
---|
158 | f.write(command) |
---|
159 | |
---|
160 | #print "********** OPTIONS: ", opt |
---|
161 | print "********************************************************** END" |
---|