Skip to content

Commit

Permalink
add docstrings and typing
Browse files Browse the repository at this point in the history
  • Loading branch information
forrestfwilliams committed Jan 17, 2024
1 parent 660cf08 commit 516fe03
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/asf_tools/opera_rgb.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Create a georefernced RGB decomposition RTC image from two input OPERA RTCs"""
import argparse
from pathlib import Path
from typing import Optional

import asf_search
import numpy as np
Expand All @@ -13,7 +14,15 @@
BROWSE_IMAGE_MAX_PERCENTILE = 97


def normalize_browse_image_band(band_image):
def normalize_browse_image_band(band_image: np.ndarray) -> np.ndarray:
"""Normalize a single band of a browse image to remove outliers.
Args:
band_image: A single band numpy array to normalize.
Returns:
A normalized numpy array.
"""
vmin = np.nanpercentile(band_image, BROWSE_IMAGE_MIN_PERCENTILE)
vmax = np.nanpercentile(band_image, BROWSE_IMAGE_MAX_PERCENTILE)

Expand All @@ -26,7 +35,17 @@ def normalize_browse_image_band(band_image):
return band_image


def create_browse_imagery(copol_path, crosspol_path, output_path, alpha_path=None):
def create_decomposition_rgb(
copol_path: str, crosspol_path: str, output_path: str, alpha_path: Optional[str] = None
) -> None:
"""Create a georeferenced RGB decomposition RTC image from co-pol and cross-pol RTCs.
Args:
copol_path: Path to co-pol RTC.
crosspol_path: Path to cross-pol RTC.
output_path: Path to save resulting RGB image to.
alpha_path: Path to alpha band image. If not provided, the alpha band will be the valid data mask.
"""
band_list = [None, None, None]

for filename, is_copol in ((copol_path, True), (crosspol_path, False)):
Expand Down Expand Up @@ -65,7 +84,15 @@ def create_browse_imagery(copol_path, crosspol_path, output_path, alpha_path=Non
output_ds.GetRasterBand(i + 1).WriteArray(image[:, :, i])


def prep_data(granule):
def prep_data(granule: str):
"""Download and prepare the data needed to create an RGB decomposition image.
Args:
granule: OPERA granule name.
Returns:
Tuple of co-pol, cross-pol, and mask filenames, None for each if not available.
"""
result = asf_search.granule_search([granule])[0]
urls = [result.properties['url']]
others = [x for x in result.properties['additionalUrls'] if 'tif' in x]
Expand Down Expand Up @@ -100,4 +127,4 @@ def main():
args = parser.parse_args()

copol, crosspol, mask = prep_data(args.granule[0])
create_browse_imagery(copol, crosspol, args.outpath, mask)
create_decomposition_rgb(copol, crosspol, args.outpath, mask)

0 comments on commit 516fe03

Please sign in to comment.