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