Skip to content

Commit c60035a

Browse files
authored
make the noise/calibration/orbit reading optional (#78)
* make the noise/calibration/orbit reading optional * catch both FileNotFound and KeyError for open_method
1 parent 4000bc7 commit c60035a

File tree

1 file changed

+43
-26
lines changed

1 file changed

+43
-26
lines changed

src/s1reader/s1_reader.py

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -485,27 +485,33 @@ def burst_from_xml(annotation_path: str, orbit_path: str, tiff_path: str,
485485
product_annotation = ProductAnnotation.from_et(tree_lads)
486486

487487
# load the Calibraton annotation
488-
calibration_annotation_path =\
489-
annotation_path.replace('annotation/', 'annotation/calibration/calibration-')
490-
with open_method(calibration_annotation_path, 'r') as f_cads:
491-
tree_cads = ET.parse(f_cads)
492-
calibration_annotation =\
493-
CalibrationAnnotation.from_et(tree_cads,
494-
calibration_annotation_path)
488+
try:
489+
calibration_annotation_path =\
490+
annotation_path.replace('annotation/', 'annotation/calibration/calibration-')
491+
with open_method(calibration_annotation_path, 'r') as f_cads:
492+
tree_cads = ET.parse(f_cads)
493+
calibration_annotation =\
494+
CalibrationAnnotation.from_et(tree_cads,
495+
calibration_annotation_path)
496+
except (FileNotFoundError, KeyError):
497+
calibration_annotation = None
495498

496499
# load the Noise annotation
497-
noise_annotation_path = annotation_path.replace('annotation/', 'annotation/calibration/noise-')
498-
with open_method(noise_annotation_path, 'r') as f_nads:
499-
tree_nads = ET.parse(f_nads)
500-
noise_annotation = NoiseAnnotation.from_et(tree_nads, ipf_version,
501-
noise_annotation_path)
500+
try:
501+
noise_annotation_path = annotation_path.replace('annotation/', 'annotation/calibration/noise-')
502+
with open_method(noise_annotation_path, 'r') as f_nads:
503+
tree_nads = ET.parse(f_nads)
504+
noise_annotation = NoiseAnnotation.from_et(tree_nads, ipf_version,
505+
noise_annotation_path)
506+
except (FileNotFoundError, KeyError):
507+
noise_annotation = None
502508

503509
# load AUX_CAL annotation
504510
eap_necessity = is_eap_correction_necessary(ipf_version)
505511
if eap_necessity.phase_correction and flag_apply_eap:
506512
path_aux_cals = os.path.join(f'{os.path.dirname(s1_annotation.__file__)}',
507-
'data',
508-
'aux_cal')
513+
'data',
514+
'aux_cal')
509515
path_aux_cal = get_path_aux_cal(path_aux_cals, annotation_path)
510516

511517
# Raise error flag when AUX_CAL file cannot be found
@@ -577,8 +583,11 @@ def burst_from_xml(annotation_path: str, orbit_path: str, tiff_path: str,
577583
iw2_mid_range = iw2_starting_range + 0.5 * iw2_n_samples * range_pxl_spacing
578584

579585
# find orbit state vectors in 'Data_Block/List_of_OSVs'
580-
orbit_tree = ET.parse(orbit_path)
581-
osv_list = orbit_tree.find('Data_Block/List_of_OSVs')
586+
if orbit_path:
587+
orbit_tree = ET.parse(orbit_path)
588+
osv_list = orbit_tree.find('Data_Block/List_of_OSVs')
589+
else:
590+
osv_list = []
582591

583592
# load individual burst
584593
burst_list_elements = tree.find('swathTiming/burstList')
@@ -611,11 +620,14 @@ def burst_from_xml(annotation_path: str, orbit_path: str, tiff_path: str,
611620
azimuth_time_interval)
612621
doppler = Doppler(poly1d, lut2d)
613622

614-
# get orbit from state vector list/element tree
615-
sensing_duration = datetime.timedelta(
616-
seconds=n_lines * azimuth_time_interval)
617-
orbit = get_burst_orbit(sensing_start, sensing_start + sensing_duration,
618-
osv_list)
623+
if len(osv_list) > 0:
624+
# get orbit from state vector list/element tree
625+
sensing_duration = datetime.timedelta(
626+
seconds=n_lines * azimuth_time_interval)
627+
orbit = get_burst_orbit(sensing_start, sensing_start + sensing_duration,
628+
osv_list)
629+
else:
630+
orbit = None
619631

620632
# determine burst offset and dimensions
621633
# TODO move to own method
@@ -634,12 +646,17 @@ def burst_from_xml(annotation_path: str, orbit_path: str, tiff_path: str,
634646

635647
burst_id = f't{track_number:03d}_{burst_num}_{swath_name.lower()}'
636648

637-
638649
# Extract burst-wise information for Calibration, Noise, and EAP correction
639-
burst_calibration = BurstCalibration.from_calibration_annotation(calibration_annotation,
640-
sensing_start)
641-
burst_noise = BurstNoise.from_noise_annotation(noise_annotation, sensing_start,
642-
i*n_lines, (i+1)*n_lines-1, ipf_version)
650+
if calibration_annotation is None:
651+
burst_calibration = None
652+
else:
653+
burst_calibration = BurstCalibration.from_calibration_annotation(calibration_annotation,
654+
sensing_start)
655+
if noise_annotation is None:
656+
burst_noise = None
657+
else:
658+
burst_noise = BurstNoise.from_noise_annotation(noise_annotation, sensing_start,
659+
i*n_lines, (i+1)*n_lines-1, ipf_version)
643660
if aux_cal_subswath is None:
644661
# Not applying EAP correction; (IPF high enough or user turned that off)
645662
# No need to fill in `burst_aux_cal`

0 commit comments

Comments
 (0)