source: trunk/UTIL/PYTHON/planetoplot_v2/pp.py @ 1335

Last change on this file since 1335 was 1335, checked in by aslmd, 10 years ago

planetoplot. added comments to suggest problem fixes in some cases (borders, links)

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