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 |
---|
8 | import sys |
---|
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 | |
---|
18 | ###################################### |
---|
19 | # define parser with version and usage |
---|
20 | ###################################### |
---|
21 | parser = OptionParser() |
---|
22 | parser.version = \ |
---|
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 |
---|
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 |
---|
34 | -------------------''' |
---|
35 | parser.print_version() |
---|
36 | |
---|
37 | ######################################## |
---|
38 | # set options for the pp.py command line |
---|
39 | ######################################## |
---|
40 | parser.add_option('--verbose',action='store_true',dest='verbose',default=False,help='make the program verbose') |
---|
41 | # field --> lower case |
---|
42 | parser.add_option('-v','--var',action='append',dest='var',type="string",default=None,help="'variable' or ['var1','var2',etc]") |
---|
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") |
---|
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") |
---|
50 | parser.add_option('-j','--vecy',action='store',dest='vecy',type="string",default=None,help="one 'variable' for wind vector x component") |
---|
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") |
---|
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 ") |
---|
57 | # plot --> upper case |
---|
58 | # -- generic |
---|
59 | parser.add_option('-T','--title',action='append',dest='title',type="string",default=None,help="change 'title'") |
---|
60 | parser.add_option('-X','--xlabel',action='append',dest='xlabel',type="string",default=None,help="change 'xlabel'") |
---|
61 | parser.add_option('-Y','--ylabel',action='append',dest='ylabel',type="string",default=None,help="change 'ylabel'") |
---|
62 | parser.add_option('-D','--div',action='store',dest='div',type="int",default=20,help="integer for number of divisions") |
---|
63 | parser.add_option('-H','--trans',action='store',dest='trans',type="float",default=1.0,help="float for transparency (0=transp,1=opaque)") |
---|
64 | parser.add_option('-Z','--logy',action='store_true',dest='logy',default=False,help="set log for vertical axis") |
---|
65 | parser.add_option('-O','--save',action='store',dest='out',type="string",default="gui",help="save mode: 'gui' 'png' 'pdf' 'eps' 'svg' 'ps'") |
---|
66 | # -- 1D plot |
---|
67 | parser.add_option('-L','--lstyle',action='append',dest='lstyle',type="string",default=None,help="[1D] linestyle: '-' '--' '.' '..'") |
---|
68 | parser.add_option('-Q','--color',action='append',dest='color',type="string",default=None,help="[1D] color: 'b' 'g' 'r' etc") |
---|
69 | parser.add_option('-K','--marker',action='append',dest='marker',type="string",default=None,help="[1D] marker: '' 'x' 'o' etc") |
---|
70 | parser.add_option('-S','--superpose',action='store_true',dest='superpose',default=False,help="[1D] use same axis for all plots") |
---|
71 | parser.add_option('-E','--label',action='append',dest='label',type="string",default=None,help="[1D] label for line") |
---|
72 | # -- 2D plot |
---|
73 | 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") |
---|
74 | 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'") |
---|
75 | parser.add_option('-B','--back',action='append',dest='back',type="string",default=None,help='[2D] predefined map background (cf. set_back.txt)') |
---|
76 | parser.add_option('-A','--area',action='append',dest='area',type="string",default=None,help='[2D] predefined region of mapping (cf. set_area.txt)') |
---|
77 | 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)') |
---|
78 | 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) ') |
---|
79 | parser.add_option('-N','--vmin',action='append',dest='vmin',type="float",default=None,help='[2D] float: minimum value for displayed field') |
---|
80 | parser.add_option('-M','--vmax',action='append',dest='vmax',type="float",default=None,help='[2D] float: maximum value for displayed field') |
---|
81 | (opt,args) = parser.parse_args() |
---|
82 | |
---|
83 | ###################################### |
---|
84 | # get arguments (one or several files) |
---|
85 | ###################################### |
---|
86 | if args is None: |
---|
87 | print "Stop here! I need file(s) as argument(s)!" ; exit() |
---|
88 | else: |
---|
89 | files = args |
---|
90 | |
---|
91 | ############################################# |
---|
92 | # a possibility to simply inspect the file(s) |
---|
93 | ############################################# |
---|
94 | if opt.var is None: |
---|
95 | for filename in files: inspect(filename) |
---|
96 | exit() |
---|
97 | |
---|
98 | ###################################### |
---|
99 | # use ppclass to get field and plot it |
---|
100 | ###################################### |
---|
101 | # treat the case of additional vectors or contours (contours must be before vectors) |
---|
102 | var = [] ; vargoal = [] |
---|
103 | for element in opt.var: |
---|
104 | var.append(element) ; vargoal.append("main") |
---|
105 | if opt.contour is not None: var.append(opt.contour) ; vargoal.append("contour") |
---|
106 | if opt.vecx is not None: var.append(opt.vecx) ; vargoal.append("vector") |
---|
107 | if opt.vecy is not None: var.append(opt.vecy) ; vargoal.append("vector") |
---|
108 | # set pp object |
---|
109 | user = pp() |
---|
110 | user.file = files |
---|
111 | user.var = var ; user.vargoal = vargoal |
---|
112 | user.x = opt.x ; user.y = opt.y |
---|
113 | user.z = opt.z ; user.t = opt.t |
---|
114 | user.verbose = opt.verbose |
---|
115 | user.compute = opt.compute |
---|
116 | user.changetime = opt.changetime |
---|
117 | # define field |
---|
118 | user.define() |
---|
119 | # retrieve field |
---|
120 | user.retrieve() |
---|
121 | # some possible operations |
---|
122 | if opt.add is not None: user = user + opt.add |
---|
123 | if opt.mult is not None: user = user * opt.mult |
---|
124 | # get some options |
---|
125 | user.superpose = opt.superpose |
---|
126 | user.filename = opt.filename |
---|
127 | user.folder = opt.folder |
---|
128 | user.out = opt.out |
---|
129 | # define plot |
---|
130 | user.defineplot() |
---|
131 | # user-defined plot settings |
---|
132 | user.getopt(opt) |
---|
133 | # make plot |
---|
134 | user.makeplot() |
---|
135 | |
---|
136 | #################################### |
---|
137 | # save a .sh file with the command # |
---|
138 | #################################### |
---|
139 | command = "" |
---|
140 | for arg in sys.argv: command = command + arg + ' ' |
---|
141 | try: |
---|
142 | f = open(opt.folder+'/'+opt.filename+'.sh', 'w') |
---|
143 | f.write(command) |
---|
144 | except IOError: |
---|
145 | print "!! WARNING !! pp.py command not saved. Probably do not have permission to write here." |
---|