OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
modis_L1B_utils.py
Go to the documentation of this file.
1 
2 import os
3 import sys
4 import subprocess
5 import shutil
6 from seadasutils.ParamUtils import ParamProcessing
7 import seadasutils.ProcUtils as ProcUtils
10 
11 
12 class ModisL1B:
13  """
14 
15  Runs l1bgen_modis for a single Level 1A granule
16  A MODIS Aqua or Terra Level 1B granule is produced from a L1A file.
17 
18  This script is a wrapper for MODIS L1A to L1B processing:
19 
20  -calls the modis_timestamp binary to determine platform & start time
21  -constructs the PCF file
22  -calls the l1bgen_modisa or l1bgen_modist binary
23  -checks exit status from the L1B processing and cleans up if no error
24 
25  """
26 
27  def __init__(self, inp_file=None,
28  parfile=None,
29  geofile=None,
30  okm=None,
31  hkm=None,
32  qkm=None,
33  obc=None,
34  lutver=None,
35  lutdir=None,
36  delfiles=0,
37  log=False,
38  verbose=False):
39  self.filename = inp_file
40  self.parfile = parfile
41  self.geofile = geofile
42  self.okm = okm
43  self.hkm = hkm
44  self.qkm = qkm
45  self.obc = obc
46  self.proctype = 'modisL1B'
47  self.lutversion = lutver
48  self.lutdir = lutdir
49  self.delfiles = delfiles
50  self.log = log
51  self.ancdir = None
52  self.pcf_file = None
53  self.curdir = False
54  self.verbose = verbose
55  self.dirs = {}
56  self.sensor = None
57  self.prefix = None
58 
59  # version specific variables
60  self.collection_id = '061'
61 
62  if self.parfile:
63  if self.verbose:
64  print("Reading parameter file: %s" % self.parfile)
65  param_proc = ParamProcessing(parfile=self.parfile)
66  param_proc.parseParFile(prog='l1bgen')
67  phash = param_proc.params['l1bgen']
68  for param in (list(phash.keys())):
69  if not self[param]:
70  self[param] = phash[param]
71 
72  # determine geo file name
73  if self.geofile is None:
75  ftype, sensor = file_typer.get_file_type()
76  stime, etime = file_typer.get_file_times()
77  data_files_list = list([mlp.obpg_data_file.ObpgDataFile(self.filename,
78  ftype,
79  sensor,
80  stime,
81  etime)])
82  self.geofile = mlp.get_output_name_utils.get_output_name(data_files_list, 'geo', None)
83 
84  if not os.path.exists(self.geofile):
85  self.geofile = os.path.join(os.path.dirname(self.filename), self.geofile)
86 
87  if not os.path.exists(self.geofile):
88  self.geofile = '.'.join([self.filename.split('.')[0], "GEO"])
89  if not os.path.exists(self.geofile):
90  geofile_parts = self.filename.split('.')[:-1]
91  geofile_parts.append('GEO')
92  self.geofile = '.'.join(geofile_parts)
93 
94  if self.verbose:
95  print("Assuming GEOFILE is %s" % self.geofile)
96 
97 
98  def __setitem__(self, index, item):
99  self.__dict__[index] = item
100 
101  def __getitem__(self, index):
102  return self.__dict__[index]
103 
104  def chk(self):
105  """
106  check parameters
107  """
108  if not os.path.exists(self.filename):
109  print("ERROR: File", self.filename, "does not exist.")
110  sys.exit(1)
111 
112  if not os.path.exists(self.geofile):
113  print("ERROR: File", self.geofile, "does not exist.")
114  sys.exit(1)
115 
116  def _clean_files(self):
117  """
118  Removes any unwanted files from the L1A -> L1B processing.
119  """
120  if self.delfiles & 1:
121  ProcUtils.remove(self.okm)
122  if self.delfiles & 2:
123  ProcUtils.remove(self.hkm)
124  if self.delfiles & 4:
125  ProcUtils.remove(self.qkm)
126  if self.delfiles & 8:
127  ProcUtils.remove(self.obc)
128 
129  if self.log is False:
130  ProcUtils.remove(self.pcf_file)
131  base = os.path.basename(self.okm)
132  ProcUtils.remove(os.path.join(self.dirs['run'],
133  '.'.join(['LogReport', base])))
134  ProcUtils.remove(os.path.join(self.dirs['run'],
135  '.'.join(['LogStatus', base])))
136  ProcUtils.remove(os.path.join(self.dirs['run'],
137  '.'.join(['LogUser', base])))
138 
139  def run(self):
140  """
141  Run l1bgen_modis (MOD_PR02)
142  """
143 
144  if self.verbose:
145  print("Processing MODIS L1A file to L1B...")
146  l1bgen = os.path.join(self.dirs['bin'],
147  ''.join(['l1bgen_', self.sensor]))
148  status = subprocess.run(l1bgen, shell=False).returncode
149  if self.verbose:
150  print(l1bgen, "exit status:", str(status))
151 
152  if not status:
153  ProcUtils.remove(os.path.join(self.dirs['run'], "GetAttr.temp"))
154  ProcUtils.remove(os.path.join(self.dirs['run'], "ShmMem"))
155  for l1bfile in (self.okm, self.hkm, self.qkm, self.obc):
156  if os.path.dirname(l1bfile) != self.dirs['run']:
157  origfile = os.path.join(self.dirs['run'],
158  os.path.basename(l1bfile))
159  if os.path.exists(origfile):
160  shutil.move(origfile, l1bfile)
161  ProcUtils.remove('.'.join([origfile, 'met']))
162  else:
163  ProcUtils.remove('.'.join([l1bfile, 'met']))
164 
165  self._clean_files()
166 
167  if self.verbose:
168  print("MODIS L1B processing complete.")
169  # else .. it failed
170  else:
171  print("ERROR: MODIS L1B processing failed.")
172  print("Please examine the LogStatus and LogUser files for more information.")
173  sys.exit(1)
def __init__(self, inp_file=None, parfile=None, geofile=None, okm=None, hkm=None, qkm=None, obc=None, lutver=None, lutdir=None, delfiles=0, log=False, verbose=False)
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 __setitem__(self, index, item)
const char * str
Definition: l1c_msi.cpp:35
def get_output_name(data_files, target_program, clopts)