[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 | |
---|
| 18 | ##################################### |
---|
| 19 | # get arguments from the command line |
---|
| 20 | ##################################### |
---|
| 21 | parser = OptionParser() |
---|
| 22 | parser.add_option('--verbose',action='store_true',dest='verbose',default=False,help='') |
---|
| 23 | # field --> lower case |
---|
| 24 | parser.add_option('-f','--file',action='append',dest='file',type="string",default=None,help='') |
---|
| 25 | parser.add_option('-v','--var',action='append',dest='var',type="string",default=None,help='') |
---|
[916] | 26 | parser.add_option('-x','--lon',action='append',dest='x',type="string",default=None,help='') |
---|
| 27 | parser.add_option('-y','--lat',action='append',dest='y',type="string",default=None,help='') |
---|
[910] | 28 | parser.add_option('-z','--vert',action='append',dest='z',type="string",default=None,help='') |
---|
| 29 | parser.add_option('-t','--time',action='append',dest='t',type="string",default=None,help='') |
---|
| 30 | parser.add_option('-c','--contour',action='store',dest='contour',type="string",default=None,help='') |
---|
| 31 | parser.add_option('-i','--vecx',action='store',dest='vecx',type="string",default=None,help='') |
---|
| 32 | parser.add_option('-j','--vecy',action='store',dest='vecy',type="string",default=None,help='') |
---|
| 33 | parser.add_option('-m','--mult',action='store',dest='mult',type="float",default=None,help='') |
---|
| 34 | parser.add_option('-a','--add',action='store',dest='add',type="float",default=None,help='') |
---|
[912] | 35 | parser.add_option('-o','--output',action='store',dest='filename',type="string",default="myplot",help='') |
---|
[913] | 36 | parser.add_option('-d','--directory',action='store',dest='folder',type="string",default="./",help='') |
---|
[910] | 37 | # plot --> upper case |
---|
| 38 | # -- generic |
---|
| 39 | parser.add_option('-T','--title',action='append',dest='title',type="string",default=None,help='') |
---|
| 40 | parser.add_option('-X','--xlabel',action='append',dest='xlabel',type="string",default=None,help='') |
---|
| 41 | parser.add_option('-Y','--ylabel',action='append',dest='ylabel',type="string",default=None,help='') |
---|
| 42 | parser.add_option('-D','--div',action='store',dest='div',type="int",default=20,help='') |
---|
| 43 | parser.add_option('-H','--trans',action='store',dest='trans',type="float",default=1.0,help='') |
---|
[912] | 44 | parser.add_option('-Z','--logy',action='store_true',dest='logy',default=False,help='') |
---|
| 45 | parser.add_option('-O','--save',action='store',dest='out',type="string",default="gui",help='') |
---|
[910] | 46 | # -- 1D plot |
---|
| 47 | parser.add_option('-L','--lstyle',action='append',dest='lstyle',type="string",default=None,help='') |
---|
| 48 | parser.add_option('-S','--superpose',action='store_true',dest='superpose',default=False,help='') |
---|
| 49 | # -- 2D plot |
---|
| 50 | parser.add_option('-C','--colorb',action='append',dest='colorb',type="string",default=None,help='') |
---|
| 51 | parser.add_option('-P','--proj',action='append',dest='proj',type="string",default=None,help='') |
---|
| 52 | parser.add_option('-B','--back',action='append',dest='back',type="string",default=None,help='') |
---|
| 53 | parser.add_option('-A','--area',action='append',dest='area',type="string",default=None,help='') |
---|
| 54 | parser.add_option('-I','--blon',action='append',dest='blon',type="float",default=None,help='') |
---|
| 55 | parser.add_option('-J','--blat',action='append',dest='blat',type="float",default=None,help='') |
---|
| 56 | parser.add_option('-N','--vmin',action='append',dest='vmin',type="float",default=None,help='') |
---|
| 57 | parser.add_option('-M','--vmax',action='append',dest='vmax',type="float",default=None,help='') |
---|
| 58 | (opt,args) = parser.parse_args() |
---|
| 59 | |
---|
| 60 | ########################################## |
---|
| 61 | # a possibility to simply inspect the file |
---|
| 62 | ########################################## |
---|
| 63 | if opt.file is None: |
---|
| 64 | print "Stop here. I need at least a file: -f FILE" ; exit() |
---|
| 65 | if opt.var is None: |
---|
| 66 | for filename in opt.file: inspect(filename) |
---|
| 67 | exit() |
---|
| 68 | |
---|
| 69 | ###################################### |
---|
| 70 | # use ppclass to get field and plot it |
---|
| 71 | ###################################### |
---|
| 72 | # treat the case of additional vectors or contours (contours must be before vectors) |
---|
| 73 | var = [] ; vargoal = [] |
---|
| 74 | for element in opt.var: |
---|
| 75 | var.append(element) ; vargoal.append("main") |
---|
| 76 | if opt.contour is not None: var.append(opt.contour) ; vargoal.append("contour") |
---|
| 77 | if opt.vecx is not None: var.append(opt.vecx) ; vargoal.append("vector") |
---|
| 78 | if opt.vecy is not None: var.append(opt.vecy) ; vargoal.append("vector") |
---|
| 79 | # set pp object |
---|
| 80 | user = pp(file=opt.file, var=var, vargoal=vargoal, \ |
---|
| 81 | x=opt.x, y=opt.y, z=opt.z, t=opt.t,\ |
---|
| 82 | verbose=opt.verbose) |
---|
| 83 | # define field |
---|
| 84 | user.define() |
---|
| 85 | # retrieve field |
---|
| 86 | user.retrieve() |
---|
| 87 | # some possible operations |
---|
| 88 | if opt.add is not None: user = user + opt.add |
---|
| 89 | if opt.mult is not None: user = user * opt.mult |
---|
[912] | 90 | # get some options |
---|
| 91 | user.superpose = opt.superpose |
---|
| 92 | user.filename = opt.filename |
---|
| 93 | user.folder = opt.folder |
---|
| 94 | user.out = opt.out |
---|
[910] | 95 | # define plot |
---|
| 96 | user.defineplot() |
---|
| 97 | # user-defined plot settings |
---|
| 98 | user.getopt(opt) |
---|
| 99 | # make plot |
---|
| 100 | user.makeplot() |
---|
[912] | 101 | |
---|
| 102 | #################################### |
---|
| 103 | # save a .sh file with the command # |
---|
| 104 | #################################### |
---|
| 105 | command = "" |
---|
| 106 | for arg in sys.argv: command = command + arg + ' ' |
---|
| 107 | f = open(opt.filename+'.sh', 'w') |
---|
| 108 | f.write(command) |
---|