OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
next_level_name.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 """
4 Program to return the name of the next level file that would be created from
5 the input file name.
6 """
7 
8 import optparse
9 import os
10 import sys
11 import traceback
12 
13 #import datetime
16 #import namer_constants
19 import mlp.obpg_data_file
21 
22 __version__ = '1.0.5-2018-05-08'
23 __author__ = 'melliott'
24 
25 #2345678901234567890123456789012345678901234567890123456789012345678901234567890
26 
27 
28 
29 FILE_TYPES_CONVERTER = {'Level 0': 'Level 0',
30  'Level 1A': 'Level 1A',
31  'l1agen' : 'Level 1A',
32  'Level 1B': 'Level 1B',
33  'Level 2': 'Level 2',
34  'L3bin': 'L3bin',
35  'Level 3 Binned': 'L3bin'}
36 
37 #def get_1_file_name(input_name, file_typer, file_type, sensor,
38 # target_program, clopts):
39 def get_1_file_name(data_file, target_program, clopts):
40  """
41  Return the next level name for a single file.
42  """
43  level_finder = mlp.name_finder_utils.get_level_finder([data_file],
44  target_program,
45  clopts)
46  next_level_name = level_finder.get_next_level_name()
47  return next_level_name
48 
49 def get_extension(file_type):
50  """
51  Returns the extension appropriate for the program.
52  """
53  extensions = {'l2bin': '.L3b', 'l3bin': '.L3b', 'smigen': '.L3m'}
54  return extensions[file_type]
55 
56 def get_multifile_next_level_name(data_files_list_info, target_program, clopts):
57  """
58  Return the next level name for a set of files.
59  """
60  for file_info in data_files_list_info:
61  if file_info.file_type == 'unknown':
62  err_msg = 'Error! File {0} is of unknown type.'.format(
63  file_info.name)
64  sys.exit(err_msg)
65  next_level_name = get_multifile_output_name(data_files_list_info,
66  target_program, clopts)
67  return next_level_name
68 
69 def get_multifile_output_name(data_files_list_info, target_program, clopts):
70  """
71  Returns the file name derived from a group of files names.
72  """
73  list_file_type = data_files_list_info[0].file_type
74  for data_file in data_files_list_info[1:]:
75  if data_file.file_type != list_file_type:
76  err_msg = 'Error! File types do not match for {0} and {1}'.\
77  format(data_files_list_info[0].name, data_file.name)
78  sys.exit(err_msg)
79  level_finder = mlp.name_finder_utils.get_level_finder(data_files_list_info,
80  target_program,
81  clopts)
82  output_name = level_finder.get_next_level_name()
83  return output_name
84 
85 
86 
88  """
89  Returns the options and arguments from a command line call.
90  """
91  ver_msg = ' '.join(['%prog', __version__])
92  use_msg = 'usage: %prog INPUT_FILE TARGET_PROGRAM'
93  cl_parser = optparse.OptionParser(usage=use_msg, version=ver_msg)
94  cl_parser.add_option('--oformat', dest='oformat', action='store',
95  type='string', help='output format')
96  cl_parser.add_option('--resolution', dest='resolution', action='store',
97  type='string',
98  help='resolution for smigen/l3mapgen')
99  cl_parser.add_option('--suite', dest='suite', action='store',
100  type='string', help='data type suite')
101  # cl_parser.add_option('--product', dest='product', action='store',
102  # type='string', help='product type (for smigen)')
103  (clopts, clargs) = cl_parser.parse_args()
104  if len(clargs) == 0:
105  print ("\nError! No input file or target program specified.\n")
106  cl_parser.print_help()
107  sys.exit(0)
108  elif len(clargs) == 1:
109  print ("\nError! No target program specified.\n")
110  cl_parser.print_help()
111  sys.exit(0)
112  elif len(clargs) > 2:
113  print ('\nError! Too many arguments specified on the command line.')
114  cl_parser.print_help()
115  sys.exit(0)
116  else:
117  return clopts, clargs[0], clargs[1]
118 
119 def get_data_files_info(file_list_file):
120  """
121  Returns a list of of data files.
122  """
123  file_info = []
124  with open(file_list_file, 'rt') as file_list_file:
125  inp_lines = file_list_file.readlines()
126  for line in inp_lines:
127  filename = line.strip()
128  if os.path.exists(filename):
129  file_typer = mlp.get_obpg_file_type.ObpgFileTyper(filename)
130  file_type, sensor = file_typer.get_file_type()
131  if file_type != 'unknown':
132  stime, etime = file_typer.get_file_times()
133  data_file = mlp.obpg_data_file.ObpgDataFile(filename, file_type,
134  sensor, stime, etime)
135  file_info.append(data_file)
136  else:
137  err_msg = 'Error! {0} is not an OBPG file.'.\
138  format(filename)
139  sys.exit(err_msg)
140  else:
141  err_msg = 'Error! File {0} could not be found.'.format(filename)
142  sys.exit(err_msg)
143  file_info.sort(key=myfunc)
144  return file_info
145 
146 def myfunc(n):
147  return n.start_time
148 
150  """
151  Builds and prints an error message from the exception information,
152  then exits.
153  """
154  exc_parts = exc_info
155  err_type = str(exc_parts[0]).split('.')[1][0:-2]
156  err_msg = 'Error! Encountered {0}:'.format(str(err_type))
157  print (err_msg)
158  if DEBUG:
159  traceback.print_exc()
160  sys.exit(1)
161 
162 def main():
163  """
164  Main function for when this module is called as a program.
165  """
166  ret_status = 0
167  clopts, inp_name, targ_prog = get_command_line_data()
168 
169  #if not targ_prog in namer_constants.PROCESSABLE_PROGRAMS:
170  if not targ_prog in PROCESSABLE_PROGRAMS:
171  err_msg = 'Error! The target program, "{0}", is not known.'.\
172  format(targ_prog)
173  sys.exit(err_msg)
174  if os.path.exists(inp_name):
175  try:
176  file_typer = mlp.get_obpg_file_type.ObpgFileTyper(inp_name)
177  ftype, sensor = file_typer.get_file_type()
178  if ftype == 'unknown':
180  # Try treating the input file as a file list file.
181  data_files_info = get_data_files_info(inp_name)
182  if len(data_files_info) > 0:
183  next_level_name = get_multifile_next_level_name(
184  data_files_info, targ_prog, clopts)
185  else:
186  err_msg = "Error! No OBPG files found in {0}".\
187  format(inp_name)
188  sys.exit(err_msg)
189  else:
190  # The input file wasn't a file list file.
191  err_msg = "File {0} is not an OBPG file.".format(inp_name)
192  sys.exit(err_msg)
193  else:
194  # The file is an OBPG file
195  stime, etime = file_typer.get_file_times()
196  file_metadata = file_typer.attributes
197  data_file = mlp.obpg_data_file.ObpgDataFile(inp_name, ftype, sensor,
198  stime, etime,
199  file_metadata)
200  next_level_name = get_1_file_name(data_file, targ_prog, clopts)
201  print ('Output Name: ' + next_level_name)
202  except SystemExit as sys_ex:
203  # The intention here is to catch exit exceptions we throw in other
204  # parts of the program and continue with the exit, outputting
205  # whatever error message was created for the exit.
206  sys.exit(sys_ex)
207  except:
208  handle_unexpected_exception(sys.exc_info())
209  else:
210  err_msg = "Error! File {0} was not found.".format(inp_name)
211  sys.exit(err_msg)
212  return ret_status
213 
214 
215 
216 #global DEBUG
217 DEBUG = False
218 DEBUG = True # Comment out for production use
219 PROCESSABLE_PROGRAMS = \
220  set(list(mlp.next_level_name_finder.NextLevelNameFinder.PROCESSING_LEVELS.keys()) +\
221  list(mlp.next_level_name_finder.HawkeyeNextLevelNameFinder.PROCESSING_LEVELS.keys()) +\
222  list(mlp.next_level_name_finder.ModisNextLevelNameFinder.PROCESSING_LEVELS.keys()) +\
223  list(mlp.next_level_name_finder.SeawifsNextLevelNameFinder.PROCESSING_LEVELS.keys()) +\
224  list(mlp.viirs_next_level_name_finder.ViirsNextLevelNameFinder.PROCESSING_LEVELS.keys()))
225 
226 if __name__ == '__main__':
227  sys.exit(main())
def get_command_line_data()
def get_level_finder(data_file_list, target_program, clopts=None)
list(APPEND LIBS ${PGSTK_LIBRARIES}) add_executable(atteph_info_modis atteph_info_modis.c) target_link_libraries(atteph_info_modis $
Definition: CMakeLists.txt:7
def is_ascii_file(filename)
Definition: MetaUtils.py:78
def get_multifile_next_level_name(data_files_list_info, target_program, clopts)
def get_1_file_name(data_file, target_program, clopts)
const char * str
Definition: l1c_msi.cpp:35
def get_extension(file_type)
def handle_unexpected_exception(exc_info)
def get_data_files_info(file_list_file)
def get_multifile_output_name(data_files_list_info, target_program, clopts)