-
Notifications
You must be signed in to change notification settings - Fork 4
DAS-2300 - provides spatial subsetting support for 3d variables in SPL3SMP #30
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
eee7180
d657053
d9f8165
a420d75
e838e46
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 |
---|---|---|
@@ -1 +1 @@ | ||
1.1.4 | ||
1.1.5 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,16 +86,17 @@ def any_absent_dimension_variables(varinfo: VarInfoFromDmr, variable: str) -> bo | |
def get_dimension_array_names( | ||
varinfo: VarInfoFromDmr, | ||
variable_name: str, | ||
) -> list[str]: | ||
) -> dict[str:str]: | ||
""" | ||
Returns the dimensions names from coordinate variables or from | ||
configuration | ||
|
||
""" | ||
variable = varinfo.get_variable(variable_name) | ||
if variable is None: | ||
return [] | ||
return {} | ||
|
||
dimension_names = variable.dimensions | ||
configured_dimensions = variable.dimensions | ||
dimension_names = get_configured_dimension_order(varinfo, configured_dimensions) | ||
|
||
if len(dimension_names) >= 2: | ||
return dimension_names | ||
|
@@ -107,26 +108,25 @@ def get_dimension_array_names( | |
# Given variable has coordinates: use latitude coordinate | ||
# to define variable spatial dimensions. | ||
if len(latitude_coordinates) == 1 and len(longitude_coordinates) == 1: | ||
dimension_array_names = create_spatial_dimension_names_from_coordinates( | ||
dimension_names = create_spatial_dimension_names_from_coordinates( | ||
varinfo, latitude_coordinates[0] | ||
) | ||
|
||
# Given variable variable has no coordinate attribute itself, | ||
# but is itself a coordinate (latitude or longitude): | ||
# use as a coordinate to define spatial dimensions | ||
elif variable.is_latitude() or variable.is_longitude(): | ||
dimension_array_names = create_spatial_dimension_names_from_coordinates( | ||
dimension_names = create_spatial_dimension_names_from_coordinates( | ||
varinfo, variable_name | ||
) | ||
else: | ||
dimension_array_names = [] | ||
|
||
return dimension_array_names | ||
dimension_names = {} | ||
return dimension_names | ||
|
||
|
||
def create_spatial_dimension_names_from_coordinates( | ||
varinfo: VarInfoFromDmr, variable_name: str | ||
) -> str: | ||
) -> dict[str:str]: | ||
"""returns the x-y variable names that would | ||
match the group of the input variable. The 'dim_y' dimension | ||
and 'dim_x' names are returned with the group pathname | ||
|
@@ -135,22 +135,21 @@ def create_spatial_dimension_names_from_coordinates( | |
variable = varinfo.get_variable(variable_name) | ||
|
||
if variable is not None: | ||
D-Auty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
dimension_array_names = [ | ||
f'{variable.group_path}/dim_y', | ||
f'{variable.group_path}/dim_x', | ||
] | ||
dimension_names = { | ||
'projection_y_coordinate': f'{variable.group_path}/y_dim', | ||
'projection_x_coordinate': f'{variable.group_path}/x_dim', | ||
} | ||
else: | ||
raise MissingVariable(variable_name) | ||
|
||
return dimension_array_names | ||
return dimension_names | ||
|
||
|
||
def create_dimension_arrays_from_coordinates( | ||
prefetch_dataset: Dataset, | ||
latitude_coordinate: VariableFromDmr, | ||
longitude_coordinate: VariableFromDmr, | ||
crs: CRS, | ||
projected_dimension_names: list[str], | ||
dimension_names: dict[str, str], | ||
) -> dict[str, np.ndarray]: | ||
"""Generate artificial 1D dimensions scales for each | ||
2D dimension or coordinate variable. | ||
|
@@ -159,9 +158,11 @@ def create_dimension_arrays_from_coordinates( | |
3) Generate the x-y dimscale array and return to the calling method | ||
|
||
""" | ||
if len(projected_dimension_names) < 2: | ||
raise InvalidDimensionNames(projected_dimension_names) | ||
# dimension_names = get_dimension_array_names(varinfo, variable_name) | ||
if len(dimension_names) < 2: | ||
raise InvalidDimensionNames(dimension_names) | ||
|
||
# check if the dimension names are configured in hoss_config | ||
lat_arr = get_2d_coordinate_array( | ||
prefetch_dataset, | ||
latitude_coordinate.full_name_path, | ||
|
@@ -171,10 +172,12 @@ def create_dimension_arrays_from_coordinates( | |
longitude_coordinate.full_name_path, | ||
) | ||
|
||
# get the max spread x and y indices | ||
row_indices, col_indices = get_valid_sample_pts( | ||
lat_arr, lon_arr, latitude_coordinate, longitude_coordinate | ||
) | ||
|
||
# get the dimension order from the coordinate data | ||
dim_order_is_y_x, row_dim_values = get_dimension_order_and_dim_values( | ||
lat_arr, lon_arr, row_indices, crs, is_row=True | ||
) | ||
D-Auty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
@@ -188,18 +191,16 @@ def create_dimension_arrays_from_coordinates( | |
lat_arr, lon_arr, dim_order_is_y_x | ||
) | ||
|
||
# calculate the dimension values | ||
y_dim = interpolate_dim_values_from_sample_pts( | ||
row_dim_values, np.transpose(row_indices)[0], row_size | ||
) | ||
|
||
x_dim = interpolate_dim_values_from_sample_pts( | ||
col_dim_values, np.transpose(col_indices)[1], col_size | ||
) | ||
|
||
projected_y, projected_x = ( | ||
projected_dimension_names[-2], | ||
projected_dimension_names[-1], | ||
) | ||
projected_y = dimension_names['projection_y_coordinate'] | ||
projected_x = dimension_names['projection_x_coordinate'] | ||
|
||
if dim_order_is_y_x: | ||
return {projected_y: y_dim, projected_x: x_dim} | ||
|
@@ -208,6 +209,23 @@ def create_dimension_arrays_from_coordinates( | |
# return {projected_x: x_dim, projected_y: y_dim} | ||
|
||
|
||
def get_configured_dimension_order( | ||
varinfo: VarInfoFromDmr, dimension_names: list[str] | ||
) -> dict[str, str]: | ||
"""This function returns the dimension order in a dictionary | ||
with standard_names that is used to define the dimensions e.g. | ||
'projection_x_coordinate' and 'projection_y_coordinate' if they | ||
are configured in hoss_config.json | ||
|
||
""" | ||
dimension_name_order = {} | ||
for dimension_name in dimension_names: | ||
attrs = varinfo.get_missing_variable_attributes(dimension_name) | ||
if 'standard_name' in attrs.keys(): | ||
dimension_name_order[attrs['standard_name']] = dimension_name | ||
return dimension_name_order | ||
|
||
|
||
def get_2d_coordinate_array( | ||
prefetch_dataset: Dataset, | ||
coordinate_name: str, | ||
|
@@ -445,27 +463,30 @@ def interpolate_dim_values_from_sample_pts( | |
def create_dimension_arrays_from_geotransform( | ||
prefetch_dataset: Dataset, | ||
latitude_coordinate: VariableFromDmr, | ||
projected_dimension_names: list[str], | ||
geotranform, | ||
projected_dimension_names: dict[str, str], | ||
geotransform, | ||
D-Auty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) -> 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, col, 0) for col in range(lat_arr.shape[-1]) | ||
col_row_to_xy(geotransform, col, 0) for col in range(lat_arr.shape[-1]) | ||
] | ||
row_dimensions = [ | ||
col_row_to_xy(geotranform, 0, row) for row in range(lat_arr.shape[-2]) | ||
col_row_to_xy(geotransform, 0, row) for row in range(lat_arr.shape[-2]) | ||
] | ||
|
||
# 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 = projected_dimension_names[-2:] | ||
|
||
projected_y = projected_dimension_names['projection_y_coordinate'] | ||
projected_x = projected_dimension_names['projection_x_coordinate'] | ||
|
||
return {projected_y: y_values, projected_x: x_values} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.