17 This class defines the parameters and sets up the environment necessary
18 to process a MODIS L0 granule to produce a L1A granule. It is implemented
19 by the modis_L1A code. This class also contains the methods necessary to
20 run the l0_write_construct and l1agen_modis binaries.
66 p.parseParFile(prog=
'l1agen')
67 phash = p.params[
'l1agen']
68 for param
in (phash.keys()):
70 self[param] = phash[param]
74 self.__dict__[index] = item
77 return self.__dict__[index]
82 check input parameters
84 if not os.path.exists(self.
filename):
85 print(
"ERROR: File: " + self.
filename +
" does not exist.")
90 print(
"ERROR: File " + self.
nextgranule +
" does not exist.")
94 print(
"* Next L0 granule is specified, therefore setting stopnudge = 0 *")
103 os.environ[
'PGS_PC_INFO_FILE'] = self.
pcf_file
107 line = line.replace(
'LOGDIR', self.
dirs[
'run'].resolve().as_posix())
108 line = line.replace(
'L1AFILE', os.path.basename(self.
filename))
109 line = line.replace(
'VARDIR', os.path.join(self.
dirs[
'var'],
'modis'))
115 print(
"Determining pass start and stop time...\n")
117 l0cnst_write_cmd = [os.path.join(self.
dirs[
'bin'],
'l0cnst_write_modis'), self.
l0file]
119 l0cnst_write_output = subprocess.run(l0cnst_write_cmd, capture_output=
True, text=
True, shell=
False)
120 if l0cnst_write_output.returncode:
121 return l0cnst_write_output.returncode
123 for line
in l0cnst_write_output.stdout.splitlines():
125 key, val = line.split(
'=')
128 if 'starttime' in key:
129 self.
start = val.strip()
130 elif 'stoptime' in key:
131 self.
stop = val.strip()
132 elif 'length' in key:
140 ftype, sensor = file_typer.get_file_type()
141 stime, etime = file_typer.get_file_times()
149 Write L0 Constructor File
155 if not os.path.exists(self.
l0file):
161 if status != 0
and status != 3:
163 print(
"l0cnst_write_modis: Unrecoverable error encountered while attempting to generate constructor file.")
169 print(
"l0cnst_write_modis: A corrupt packet or a packet of the wrong size")
170 print(
" was detected while generating the constructor file.")
172 print(
"Fixing of Level-0 file is currently disabled.")
173 print(
"Please re-run without the '--disable-fix_L0' option.")
178 print(
"Attempting to fix the Level 0 file using l0fix_modis")
180 l0fixcmd = [os.path.join(self.
dirs[
'bin'],
'l0fix_modis'), self.
l0file,
'-1',
'-1']
183 l0fix = subprocess.run(l0fixcmd, capture_output=
True, text=
True, shell=
False)
185 print(
"l0fix_modis: Unrecoverable error in l0fix_modis!")
189 for line
in l0fix.stdout.splitlines():
190 if "taitime_start:" in line:
192 if "taitime_stop:" in line:
199 l0fix = subprocess.run(l0fixcmd, capture_output=
True, shell=
False)
201 print(
"l0fix_modis: Unrecoverable error in l0fix_modis!")
204 print(
"New Level 0 file successfully generated. Regenerating constructor file...")
210 print(
"Failed to generate constructor file after running l0fix_modis.")
211 print(
"Please examine your Level 0 file to determine if it is completely corrupt.")
217 if self.
l1a is not None:
219 print(
"Using specified output L1A filename: %s" % self.
l1a)
225 starttime = ProcUtils.round_minutes(self.
start,
't',5)
226 stoptime = ProcUtils.round_minutes(self.
stop,
't',5)
227 if starttime == stoptime:
229 print(
"Short granule: extending end time to 5-min boundary")
230 stoptime = ProcUtils.round_minutes(self.
stop,
't',5,rounding=1)
231 if starttime != stoptime:
242 print(
"Input Level 0:", self.
filename)
243 print(
"Output Level 1A:", self.
l1a)
245 print(
"Start Time:", self.
start)
246 print(
"Stop Time:", self.
stop)
247 print(
"Granule Duration:", self.
length,
"seconds")
252 Run l1agen_modis (MOD_PR01)
258 cat = open(self.
l0file,
'wb')
259 shutil.copyfileobj(open(self.
l0file +
'.tmp',
'rb'), cat)
260 shutil.copyfileobj(open(self.
nextgranule,
'rb'), cat)
264 print(
"Processing MODIS L0 file to L1A...")
265 status = subprocess.run(os.path.join(self.
dirs[
'bin'],
'l1agen_modis'), shell=
False).returncode
267 print(
'l1agen_modis exit status:',
str(status))
275 ProcUtils.remove(self.
l0file +
'.constr')
276 ProcUtils.remove(self.
l0file +
'.pcf')
277 base = os.path.basename(self.
l0file)
278 ProcUtils.remove(os.path.join(self.
dirs[
'run'],
'LogReport.' + base))
279 ProcUtils.remove(os.path.join(self.
dirs[
'run'],
'LogStatus.' + base))
280 ProcUtils.remove(os.path.join(self.
dirs[
'run'],
'LogUser.' + base))
281 ProcUtils.remove(os.path.join(self.
dirs[
'run'],
"GetAttr.temp"))
282 ProcUtils.remove(os.path.join(self.
dirs[
'run'],
"ShmMem"))
284 fixed_l0file = self.
l0file.replace(
'.fixed',
'')
285 ProcUtils.remove(fixed_l0file +
'.constr')
286 ProcUtils.remove(fixed_l0file +
'.pcf')
287 base = os.path.basename(fixed_l0file)
288 ProcUtils.remove(os.path.join(self.
dirs[
'run'],
'LogReport.' + base))
289 ProcUtils.remove(os.path.join(self.
dirs[
'run'],
'LogStatus.' + base))
290 ProcUtils.remove(os.path.join(self.
dirs[
'run'],
'LogUser.' + base))
292 if os.path.dirname(self.
l1a) != self.
dirs[
'run']:
293 origfile = os.path.join(self.
dirs[
'run'],
294 os.path.basename(self.
l1a))
295 if os.path.exists(origfile):
296 shutil.move(origfile, self.
l1a)
297 ProcUtils.remove(
'.'.join([origfile,
'met']))
299 ProcUtils.remove(
'.'.join([self.
l1a,
'met']))
302 if os.path.islink(self.
l0file):
305 if os.path.islink(os.path.basename(self.
filename)):
306 os.remove(os.path.basename(self.
filename))
308 if self.
log is False:
310 base = os.path.basename(self.
l1a)
311 ProcUtils.remove(os.path.join(self.
dirs[
'run'],
'LogReport.' + base))
312 ProcUtils.remove(os.path.join(self.
dirs[
'run'],
'LogStatus.' + base))
313 ProcUtils.remove(os.path.join(self.
dirs[
'run'],
'LogUser.' + base))
316 print(
"MODIS L1A processing complete.")
318 print(
"modis_l1a: ERROR: MODIS L1A processing failed.")
319 print(
"Please examine the LogStatus and LogUser files for more information.")