Skip to content

Commit

Permalink
fix the case when an auxiliary variable is requested (#186)
Browse files Browse the repository at this point in the history
without asking for the main regular variable related it. Now the regular variable is added to the request.
  • Loading branch information
garciampred authored Jul 29, 2024
1 parent cb30ccc commit 34731d8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
16 changes: 15 additions & 1 deletion cads_adaptors/adaptors/cadsobs/adaptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,21 @@ def handle_auxiliary_variables(
)
requested_variables.remove(auxvar)
requested_metadata_fields.add(metadata_field)

# Check that there are no orphan auxiliary variables without its regular
# variable and if any, add the regular variable
inverse_aux_var_mapping = {
auxvar_dict["auxvar"]: regular_var
for regular_var, auxiliary_vars in aux_var_mapping.items()
for auxvar_dict in auxiliary_vars
}
for auxvar in auxiliary_variables:
regular_variable = inverse_aux_var_mapping[auxvar]
if regular_variable not in requested_variables:
self.context.warning(
f"{auxvar} is auxiliary metadata of variable {regular_variable}, "
f"adding ir to the request."
)
requested_variables.append(regular_variable)
mapped_request["variables"] = requested_variables
return requested_metadata_fields

Expand Down
10 changes: 8 additions & 2 deletions tests/test_cadsobs_adaptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ def get_objects_to_retrieve(
"_timestamp": str(time.time()),
}

TEST_REQUEST_ORPHAN_AUXVAR = TEST_REQUEST.copy()
TEST_REQUEST_ORPHAN_AUXVAR.update(
dict(variable=["maximum_air_temperature_negative_total_uncertainty"])
)

TEST_ADAPTOR_CONFIG = {
"entry_point": "cads_adaptors:ObservationsAdaptor",
"collection_id": "insitu-observations-near-surface-temperature-us-climate-reference-network",
Expand Down Expand Up @@ -208,7 +213,8 @@ def get_objects_to_retrieve(
}


def test_adaptor(tmp_path, monkeypatch):
@pytest.mark.parametrize("test_request", [TEST_REQUEST, TEST_REQUEST_ORPHAN_AUXVAR])
def test_adaptor(tmp_path, monkeypatch, test_request):
monkeypatch.setattr(
"cads_adaptors.adaptors.cadsobs.adaptor.CadsobsApiClient",
MockerCadsobsApiClient,
Expand All @@ -218,7 +224,7 @@ def test_adaptor(tmp_path, monkeypatch):
# is not needed for this test

adaptor = ObservationsAdaptor(test_form, **TEST_ADAPTOR_CONFIG)
result = adaptor.retrieve(TEST_REQUEST)
result = adaptor.retrieve(test_request)
tempfile = Path(tmp_path, "test_adaptor.nc")
with tempfile.open("wb") as tmpf:
tmpf.write(result.read())
Expand Down

0 comments on commit 34731d8

Please sign in to comment.