-
Notifications
You must be signed in to change notification settings - Fork 4
DAS-2278 - Handle spatial subsetting in products with all fills in lat/lon coordinates #26
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
Changes from 4 commits
dc28ad8
7b581ec
70ac694
2296395
8fb7cf6
e0b43b6
7d5e34c
a43c291
1b23ebd
2443ed8
5a02020
69fbe69
beb8658
56d64a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -405,3 +405,42 @@ def interpolate_dim_values_from_sample_pts( | |
dim_max = dim_values[1] + (dim_resolution * (dim_size - 1 - dim_indices[1])) | ||
|
||
return np.linspace(dim_min, dim_max, dim_size) | ||
|
||
|
||
def create_dimension_arrays_from_geotransform( | ||
prefetch_dataset: Dataset, | ||
latitude_coordinate: VariableFromDmr, | ||
projected_dimension_names: list[str], | ||
geotranform, | ||
) -> dict[str, np.ndarray]: | ||
"""Generate artificial 1D dimensions scales from geotransform""" | ||
lat_arr = get_2d_coordinate_array( | ||
prefetch_dataset, | ||
latitude_coordinate.full_name_path, | ||
) | ||
|
||
# compute the x,y locations along a column and row | ||
column_dimensions = [ | ||
col_row_to_xy(geotranform, i, 0) for i in range(lat_arr.shape[1]) | ||
] | ||
row_dimensions = [col_row_to_xy(geotranform, 0, i) for i in range(lat_arr.shape[0])] | ||
|
||
# pull out dimension values | ||
x_values = np.array([x for x, y in column_dimensions], dtype=np.float64) | ||
y_values = np.array([y for x, y in row_dimensions], dtype=np.float64) | ||
projected_y, projected_x = tuple(projected_dimension_names) | ||
|
||
|
||
return {projected_y: y_values, projected_x: x_values} | ||
|
||
|
||
def col_row_to_xy(geotransform, col: int, row: int) -> tuple[np.float64, np.float64]: | ||
"""Convert grid cell location to x,y coordinate.""" | ||
# Geotransform is defined from upper left corner as (0,0), so adjust | ||
# input value to the center of grid at (.5, .5) | ||
adj_col = col + 0.5 | ||
adj_row = row + 0.5 | ||
|
||
x = geotransform[0] + adj_col * geotransform[1] + adj_row * geotransform[2] | ||
y = geotransform[3] + adj_col * geotransform[4] + adj_row * geotransform[5] | ||
|
||
return x, y |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -119,13 +119,27 @@ | |||||
{ | ||||||
"Applicability": { | ||||||
"Mission": "SMAP", | ||||||
"ShortNamePath": "SPL3FT(P|P_E)", | ||||||
"ShortNamePath": "SPL3FTP", | ||||||
"VariablePattern": "(?i).*polar.*" | ||||||
|
"VariablePattern": "(?i).*polar.*" | |
"VariablePattern": "(?i).*Polar.*" |
Well, I look dumb, the (?i) is the case insensitive flag..
That aside, if this is only for SPL3FTP, the group is defined as Freeze_Thaw_Retrieval_Data_Polar
and I'm assuming (from looking at the file) the variables don't have the name polar except in their fully qualified path.
e.g.
Variable full name: Freeze_Thaw_Retrieval_Data_Polar/open_water_body_fraction
So why not just be explicit and make that
"VariablePattern": "Freeze_Thaw_Retrieval_Data_Polar.*"
Would that work and be clearer?
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 agree this would make it more clear. I've updated this in 7d5e34c. A leading slash is required so I decided to use:
"VariablePattern": "/Freeze_Thaw_Retrieval_Data_Polar/.*"
Outdated
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.
same comment as before
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.
Updated in 7d5e34c
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,10 +40,17 @@ | |
Shape = Union[LineString, Point, Polygon, MultiShape] | ||
|
||
|
||
def get_variable_crs(variable: str, varinfo: VarInfoFromDmr) -> CRS: | ||
def get_variable_crs(cf_attributes: str) -> CRS: | ||
"""Create a `pyproj.CRS` object from the grid mapping variable metadata | ||
attributes. | ||
|
||
""" | ||
return CRS.from_cf(cf_attributes) | ||
|
||
|
||
|
||
def get_grid_mapping_attributes(variable: str, varinfo: VarInfoFromDmr) -> CRS: | ||
"""Check the metadata attributes for the variable to find the associated | ||
grid mapping variable. Create a `pyproj.CRS` object from the grid | ||
mapping variable metadata attributes. | ||
grid mapping variable. | ||
|
||
All metadata attributes that contain references from one variable to | ||
another are stored in the `Variable.references` dictionary attribute | ||
|
@@ -70,7 +77,7 @@ def get_variable_crs(variable: str, varinfo: VarInfoFromDmr) -> CRS: | |
cf_attributes = varinfo.get_missing_variable_attributes(grid_mapping) | ||
D-Auty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if cf_attributes: | ||
crs = CRS.from_cf(cf_attributes) | ||
return cf_attributes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was looking at this and going to complain that the complexity is a little out of hand, but I see that none of it was you... So this seem fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a thought for optimization if grid_mapping_variable.attributes and varinfo.get_missing_variable_attributes() always return value
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks fine now. |
||
else: | ||
raise MissingGridMappingVariable(grid_mapping, variable) | ||
|
||
|
@@ -80,8 +87,6 @@ def get_variable_crs(variable: str, varinfo: VarInfoFromDmr) -> CRS: | |
else: | ||
raise MissingGridMappingMetadata(variable) | ||
|
||
return crs | ||
|
||
|
||
def get_projected_x_y_variables( | ||
varinfo: VarInfoFromDmr, variable: str | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ | |
) | ||
from hoss.coordinate_utilities import ( | ||
create_dimension_arrays_from_coordinates, | ||
create_dimension_arrays_from_geotransform, | ||
get_coordinate_variables, | ||
get_dimension_array_names_from_coordinate_variables, | ||
get_variables_with_anonymous_dims, | ||
|
@@ -49,6 +50,7 @@ | |
get_dimension_index_range, | ||
) | ||
from hoss.projection_utilities import ( | ||
get_grid_mapping_attributes, | ||
get_projected_x_y_extents, | ||
get_projected_x_y_variables, | ||
get_variable_crs, | ||
|
@@ -183,7 +185,10 @@ def get_projected_x_y_index_ranges( | |
and projected_y is not None | ||
and not set((projected_x, projected_y)).issubset(set(index_ranges.keys())) | ||
): | ||
crs = get_variable_crs(non_spatial_variable, varinfo) | ||
grid_mapping_attributes = get_grid_mapping_attributes( | ||
non_spatial_variable, varinfo | ||
) | ||
crs = get_variable_crs(grid_mapping_attributes) | ||
|
||
x_y_extents = get_projected_x_y_extents( | ||
dimensions_file[projected_x][:], | ||
|
@@ -245,19 +250,27 @@ def get_x_y_index_ranges_from_coordinates( | |
|
||
""" | ||
|
||
crs = get_variable_crs(non_spatial_variable, varinfo) | ||
grid_mapping_attributes = get_grid_mapping_attributes(non_spatial_variable, varinfo) | ||
crs = get_variable_crs(grid_mapping_attributes) | ||
|
||
projected_dimension_names = get_dimension_array_names_from_coordinate_variables( | ||
varinfo, non_spatial_variable | ||
) | ||
|
||
dimension_arrays = create_dimension_arrays_from_coordinates( | ||
prefetch_coordinate_datasets, | ||
latitude_coordinate, | ||
longitude_coordinate, | ||
crs, | ||
projected_dimension_names, | ||
) | ||
if 'master_geotransform' in grid_mapping_attributes: | ||
|
||
dimension_arrays = create_dimension_arrays_from_geotransform( | ||
prefetch_coordinate_datasets, | ||
latitude_coordinate, | ||
projected_dimension_names, | ||
grid_mapping_attributes['master_geotransform'], | ||
) | ||
else: | ||
dimension_arrays = create_dimension_arrays_from_coordinates( | ||
prefetch_coordinate_datasets, | ||
latitude_coordinate, | ||
longitude_coordinate, | ||
crs, | ||
projected_dimension_names, | ||
) | ||
|
||
projected_y, projected_x = dimension_arrays.keys() | ||
|
||
|
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.
This is
probablya massive nit. but can you use different temp vars in your comprehensions? I would use i, and j and follow the standard conventions, or I'd probably just use row and col.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 should go look and see if this was in my code too :awkwardsockmonkey:
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.
also just so I understand, this routine doesn't need lat_arr, an array of the latitudes, it just needs the shape of that variable? is there a way to get that without reading the whole array?
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.
There isn't a current way I'm aware of to get the shape of the variable without reading the whole array. DAS-2287 is addressing this.
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.
Updated to use 'row' and 'col' in 7d5e34c
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.
looks good