diff --git a/Makefile.in b/Makefile.in index 8762ab3f..2c4f120b 100644 --- a/Makefile.in +++ b/Makefile.in @@ -245,6 +245,7 @@ test_python: python env TEST_NAME=Test/test_cmor_associated_variable.py make test_a_python env TEST_NAME=Test/test_cmor_frequency_required.py make test_a_python env TEST_NAME=Test/test_cmor_parent_attrs.py make test_a_python + env TEST_NAME=Test/test_cmor_variable_attr_comment.py make test_a_python test_cmip6_cv: python env TEST_NAME=Test/test_python_CMIP6_CV_sub_experimentnotset.py make test_a_python env TEST_NAME=Test/test_python_CMIP6_CV_sub_experimentbad.py make test_a_python diff --git a/Src/cmor_variables.c b/Src/cmor_variables.c index 5b84bb84..037d9fe8 100644 --- a/Src/cmor_variables.c +++ b/Src/cmor_variables.c @@ -7,6 +7,7 @@ #include #include #include +#include float fvalue; @@ -1270,14 +1271,12 @@ int cmor_variable(int *var_id, char *name, char *units, int ndims, if ((comment != NULL) && (comment[0] != '\0') && strcmp(comment, COMMENT_VARIABLE_ZFACTOR) != 0) { if (cmor_has_variable_attribute(vrid, VARIABLE_ATT_COMMENT) == 0) { - char szActivity[CMOR_MAX_STRING]; - - cmor_get_cur_dataset_attribute(GLOBAL_ATT_ACTIVITY_ID, szActivity); + char *table_path = cmor_tables[cmor_vars[vrid].ref_table_id].path; strncpy(msg, comment, CMOR_MAX_STRING); strncat(msg, ", ", CMOR_MAX_STRING - strlen(msg)); - strncat(msg, szActivity, CMOR_MAX_STRING - strlen(msg)); - strncat(msg, "_table_comment: ", CMOR_MAX_STRING - strlen(msg)); + strncat(msg, basename(table_path), CMOR_MAX_STRING - strlen(msg)); + strncat(msg, " comment: ", CMOR_MAX_STRING - strlen(msg)); strncat(msg, refvar.comment, CMOR_MAX_STRING - strlen(msg)); } else { diff --git a/Test/test_cmor_variable_attr_comment.py b/Test/test_cmor_variable_attr_comment.py new file mode 100644 index 00000000..2599d000 --- /dev/null +++ b/Test/test_cmor_variable_attr_comment.py @@ -0,0 +1,56 @@ +import json +import cmor +import unittest +import numpy +from pathlib import Path +from base_CMIP6_CV import BaseCVsTest + +from netCDF4 import Dataset + + +class TestVariableAttributeComment(BaseCVsTest): + + def test_variable_comment_from_user(self): + user_comment = "Comment from the user" + table_comment = "Temperature of the lower boundary of the atmosphere" + table = "CMIP6_Amon.json" + + cmor.setup(inpath='Tables', + netcdf_file_action=cmor.CMOR_REPLACE_4, + logfile=self.tmpfile) + + cmor.dataset_json("Test/CMOR_input_example.json") + cmor.load_table(table) + + itime = cmor.axis(table_entry='time', + units='days since 2000-01-01 00:00:00') + ilat = cmor.axis(table_entry='latitude', + units='degrees_north', + coord_vals=[0], + cell_bounds=[-1, 1]) + ilon = cmor.axis(table_entry='longitude', + units='degrees_east', + coord_vals=[90], + cell_bounds=[89, 91]) + + axis_ids = [itime, ilat, ilon] + + varid = cmor.variable('ts', 'K', axis_ids, comment=user_comment) + + _ = cmor.write(varid, [273, 273, 273], ntimes_passed=1, + time_vals=[15], time_bnds=[10, 20]) + filename = cmor.close(varid, file_name=True) + self.assertEqual(cmor.close(), 0) + + ds = Dataset(filename) + var = ds.variables["ts"] + var_comment = var.getncattr("comment") + + expected_comment = f"{user_comment}, {table} comment: {table_comment}" + self.assertEqual(var_comment, expected_comment) + + ds.close() + + +if __name__ == '__main__': + unittest.main() diff --git a/ci-support/test-wheel.sh b/ci-support/test-wheel.sh index 50af2096..8b02c26e 100755 --- a/ci-support/test-wheel.sh +++ b/ci-support/test-wheel.sh @@ -210,6 +210,8 @@ wheel_python_tests=( "Test/test_cmor_chunking.py" "Test/test_cmor_associated_variable.py" "Test/test_cmor_frequency_required.py" + "Test/test_cmor_parent_attrs.py" + "Test/test_cmor_variable_attr_comment.py" "Test/test_python_CMIP6_CV_sub_experimentnotset.py" "Test/test_python_CMIP6_CV_sub_experimentbad.py" "Test/test_python_CMIP6_CV_furtherinfourl.py"