NASA Logo
Ocean Color Science Software

ocssw V2022
copy_sim_met.py
Go to the documentation of this file.
1 '''
2 * NAME: copy_sim_met.py
3 *
4 * DESCRIPTION: Executable program to copy meteorological data from simulation
5 * files to a group called meteorology in another file.
6 
7 * USAGE: python copy_sim_met.py -l [L1A directory] -o [output directory]
8 
9 * Created on May 10, 2019
10 
11 * Author: Samuel Anderson
12 '''
13 
14 import os
15 import sys
16 import StringIO
17 import optparse as optparse
18 import logging
19 import numpy as np
20 import tables
21 
22 LOG = logging.getLogger('copy_sim_met')
23 
24 
27 
28 def main():
29 
30  args = sys.argv
31 
32  description = '''Copy ancillary data from simulation files to output.'''
33  usage = "usage: python copy_sim_met.py -a [aer file] \
34  -c [chm file] -m [met file] -o [output file] [options]"
35  version = "v1"
36 
37  parser = optparse.OptionParser(description=description,usage=usage,version=version)
38 
39  # Mandatory arguments
40  mandatoryGroup = optparse.OptionGroup(parser, "Mandatory Arguments",
41  "At a minimum these arguments must be specified")
42 
43  parser.add_option_group(mandatoryGroup)
44 
45  mandatoryGroup.add_option('-a','--aer',
46  action="store",
47  dest="aer" ,
48  type="string",
49  help="The full path of the input aer file")
50 
51  mandatoryGroup.add_option('-c','--chm',
52  action="store",
53  dest="chm" ,
54  type="string",
55  help="The full path of the input chm file")
56 
57  mandatoryGroup.add_option('-m','--met',
58  action="store",
59  dest="met" ,
60  type="string",
61  help="The full path of the input met file")
62 
63  mandatoryGroup.add_option('-o','--out',
64  action="store",
65  dest="out" ,
66  type="string",
67  help="The output directory path")
68 
69  # Optional arguments
70  optionalGroup = optparse.OptionGroup(parser, "Extra Options",
71  "These options may be used to customize behavior of this program.")
72 
73  parser.add_option('-v', '--verbose',
74  dest='verbosity',
75  action="count",
76  default=0,
77  help='each occurrence increases verbosity 1 level from ERROR: -v=WARNING -vv=INFO -vvv=DEBUG')
78 
79  parser.add_option_group(optionalGroup)
80 
81  # Parse the arguments from the command line
82  (options, args) = parser.parse_args()
83 
84  # Set up the logging levels
85  levels = [logging.ERROR, logging.WARN, logging.INFO, logging.DEBUG]
86  logging.basicConfig(level = levels[options.verbosity])
87 
88  # Check that all of the mandatory options are given. If one or more
89  # are missing, print error message and exit...
90  mandatories = []
91  mand_errors = []
92  isMissingMand = False
93  for m,m_err in zip(mandatories,mand_errors):
94  if not options.__dict__[m]:
95  isMissingMand = True
96  print m_err
97  if isMissingMand :
98  parser.error("Incomplete mandatory arguments, aborting...")
99 
100  # Check that the output directory actually exist
101  options.out = os.path.expanduser(options.out)
102 
103  # Check that we don't have mutually exclusive options
104 
105  LOG.info('Copying ancillary data %s' % (options.out) )
106 
107  ydim = 1721
108  xdim = 1271
109 
110  try:
111  df = tables.open_file(options.out, "w")
112  mg = df.create_group("/", "Meteorology", 'Meteorology data')
113  atom = tables.Float32Atom()
114 
115  met_file = tables.open_file(options.met, "r")
116  QV10M_o = df.create_carray(mg, 'QV10M', atom, (ydim,xdim), "")
117  QV10M_o[:,:] = met_file.get_node("/", "QV10M")[0,:,:]
118  SLP_o = df.create_carray(mg, 'SLP', atom, (ydim,xdim), "")
119  SLP_o[:,:] = met_file.get_node("/", "SLP")[0,:,:]
120  T10M_o = df.create_carray(mg, 'T10M', atom, (ydim,xdim), "")
121  T10M_o[:,:] = met_file.get_node("/", "T10M")[0,:,:]
122  TQV_o = df.create_carray(mg, 'TQV', atom, (ydim,xdim), "")
123  TQV_o[:,:] = met_file.get_node("/", "TQV")[0,:,:]
124  PS_o = df.create_carray(mg, 'PS', atom, (ydim,xdim), "")
125  PS_o[:,:] = met_file.get_node("/", "PS")[0,:,:]
126  U10M_o = df.create_carray(mg, 'U10M', atom, (ydim,xdim), "")
127  U10M_o[:,:] = met_file.get_node("/", "U10M")[0,:,:]
128  V10M_o = df.create_carray(mg, 'V10M', atom, (ydim,xdim), "")
129  V10M_o[:,:] = met_file.get_node("/", "V10M")[0,:,:]
130  met_file.close()
131 
132  chm_file = tables.open_file(options.chm, "r")
133  chm_file.close()
134 
135  aer_file = tables.open_file(options.aer, "r")
136  aer_file.close()
137 
138  df.flush()
139  df.close()
140 
141  except Exception, e:
142  print e.message + " ... exiting"
143  df.close()
144 
145  LOG.info('Exiting...')
146  return 0
147 
148 if __name__=='__main__':
149  sys.exit(main())
150 
def main()
Main Function #.
Definition: copy_sim_met.py:28