Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions Src/cmor_variables.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <stdlib.h>
#include <math.h>
#include <signal.h>
#include <libgen.h>

float fvalue;

Expand Down Expand Up @@ -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 {
Expand Down
56 changes: 56 additions & 0 deletions Test/test_cmor_variable_attr_comment.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 2 additions & 0 deletions ci-support/test-wheel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading