OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
aquarius_next_level_name_finder.py
Go to the documentation of this file.
1 """
2 A module containing the ViirsNextLevelNameFinder class for finding standard
3 output file names for VIIRS files when run through OBPG software.
4 """
5 
6 
7 import datetime
8 import os
9 import re
10 import sys
11 import types
12 import mlp.next_level_name_finder as next_level_name_finder
13 # import next_level_name_finder
14 
15 __author__ = 'melliott'
16 
17 __version__ = '1.0.4-2018-06-27'
18 
20  """
21  A class to determine what the standard OBPG filename would be for
22  Aquarius files when the given input name is run through the next
23  level of OBPG processing.
24  """
25  PROCESSING_LEVELS = {
26  'l1agen': 'Level 1A',
27  'Level 1A': 'Level 1A',
28  'l1aextract_seawifs' : 'l1aextract_seawifs',
29  'l1bgen': 'Level 1B',
30  'Level 1B': 'Level 1B',
31  'l1brsgen': 'l1brsgen',
32  'l1mapgen': 'l1mapgen',
33  'l2gen_aquarius': 'Level 2',
34  'Level 2': 'Level 2',
35  'l2bin_aquarius': 'l2bin_aquarius',
36  'l2brsgen': 'l2brsgen',
37  'l2extract': 'l2extract',
38  'l2mapgen': 'l2mapgen',
39  'l3bin': 'l3bin',
40  'L3b': 'l3bin',
41  'l3gen': 'l3gen',
42  'l3mapgen': 'SMI',
43  'SMI': 'SMI',
44  'smigen': 'SMI'
45  }
46 
47  def __init__(self, data_files_list, next_level, suite=None,
48  resolution=None, oformat=None):
49  super(AquariusNextLevelNameFinder, self).__init__(data_files_list,
50  next_level, suite,
51  resolution, oformat)
52 
53  def _get_data_version(self):
54  """
55  Return the data version for Aquarius, such as "_V5.0.0".
56  """
57  aq_ver = ''
58  if len(self.data_files) == 1:
59  base_name = os.path.basename(self.data_files[0].name)
60  if base_name.count('.') > 1:
61  aq_ver = re.findall('(\_V\d*\.\d*.*$)', base_name)[-1]
62  return aq_ver
63 
64  def _get_l2_extension(self):
65  l1_parts = self.data_files[0].name.split('.')
66  if len(l1_parts) > 2:
67  err_msg = 'Error! Unable to determine extension for {0}'.\
68  format(self.data_files[0].name)
69  sys.exit(err_msg)
70  extension = re.sub(r'L1[AB]_(.*)', r'.L2_\g<1>', l1_parts[1])
71  return extension
72 
73  def _get_l2_name(self):
74  """
75  An internal method to return the L2 name from an L1 file.
76  """
77  basename = self._get_single_file_basename()
78  if basename != 'indeterminate':
79  if self.suite:
80  next_lvl_name = basename + self._get_l2_extension() + self.suite
81  else:
82  next_lvl_name = basename + self._get_l2_extension()
83  else:
84  err_msg = 'Error! Could not determine L2 name for {0}'.\
85  format(self.data_files[0].name)
86  sys.exit(err_msg)
87  return next_lvl_name
88 
89  def _get_l3bin_name(self):
90  """
91  An internal method to return the L3bin name from an L2 or L3bin file.
92  """
93  first_char = self.get_platform_indicator()
94  sday, syear = self._get_start_doy_year()
95  eday, eyear = self._get_end_doy_year()
96  if sday and syear and sday > 0 and syear > 0:
97  sdate = datetime.datetime.strptime(str(syear) + '-' + str(sday),
98  '%Y-%j')
99  else:
100  err_msg = 'Error! Cannot process start date data: year = ' \
101  '{0}, doy = {1}'.format(syear, sday)
102  sys.exit(err_msg)
103  if eday and eyear and eday > 0 and eyear > 0:
104  edate = datetime.datetime.strptime(str(eyear) + '-' + str(eday),
105  '%Y-%j')
106  else:
107  err_msg = 'Error! Cannot process end date data: year = {0},' \
108  'doy = {1}'.format(eyear, eday)
109  sys.exit(err_msg)
110  days_diff = next_level_name_finder._get_days_diff(edate, sdate)
111 
112  if self.suite is None:
113  self.suite = '_SCI'
114 
115  if days_diff == 0:
116  extension = '.L3b_DAY'
117  next_lvl_name = first_char + str(syear) + str(sday) + extension +\
118  self.suite
119  else:
120  if days_diff == 7:
121  extension = '.L3b_8D'
122  else:
123  extension = '.L3b_CU'
124  next_lvl_name = first_char + str(syear) + str(sday) +\
125  str(eyear) + str(eday) + extension + self.suite
126  return next_lvl_name
127 
129  """
130  The API method to return the file name of the next level file(s) from
131  the file given when the object was instantiated. For example, if the
132  given filename is an L1B file, the next level file name will be an L2.
133  For some levels, if the filename already follows the OBPG convention,
134  the only change will be to the extension; e.g. A2000123010100.L1B_LAC ->
135  A2000123010100.L2_LAC. However, it is not assumed that the input file
136  follows this convention, so the output file name is derived from data
137  in the file header.
138  """
139  next_level_name = 'indeterminate'
140  if len(self.data_files) > 0:
141  if isinstance(self.transition_functions[self.data_files[0].file_type], types.FunctionType) \
142  or isinstance(self.transition_functions[self.data_files[0].file_type], types.MethodType):
143  next_level_name = self.transition_functions[self.data_files[0].file_type]()
144  elif self.next_level in self.transition_functions[
145  self.data_files[0].file_type].keys():
146  next_level_name = self.transition_functions[\
147  self.data_files[0].file_type]\
148  [self.next_level]()
149  else:
150  err_msg = 'Error! Cannot transition {0} to {1}.'.format(
151  self.data_files[0].name, self.next_level)
152  sys.exit(err_msg)
153  next_level_name += self._get_data_version()
154 # extra_ext = self._get_extra_extensions()
155 # if extra_ext:
156 # if next_level_name.find(extra_ext) == -1:
157 # # extra_ext in not in the name yet, add it.
158 # if extra_ext[0] != '.':
159 # next_level_name += '.' + extra_ext
160 # else:
161 # next_level_name += extra_ext
162 
163  return next_level_name
164 
165  def _get_single_file_basename(self):
166  """
167  For Aquarius, the base name is just copied over, instead of taken from
168  the metadata (JIRA ticket 1074).
169  """
170  basename = os.path.basename(self.data_files[0].name).split('.')[0]
171  return basename
172 
173  def _get_transition_functions(self):
174  """
175  An internal method to set up the "table" of functions to be
176  called for each level of processing.
177  """
178  return {'Level 1A': {'Level 1B': self._get_l1b_name,
179  'l1aextract_seawifs' : self._get_l1aextract_name,
180  'l1bgen' : self._get_l1b_name,
181  'l1mapgen': self._get_l1mapgen_name,
182  'Level 2' : self._get_l2_name
183  },
184  'Level 1B': {'Level 2': self._get_l2_name,
185  'l1brsgen': self._get_l1brsgen_name,
186  'l1mapgen': self._get_l1mapgen_name
187  },
188  'Level 2': {'l2bin_aquarius': self._get_l3bin_name,
189  'l2extract': self._get_l2extract_name,
190  'l3bin': self._get_l3bin_name,
191  'l2brsgen': self._get_l2brsgen_name,
192  'l2mapgen': self._get_l2mapgen_name
193  },
194  'Level 3 Binned': {'l3bin' : self._get_l3bin_name,
195  'SMI' : self._get_l3mapgen_name,
196  'l3gen': self._get_l3gen_name
197  }
198  }
def __init__(self, data_files_list, next_level, suite=None, resolution=None, oformat=None)
const char * str
Definition: l1c_msi.cpp:35