-
Notifications
You must be signed in to change notification settings - Fork 5
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
Create an OPERA RGB workflow #221
Open
forrestfwilliams
wants to merge
7
commits into
develop
Choose a base branch
from
color
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cd756d6
create first version of RGB workflow
forrestfwilliams eb54cce
update changelog
forrestfwilliams 660cf08
add ability to grab mask data from OPERA RTC products
forrestfwilliams 516fe03
add docstrings and typing
forrestfwilliams c11d8db
remove un-needed variables
forrestfwilliams 1140622
make percentiles function args
forrestfwilliams 9d02e90
add code to make GIBS-compatible tifs
forrestfwilliams File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ dependencies: | |
- pytest-cov | ||
# For running | ||
- astropy | ||
- asf_search | ||
- boto3 | ||
- fiona | ||
- gdal>=3.7 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
"""Create a georefernced RGB decomposition RTC image from two input OPERA RTCs""" | ||
import argparse | ||
from pathlib import Path | ||
|
||
import asf_search | ||
import numpy as np | ||
from osgeo import gdal | ||
|
||
|
||
gdal.UseExceptions() | ||
|
||
BROWSE_IMAGE_MIN_PERCENTILE = 3 | ||
BROWSE_IMAGE_MAX_PERCENTILE = 97 | ||
|
||
|
||
def normalize_browse_image_band(band_image): | ||
vmin = np.nanpercentile(band_image, BROWSE_IMAGE_MIN_PERCENTILE) | ||
vmax = np.nanpercentile(band_image, BROWSE_IMAGE_MAX_PERCENTILE) | ||
|
||
# gamma correction: 0.5 | ||
is_not_negative = band_image - vmin >= 0 | ||
is_negative = band_image - vmin < 0 | ||
band_image[is_not_negative] = np.sqrt((band_image[is_not_negative] - vmin) / (vmax - vmin)) | ||
band_image[is_negative] = 0 | ||
band_image = np.clip(band_image, 0, 1) | ||
return band_image | ||
|
||
|
||
def create_browse_imagery(copol_path, crosspol_path, output_path, alpha_channel=None): | ||
band_list = [None, None, None] | ||
|
||
for filename, is_copol in ((copol_path, True), (crosspol_path, False)): | ||
gdal_ds = gdal.Open(filename, gdal.GA_ReadOnly) | ||
|
||
gdal_band = gdal_ds.GetRasterBand(1) | ||
band_image = np.asarray(gdal_band.ReadAsArray(), dtype=np.float32) | ||
|
||
if is_copol: | ||
# Using copol as template for output | ||
is_valid = np.isfinite(band_image) | ||
if alpha_channel is None: | ||
alpha_channel = np.asarray(is_valid, dtype=np.float32) | ||
geotransform = gdal_ds.GetGeoTransform() | ||
projection = gdal_ds.GetProjection() | ||
shape = band_image.shape | ||
band_list_index = [0, 2] | ||
else: | ||
band_list_index = [1] | ||
|
||
normalized_image = normalize_browse_image_band(band_image) | ||
for index in band_list_index: | ||
band_list[index] = normalized_image | ||
|
||
image = np.dstack((band_list[0], band_list[1], band_list[2], alpha_channel)) | ||
|
||
driver = gdal.GetDriverByName('GTiff') | ||
output_ds = driver.Create(output_path, shape[1], shape[0], 4, gdal.GDT_Float32) | ||
output_ds.SetGeoTransform(geotransform) | ||
output_ds.SetProjection(projection) | ||
for i in range(4): | ||
output_ds.GetRasterBand(i + 1).WriteArray(image[:, :, i]) | ||
|
||
|
||
def prep_data(granule): | ||
result = asf_search.granule_search([granule])[0] | ||
urls = [result.properties['url']] | ||
others = [x for x in result.properties['additionalUrls'] if 'tif' in x] | ||
urls += others | ||
asf_search.download_urls(urls, path='.') | ||
names = [Path(x).name for x in urls] | ||
for name in names: | ||
image_type = name.split('_')[-1].split('.')[0] | ||
if image_type in ['VV', 'HH']: | ||
copol = name | ||
elif image_type in ['VH', 'HV']: | ||
crosspol = name | ||
elif image_type == 'mask': | ||
mask = name | ||
return copol, crosspol, mask | ||
|
||
|
||
def main(): | ||
"""Entrypoint for OPERA RTC decomposition image creation.""" | ||
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter) | ||
parser.add_argument('granule', nargs=1, default=None, help='OPERA granule name') | ||
parser.add_argument('--outpath', default='rgb.tif', help='Path to save resulting RGB image to') | ||
args = parser.parse_args() | ||
|
||
copol, crosspol, _ = prep_data(args.granule[0]) | ||
create_browse_imagery(Path(copol), Path(crosspol), Path(args.outpath)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd make more sense to make these arguments to
normalize_browse_image_band
with default values than hard coded constants