Due to the lapse in federal government funding, NASA is not updating this website. We sincerely regret this inconvenience.
NASA Logo
Ocean Color Science Software

ocssw V2022
afrt.py
Go to the documentation of this file.
1 '''
2 * NAME: afrt.py
3 *
4 * DESCRIPTION: Executable program to automate running of afrt (radiative
5 * transfer) application in configurable increments of the inputs and outputs
6 
7 * USAGE: python afrt.py -p [PCF file path] -wl [wavelength range]
8  -sd [particle model range] -tau [optical depth range]
9  -wnd [wind speed range]
10 
11 * Created on October 09, 2018
12 
13 * Author: Samuel Anderson
14 '''
15 
16 import os
17 import sys
18 from datetime import datetime as dt
19 import optparse as optparse
20 import logging
21 LOG = logging.getLogger('afrt')
22 
23 
24 
27 
28 def main():
29 
30  args = sys.argv
31 
32  description = '''Execute afrt and generate NetCDF output files.'''
33  usage = "usage: python afrt.py -p [PCF] -wl [wavelength id range] -sd [model id range] -tau [tau id range] -wnd [wind id range] -o [output directory] [options]"
34  version = "v1"
35 
36  parser = optparse.OptionParser(description=description,usage=usage,version=version)
37 
38  # Mandatory arguments
39  mandatoryGroup = optparse.OptionGroup(parser, "Mandatory Arguments",
40  "At a minimum these arguments must be specified")
41 
42  parser.add_option_group(mandatoryGroup)
43 
44  mandatoryGroup.add_option('-p','--par',
45  action="store",
46  dest="parfile" ,
47  type="string",
48  help="The full path of the parameters file")
49 
50  # Optional arguments
51  optionalGroup = optparse.OptionGroup(parser, "Extra Options",
52  "These options may be used to customize behavior of this program.")
53 
54  optionalGroup.add_option('-l','--wl',
55  action="store",
56  dest="wl" ,
57  type="int",
58  nargs=2,
59  help="Range of wavelength indices")
60 
61  optionalGroup.add_option('-s','--sd',
62  action="store",
63  dest="sd" ,
64  type="int",
65  nargs=2,
66  help="Range of particle model indices")
67 
68  optionalGroup.add_option('-t','--tau',
69  action="store",
70  dest="tau" ,
71  type="int",
72  nargs=2,
73  help="Range of optical depth indices")
74 
75  optionalGroup.add_option('-w','--wnd',
76  action="store",
77  dest="wnd" ,
78  type="int",
79  nargs=2,
80  help="Range of optical depth indices")
81 
82  optionalGroup.add_option('-o','--odir',
83  action="store",
84  dest="odir" ,
85  type="string",
86  help="The full path of the output directories")
87 
88  parser.add_option('-v', '--verbose',
89  dest='verbosity',
90  action="count",
91  default=0,
92  help='each occurrence increases verbosity 1 level from ERROR: -v=WARNING -vv=INFO -vvv=DEBUG')
93 
94  parser.add_option_group(optionalGroup)
95 
96  # Parse the arguments from the command line
97  (opt, args) = parser.parse_args()
98 
99  # Set up the logging levels
100  levels = [logging.ERROR, logging.WARN, logging.INFO, logging.DEBUG]
101  logging.basicConfig(level = levels[opt.verbosity])
102 
103  # Check that all of the mandatory options are given. If one or more
104  # are missing, print error message and exit...
105  mandatories = []
106  mand_errors = []
107  isMissingMand = False
108  for m,m_err in zip(mandatories,mand_errors):
109  if not opt.__dict__[m]:
110  isMissingMand = True
111  print (m_err)
112  if isMissingMand :
113  parser.error("Incomplete mandatory arguments, aborting...")
114 
115  # Check that the output directory actually exist
116  if opt.odir:
117  opt.odir = os.path.expanduser(opt.odir)
118 
119  # Check for mutually exclusive options
120  LOG.info('Generating radiative transfer tables in %s' % (opt.odir) )
121 
122  if os.path.isdir(opt.odir):
123  o_path = opt.odir
124  elif os.path.isdir(os.path.dirname(opt.odir)):
125  o_path = opt.odir
126  os.mkdir(o_path)
127  else:
128  print ("Output path invalid")
129  return
130 
131  # Read and parse parameters file
132  if opt.parfile:
133  spcf = opt.parfile
134  else:
135  spcf = ""
136 
137  # Process radiative transfer tables
138  rid = 0
139  for isd in range(opt.sd[0],opt.sd[1]+1):
140  for iwl in range(opt.wl[0],opt.wl[1]+1):
141  for itau in range(opt.tau[0],opt.tau[1]+1):
142  for iwnd in range(opt.wnd[0],opt.wnd[1]+1):
143  print ("wl:" + str(iwl) + " sd:" + str(isd) + " tau:" + str(itau) + " wnd:" + str(iwnd))
144  wl_istr = " wave_id=" + str(iwl) + "," + str(iwl)
145  sd_istr = " model_id=" + str(isd) + "," + str(isd)
146  tau_istr = " tau_id=" + str(itau) + "," + str(itau)
147  wnd_istr = " wind_id=" + str(iwnd) + "," + str(iwnd)
148  ofile_istr = " ofile=" + opt.odir + "/RT" + str(rid) + "_MD" + str(isd) + "_WL" + str(iwl) + "_TA" + str(itau) + "_WD" + str(iwnd) + ".nc"
149  command = "afrt_nc4 " + spcf + sd_istr + wl_istr + tau_istr + wnd_istr + ofile_istr
150  print (command)
151  result = os.system(command)
152  rid += 1
153 
154  print( 'Processing completed')
155  LOG.info('Exiting...')
156  return 0
157 
158 if __name__=='__main__':
159  sys.exit(main())
160 
void print(std::ostream &stream, const char *format)
Definition: PrintDebug.hpp:38
Definition: aerosol.c:136
def main()
Main Function #.
Definition: afrt.py:28