| 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 | import myplot |
|---|
| 11 | import os |
|---|
| 12 | import numpy as np |
|---|
| 13 | import netCDF4 |
|---|
| 14 | import glob |
|---|
| 15 | |
|---|
| 16 | from optparse import OptionParser ### to be replaced by argparse |
|---|
| 17 | from api_wrapper import api_onelevel |
|---|
| 18 | from gcm_transformations import call_zrecast |
|---|
| 19 | from planetoplot import planetoplot |
|---|
| 20 | from myscript import getparseroptions |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | ############################# |
|---|
| 24 | ### Get options and variables |
|---|
| 25 | parser = OptionParser() ; getparseroptions(parser) ; (opt,args) = parser.parse_args() |
|---|
| 26 | if opt.file is None: myplot.errormess("I want to eat one file at least ! Use pp.py -f name_of_my_file. Or type pp.py -h") |
|---|
| 27 | if opt.var is None and opt.anomaly is True: myplot.errormess("Cannot ask to compute anomaly if no variable is set") |
|---|
| 28 | if opt.fref is not None and opt.operat is None: myplot.errormess("you must specify an operation when using a reference file") |
|---|
| 29 | if opt.operat in ["+","-"] and opt.fref is None: myplot.errormess("you must specifiy a reference file when using inter-file operations") |
|---|
| 30 | if opt.fref is not None and opt.operat is not None and opt.itp is not None: interpref=True |
|---|
| 31 | else: interpref=False |
|---|
| 32 | if opt.rate is not None: opt.save = "avi" |
|---|
| 33 | elif opt.save == "avi": opt.rate = 8 ## this is a default value for -S avi |
|---|
| 34 | if opt.save == "html": opt.rate = -1 ## this is convenient because everything is done in planetoplot with mrate |
|---|
| 35 | |
|---|
| 36 | ############################# |
|---|
| 37 | ### Get infos about slices |
|---|
| 38 | zeslat = myplot.readslices(opt.slat) ; zeslon = myplot.readslices(opt.slon) ; zesvert = myplot.readslices(opt.svert) ; zestime = myplot.readslices(opt.stime) |
|---|
| 39 | |
|---|
| 40 | reffile = opt.fref |
|---|
| 41 | zexaxis = [opt.xmin,opt.xmax] ; zeyaxis=[opt.ymin,opt.ymax] |
|---|
| 42 | |
|---|
| 43 | ############################# |
|---|
| 44 | ### Catch multiple files |
|---|
| 45 | if "*" in opt.file[0] or "?" in opt.file[0]: |
|---|
| 46 | yeah = glob.glob(opt.file[0]) ; yeah.sort() |
|---|
| 47 | opt.file[0] = yeah[0] |
|---|
| 48 | for file in yeah[1:]: opt.file[0] = opt.file[0] + "," + file |
|---|
| 49 | |
|---|
| 50 | ############################# |
|---|
| 51 | ### 1. LOOP ON FILE LISTS TO BE PUT IN DIFFERENT FIGURES |
|---|
| 52 | for i in range(len(opt.file)): |
|---|
| 53 | |
|---|
| 54 | zefiles = myplot.separatenames(opt.file[i]) |
|---|
| 55 | |
|---|
| 56 | typefile = myplot.whatkindfile(netCDF4.Dataset(zefiles[0])) ; stralt = None |
|---|
| 57 | if typefile in ["meso"]: |
|---|
| 58 | [lschar,zehour,zehourin] = myplot.getlschar ( zefiles[0] ) |
|---|
| 59 | if opt.var is None: opt.var = ["HGT"] ; opt.nocolorb = True |
|---|
| 60 | elif typefile in ["geo"]: |
|---|
| 61 | lschar="" |
|---|
| 62 | if opt.var is None: opt.var = ["HGT_M"] ; opt.nocolorb = True |
|---|
| 63 | else: |
|---|
| 64 | lschar="" |
|---|
| 65 | if opt.var is None: |
|---|
| 66 | opt.var = ["phisinit"] ; opt.clb = "nobar" |
|---|
| 67 | ### temporaire... en attendant mieux. |
|---|
| 68 | if opt.back == "titan": opt.var = ["phis"] ; opt.nocolorb = True |
|---|
| 69 | |
|---|
| 70 | if opt.vmin is not None : zevmin = opt.vmin[min(i,len(opt.vmin)-1)] |
|---|
| 71 | else: zevmin = None |
|---|
| 72 | if opt.vmax is not None : zevmax = opt.vmax[min(i,len(opt.vmax)-1)] |
|---|
| 73 | else: zevmax = None |
|---|
| 74 | #print "vmin, zevmin", opt.vmin, zevmin ; print "vmax, zevmax", opt.vmax, zevmax |
|---|
| 75 | |
|---|
| 76 | ############################# |
|---|
| 77 | ### 2. LOOP ON VAR LISTS TO BE PUT IN DIFFERENT FIGURES |
|---|
| 78 | for j in range(len(opt.var)): |
|---|
| 79 | |
|---|
| 80 | zevars = myplot.separatenames(opt.var[j]) |
|---|
| 81 | |
|---|
| 82 | inputnvert = myplot.separatenames(opt.lvl) |
|---|
| 83 | if np.array(inputnvert).size == 1: |
|---|
| 84 | zelevel = float(inputnvert[0]) |
|---|
| 85 | ze_interp_levels = [-9999.] |
|---|
| 86 | elif np.array(inputnvert).size > 2: |
|---|
| 87 | zelevel = -99. |
|---|
| 88 | start = float(inputnvert[0]) |
|---|
| 89 | stop = float(inputnvert[1]) |
|---|
| 90 | if np.array(inputnvert).size == 2: numsample = 20 |
|---|
| 91 | else: numsample = float(inputnvert[2]) |
|---|
| 92 | if stop > start: |
|---|
| 93 | ## altitude coordinates |
|---|
| 94 | ze_interp_levels = np.linspace(start,stop,numsample) |
|---|
| 95 | else: |
|---|
| 96 | ## pressure coordinates |
|---|
| 97 | ze_interp_levels = np.logspace(np.log10(start),np.log10(stop),numsample) |
|---|
| 98 | |
|---|
| 99 | ######################################################### |
|---|
| 100 | if opt.itp is not None: |
|---|
| 101 | if opt.itp > 0: |
|---|
| 102 | ##### |
|---|
| 103 | ##### MESOSCALE : written by AS |
|---|
| 104 | ##### |
|---|
| 105 | if typefile in ["meso"]: |
|---|
| 106 | if zelevel == 0. and opt.itp == 4: zelevel = 0.010 |
|---|
| 107 | if np.array(inputnvert).size == 1: zesvert = myplot.readslices([str(zelevel)]) |
|---|
| 108 | ### winds or no winds |
|---|
| 109 | if opt.winds : zefields = 'uvmet' |
|---|
| 110 | elif opt.var[j] in ['deltat','DELTAT'] : zefields = 'tk,TSURF' |
|---|
| 111 | elif opt.var[j] in ['uv','UV','hodograph','hodograph_2'] : zefields = 'U,V' |
|---|
| 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=myplot.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=myplot.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=myplot.separatenames(opt.labels),\ |
|---|
| 181 | lstyle=myplot.separatenames(opt.linestyle),cross=myplot.readslices(opt.mark),markdevil=opt.mdevil,facwind=opt.facwind,\ |
|---|
| 182 | trycol=opt.trycol,streamflag=opt.stream,nocolorb=opt.nocolorb,analysis=opt.analysis,monster=opt.monster) |
|---|
| 183 | print 'DONE: '+name |
|---|
| 184 | os.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": os.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" |
|---|