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':
'.hdf',
196 'geolocate_hawkeye':
'.hdf',
197 'geolocate_viirs':
'.hdf',
199 'l1aextract_modis':
'.nc',
200 'l1aextract_viirs':
'.nc',
201 'l1aextract_seawifs':
'.nc',
206 'calibrate_viirs':
'.hdf',
216 extension_allowed_dict = {
'level 1a':
'.hdf',
219 'geolocate_hawkeye':
'.hdf',
220 'geolocate_viirs':
'.hdf',
222 'l1aextract_modis':
'.nc',
223 'l1aextract_viirs':
'.nc',
224 'l1aextract_seawifs':
'.nc',
225 'l1brsgen': {
'.hdf',
'.bin',
'.png',
'.ppm'},
226 'l1mapgen': {
'.ppm',
'.png',
'.tiff'},
229 'calibrate_viirs':
'.hdf',
233 'l2brsgen': {
'.hdf',
'.png',
'.ppm'},
237 'l3mapgen': {
'.nc',
'.ppm',
'.png',
'.tiff'},
238 'mapgen': {
'.nc',
'.ppm',
'.png',
'.tiff'}}
239 if program
in list(extension_dict.keys()):
240 if clopts
and 'oformat' in clopts
and clopts[
'oformat'] !=
None :
242 format_ext =
'.' +
find_extension(file_formats, clopts[
'oformat'])
243 if format_ext ==
'.':
245 elif format_ext
in extension_allowed_dict[program]:
248 err_msg =
'Error! The oformat {0} is not supported by {1}.'.format(
249 clopts[
'oformat'], program)
252 ext = extension_dict[program]
257 A method to get the extra bits for l2bin, l3mapgen.
260 if target_program.find(
'bin') != -1
or \
261 target_program ==
'l3mapgen' or target_program ==
'mapgen':
264 if sday
and syear
and sday > 0
and syear > 0:
265 sdate = datetime.datetime.strptime(
str(syear) +
'-' +
str(sday),
268 err_msg =
'Error! Cannot process start date data: year = ' \
269 '{0}, doy = {1}'.format(syear, sday)
271 if eday
and eyear
and eday > 0
and eyear > 0:
272 edate = datetime.datetime.strptime(
str(eyear) +
'-' +
str(eday),
275 err_msg =
'Error! Cannot process end date data: year = {0},' \
276 'doy = {1}'.format(eyear, eday)
279 if clopts
and 'suite' in clopts
and clopts[
'suite']!=
None:
280 suite =
'.' + clopts[
'suite']
289 extra_bits =
'.DAY' + suite
292 extra_bits =
'.8D' + suite
294 extra_bits =
'.CU' + suite
295 if (target_program.find(
'l3mapgen') != -1
or target_program.find(
'mapgen') != -1)\
296 and clopts
and 'resolution' in clopts
and clopts[
'resolution'] !=
None:
297 extra_bits +=
'.' + clopts[
'resolution']
298 elif target_program.find(
'l2gen') != -1:
299 if clopts
and 'suite' in clopts
and clopts[
'suite'] !=
None:
300 extra_bits =
'.' + clopts[
'suite']
301 if data_files[0].name.find(
'sub') != -1:
307 An internal method to return the L3bin time from an L2 or
313 if sday
and syear
and sday > 0
and syear > 0:
314 sdate = datetime.datetime.strptime(
str(syear) +
'-' +
str(sday),
317 err_msg =
'Error! Cannot process start date data: year = {0}' \
318 ', doy = {1}'.format(syear, sday)
320 if eday
and eyear
and eday > 0
and eyear > 0:
321 edate = datetime.datetime.strptime(
str(eyear) +
'-' +
str(eday),
324 err_msg =
'Error! Cannot process end date data: year = {0},' \
325 'doy = {1}'.format(eyear, eday)
327 days_diff = (edate, sdate)
329 l3_time =
'%d%02d%02d' % (syear, sdate.month, sdate.day)
331 l3_time =
'%d%02d%02d%d%02d%02d' % (syear, sdate.month, sdate.day, eyear, edate.month, edate.day)
336 Returns the level element for the target_program.
338 level_dict = {
'level 1a':
'.L1A',
342 'geolocate_hawkeye':
'.GEO',
343 'geolocate_viirs':
'.GEO',
344 'l1aextract':
'.L1A.sub',
345 'l1aextract_modis':
'.L1A.sub',
346 'l1aextract_viirs':
'.L1A.sub',
347 'l1aextract_seawifs':
'.L1A.sub',
348 'l1brsgen':
'.L1BRS',
351 'calibrate_viirs':
'.L1B',
354 'l2extract':
'.L2.sub',
355 'l2brsgen':
'.L2BRS',
361 if program ==
'geo' and data_files[0].sensor.find(
'VIIRS') != -1:
362 program =
'geolocate_viirs'
363 if program
in list(level_dict.keys()):
364 level = level_dict[program]
365 elif program ==
'l1mapgen':
366 if data_files[0].file_type.find(
'Level 1A') != -1:
368 elif data_files[0].file_type.find(
'Level 1B') != -1:
374 Returns the file name derived from the input file name, target program name and oformat .
376 if clopts
and not isinstance(clopts, dict):
378 clopts = vars(clopts)
379 if target_program ==
'mapgen':
380 if data_files[0].file_type.find(
'Level 1') != -1
and len(data_files) ==1:
381 target_program =
'l1mapgen'
388 Returns a character which indicates what platform (instrument) the
389 data in the file is from.
391 indicator_dict = {
'Aquarius':
'SACD',
397 'HAWKEYE':
'SEAHAWK1',
406 'SeaWiFS':
'SEASTAR',
409 if data_file.name.find(
'CROSS_SENSOR') != -1:
410 indicator =
'CROSS_SENSOR.'
412 elif data_file.name.find(
'CROSS_MODIS') != -1:
413 indicator =
'CROSS_MODIS.'
415 elif data_file.name.find(
'CROSS_VIIRS') != -1:
416 indicator =
'CROSS_VIIRS.'
418 if data_file.sensor
in list(indicator_dict.keys()):
419 sensor = data_file.sensor.upper()
420 indicator = indicator_dict[data_file.sensor]
421 if sensor.find(
'OCTS') != -1:
423 elif sensor.find(
'MERIS') != -1:
424 if 'FRS' in data_file.name:
426 elif 'RR' in data_file.name:
428 elif sensor.find(
'SEAWIFS') != -1:
429 if 'GAC' in data_file.name:
431 elif 'infile' in data_file.metadata
and 'GAC' in data_file.metadata[
'infile']:
435 elif sensor.find(
'OLCI') != -1:
437 if 'data_type' in data_file.metadata:
438 data_type = data_file.metadata[
'data_type']
439 elif 'EFR' in data_file.name:
441 elif 'ERR' in data_file.name:
443 elif data_file.sensor.find(
'MODIS') != -1:
445 if data_file.sensor.find(
'Aqua') != -1:
447 elif data_file.sensor.find(
'Terra') != -1:
450 err_msg =
'Error! Could not determine platform indicator for MODIS file {0}.'.\
451 format(data_file.name)
453 elif data_file.sensor.find(
'VIIRS') != -1:
455 if data_file.sensor.find(
'J1') != -1:
457 elif data_file.sensor.find(
'NPP')!= -1:
460 err_msg =
'Error! Could not determine platform indicator for VIIRS file {0}.'.\
461 format(data_file.name)
463 elif data_file.sensor.find(
'MSI') != -1:
465 if data_file.sensor ==
'MSI S2A':
467 elif data_file.sensor ==
'MSI S2B':
470 err_msg =
'Error! Could not determine platform indicator for MSI file {0}.'.\
471 format(data_file.name)
473 elif data_file.sensor.find(
'OLI') != -1:
475 if data_file.sensor ==
'OLI L8':
476 indicator =
'LANDSAT8'
477 elif data_file.sensor ==
'OLI L9':
478 indicator =
'LANDSAT9'
480 err_msg =
'Error! Could not determine platform indicator for MSI file {0}.'.\
481 format(data_file.name)
484 err_msg =
'Error! Platform indicator, {0}, for {1} is not known.'.\
485 format(data_file.sensor, data_file.name)
496 indicator +=
'_' + sensor +
'_' + data_type +
'.'
498 indicator +=
'_' + sensor +
'.'
503 Extract a day of year and year from a file's metadata and return
504 them as integer values .
506 if data_files[0].end_time:
509 elif data_files[0].metadata:
510 day_str =
'Start Day'
511 yr_str =
'Start Year'
515 err_msg =
'Error! Cannot find end time for {0}'.format(
522 Returns the start day and year for a file, determined from the contents of
525 if 'Start Day' in metadata:
527 elif 'Period Start Day' in metadata:
529 elif 'time_coverage_start' in metadata:
530 sday = time_utils.convert_month_day_to_doy(
531 metadata[
'time_coverage_start'][5:7],
532 metadata[
'time_coverage_start'][8:10],
533 metadata[
'time_coverage_start'][0:4])
535 err_msg =
'Error! Cannot determine start day.'
537 if 'Start Year' in metadata:
539 elif 'Period Start Year' in metadata:
541 elif 'time_coverage_start' in metadata:
544 err_msg =
'Error! Cannot determine start year.'
550 Return the part of the file extension based on the time period within the
553 first_date = datetime.datetime.strptime(start_date_str,
'%Y%j%H%M%S')
554 last_date = datetime.datetime.strptime(end_date_str,
'%Y%j%H%M%S')
555 date_diff = last_date - first_date
556 if date_diff.days == 0:
558 elif date_diff.days == 7:
560 elif is_month(first_date, last_date):
562 elif is_year(first_date, last_date):
570 Returns True if the days are the endpoints of a month; False otherwise.
572 return day1.month == day2.month
and day1.day == 1
and\
573 day2.day == calendar.monthrange(day1.year, day1.month)[1]
577 Returns True if the days are the endpoints of a year; False otherwise.
579 return day1.year == day2.year
and day1.month == 1
and day1.day == 1
and\
580 day2.month == 12
and day2.day == 31
584 Returns a tuple containing the file formats.
587 format_file_path = os.path.join(os.getenv(
'OCDATAROOT'),
'common',
589 if os.path.exists(format_file_path):
591 format_file_hndl = open(format_file_path)
592 inp_lines = format_file_hndl.readlines()
593 format_file_hndl.close()
594 for line
in inp_lines:
595 cleaned_line = line.strip()
596 if cleaned_line[0] !=
'#':
598 file_format = tuple(cleaned_line.split(
':'))
600 file_formats.append(file_format)
604 err_msg =
'Error! Cannot find file {0}.'.format(format_file_path)