Skip to content

Commit

Permalink
do not define units if already present
Browse files Browse the repository at this point in the history
  • Loading branch information
garciampred committed Dec 30, 2024
1 parent 7597fde commit 9bdf939
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
13 changes: 7 additions & 6 deletions cdsobs/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cdsobs/cdm/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions tests/test_cdm_api.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"]
)

0 comments on commit 9bdf939

Please sign in to comment.