2 Utility functions for get_output_name.
20 __version__ =
'1.0.6-2021-09-29'
27 Returns an integer taken from the passed in string.
30 int_value =
int(short_str)
32 err_msg =
"Error! Unable to convert {0} to integer.".format(short_str)
38 Returns the extension from format_data_list that is indicated by
50 format_index = next((i
for i, t
in enumerate(format_data_list)
if format_data_list[i][tuple_index].lower() == search_term.lower()),
None)
51 if (format_index !=
None)
and (format_index < len(format_data_list)):
52 extension = format_data_list[format_index][2]
54 for ext_candidate
in format_data_list:
55 if search_term.lower() == ext_candidate[2].lower():
56 extension = ext_candidate[2]
62 Returns the base element from input filename.
65 if len(data_files) > 1:
66 for file
in data_files:
68 if indctr.find(indicator) == -1:
69 if indctr.find(
'MODIS') != -1
and indicator.find(
'MODIS') != -1:
70 indicator =
'CROSS_MODIS.'
71 elif indctr.find(
'VIIRS') != -1
and indicator.find(
'VIIRS') != -1:
72 indicator =
'CROSS_VIIRS.'
74 indicator =
'CROSS_SENSOR.'
76 if target_program.find(
'bin') != -1
or \
77 target_program ==
'mapgen' or target_program ==
'l3mapgen':
78 base_element_name = indicator +
get_l3_time(data_files)
80 if data_files[0].file_type.find(
'Level 0') != -1:
83 time_stamp = data_files[0].start_time
84 dt_obj = datetime.datetime.strptime(time_stamp,
"%Y%j%H%M%S")
86 base_element_name =
"{}{}T{}".format(indicator, dt_obj.strftime(
"%Y%m%d"), time_stamp[7:])
88 return base_element_name
92 Extract a day of year and year from an L0 file's metadata and return
93 them as integer values .
95 if data_files[-1].end_time:
98 elif data_files[-1].metadata:
104 err_msg =
'Error! Cannot find end time for {0}'.format(
109 def _get_data_files_info(flf):
111 Returns a list of data files read from the specified input file.
114 with open(flf,
'rt')
as file_list_file:
115 inp_lines = file_list_file.readlines()
116 for line
in inp_lines:
117 filename = line.strip()
118 if os.path.exists(filename):
120 file_type, sensor = file_typer.get_file_type()
121 stime, etime = file_typer.get_file_times()
123 sensor, stime, etime)
124 data_file_list.append(data_file)
125 data_file_list.sort()
126 return data_file_list
130 A method to get the date/time stamp from L0 files.
133 if os.path.exists(l0_file_name +
'.const'):
134 with open(l0_file_name +
'.const')
as constructor_file:
135 constructor_data = constructor_file.readlines()
136 for line
in constructor_data:
137 if line.find(
'starttime=') != -1:
138 start_time = line[line.find(
'=') + 1].strip()
140 time_stamp = ProcUtils.date_convert(start_time,
't',
'j')
142 input_basename = os.path.basename(l0_file_name)
143 matched_name = re.match(
r"MOD00.?.[AP](\d\d\d\d\d\d\d).(\d\d\d\d)", input_basename)
144 if matched_name
is None:
145 matched_name = re.match(
r"[AP](\d\d\d\d\d\d\d)(\d\d\d\d)\d\d\.L0_.{3}", input_basename)
147 time_stamp = matched_name.group(1)+matched_name.group(2) +
'00'
149 err_msg =
"Unable to determine time stamp for input file {0}".\
156 Returns the number of days between two days, by subtracting day2 from day1.
158 return (day1 - day2).days
162 Returns the end day and year for a file, determined from the contents of
165 if 'End Day' in metadata:
167 elif 'Period End Day' in metadata:
169 elif 'time_coverage_end' in metadata:
170 eday = time_utils.convert_month_day_to_doy(
171 metadata[
'time_coverage_end'][5:7],
172 metadata[
'time_coverage_end'][8:10],
173 metadata[
'time_coverage_end'][0:4])
175 err_msg =
'Error! Cannot determine end day.'
177 if 'End Year' in metadata:
179 elif 'Period End Year' in metadata:
181 elif 'time_coverage_end' in metadata:
184 err_msg =
'Error! Cannot determine end year.'
190 Returns the extension appropriate for the program.
192 extension_dict = {
'level 1a':
'.nc',
196 'geolocate_hawkeye':
'.nc',
197 'geolocate_viirs':
'.nc',
199 'l1aextract_modis':
'.hdf',
200 'l1aextract_viirs':
'.nc',
201 'l1aextract_seawifs':
'.hdf',
206 'calibrate_viirs':
'.nc',
216 extension_allowed_dict = {
'level 1a':
'.nc',
218 'geo': {
'.hdf',
'.nc'},
220 'geolocate_hawkeye':
'.nc',
221 'geolocate_viirs':
'.nc',
223 'l1aextract_modis':
'.hdf',
224 'l1aextract_viirs':
'.nc',
225 'l1aextract_seawifs':
'.hdf',
226 'l1brsgen': {
'.hdf',
'.bin',
'.png',
'.ppm'},
227 'l1mapgen': {
'.ppm',
'.png',
'.tiff'},
228 'level 1b': {
'.hdf',
'nc'},
230 'calibrate_viirs':
'.nc',
231 'l1bgen': {
'.nc',
'.hdf'},
232 'l2gen': {
'.nc',
'.hdf'},
233 'l2extract': {
'.nc',
'.hdf'},
234 'l2brsgen': {
'.hdf',
'.png',
'.ppm'},
238 'l3mapgen': {
'.nc',
'.ppm',
'.png',
'.tiff'},
239 'mapgen': {
'.nc',
'.ppm',
'.png',
'.tiff'}}
240 if program
in list(extension_dict.keys()):
241 if clopts
and 'oformat' in clopts
and clopts[
'oformat'] !=
None :
243 format_ext =
'.' +
find_extension(file_formats, clopts[
'oformat'])
244 if format_ext ==
'.':
246 elif format_ext
in extension_allowed_dict[program]:
249 err_msg =
'Error! The oformat {0} is not supported by {1}.'.format(
250 clopts[
'oformat'], program)
253 ext = extension_dict[program]
258 A method to get the extra bits for l2bin, l3mapgen.
261 if target_program.find(
'bin') != -1
or \
262 target_program ==
'l3mapgen' or target_program ==
'mapgen':
265 if sday
and syear
and sday > 0
and syear > 0:
266 sdate = datetime.datetime.strptime(
str(syear) +
'-' +
str(sday),
269 err_msg =
'Error! Cannot process start date data: year = ' \
270 '{0}, doy = {1}'.format(syear, sday)
272 if eday
and eyear
and eday > 0
and eyear > 0:
273 edate = datetime.datetime.strptime(
str(eyear) +
'-' +
str(eday),
276 err_msg =
'Error! Cannot process end date data: year = {0},' \
277 'doy = {1}'.format(eyear, eday)
280 if clopts
and 'suite' in clopts
and clopts[
'suite']!=
None:
281 suite =
'.' + clopts[
'suite']
290 extra_bits =
'.DAY' + suite
293 extra_bits =
'.8D' + suite
295 extra_bits =
'.CU' + suite
296 if (target_program.find(
'l3mapgen') != -1
or target_program.find(
'mapgen') != -1)\
297 and clopts
and 'resolution' in clopts
and clopts[
'resolution'] !=
None:
298 extra_bits +=
'.' + clopts[
'resolution']
299 elif target_program.find(
'l2gen') != -1:
300 if clopts
and 'suite' in clopts
and clopts[
'suite'] !=
None:
301 extra_bits =
'.' + clopts[
'suite']
302 if data_files[0].name.find(
'sub') != -1:
308 An internal method to return the L3bin time from an L2 or
312 if len(data_files) == 1
and (re.search(
"\.\d\d\d\d\d\d\d\dT\d\d\d\d\d\d\.", data_files[0].name)
or
313 re.search(
"\d\d\d\d\d\d\d\d\d\d\d\d\d\.L2", data_files[0].name)):
314 time_stamp = data_files[0].start_time
315 dt_obj = datetime.datetime.strptime(time_stamp,
"%Y%j%H%M%S")
316 l3_time =
"{}T{}".format(dt_obj.strftime(
"%Y%m%d"), time_stamp[7:])
320 if sday
and syear
and sday > 0
and syear > 0:
321 sdate = datetime.datetime.strptime(
str(syear) +
'-' +
str(sday),
324 err_msg =
'Error! Cannot process start date data: year = {0}' \
325 ', doy = {1}'.format(syear, sday)
327 if eday
and eyear
and eday > 0
and eyear > 0:
328 edate = datetime.datetime.strptime(
str(eyear) +
'-' +
str(eday),
331 err_msg =
'Error! Cannot process end date data: year = {0},' \
332 'doy = {1}'.format(eyear, eday)
334 days_diff = (edate, sdate)
336 l3_time =
'%d%02d%02d' % (syear, sdate.month, sdate.day)
338 l3_time =
'%d%02d%02d%d%02d%02d' % (syear, sdate.month, sdate.day, eyear, edate.month, edate.day)
343 Returns the level element for the target_program.
345 level_dict = {
'level 1a':
'.L1A',
349 'geolocate_hawkeye':
'.GEO',
350 'geolocate_viirs':
'.GEO',
351 'l1aextract':
'.L1A.sub',
352 'l1aextract_modis':
'.L1A.sub',
353 'l1aextract_viirs':
'.L1A.sub',
354 'l1aextract_seawifs':
'.L1A.sub',
355 'l1brsgen':
'.L1BRS',
358 'calibrate_viirs':
'.L1B',
361 'l2extract':
'.L2.sub',
362 'l2brsgen':
'.L2BRS',
368 if program ==
'geo' and data_files[0].sensor.find(
'VIIRS') != -1:
369 program =
'geolocate_viirs'
370 if program
in list(level_dict.keys()):
371 level = level_dict[program]
372 elif program ==
'l1mapgen':
373 if data_files[0].file_type.find(
'Level 1A') != -1:
375 elif data_files[0].file_type.find(
'Level 1B') != -1:
381 Returns the file name derived from the input file name, target program name and oformat .
383 if clopts
and not isinstance(clopts, dict):
385 clopts = vars(clopts)
386 if target_program ==
'mapgen':
387 if data_files[0].file_type.find(
'Level 1') != -1
and len(data_files) ==1:
388 target_program =
'l1mapgen'
395 Returns a character which indicates what platform (instrument) the
396 data in the file is from.
398 indicator_dict = {
'Aquarius':
'SACD',
404 'HAWKEYE':
'SEAHAWK1',
414 'SeaWiFS':
'SEASTAR',
417 if data_file.name.find(
'CROSS_SENSOR') != -1:
418 indicator =
'CROSS_SENSOR.'
420 elif data_file.name.find(
'CROSS_MODIS') != -1:
421 indicator =
'CROSS_MODIS.'
423 elif data_file.name.find(
'CROSS_VIIRS') != -1:
424 indicator =
'CROSS_VIIRS.'
426 if data_file.sensor
in list(indicator_dict.keys()):
427 sensor = data_file.sensor.upper()
428 indicator = indicator_dict[data_file.sensor]
429 if sensor.find(
'OCTS') != -1:
431 elif sensor.find(
'MERIS') != -1:
432 if 'FRS' in data_file.name:
434 elif 'RR' in data_file.name:
436 elif sensor.find(
'SEAWIFS') != -1:
437 if 'GAC' in data_file.name:
439 elif 'infile' in data_file.metadata
and 'GAC' in data_file.metadata[
'infile']:
443 elif sensor.find(
'OLCI') != -1:
445 if 'data_type' in data_file.metadata:
446 data_type = data_file.metadata[
'data_type']
447 elif 'EFR' in data_file.name:
449 elif 'ERR' in data_file.name:
451 elif data_file.sensor.find(
'MODIS') != -1:
453 if data_file.sensor.find(
'Aqua') != -1:
455 elif data_file.sensor.find(
'Terra') != -1:
458 err_msg =
'Error! Could not determine platform indicator for MODIS file {0}.'.\
459 format(data_file.name)
461 elif data_file.sensor.find(
'VIIRS') != -1:
463 if data_file.sensor.find(
'J1') != -1:
465 elif data_file.sensor.find(
'J2')!= -1:
467 elif data_file.sensor.find(
'NPP')!= -1:
470 err_msg =
'Error! Could not determine platform indicator for VIIRS file {0}.'.\
471 format(data_file.name)
473 elif data_file.sensor.find(
'MSI') != -1:
475 if data_file.sensor ==
'MSI S2A':
477 elif data_file.sensor ==
'MSI S2B':
480 err_msg =
'Error! Could not determine platform indicator for MSI file {0}.'.\
481 format(data_file.name)
483 elif data_file.sensor.find(
'OLI') != -1:
485 if data_file.sensor ==
'OLI L8':
486 indicator =
'LANDSAT8'
487 elif data_file.sensor ==
'OLI L9':
488 indicator =
'LANDSAT9'
490 err_msg =
'Error! Could not determine platform indicator for MSI file {0}.'.\
491 format(data_file.name)
494 err_msg =
'Error! Platform indicator, {0}, for {1} is not known.'.\
495 format(data_file.sensor, data_file.name)
506 indicator +=
'_' + sensor +
'_' + data_type +
'.'
508 indicator +=
'_' + sensor +
'.'
513 Extract a day of year and year from a file's metadata and return
514 them as integer values .
516 if data_files[0].end_time:
519 elif data_files[0].metadata:
520 day_str =
'Start Day'
521 yr_str =
'Start Year'
525 err_msg =
'Error! Cannot find end time for {0}'.format(
532 Returns the start day and year for a file, determined from the contents of
535 if 'Start Day' in metadata:
537 elif 'Period Start Day' in metadata:
539 elif 'time_coverage_start' in metadata:
540 sday = time_utils.convert_month_day_to_doy(
541 metadata[
'time_coverage_start'][5:7],
542 metadata[
'time_coverage_start'][8:10],
543 metadata[
'time_coverage_start'][0:4])
545 err_msg =
'Error! Cannot determine start day.'
547 if 'Start Year' in metadata:
549 elif 'Period Start Year' in metadata:
551 elif 'time_coverage_start' in metadata:
554 err_msg =
'Error! Cannot determine start year.'
560 Return the part of the file extension based on the time period within the
563 first_date = datetime.datetime.strptime(start_date_str,
'%Y%j%H%M%S')
564 last_date = datetime.datetime.strptime(end_date_str,
'%Y%j%H%M%S')
565 date_diff = last_date - first_date
566 if date_diff.days == 0:
568 elif date_diff.days == 7:
570 elif is_month(first_date, last_date):
572 elif is_year(first_date, last_date):
580 Returns True if the days are the endpoints of a month; False otherwise.
582 return day1.month == day2.month
and day1.day == 1
and\
583 day2.day == calendar.monthrange(day1.year, day1.month)[1]
587 Returns True if the days are the endpoints of a year; False otherwise.
589 return day1.year == day2.year
and day1.month == 1
and day1.day == 1
and\
590 day2.month == 12
and day2.day == 31
594 Returns a tuple containing the file formats.
597 format_file_path = os.path.join(os.getenv(
'OCDATAROOT'),
'common',
599 if os.path.exists(format_file_path):
601 format_file_hndl = open(format_file_path)
602 inp_lines = format_file_hndl.readlines()
603 format_file_hndl.close()
604 for line
in inp_lines:
605 cleaned_line = line.strip()
606 if cleaned_line[0] !=
'#':
608 file_format = tuple(cleaned_line.split(
':'))
610 file_formats.append(file_format)
614 err_msg =
'Error! Cannot find file {0}.'.format(format_file_path)