1 | #! /usr/bin/env python |
---|
2 | from api_wrapper import api_onelevel |
---|
3 | from ppclass import separatenames,inspect |
---|
4 | from optparse import OptionParser ### TBR by argparse |
---|
5 | import numpy as np |
---|
6 | import sys |
---|
7 | # ----------------------------------------------------------------------- |
---|
8 | # A simple script to call the API program to interpolate mesoscale files |
---|
9 | # ... NB: API_WRAPPER necessary with api compiled with f2py |
---|
10 | # Author: A. Spiga 04/2013 |
---|
11 | # ----------------------------------------------------------------------- |
---|
12 | |
---|
13 | # default settings |
---|
14 | # ---------------- |
---|
15 | defitp = 4 |
---|
16 | deflvl = "0.1" |
---|
17 | |
---|
18 | # define parser with version and usage |
---|
19 | # ------------------------------------ |
---|
20 | parser = OptionParser() |
---|
21 | parser.version = \ |
---|
22 | '''*********************************************** |
---|
23 | ******** EXAPI (for help: exapi.py -h) ******** |
---|
24 | ***********************************************''' |
---|
25 | parser.usage = \ |
---|
26 | '''exapi.py [options] netCDF file(s)''' |
---|
27 | parser.print_version() |
---|
28 | |
---|
29 | # define options and get options+arguments |
---|
30 | # ---------------------------------------- |
---|
31 | parser.add_option('-v','--var',action='store',dest='var',type="string",default=None,help='Variables processed. Not used means all variables. tk,tpot,uvmet can be requested.') |
---|
32 | parser.add_option('-i','--interp',action='store',dest='itp',type="int",default=defitp,help='Mode: 2=press / 3=z-amr / 4=z-als (default)') |
---|
33 | parser.add_option('-l','--level',action='store',dest='lvl',type="string",default=deflvl,help='Levels: start[,stop,step] (-i 2: Pa)(-i 3,4: km)') |
---|
34 | parser.add_option('-o','--output',action='store',dest='output',type="string",default=None,help="name of output files") |
---|
35 | parser.add_option('-d','--directory',action='store',dest='folder',type="string",default=None,help="directory of output files") |
---|
36 | (opt,args) = parser.parse_args() |
---|
37 | |
---|
38 | # get variables |
---|
39 | # if None, take all! |
---|
40 | # ------------------ |
---|
41 | if opt.var is None: |
---|
42 | ze_process = "all" |
---|
43 | zevars = None |
---|
44 | else: |
---|
45 | ze_process = "list" |
---|
46 | # (we start by unravelling user input in an array) |
---|
47 | zevars = separatenames(opt.var) |
---|
48 | # (we help a little the user about naming certain variables) |
---|
49 | for i in range(len(zevars)): |
---|
50 | if zevars[i] in ['t','temp','temperature']: zevars[i] = 'tk' |
---|
51 | elif zevars[i] in ['T','temppot','theta']: zevars[i] = 'tpot' |
---|
52 | elif zevars[i] in ['u','v','U','V','Um','Vm','uv','UV','wind']: zevars[i] = 'uvmet' |
---|
53 | # (we recombine them all for call to api) |
---|
54 | list = "" |
---|
55 | for el in zevars: list = list + el + "," |
---|
56 | zevars = list |
---|
57 | |
---|
58 | # get the kind of interpolation |
---|
59 | # ----------------------------- |
---|
60 | inputnvert = separatenames(opt.lvl) |
---|
61 | |
---|
62 | # prepare levels: one-level only |
---|
63 | # ------------------------------ |
---|
64 | if len(inputnvert) == 1: |
---|
65 | zelevel = float(inputnvert[0]) |
---|
66 | ze_interp_levels = [-9999.] |
---|
67 | |
---|
68 | # prepare levels: several levels |
---|
69 | # ------------------------------ |
---|
70 | elif len(inputnvert) > 1: |
---|
71 | # initialize. make number of interp levels to 20 if not given. |
---|
72 | zelevel = -99. |
---|
73 | start = float(inputnvert[0]) |
---|
74 | stop = float(inputnvert[1]) |
---|
75 | if len(inputnvert) == 2: numsample = 20 |
---|
76 | else: numsample = float(inputnvert[2]) |
---|
77 | # make the interval. either normal -- or log if pressure. |
---|
78 | if stop > start: |
---|
79 | # altitude coordinates |
---|
80 | ze_interp_levels = np.linspace(start,stop,numsample) |
---|
81 | else: |
---|
82 | # pressure coordinates |
---|
83 | ze_interp_levels = np.logspace(np.log10(start),np.log10(stop),numsample) |
---|
84 | |
---|
85 | # main api call |
---|
86 | # ------------- |
---|
87 | for file in args: |
---|
88 | print "EXAPI: working on file",file |
---|
89 | newname = api_onelevel ( \ |
---|
90 | path_to_input = '', \ |
---|
91 | path_to_output = None, \ |
---|
92 | input_name = file, \ |
---|
93 | output_name = opt.output, \ |
---|
94 | fields = zevars, \ |
---|
95 | interp_method = opt.itp, \ |
---|
96 | interp_level = ze_interp_levels, \ |
---|
97 | onelevel = zelevel, \ |
---|
98 | process = ze_process ) |
---|
99 | print "EXAPI: inspect the new file." |
---|
100 | inspect(newname) |
---|
101 | print "EXAPI: OK. done with this file." |
---|
102 | |
---|
103 | #################################### |
---|
104 | # save a .sh file with the command # |
---|
105 | #################################### |
---|
106 | command = "" |
---|
107 | if opt.output is None: |
---|
108 | opt.output = "exapi" |
---|
109 | for arg in sys.argv: command = command + arg + ' ' |
---|
110 | try: |
---|
111 | f = open(opt.output+'.sh','w') |
---|
112 | f.write(command) |
---|
113 | except IOError: |
---|
114 | print "!! WARNING !! pp.py command not saved. Probably do not have permission to write here." |
---|