Skip to content

Commit

Permalink
simplify gslc s3 search interface, fix pycharm warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jtherrmann committed Oct 4, 2024
1 parent 762f240 commit d880b47
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
52 changes: 26 additions & 26 deletions src/hyp3_srg/time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,26 @@ def load_products(uris: Iterable[str], overwrite: bool = False):
return granule_names


def get_size_from_dem(dem_file: str) -> tuple[int]:
def get_size_from_dem(dem_path: str) -> tuple[int, int]:
"""Get the length and width from a .rsc DEM file
Args:
dem_file: path to the .rsc dem file.
dem_path: path to the .rsc dem file.
Returns:
dem_width, dem_length: tuple containing the dem width and dem length
"""
with open(dem_file) as dem:
width_line = dem.readline()
with open(dem_path) as dem_file:
width_line = dem_file.readline()
dem_width = width_line.split()[1]
length_line = dem.readline()
length_line = dem_file.readline()
dem_length = length_line.split()[1]

return int(dem_width), int(dem_length)


def generate_wrapped_interferograms(
looks: tuple[int], baselines: tuple[int], dem_shape: tuple[int], work_dir: Path
looks: tuple[int, int], baselines: tuple[int, int], dem_shape: tuple[int, int], work_dir: Path
) -> None:
"""Generates wrapped interferograms from GSLCs
Expand All @@ -113,7 +113,7 @@ def generate_wrapped_interferograms(
utils.call_stanford_module('sentinel/ps_sbas_igrams.py', args=sbas_args, work_dir=work_dir)


def unwrap_interferograms(dem_shape: tuple[int], unw_shape: tuple[int], work_dir: Path) -> None:
def unwrap_interferograms(dem_shape: tuple[int, int], unw_shape: tuple[int, int], work_dir: Path) -> None:
"""Unwraps wrapped interferograms in parallel
Args:
Expand All @@ -130,7 +130,7 @@ def unwrap_interferograms(dem_shape: tuple[int], unw_shape: tuple[int], work_dir


def compute_sbas_velocity_solution(
threshold: float, do_tropo_correction: bool, unw_shape: tuple[int], work_dir: Path
threshold: float, do_tropo_correction: bool, unw_shape: tuple[int, int], work_dir: Path
) -> None:
"""Computes the sbas velocity solution from the unwrapped interferograms
Expand All @@ -153,11 +153,9 @@ def compute_sbas_velocity_solution(
tropo_correct_args = ['unwlist', unw_width, unw_length]
utils.call_stanford_module('int/tropocorrect.py', args=tropo_correct_args, work_dir=work_dir)

num_unw_files = 0
with open(work_dir / 'unwlist', 'r') as unw_list:
num_unw_files = len(unw_list.readlines())

num_slcs = 0
with open(work_dir / 'geolist', 'r') as slc_list:
num_slcs = len(slc_list.readlines())

Expand All @@ -166,8 +164,8 @@ def compute_sbas_velocity_solution(


def create_time_series(
looks: tuple[int] = (10, 10),
baselines: tuple[int] = (1000, 1000),
looks: tuple[int, int] = (10, 10),
baselines: tuple[int, int] = (1000, 1000),
threshold: float = 0.5,
do_tropo_correction: bool = True,
work_dir: Path | None = None,
Expand All @@ -184,7 +182,7 @@ def create_time_series(
dem_shape = get_size_from_dem('elevation.dem.rsc')
generate_wrapped_interferograms(looks=looks, baselines=baselines, dem_shape=dem_shape, work_dir=work_dir)

unw_shape = get_size_from_dem(work_dir / 'dem.rsc')
unw_shape = get_size_from_dem(str(work_dir / 'dem.rsc'))
unwrap_interferograms(dem_shape=dem_shape, unw_shape=unw_shape, work_dir=work_dir)

compute_sbas_velocity_solution(
Expand All @@ -199,7 +197,7 @@ def create_time_series_product_name(
"""Create a product name for the given granules.
Args:
granules: list of the granule names
granule_names: list of the granule names
bounds: bounding box that was used to generate the GSLCs
Returns:
Expand Down Expand Up @@ -276,7 +274,7 @@ def package_time_series(
'velocity',
]
[shutil.copy(sbas_dir / f, product_path / f) for f in to_keep]
shutil.make_archive(product_path, 'zip', product_path)
shutil.make_archive(str(product_path), 'zip', product_path)
return zip_path


Expand All @@ -285,7 +283,6 @@ def time_series(
bounds: list[float],
bucket: str = None,
bucket_prefix: str = '',
gslc_bucket: str = None,
gslc_bucket_prefix: str = '',
work_dir: Optional[Path] = None,
) -> None:
Expand All @@ -296,8 +293,7 @@ def time_series(
bounds: bounding box that was used to generate the GSLCs
bucket: AWS S3 bucket for uploading the final product(s)
bucket_prefix: Add a bucket prefix to the product(s)
gslc_bucket: AWS S3 bucket containing GSLCs for time-series processing
gslc_bucket_prefix: Path to GSLCs within gslc_bucket.
gslc_bucket_prefix: GSLCs are found at bucket_prefix/gslc_bucket_prefix within bucket
work_dir: Working directory for processing
"""
if work_dir is None:
Expand All @@ -306,13 +302,14 @@ def time_series(
if not sbas_dir.exists():
mkdir(sbas_dir)

if granules and gslc_bucket:
raise ValueError('One of a list of granules or a s3 bucket must be provided, but got both.')
if granules and gslc_bucket_prefix:
raise ValueError('One of a list of granules or a GSLC S3 bucket prefix must be provided, but got both.')

if granules == []:
if gslc_bucket is None:
raise ValueError('Either a list of granules or a s3 bucket must be provided, but got neither.')
granules = get_gslc_uris_from_s3(gslc_bucket, gslc_bucket_prefix)
if not granules:
if gslc_bucket_prefix is None:
raise ValueError('Either a list of granules or a GSLC S3 bucket prefix must be provided, but got neither.')
# TODO: check that bucket and bucket_prefix were passed
granules = get_gslc_uris_from_s3(bucket, f'{bucket_prefix}/{gslc_bucket_prefix}')

granule_names = load_products(granules)
dem_path = dem.download_dem_for_srg(bounds, work_dir)
Expand Down Expand Up @@ -343,8 +340,11 @@ def main():
)
parser.add_argument('--bucket', help='AWS S3 bucket HyP3 for upload the final product(s)')
parser.add_argument('--bucket-prefix', default='', help='Add a bucket prefix to product(s)')
parser.add_argument('--gslc-bucket', help='AWS S3 bucket containing GSLCs to process')
parser.add_argument('--gslc-bucket-prefix', default='', help='Path to GSLCs within gslc-bucket.')
parser.add_argument(
'--gslc-bucket-prefix',
default='',
help='GSLCs are found at bucket-prefix/gslc-bucket_prefix within bucket'
)
parser.add_argument('granules', type=str.split, nargs='*', default='', help='GSLC granules.')
args = parser.parse_args()
args.granules = [item for sublist in args.granules for item in sublist]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_get_size_from_dem(tmp_path):
rsc_path = tmp_path / 'elevation.dem.rsc'
with open(rsc_path, 'w') as rsc_file:
rsc_file.write(rsc_content.strip())
dem_width, dem_height = time_series.get_size_from_dem(dem_file=rsc_path)
dem_width, dem_height = time_series.get_size_from_dem(dem_path=rsc_path)
assert dem_width, dem_height == (1235, 873)


Expand Down

0 comments on commit d880b47

Please sign in to comment.