Skip to content

Commit 8482314

Browse files
committed
DAS-2368: Modify get_variable_dimension_map to support up-level dimensions
1 parent 1644517 commit 8482314

2 files changed

Lines changed: 49 additions & 6 deletions

File tree

metadata_annotator/history_functions.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ def get_dimension_index_map(
9090
variable_start_indices_map = parse_start_indices_from_history_attr(datatree)
9191

9292
# Retrieve the mapping of requested variables and the corresponding dimension paths
93-
variable_dimension_map = get_variable_dimension_map(granule_var_info)
94-
93+
variable_dimension_map = get_variable_dimension_map(
94+
granule_var_info, datatree, dimension_variables
95+
)
9596
# Retrieve the mapping from the dimension variable to the start index
9697
dimension_index_map = get_dim_index_from_var_dim_map(
9798
variable_dimension_map, variable_start_indices_map
@@ -125,14 +126,49 @@ def parse_start_indices_from_history_attr(datatree: xr.DataTree) -> dict[str, st
125126

126127
def get_variable_dimension_map(
127128
granule_var_info: VarInfoFromNetCDF4,
129+
datatree: xr.DataTree,
130+
dimension_variables: set[str],
128131
) -> dict[tuple, str]:
129-
"""Return a mapping from dimensions list to a requested variable."""
132+
"""Return a mapping from dimensions list to a requested variable.
133+
134+
Note that this function utilizes a temporary solution for location up-level (shared)
135+
dimension variables by updating the dimension map returned by the earthdata-varinfo
136+
method `group_variables_by_dimensions`. This should remain place until
137+
earthdata-varinfo supports up-level dimensions.
138+
"""
130139
var_dim_map = {
131140
dimlist: list(varlist)[0]
132141
for dimlist, varlist in granule_var_info.group_variables_by_dimensions().items()
133142
}
134143

135-
return var_dim_map
144+
# Identify dimensions that have been created up-level and update map
145+
updated_map = {}
146+
for dim_list, var_path in var_dim_map.items():
147+
new_dim_list = []
148+
for dim in dim_list:
149+
if dim in dimension_variables:
150+
new_dim_list.append(dim)
151+
continue
152+
153+
group_path, dimension_name = os.path.split(dim)
154+
new_dim = None
155+
for parent in datatree[group_path].parents:
156+
parent_dim_path = (
157+
f'{parent.path}/{dimension_name}'
158+
if parent.path != '/'
159+
else f'/{dimension_name}'
160+
)
161+
if parent_dim_path in dimension_variables:
162+
new_dim = parent_dim_path
163+
break
164+
165+
if not new_dim:
166+
raise MissingDimensionVariable(dim)
167+
new_dim_list.append(new_dim)
168+
169+
updated_map[tuple(new_dim_list)] = var_path
170+
171+
return updated_map
136172

137173

138174
def get_dim_index_from_var_dim_map(

tests/unit/test_history_functions.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,16 @@ def test_parse_start_indices_from_history_attr(sample_netcdf4_file) -> None:
131131

132132
def test_get_variable_dimension_map() -> None:
133133
"""Ensure that the correct dimensions list is returned for requested variables."""
134-
# with xr.open_datatree('tests/data/SC_SPL3FTP_spatially_subsetted.nc4') as dtree:
135134
granule_varinfo = VarInfoFromNetCDF4(
136135
'tests/data/SC_SPL3FTP_spatially_subsetted.nc4',
137136
short_name='SPL3FTP',
138137
config_file='metadata_annotator/earthdata_varinfo_config.json',
139138
)
140-
variable_dimensions_dict = get_variable_dimension_map(granule_varinfo)
139+
dimension_variables = {
140+
'/Freeze_Thaw_Retrieval_Data_Global/am_pm',
141+
'/Freeze_Thaw_Retrieval_Data_Global/y',
142+
'/Freeze_Thaw_Retrieval_Data_Global/x',
143+
}
141144
expected_dimensions = tuple(
142145
[
143146
'/Freeze_Thaw_Retrieval_Data_Global/am_pm',
@@ -151,6 +154,10 @@ def test_get_variable_dimension_map() -> None:
151154
'/Freeze_Thaw_Retrieval_Data_Global/longitude',
152155
'/Freeze_Thaw_Retrieval_Data_Global/surface_flag',
153156
}
157+
with xr.open_datatree('tests/data/SC_SPL3FTP_spatially_subsetted.nc4') as dtree:
158+
variable_dimensions_dict = get_variable_dimension_map(
159+
granule_varinfo, dtree, dimension_variables
160+
)
154161

155162
assert variable_dimensions_dict[expected_dimensions] in expected_variables
156163

0 commit comments

Comments
 (0)