[349] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
[392] | 3 | ### A. Spiga + T. Navarro + A. Colaitis |
---|
[349] | 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 |
---|
[464] | 12 | from gcm_transformations import call_zrecast |
---|
[349] | 13 | from netCDF4 import Dataset |
---|
[392] | 14 | from myplot import getlschar, separatenames, readslices, adjust_length, whatkindfile, errormess |
---|
[349] | 15 | from os import system |
---|
| 16 | from planetoplot import planetoplot |
---|
[377] | 17 | from myscript import getparseroptions |
---|
[478] | 18 | import glob |
---|
[349] | 19 | import numpy as np |
---|
| 20 | |
---|
[478] | 21 | |
---|
[349] | 22 | ############################# |
---|
| 23 | ### Get options and variables |
---|
[392] | 24 | parser = OptionParser() ; getparseroptions(parser) ; (opt,args) = parser.parse_args() |
---|
[721] | 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") |
---|
[392] | 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 |
---|
[451] | 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 |
---|
[349] | 34 | |
---|
[392] | 35 | ############################# |
---|
| 36 | ### Get infos about slices |
---|
| 37 | zeslat = readslices(opt.slat) ; zeslon = readslices(opt.slon) ; zesvert = readslices(opt.svert) ; zestime = readslices(opt.stime) |
---|
[818] | 38 | |
---|
[392] | 39 | reffile = opt.fref |
---|
| 40 | zexaxis = [opt.xmin,opt.xmax] ; zeyaxis=[opt.ymin,opt.ymax] |
---|
[349] | 41 | |
---|
[392] | 42 | ############################# |
---|
[478] | 43 | ### Catch multiple files |
---|
[479] | 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] |
---|
[489] | 47 | for file in yeah[1:]: opt.file[0] = opt.file[0] + "," + file |
---|
[478] | 48 | |
---|
| 49 | ############################# |
---|
[392] | 50 | ### 1. LOOP ON FILE LISTS TO BE PUT IN DIFFERENT FIGURES |
---|
| 51 | for i in range(len(opt.file)): |
---|
[349] | 52 | |
---|
[392] | 53 | zefiles = separatenames(opt.file[i]) |
---|
[380] | 54 | |
---|
[426] | 55 | typefile = whatkindfile(Dataset(zefiles[0])) ; stralt = None |
---|
[562] | 56 | if typefile in ["meso"]: |
---|
[392] | 57 | [lschar,zehour,zehourin] = getlschar ( zefiles[0] ) |
---|
[818] | 58 | if opt.var is None: opt.var = ["HGT"] ; opt.nocolorb = True |
---|
[429] | 59 | elif typefile in ["geo"]: |
---|
[763] | 60 | lschar="" |
---|
[818] | 61 | if opt.var is None: opt.var = ["HGT_M"] ; opt.nocolorb = True |
---|
[763] | 62 | else: |
---|
| 63 | lschar="" |
---|
[721] | 64 | if opt.var is None: |
---|
| 65 | opt.var = ["phisinit"] ; opt.clb = "nobar" |
---|
| 66 | ### temporaire... en attendant mieux. |
---|
[818] | 67 | if opt.back == "titan": opt.var = ["phis"] ; opt.nocolorb = True |
---|
[380] | 68 | |
---|
[392] | 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 |
---|
[380] | 74 | |
---|
[392] | 75 | ############################# |
---|
| 76 | ### 2. LOOP ON VAR LISTS TO BE PUT IN DIFFERENT FIGURES |
---|
| 77 | for j in range(len(opt.var)): |
---|
[380] | 78 | |
---|
[392] | 79 | zevars = separatenames(opt.var[j]) |
---|
[380] | 80 | |
---|
[377] | 81 | inputnvert = separatenames(opt.lvl) |
---|
[351] | 82 | if np.array(inputnvert).size == 1: |
---|
| 83 | zelevel = float(inputnvert[0]) |
---|
| 84 | ze_interp_levels = [-9999.] |
---|
[610] | 85 | elif np.array(inputnvert).size > 2: |
---|
[489] | 86 | zelevel = -99. |
---|
[610] | 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) |
---|
[351] | 97 | |
---|
[392] | 98 | ######################################################### |
---|
[377] | 99 | if opt.itp is not None: |
---|
[562] | 100 | if opt.itp > 0: |
---|
[392] | 101 | ##### |
---|
| 102 | ##### MESOSCALE : written by AS |
---|
| 103 | ##### |
---|
[562] | 104 | if typefile in ["meso"]: |
---|
[377] | 105 | if zelevel == 0. and opt.itp == 4: zelevel = 0.010 |
---|
[822] | 106 | if np.array(inputnvert).size == 1: zesvert = readslices([str(zelevel)]) |
---|
[349] | 107 | ### winds or no winds |
---|
| 108 | if opt.winds : zefields = 'uvmet' |
---|
[612] | 109 | elif opt.var[j] in ['deltat','DELTAT'] : zefields = 'tk,TSURF' |
---|
[763] | 110 | elif opt.var[j] in ['uv','UV','hodograph','hodograph_2'] : zefields = 'U,V' |
---|
[349] | 111 | else : zefields = '' |
---|
| 112 | ### var or no var |
---|
[392] | 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) |
---|
[569] | 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 ) |
---|
[392] | 135 | zefiles = zetab #; print zefiles |
---|
[349] | 136 | zelevel = 0 ## so that zelevel could play again the role of nvert |
---|
[392] | 137 | ##### |
---|
| 138 | ##### GCM : written by AC |
---|
| 139 | ##### |
---|
| 140 | elif typefile == "gcm": |
---|
[475] | 141 | inputvar = zevars |
---|
| 142 | if opt.var2 is not None : inputvar = np.append(inputvar,opt.var2) |
---|
[392] | 143 | interpolated_files="" |
---|
| 144 | interpolated_files=call_zrecast(interp_mode=opt.itp,\ |
---|
| 145 | input_name=zefiles,\ |
---|
[475] | 146 | fields=inputvar,\ |
---|
[489] | 147 | limites = ze_interp_levels,\ |
---|
[464] | 148 | predefined=opt.intas) |
---|
[349] | 149 | |
---|
[392] | 150 | zefiles=interpolated_files |
---|
| 151 | if interpref: |
---|
| 152 | interpolated_ref="" |
---|
| 153 | interpolated_ref=call_zrecast(interp_mode=opt.itp,\ |
---|
[380] | 154 | input_name=[opt.fref],\ |
---|
[409] | 155 | fields=zevars,\ |
---|
[464] | 156 | predefined=opt.intas) |
---|
[380] | 157 | |
---|
[392] | 158 | reffile=interpolated_ref[0] |
---|
| 159 | else: |
---|
[429] | 160 | print "type not supported" |
---|
[392] | 161 | exit() |
---|
[377] | 162 | |
---|
[392] | 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,\ |
---|
[760] | 167 | clb=separatenames(opt.clb),winds=opt.winds,\ |
---|
[763] | 168 | addchar=lschar,vmin=zevmin,vmax=zevmax,\ |
---|
[349] | 169 | tile=opt.tile,zoom=opt.zoom,display=opt.display,\ |
---|
[424] | 170 | hole=opt.hole,save=opt.save,\ |
---|
| 171 | anomaly=opt.anomaly,var2=opt.var2,ndiv=opt.ndiv,\ |
---|
[865] | 172 | mult=opt.mult,add=opt.add,zetitle=separatenames(opt.zetitle),\ |
---|
[359] | 173 | slon=zeslon,slat=zeslat,svert=zesvert,stime=zestime,\ |
---|
[377] | 174 | outputname=opt.out,resolution=opt.res,\ |
---|
[380] | 175 | ope=opt.operat,fileref=reffile,minop=opt.vminope,maxop=opt.vmaxope,titleref=opt.titref,\ |
---|
[763] | 176 | invert_y=opt.inverty,xaxis=zexaxis,yaxis=zeyaxis,ylog=opt.logy,xlog=opt.logx,yintegral=opt.column,\ |
---|
[453] | 177 | blat=opt.blat,blon=opt.blon,tsat=opt.tsat,flagnolow=opt.nolow,\ |
---|
[483] | 178 | mrate=opt.rate,mquality=opt.quality,trans=opt.trans,zarea=opt.area,axtime=opt.axtime,\ |
---|
[612] | 179 | redope=opt.redope,seevar=opt.seevar,xlab=opt.xlab,ylab=opt.ylab,lbls=separatenames(opt.labels),\ |
---|
[792] | 180 | lstyle=separatenames(opt.linestyle),cross=readslices(opt.mark),markdevil=opt.mdevil,facwind=opt.facwind,\ |
---|
[817] | 181 | trycol=opt.trycol,streamflag=opt.stream,nocolorb=opt.nocolorb,analysis=opt.analysis) |
---|
[392] | 182 | print 'DONE: '+name |
---|
[349] | 183 | system("rm -f to_be_erased") |
---|
| 184 | |
---|
| 185 | ######################################################### |
---|
| 186 | ### Generate a .sh file with the used command saved in it |
---|
[392] | 187 | command = "" |
---|
[349] | 188 | for arg in sys.argv: command = command + arg + ' ' |
---|
[424] | 189 | #if typefile not in ["meso","mesoapi"]: name = 'pycommand' |
---|
[451] | 190 | if opt.save == "gui": name = 'pycommand' |
---|
| 191 | 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) |
---|
[349] | 192 | f = open(name+'.sh', 'w') |
---|
| 193 | f.write(command) |
---|
[392] | 194 | |
---|
[468] | 195 | #print "********** OPTIONS: ", opt |
---|
[392] | 196 | print "********************************************************** END" |
---|