From 9bdf939ee8b6da891f42201f90f72583ab70daa2 Mon Sep 17 00:00:00 2001 From: garciam Date: Mon, 30 Dec 2024 11:53:26 +0100 Subject: [PATCH] do not define units if already present --- cdsobs/api.py | 13 +++++++------ cdsobs/cdm/api.py | 2 +- tests/test_cdm_api.py | 6 +++--- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/cdsobs/api.py b/cdsobs/api.py index 2252937..ddc7598 100644 --- a/cdsobs/api.py +++ b/cdsobs/api.py @@ -8,8 +8,8 @@ from sqlalchemy.orm import Session from cdsobs.cdm.api import ( - apply_unit_changes, check_cdm_compliance, + define_units, ) from cdsobs.config import CDSObsConfig, DatasetConfig from cdsobs.ingestion.api import ( @@ -294,11 +294,12 @@ def _read_homogenise_and_partition( # Check CDM compliance check_cdm_compliance(homogenised_data, dataset_metadata.cdm_tables) # Apply unit changes - homogenised_data = apply_unit_changes( - homogenised_data, - service_definition.sources[source], - dataset_metadata.cdm_code_tables["observed_variable"], - ) + if "units" not in homogenised_data.columns: + homogenised_data = define_units( + homogenised_data, + service_definition.sources[source], + dataset_metadata.cdm_code_tables["observed_variable"], + ) year = time_space_batch.time_batch.year lon_tile_size = dataset_config.get_tile_size("lon", source, year) lat_tile_size = dataset_config.get_tile_size("lat", source, year) diff --git a/cdsobs/cdm/api.py b/cdsobs/cdm/api.py index 1fba05a..7919c5b 100644 --- a/cdsobs/cdm/api.py +++ b/cdsobs/cdm/api.py @@ -328,7 +328,7 @@ def check_cdm_compliance( return table_field_mappings -def apply_unit_changes( +def define_units( homogenised_data: pandas.DataFrame, source_definition: SourceDefinition, cdm_variable_table: CDMCodeTable, diff --git a/tests/test_cdm_api.py b/tests/test_cdm_api.py index 3f27f1f..ac168a6 100644 --- a/tests/test_cdm_api.py +++ b/tests/test_cdm_api.py @@ -1,8 +1,8 @@ import pytest from cdsobs.cdm.api import ( - apply_unit_changes, check_cdm_compliance, + define_units, read_cdm_code_tables, ) from cdsobs.cdm.tables import read_cdm_tables @@ -49,13 +49,13 @@ def test_apply_variable_unit_change(test_config): ) source_definition = service_definition.sources[source] cdm_code_tables = read_cdm_code_tables(test_config.cdm_tables_location) - actual = apply_unit_changes( + actual = define_units( homogenised_data, source_definition, cdm_code_tables["observed_variable"] ) assert "original_units" in actual and "units" in actual and len(actual) > 0 with pytest.raises(RuntimeError): source_definition.descriptions["geopotential_height"].units = "wrong" - apply_unit_changes( + define_units( homogenised_data, source_definition, cdm_code_tables["observed_variable"] )