[941] | 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 | elif opt.var[j] in ['uvmrams']: zefields = 'Umetmrams,Vmetmrams' |
---|
| 112 | else : zefields = '' |
---|
| 113 | ### var or no var |
---|
| 114 | if zefields == '' : zefields = opt.var[j] |
---|
| 115 | else : zefields = zefields + "," + opt.var[j] |
---|
| 116 | if opt.var2 is not None : zefields = zefields + "," + opt.var2 |
---|
| 117 | ### call fortran routines |
---|
| 118 | for fff in range(len(zefiles)): |
---|
| 119 | newname = api_onelevel ( path_to_input = '', \ |
---|
| 120 | input_name = zefiles[fff], \ |
---|
| 121 | fields = zefields, \ |
---|
| 122 | interp_method = opt.itp, \ |
---|
| 123 | interp_level = ze_interp_levels, \ |
---|
| 124 | onelevel = zelevel, \ |
---|
| 125 | nocall = opt.nocall ) |
---|
| 126 | if fff == 0: zetab = newname |
---|
| 127 | else: zetab = np.append(zetab,newname) |
---|
| 128 | if interpref: |
---|
| 129 | reffile = api_onelevel ( path_to_input = '', \ |
---|
| 130 | input_name = opt.fref, \ |
---|
| 131 | fields = zefields, \ |
---|
| 132 | interp_method = opt.itp, \ |
---|
| 133 | interp_level = ze_interp_levels, \ |
---|
| 134 | onelevel = zelevel, \ |
---|
| 135 | nocall = opt.nocall ) |
---|
| 136 | zefiles = zetab #; print zefiles |
---|
| 137 | zelevel = 0 ## so that zelevel could play again the role of nvert |
---|
| 138 | ##### |
---|
| 139 | ##### GCM : written by AC |
---|
| 140 | ##### |
---|
| 141 | elif typefile == "gcm": |
---|
| 142 | inputvar = zevars |
---|
| 143 | if opt.var2 is not None : inputvar = np.append(inputvar,opt.var2) |
---|
| 144 | interpolated_files="" |
---|
| 145 | interpolated_files=call_zrecast(interp_mode=opt.itp,\ |
---|
| 146 | input_name=zefiles,\ |
---|
| 147 | fields=inputvar,\ |
---|
| 148 | limites = ze_interp_levels,\ |
---|
| 149 | predefined=opt.intas) |
---|
| 150 | |
---|
| 151 | zefiles=interpolated_files |
---|
| 152 | if interpref: |
---|
| 153 | interpolated_ref="" |
---|
| 154 | interpolated_ref=call_zrecast(interp_mode=opt.itp,\ |
---|
| 155 | input_name=[opt.fref],\ |
---|
| 156 | fields=zevars,\ |
---|
| 157 | predefined=opt.intas) |
---|
| 158 | |
---|
| 159 | reffile=interpolated_ref[0] |
---|
| 160 | else: |
---|
| 161 | print "type not supported" |
---|
| 162 | exit() |
---|
| 163 | |
---|
| 164 | ############# |
---|
| 165 | ### Main call |
---|
| 166 | name = planetoplot (zefiles,level=int(zelevel),vertmode=opt.itp,\ |
---|
| 167 | proj=opt.proj,back=opt.back,target=opt.tgt,stride=opt.ste,var=zevars,\ |
---|
| 168 | clb=separatenames(opt.clb),winds=opt.winds,\ |
---|
| 169 | addchar=lschar,vmin=zevmin,vmax=zevmax,\ |
---|
| 170 | tile=opt.tile,zoom=opt.zoom,display=opt.display,\ |
---|
| 171 | hole=opt.hole,save=opt.save,\ |
---|
| 172 | anomaly=opt.anomaly,var2=opt.var2,ndiv=opt.ndiv,\ |
---|
| 173 | mult=opt.mult,add=opt.add,zetitle=separatenames(opt.zetitle),\ |
---|
| 174 | slon=zeslon,slat=zeslat,svert=zesvert,stime=zestime,\ |
---|
| 175 | outputname=opt.out,resolution=opt.res,\ |
---|
| 176 | ope=opt.operat,fileref=reffile,minop=opt.vminope,maxop=opt.vmaxope,titleref=opt.titref,\ |
---|
| 177 | invert_y=opt.inverty,xaxis=zexaxis,yaxis=zeyaxis,ylog=opt.logy,xlog=opt.logx,yintegral=opt.column,\ |
---|
| 178 | blat=opt.blat,blon=opt.blon,tsat=opt.tsat,flagnolow=opt.nolow,\ |
---|
| 179 | mrate=opt.rate,mquality=opt.quality,trans=opt.trans,zarea=opt.area,axtime=opt.axtime,\ |
---|
| 180 | redope=opt.redope,seevar=opt.seevar,xlab=opt.xlab,ylab=opt.ylab,lbls=separatenames(opt.labels),\ |
---|
| 181 | lstyle=separatenames(opt.linestyle),cross=readslices(opt.mark),markdevil=opt.mdevil,facwind=opt.facwind,\ |
---|
| 182 | trycol=opt.trycol,streamflag=opt.stream,nocolorb=opt.nocolorb,analysis=opt.analysis) |
---|
| 183 | print 'DONE: '+name |
---|
| 184 | system("rm -f to_be_erased") |
---|
| 185 | |
---|
| 186 | ######################################################### |
---|
| 187 | ### Generate a .sh file with the used command saved in it |
---|
| 188 | command = "" |
---|
| 189 | for arg in sys.argv: command = command + arg + ' ' |
---|
| 190 | #if typefile not in ["meso","mesoapi"]: name = 'pycommand' |
---|
| 191 | if opt.save == "gui": name = 'pycommand' |
---|
| 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" |
---|