[910] | 1 | #! /usr/bin/env python |
---|
| 2 | ############################################## |
---|
| 3 | ## A MINIMAL PP.PY SCRIPT USING PPCLASS.PY ## |
---|
| 4 | ## Author: A. Spiga 03/2013 ## |
---|
| 5 | ############################################## |
---|
| 6 | from optparse import OptionParser ### TBR by argparse |
---|
| 7 | from ppclass import pp, inspect |
---|
[912] | 8 | import sys |
---|
[910] | 9 | ############################################## |
---|
| 10 | |
---|
| 11 | # NB: this is a convenient command-line script |
---|
| 12 | # ... but ppclass is more versatile |
---|
| 13 | # ... than what is proposed here |
---|
| 14 | # ... e.g. differences between files, |
---|
| 15 | # ... complex operations, |
---|
| 16 | # ... see sample scripts |
---|
| 17 | |
---|
[923] | 18 | ###################################### |
---|
| 19 | # define parser with version and usage |
---|
| 20 | ###################################### |
---|
[910] | 21 | parser = OptionParser() |
---|
[923] | 22 | parser.version = \ |
---|
[924] | 23 | '''************************************************** |
---|
| 24 | ******** PLANETOPLOT (for help: pp.py -h) ******** |
---|
| 25 | **************************************************''' |
---|
| 26 | parser.usage = \ |
---|
| 27 | '''pp.py [options] netCDF file(s) |
---|
| 28 | (NB: no options --> simple inspection of variables and dimensions in netCDF files) |
---|
| 29 | ------------------- |
---|
| 30 | PLANETOPLOT |
---|
[923] | 31 | --> command line tool to make nice & quick plots from netCDF files |
---|
| 32 | --> based on python + numpy + scipy + matplotlib + basemap + netCDF4 |
---|
| 33 | --> Author: A. Spiga (LMD/UPMC) aymeric.spiga@upmc.fr |
---|
[924] | 34 | -------------------''' |
---|
[923] | 35 | parser.print_version() |
---|
| 36 | |
---|
| 37 | ######################################## |
---|
| 38 | # set options for the pp.py command line |
---|
| 39 | ######################################## |
---|
[920] | 40 | parser.add_option('--verbose',action='store_true',dest='verbose',default=False,help='make the program verbose') |
---|
[910] | 41 | # field --> lower case |
---|
[920] | 42 | parser.add_option('-v','--var',action='append',dest='var',type="string",default=None,help="'variable' or ['var1','var2',etc]") |
---|
[923] | 43 | parser.add_option('-x','--lon',action='append',dest='x',type="string",default=None,help="x axis value. one value; or val1,val2 (computations)") |
---|
| 44 | parser.add_option('-y','--lat',action='append',dest='y',type="string",default=None,help="y axis value. one value; or val1,val2 (computations)") |
---|
| 45 | parser.add_option('-z','--vert',action='append',dest='z',type="string",default=None,help="z axis value. one value; or val1,val2 (computations)") |
---|
| 46 | parser.add_option('-t','--time',action='append',dest='t',type="string",default=None,help="t axis value. one value; or val1,val2 (computations)") |
---|
| 47 | parser.add_option('-u','--compute',action='store',dest='compute',type="string",default="mean",help="computation: mean, min, max, meanarea") |
---|
[920] | 48 | parser.add_option('-c','--contour',action='store',dest='contour',type="string",default=None,help="one 'variable' for contour") |
---|
| 49 | parser.add_option('-i','--vecx',action='store',dest='vecx',type="string",default=None,help="one 'variable' for wind vector x component") |
---|
[981] | 50 | parser.add_option('-j','--vecy',action='store',dest='vecy',type="string",default=None,help="one 'variable' for wind vector y component") |
---|
[920] | 51 | parser.add_option('-m','--mult',action='store',dest='mult',type="float",default=None,help="multiplicative factor on field") |
---|
| 52 | parser.add_option('-a','--add',action='store',dest='add',type="float",default=None,help="additive factor on field") |
---|
| 53 | parser.add_option('-o','--output',action='store',dest='filename',type="string",default="myplot",help="name of output files") |
---|
| 54 | parser.add_option('-d','--directory',action='store',dest='folder',type="string",default="./",help="directory of output files") |
---|
[942] | 55 | parser.add_option('-s','--changetime',action='store',dest='changetime',type="string",default=None,\ |
---|
| 56 | help="transformation on time axis : [None] | mars_sol2ls | mars_meso_ls | mars_meso_sol | mars_meso_utc | mars_meso_lt ") |
---|
[990] | 57 | parser.add_option('-p','--print',action='store_true',dest='savtxt',default=False,help="[1D] output field+coord in an ASCII file") |
---|
[981] | 58 | parser.add_option('--stridex',action='store',dest='stridex',type="int",default=1,help="Load data every stridex grid points over x dimension") |
---|
| 59 | parser.add_option('--stridey',action='store',dest='stridey',type="int",default=1,help="Load data every stridex grid points over y dimension") |
---|
| 60 | parser.add_option('--stridez',action='store',dest='stridez',type="int",default=1,help="Load data every stridex grid points over z dimension") |
---|
| 61 | parser.add_option('--stridet',action='store',dest='stridet',type="int",default=1,help="Load data every stridex grid points over t dimension") |
---|
[910] | 62 | # plot --> upper case |
---|
| 63 | # -- generic |
---|
[920] | 64 | parser.add_option('-T','--title',action='append',dest='title',type="string",default=None,help="change 'title'") |
---|
| 65 | parser.add_option('-X','--xlabel',action='append',dest='xlabel',type="string",default=None,help="change 'xlabel'") |
---|
| 66 | parser.add_option('-Y','--ylabel',action='append',dest='ylabel',type="string",default=None,help="change 'ylabel'") |
---|
| 67 | parser.add_option('-D','--div',action='store',dest='div',type="int",default=20,help="integer for number of divisions") |
---|
| 68 | parser.add_option('-H','--trans',action='store',dest='trans',type="float",default=1.0,help="float for transparency (0=transp,1=opaque)") |
---|
| 69 | parser.add_option('-Z','--logy',action='store_true',dest='logy',default=False,help="set log for vertical axis") |
---|
| 70 | parser.add_option('-O','--save',action='store',dest='out',type="string",default="gui",help="save mode: 'gui' 'png' 'pdf' 'eps' 'svg' 'ps'") |
---|
[972] | 71 | parser.add_option('-V','--void',action='store_true',dest='void',default=False,help="no colorbar, no title, no labels") |
---|
| 72 | parser.add_option('-U','--units',action='append',dest='units',type="string",default=None,help="units for the field") |
---|
[910] | 73 | # -- 1D plot |
---|
[920] | 74 | parser.add_option('-L','--lstyle',action='append',dest='lstyle',type="string",default=None,help="[1D] linestyle: '-' '--' '.' '..'") |
---|
| 75 | parser.add_option('-Q','--color',action='append',dest='color',type="string",default=None,help="[1D] color: 'b' 'g' 'r' etc") |
---|
| 76 | parser.add_option('-K','--marker',action='append',dest='marker',type="string",default=None,help="[1D] marker: '' 'x' 'o' etc") |
---|
| 77 | parser.add_option('-S','--superpose',action='store_true',dest='superpose',default=False,help="[1D] use same axis for all plots") |
---|
[923] | 78 | parser.add_option('-E','--label',action='append',dest='label',type="string",default=None,help="[1D] label for line") |
---|
[963] | 79 | parser.add_option('--xcoeff',action='append',dest='xcoeff',type="float",default=None,help="[1D] multiply x axis") |
---|
| 80 | parser.add_option('--ycoeff',action='append',dest='ycoeff',type="float",default=None,help="[1D] multiply y axis") |
---|
[977] | 81 | parser.add_option('--xmin',action='append',dest='xmin',type="float",default=None,help="[1D] min bound x axis") |
---|
| 82 | parser.add_option('--ymin',action='append',dest='ymin',type="float",default=None,help="[1D] min bound y axis") |
---|
| 83 | parser.add_option('--xmax',action='append',dest='xmax',type="float",default=None,help="[1D] max bound x axis") |
---|
| 84 | parser.add_option('--ymax',action='append',dest='ymax',type="float",default=None,help="[1D] max bound y axis") |
---|
[991] | 85 | parser.add_option('--modx',action='append',dest='modx',type="float",default=None,help="[1D] change xticks with a modulo") |
---|
[910] | 86 | # -- 2D plot |
---|
[920] | 87 | parser.add_option('-C','--colorb',action='append',dest='colorb',type="string",default=None,help="[2D] colormap: http://micropore.files.wordpress.com/2010/06/colormaps.png") |
---|
[972] | 88 | parser.add_option('-P','--proj',action='append',dest='proj',type="string",default=None,help="[2D] map projection: 'cyl' 'npstere' 'spstere' 'ortho' 'moll' 'robin' 'lcc' 'laea' 'merc' 'noproj'") |
---|
[920] | 89 | parser.add_option('-B','--back',action='append',dest='back',type="string",default=None,help='[2D] predefined map background (cf. set_back.txt)') |
---|
| 90 | parser.add_option('-A','--area',action='append',dest='area',type="string",default=None,help='[2D] predefined region of mapping (cf. set_area.txt)') |
---|
| 91 | parser.add_option('-I','--blon',action='append',dest='blon',type="float",default=None,help='[2D] float: bounding longitude for stere (or center longitude for ortho)') |
---|
| 92 | parser.add_option('-J','--blat',action='append',dest='blat',type="float",default=None,help='[2D] float: bounding latitude for stere (or center latitude for ortho) ') |
---|
| 93 | parser.add_option('-N','--vmin',action='append',dest='vmin',type="float",default=None,help='[2D] float: minimum value for displayed field') |
---|
| 94 | parser.add_option('-M','--vmax',action='append',dest='vmax',type="float",default=None,help='[2D] float: maximum value for displayed field') |
---|
[972] | 95 | parser.add_option('-W','--wscale',action='append',dest='wscale',type="float",default=None,help='[2D] float: set size of reference wind vector') |
---|
[982] | 96 | parser.add_option('--stridevecx',action='store',dest='stridevecx',type="int",default=1,help="Define an abscissa stride on vectors only -- not on field") |
---|
| 97 | parser.add_option('--stridevecy',action='store',dest='stridevecy',type="int",default=1,help="Define an ordinate stride on vectors only -- not on field") |
---|
| 98 | ########################### |
---|
[910] | 99 | (opt,args) = parser.parse_args() |
---|
[972] | 100 | # remains F G R |
---|
[910] | 101 | |
---|
[923] | 102 | ###################################### |
---|
| 103 | # get arguments (one or several files) |
---|
| 104 | ###################################### |
---|
| 105 | if args is None: |
---|
| 106 | print "Stop here! I need file(s) as argument(s)!" ; exit() |
---|
| 107 | else: |
---|
| 108 | files = args |
---|
| 109 | |
---|
| 110 | ############################################# |
---|
| 111 | # a possibility to simply inspect the file(s) |
---|
| 112 | ############################################# |
---|
[910] | 113 | if opt.var is None: |
---|
[923] | 114 | for filename in files: inspect(filename) |
---|
[910] | 115 | exit() |
---|
| 116 | |
---|
| 117 | ###################################### |
---|
| 118 | # use ppclass to get field and plot it |
---|
| 119 | ###################################### |
---|
| 120 | # treat the case of additional vectors or contours (contours must be before vectors) |
---|
| 121 | var = [] ; vargoal = [] |
---|
| 122 | for element in opt.var: |
---|
| 123 | var.append(element) ; vargoal.append("main") |
---|
| 124 | if opt.contour is not None: var.append(opt.contour) ; vargoal.append("contour") |
---|
| 125 | if opt.vecx is not None: var.append(opt.vecx) ; vargoal.append("vector") |
---|
| 126 | if opt.vecy is not None: var.append(opt.vecy) ; vargoal.append("vector") |
---|
| 127 | # set pp object |
---|
[920] | 128 | user = pp() |
---|
[923] | 129 | user.file = files |
---|
| 130 | user.var = var ; user.vargoal = vargoal |
---|
| 131 | user.x = opt.x ; user.y = opt.y |
---|
| 132 | user.z = opt.z ; user.t = opt.t |
---|
[920] | 133 | user.verbose = opt.verbose |
---|
| 134 | user.compute = opt.compute |
---|
[939] | 135 | user.changetime = opt.changetime |
---|
[981] | 136 | user.stridex = opt.stridex |
---|
| 137 | user.stridey = opt.stridey |
---|
| 138 | user.stridez = opt.stridez |
---|
| 139 | user.stridet = opt.stridet |
---|
[982] | 140 | user.stridevecx = opt.stridevecx |
---|
| 141 | user.stridevecy = opt.stridevecy |
---|
[990] | 142 | user.savtxt = opt.savtxt |
---|
[910] | 143 | # define field |
---|
| 144 | user.define() |
---|
| 145 | # retrieve field |
---|
| 146 | user.retrieve() |
---|
| 147 | # some possible operations |
---|
| 148 | if opt.add is not None: user = user + opt.add |
---|
| 149 | if opt.mult is not None: user = user * opt.mult |
---|
[912] | 150 | # get some options |
---|
| 151 | user.superpose = opt.superpose |
---|
| 152 | user.filename = opt.filename |
---|
| 153 | user.folder = opt.folder |
---|
| 154 | user.out = opt.out |
---|
[972] | 155 | # if noproj is given for proj, no map mode |
---|
| 156 | if opt.proj is not None: |
---|
| 157 | if 'noproj' in opt.proj: |
---|
| 158 | user.noproj = True |
---|
[963] | 159 | # if user wants to give a name, we drop the indication of date |
---|
| 160 | if opt.filename != "myplot": |
---|
| 161 | user.includedate = False |
---|
[910] | 162 | # define plot |
---|
| 163 | user.defineplot() |
---|
| 164 | # user-defined plot settings |
---|
[972] | 165 | # ... shouldn't this be before defineplot? |
---|
[910] | 166 | user.getopt(opt) |
---|
| 167 | # make plot |
---|
| 168 | user.makeplot() |
---|
[912] | 169 | |
---|
| 170 | #################################### |
---|
| 171 | # save a .sh file with the command # |
---|
| 172 | #################################### |
---|
| 173 | command = "" |
---|
| 174 | for arg in sys.argv: command = command + arg + ' ' |
---|
[920] | 175 | try: |
---|
| 176 | f = open(opt.folder+'/'+opt.filename+'.sh', 'w') |
---|
[923] | 177 | f.write(command) |
---|
[920] | 178 | except IOError: |
---|
| 179 | print "!! WARNING !! pp.py command not saved. Probably do not have permission to write here." |
---|