[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) |
---|
| 38 | reffile = opt.fref |
---|
| 39 | zexaxis = [opt.xmin,opt.xmax] ; zeyaxis=[opt.ymin,opt.ymax] |
---|
[349] | 40 | |
---|
[392] | 41 | ############################# |
---|
[478] | 42 | ### Catch multiple files |
---|
[479] | 43 | if "*" in opt.file[0] or "?" in opt.file[0]: |
---|
| 44 | yeah = glob.glob(opt.file[0]) ; yeah.sort() |
---|
| 45 | opt.file[0] = yeah[0] |
---|
[489] | 46 | for file in yeah[1:]: opt.file[0] = opt.file[0] + "," + file |
---|
[478] | 47 | |
---|
| 48 | ############################# |
---|
[392] | 49 | ### 1. LOOP ON FILE LISTS TO BE PUT IN DIFFERENT FIGURES |
---|
| 50 | for i in range(len(opt.file)): |
---|
[349] | 51 | |
---|
[392] | 52 | zefiles = separatenames(opt.file[i]) |
---|
[380] | 53 | |
---|
[426] | 54 | typefile = whatkindfile(Dataset(zefiles[0])) ; stralt = None |
---|
[562] | 55 | if typefile in ["meso"]: |
---|
[392] | 56 | [lschar,zehour,zehourin] = getlschar ( zefiles[0] ) |
---|
[760] | 57 | if opt.var is None: opt.var = ["HGT"] #; opt.clb = "nobar" # otherwise breaks the automatic one-var file capability |
---|
[429] | 58 | elif typefile in ["geo"]: |
---|
[763] | 59 | lschar="" |
---|
[426] | 60 | if opt.var is None: opt.var = ["HGT_M"] ; opt.clb = "nobar" |
---|
[763] | 61 | else: |
---|
| 62 | lschar="" |
---|
[721] | 63 | if opt.var is None: |
---|
| 64 | opt.var = ["phisinit"] ; opt.clb = "nobar" |
---|
| 65 | ### temporaire... en attendant mieux. |
---|
| 66 | if opt.back == "titan": opt.var = ["phis"] ; opt.clb = "nobar" |
---|
[380] | 67 | |
---|
[392] | 68 | if opt.vmin is not None : zevmin = opt.vmin[min(i,len(opt.vmin)-1)] |
---|
| 69 | else: zevmin = None |
---|
| 70 | if opt.vmax is not None : zevmax = opt.vmax[min(i,len(opt.vmax)-1)] |
---|
| 71 | else: zevmax = None |
---|
| 72 | #print "vmin, zevmin", opt.vmin, zevmin ; print "vmax, zevmax", opt.vmax, zevmax |
---|
[380] | 73 | |
---|
[392] | 74 | ############################# |
---|
| 75 | ### 2. LOOP ON VAR LISTS TO BE PUT IN DIFFERENT FIGURES |
---|
| 76 | for j in range(len(opt.var)): |
---|
[380] | 77 | |
---|
[392] | 78 | zevars = separatenames(opt.var[j]) |
---|
[380] | 79 | |
---|
[377] | 80 | inputnvert = separatenames(opt.lvl) |
---|
[351] | 81 | if np.array(inputnvert).size == 1: |
---|
| 82 | zelevel = float(inputnvert[0]) |
---|
| 83 | ze_interp_levels = [-9999.] |
---|
[610] | 84 | elif np.array(inputnvert).size > 2: |
---|
[489] | 85 | zelevel = -99. |
---|
[610] | 86 | start = float(inputnvert[0]) |
---|
| 87 | stop = float(inputnvert[1]) |
---|
| 88 | if np.array(inputnvert).size == 2: numsample = 20 |
---|
| 89 | else: numsample = float(inputnvert[2]) |
---|
| 90 | if stop > start: |
---|
| 91 | ## altitude coordinates |
---|
| 92 | ze_interp_levels = np.linspace(start,stop,numsample) |
---|
| 93 | else: |
---|
| 94 | ## pressure coordinates |
---|
| 95 | ze_interp_levels = np.logspace(np.log10(start),np.log10(stop),numsample) |
---|
[351] | 96 | |
---|
[392] | 97 | ######################################################### |
---|
[377] | 98 | if opt.itp is not None: |
---|
[562] | 99 | if opt.itp > 0: |
---|
[392] | 100 | ##### |
---|
| 101 | ##### MESOSCALE : written by AS |
---|
| 102 | ##### |
---|
[562] | 103 | if typefile in ["meso"]: |
---|
[377] | 104 | if zelevel == 0. and opt.itp == 4: zelevel = 0.010 |
---|
[349] | 105 | ### winds or no winds |
---|
| 106 | if opt.winds : zefields = 'uvmet' |
---|
[612] | 107 | elif opt.var[j] in ['deltat','DELTAT'] : zefields = 'tk,TSURF' |
---|
[763] | 108 | elif opt.var[j] in ['uv','UV','hodograph','hodograph_2'] : zefields = 'U,V' |
---|
[349] | 109 | else : zefields = '' |
---|
| 110 | ### var or no var |
---|
[392] | 111 | if zefields == '' : zefields = opt.var[j] |
---|
| 112 | else : zefields = zefields + "," + opt.var[j] |
---|
| 113 | if opt.var2 is not None : zefields = zefields + "," + opt.var2 |
---|
| 114 | ### call fortran routines |
---|
| 115 | for fff in range(len(zefiles)): |
---|
| 116 | newname = api_onelevel ( path_to_input = '', \ |
---|
| 117 | input_name = zefiles[fff], \ |
---|
| 118 | fields = zefields, \ |
---|
| 119 | interp_method = opt.itp, \ |
---|
| 120 | interp_level = ze_interp_levels, \ |
---|
| 121 | onelevel = zelevel, \ |
---|
| 122 | nocall = opt.nocall ) |
---|
| 123 | if fff == 0: zetab = newname |
---|
| 124 | else: zetab = np.append(zetab,newname) |
---|
[569] | 125 | if interpref: |
---|
| 126 | reffile = api_onelevel ( path_to_input = '', \ |
---|
| 127 | input_name = opt.fref, \ |
---|
| 128 | fields = zefields, \ |
---|
| 129 | interp_method = opt.itp, \ |
---|
| 130 | interp_level = ze_interp_levels, \ |
---|
| 131 | onelevel = zelevel, \ |
---|
| 132 | nocall = opt.nocall ) |
---|
[392] | 133 | zefiles = zetab #; print zefiles |
---|
[349] | 134 | zelevel = 0 ## so that zelevel could play again the role of nvert |
---|
[392] | 135 | ##### |
---|
| 136 | ##### GCM : written by AC |
---|
| 137 | ##### |
---|
| 138 | elif typefile == "gcm": |
---|
[475] | 139 | inputvar = zevars |
---|
| 140 | if opt.var2 is not None : inputvar = np.append(inputvar,opt.var2) |
---|
[392] | 141 | interpolated_files="" |
---|
| 142 | interpolated_files=call_zrecast(interp_mode=opt.itp,\ |
---|
| 143 | input_name=zefiles,\ |
---|
[475] | 144 | fields=inputvar,\ |
---|
[489] | 145 | limites = ze_interp_levels,\ |
---|
[464] | 146 | predefined=opt.intas) |
---|
[349] | 147 | |
---|
[392] | 148 | zefiles=interpolated_files |
---|
| 149 | if interpref: |
---|
| 150 | interpolated_ref="" |
---|
| 151 | interpolated_ref=call_zrecast(interp_mode=opt.itp,\ |
---|
[380] | 152 | input_name=[opt.fref],\ |
---|
[409] | 153 | fields=zevars,\ |
---|
[464] | 154 | predefined=opt.intas) |
---|
[380] | 155 | |
---|
[392] | 156 | reffile=interpolated_ref[0] |
---|
| 157 | else: |
---|
[429] | 158 | print "type not supported" |
---|
[392] | 159 | exit() |
---|
[377] | 160 | |
---|
[392] | 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=zevars,\ |
---|
[760] | 165 | clb=separatenames(opt.clb),winds=opt.winds,\ |
---|
[763] | 166 | addchar=lschar,vmin=zevmin,vmax=zevmax,\ |
---|
[349] | 167 | tile=opt.tile,zoom=opt.zoom,display=opt.display,\ |
---|
[424] | 168 | hole=opt.hole,save=opt.save,\ |
---|
| 169 | anomaly=opt.anomaly,var2=opt.var2,ndiv=opt.ndiv,\ |
---|
[578] | 170 | mult=opt.mult,zetitle=separatenames(opt.zetitle),\ |
---|
[359] | 171 | slon=zeslon,slat=zeslat,svert=zesvert,stime=zestime,\ |
---|
[377] | 172 | outputname=opt.out,resolution=opt.res,\ |
---|
[380] | 173 | ope=opt.operat,fileref=reffile,minop=opt.vminope,maxop=opt.vmaxope,titleref=opt.titref,\ |
---|
[763] | 174 | invert_y=opt.inverty,xaxis=zexaxis,yaxis=zeyaxis,ylog=opt.logy,xlog=opt.logx,yintegral=opt.column,\ |
---|
[453] | 175 | blat=opt.blat,blon=opt.blon,tsat=opt.tsat,flagnolow=opt.nolow,\ |
---|
[483] | 176 | mrate=opt.rate,mquality=opt.quality,trans=opt.trans,zarea=opt.area,axtime=opt.axtime,\ |
---|
[612] | 177 | redope=opt.redope,seevar=opt.seevar,xlab=opt.xlab,ylab=opt.ylab,lbls=separatenames(opt.labels),\ |
---|
[760] | 178 | lstyle=separatenames(opt.linestyle),cross=readslices(opt.mark),facwind=opt.facwind,\ |
---|
[763] | 179 | trycol=opt.trycol,streamflag=opt.stream,analysis=opt.analysis) |
---|
[392] | 180 | print 'DONE: '+name |
---|
[349] | 181 | system("rm -f to_be_erased") |
---|
| 182 | |
---|
| 183 | ######################################################### |
---|
| 184 | ### Generate a .sh file with the used command saved in it |
---|
[392] | 185 | command = "" |
---|
[349] | 186 | for arg in sys.argv: command = command + arg + ' ' |
---|
[424] | 187 | #if typefile not in ["meso","mesoapi"]: name = 'pycommand' |
---|
[451] | 188 | if opt.save == "gui": name = 'pycommand' |
---|
| 189 | elif opt.save == "avi": system("mv -f movie*.avi "+name+".avi") |
---|
| 190 | 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] | 191 | f = open(name+'.sh', 'w') |
---|
| 192 | f.write(command) |
---|
[392] | 193 | |
---|
[468] | 194 | #print "********** OPTIONS: ", opt |
---|
[392] | 195 | print "********************************************************** END" |
---|