Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated resampling resolution comparison #112

Open
wants to merge 11 commits into
base: dswx-ni-beta-point
Choose a base branch
from
272 changes: 202 additions & 70 deletions src/dswx_sar/dswx_ni_runconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import logging
from types import SimpleNamespace
import numpy as np

import warnings
import yamale
from ruamel.yaml import YAML

Expand Down Expand Up @@ -204,10 +204,24 @@ def check_file_path(file_path: str) -> None:


def get_pol_rtc_hdf5(input_rtc, freq_group):
# convenience function to get polarization from RTC file path
# basename separates file name from directory in path string
# splitext removes the file extension from basename
# split('_')[-1] gets polarization
""" convenience function to get polarization from RTC file path
basename separates file name from directory in path string
splitext removes the file extension from basename
split('_')[-1] gets polarization

Parameters
__________
input_rtc: list
list of all RTC input files

freq_group: list
Frequency groups in the RTC inputs, e.g. A or B, or A and B

Returns
-------
pols: list
polarizations of data from input frequency group
"""
path_pol = f'/science/LSAR/GCOV/grids/frequency{freq_group}/listOfPolarizations'

with h5py.File(input_rtc) as src:
Expand All @@ -218,6 +232,18 @@ def get_pol_rtc_hdf5(input_rtc, freq_group):


def get_freq_rtc_hdf5(input_rtc):
"""Read frequency groups for each of the input RTC files

Parameters
__________
input_rtc: list
list of all RTC input files

Returns
-------
freq: list
RTC input frequency group(s), A or B, or A and B
"""
path_freq = f'/science/LSAR/identification/listOfFrequencies'

with h5py.File(input_rtc) as src_h5:
Expand All @@ -226,6 +252,28 @@ def get_freq_rtc_hdf5(input_rtc):

return freq

def get_res_rtc_hdf5(input_rtc, freq_group):
hb6688 marked this conversation as resolved.
Show resolved Hide resolved
""" convenience function to get the image postings from RTC file path
Since x and y postings are equal, x posting is used

Parameters
__________
input_rtc: list
list of all RTC input files
freq_group: str
RTC input frequency group, A or B

Returns
-------
res: float
Data resolution with respect to input frequency group
"""
x_posting = f'/science/LSAR/GCOV/grids/frequency{freq_group}/xCoordinateSpacing'

with h5py.File(input_rtc, 'r') as src:
res = src[x_posting][()]

return res

def check_rtc_frequency(input_h5_list):
"""Read Frequency group information from input RTC file(s)
Expand All @@ -250,30 +298,47 @@ def check_rtc_frequency(input_h5_list):
if num_input_files == 1:
freq_list = [get_freq_rtc_hdf5(input_h5_list[0])]
return True, freq_list # If only one file, frequencies are trivially equal
else:
freq_list = np.empty(num_input_files , dtype=object)

freq_list = np.empty(num_input_files , dtype=object)
flag_pol_equal = True
for input_idx, input_h5 in enumerate(input_h5_list):
freq_list[input_idx] = get_freq_rtc_hdf5(input_h5)

for input_idx, input_h5 in enumerate(input_h5_list):
freq_list[input_idx] = get_freq_rtc_hdf5(input_h5)
for idx in range(num_input_files - 1):
if freq_list[idx] == freq_list[idx + 1]:
flag_freq_equal = True
else:
# Frequency groups between frames are different.
flag_freq_equal = False
break

for idx in range(num_input_files - 1):
if freq_list[idx] == freq_list[idx + 1]:
flag_freq_equal = True
else:
# Frequency groups between frames are different.
flag_freq_equal = False
break
for freq_idx, freq_group in enumerate(freq_list):
if all(item is None for item in freq_group):
warnings.warn(f'Warning: All items in frequency sublist {freq_idx} are None')

return flag_freq_equal, freq_list


def read_rtc_polarization(input_h5_list, freq_list):
"""Read polarizations from all frequency groups of all inputs

Parameters
----------
input_h5_list : list of strings
input RTC files in a list
freq_list: list
List of frequency group(s) in each of the files
e.g. A, B, or A and B

Returns
-------
pol_list: list
polarizations of all frequency groups from all inputs
"""
num_input_files = len(input_h5_list)
pol_list = np.empty((num_input_files, 2) , dtype=object)

for input_idx, input_h5 in enumerate(input_h5_list):
extracted_strings = []
# Check to see if frequency group of an input file is empty
if freq_list[input_idx]:
for freq_idx, freq_group in enumerate(freq_list[input_idx]):
Expand All @@ -282,7 +347,92 @@ def read_rtc_polarization(input_h5_list, freq_list):
return pol_list


def read_rtc_resolution(input_h5_list, freq_list):
hb6688 marked this conversation as resolved.
Show resolved Hide resolved
"""Find highest resolution of the input data from all frequency groups

Parameters
----------
input_h5_list : list of strings
input RTC files in a list
freq_list: list
List of frequency group(s) in each of the files
e.g. A, B, or A and B

Returns
-------
res_list: list
Input resolution of each frequency group of each input file

res_highest: float
Highest resolution of each input RTC
"""
num_input_files = len(input_h5_list)
res_list = np.empty((num_input_files, 2) , dtype=object)
#y_res_list = np.empty((num_input_files, 2) , dtype=object)

for input_idx, input_h5 in enumerate(input_h5_list):
# Check to see if frequency group of an input file is empty
if freq_list[input_idx]:
for freq_idx, freq_group in enumerate(freq_list[input_idx]):
res_list[input_idx, freq_idx] = \
get_res_rtc_hdf5(input_h5, freq_group)
hb6688 marked this conversation as resolved.
Show resolved Hide resolved

# Flatten the array and filter out None values
res_list_valid = [item for item in res_list.flatten() if item is not None]
res_highest = max(res_list_valid)

return res_list, res_highest


def compare_rtc_resolution(res_list):
"""Find highest resolution from all input resolutions
If an item in the list is None, then generate an error

Parameters
----------
res_list
input RTC files resolutions

Returns
-------

res_highest: float
Highest resolution of each input RTC
"""
res_highest = 0

for res in res_list:
if (res is not None) and (res > res_highest):
res_highest = res
else:
continue

if res_highest == 0:
raise ValueError(
f'Incorrect highest RTC resolution: {res_highest} meter.'
)


return res_highest

def compare_rtc_polarization(pol_list):
"""Verify polarizations of all inputs from same the frequency group
to see if they are of the same.

Parameters
----------
pol_list
Input polarization list

Returns
-------
flag_bw_freq_a_equal: bool
Flag which indicates if polarziations of all inputs with respect
to Frequency group A are equal. True = Equal
flag_bw_freq_b_equal: bool
Flag which indicates if polarziations of all inputs with respect
to Frequency group B are equal. True = Equal
"""
num_input_files = len(pol_list)

pol_freq_a = pol_list[:, 0]
Expand Down Expand Up @@ -320,48 +470,39 @@ def compare_rtc_polarization(pol_list):

return flag_pol_freq_a_equal, flag_pol_freq_b_equal

def compare_rtc_bandwidth(bandwidth_list):
num_input_files = len(bandwidth_list)
bw_freq_a = bandwidth_list[:, 0]
bw_freq_b = bandwidth_list[:, 1]

if np.all(bw_freq_a == bw_freq_a[0]):
flag_bw_freq_a_equal = True
min_bw_freq_a = bw_freq_a[0]
else:
flag_bw_freq_a_equal = False

# Need to identify highest and lowest bandwidth (resolution)
bw_freq_a_valid = bw_freq_a > 5

if bw_freq_a_valid.size > 0:
min_bw_freq_a = np.min(bw_freq_a_valid)
else:
min_bw_freq_a = bw_freq_a[0]

if np.all(bw_freq_b == bw_freq_b[0]):
flag_bw_freq_b_equal = True
min_bw_freq_b = bw_freq_b[0]
else:
flag_bw_freq_b_equal = False

# Need to identify highest and lowest bandwidth (resolution)
bw_freq_b_valid = bw_freq_b > 5

if bw_freq_b_valid.size > 0:
min_bw_freq_b = np.min(bw_freq_b_valid)
else:
min_bw_freq_b = bw_freq_b[0]

def verify_nisar_mode(input_h5_list):
"""Determine the mode of processing for input RTC

return flag_bw_freq_a_equal, flag_bw_freq_b_equal, min_bw_freq_a, min_bw_freq_b
Parameters
----------
input_h5_list : list of strings
input RTC files in a list

def verify_nisar_mode(input_dir_list):
Returns
-------
flag_freq_equal: bool
Flag that indicates whether the frequency groups are equal
among input files
flag_bw_freq_a_equal: bool
Flag which indicates if polarziations of all inputs with respect
to Frequency group A are equal. True = Equal
flag_bw_freq_b_equal: bool
Flag which indicates if polarziations of all inputs with respect
to Frequency group B are equal. True = Equal
freq_list: list
List of frequency group(s) in each of the files
e.g. A, B, or A and B
nisar_uni_mode: bool
If true, processing mode is nisar_uni_mode
No additional processing such as resampling is required. Mosaic operation
will follow.
"""
# Extract Frequency Groups of input files
flag_freq_equal, freq_list = check_rtc_frequency(input_dir_list)
flag_freq_equal, freq_list = check_rtc_frequency(input_h5_list)

# Extract polarizations of each frequency group of the input files
pol_list = read_rtc_polarization(input_dir_list, freq_list)
pol_list = read_rtc_polarization(input_h5_list, freq_list)
hb6688 marked this conversation as resolved.
Show resolved Hide resolved

# Compare polariztions of frequency groups among input files
flag_pol_freq_a_equal, flag_pol_freq_b_equal = compare_rtc_polarization(pol_list)
Expand All @@ -372,14 +513,7 @@ def verify_nisar_mode(input_dir_list):
else:
nisar_uni_mode = False

return flag_freq_equal, flag_pol_freq_a_equal, flag_pol_freq_b_equal, nisar_uni_mode

def find_unique_polarizations(pol_list):
pol_list_flatten = pol_list.copy()
pol_list_flatten = np.concatenate(pol_list_flatten.ravel()).ravel()
pol_list_unique = set(pol_list_flatten)

return pol_list_unique
return flag_freq_equal, flag_pol_freq_a_equal, flag_pol_freq_b_equal, freq_list, nisar_uni_mode


def _find_polarization_from_data_dirs(input_h5_list):
Expand Down Expand Up @@ -691,22 +825,20 @@ def load_from_yaml(cls, yaml_path: str, workflow_name: str, args):
flag_freq_equal,
flag_pol_freq_a_equal,
flag_pol_freq_b_equal,
freq_list,
nisar_uni_mode
) = verify_nisar_mode(input_dir_list)

# Update NiSAR processing mode
algorithm_cfg[
'runconfig']['processing']['nisar_uni_mode'] = nisar_uni_mode

# Extract bandwidth from input RTC
bandwidth_list = extract_bandwidth(input_dir_list)

(
flag_bw_freq_a_equal,
flag_bw_freq_b_equal,
min_bw_freq_a,
min_bw_freq_b
) = compare_rtc_bandwidth(bandwidth_list)
# Determine highest resolution in an RTC input
res_list, res_highest = read_rtc_resolution(input_dir_list, freq_list)
hb6688 marked this conversation as resolved.
Show resolved Hide resolved

algorithm_cfg[
'runconfig']['processing']['mosaic']['resamp_out_res'] = res_highest


log_file = sns.log_file
if args.log_file is not None:
Expand Down
12 changes: 10 additions & 2 deletions src/dswx_sar/mosaic_gcov_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
mosaic_single_output_file)
from dswx_sar.dswx_sar_util import change_epsg_tif
from dswx_sar.dswx_geogrid import DSWXGeogrid
from dswx_sar.dswx_ni_runconfig import _get_parser, RunConfig
from dswx_sar.dswx_ni_runconfig import (_get_parser,
RunConfig,
extract_bandwidth)
from dswx_sar.dswx_sar_util import (_calculate_output_bounds,
_aggregate_10m_to_30m_conv)

Expand Down Expand Up @@ -1012,8 +1014,14 @@ def run(cfg):
mosaic_cfg = processing_cfg.mosaic
mosaic_mode = mosaic_cfg.mosaic_mode
mosaic_prefix = mosaic_cfg.mosaic_prefix
nisar_uni_mode = processing_cfg.nisar_uni_mode

# Determine if resampling is required
Copy link
Contributor

@oberonia78 oberonia78 Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hb6688 I hope you add more descriptions. nisar_uni_mode and resamp_required, and resamp_out_res are determined from dswx_ni_runconfig.py, not from user. I think this should be clarified and mentioned here.

if nisar_uni_mode:
resamp_required = False
else:
resamp_required = True

resamp_required = mosaic_cfg.resamp_required
resamp_method = mosaic_cfg.resamp_method
resamp_out_res = mosaic_cfg.resamp_out_res

Expand Down