diff --git a/cdm/core/src/main/java/ucar/nc2/dataset/VariableDS.java b/cdm/core/src/main/java/ucar/nc2/dataset/VariableDS.java index a57ece8994..dc82c774f5 100644 --- a/cdm/core/src/main/java/ucar/nc2/dataset/VariableDS.java +++ b/cdm/core/src/main/java/ucar/nc2/dataset/VariableDS.java @@ -1,7 +1,8 @@ /* - * Copyright (c) 1998-2025 John Caron and University Corporation for Atmospheric Research/Unidata + * Copyright (c) 1998-2026 John Caron and University Corporation for Atmospheric Research/Unidata * See LICENSE for license information. */ + package ucar.nc2.dataset; import com.google.common.collect.ImmutableList; @@ -239,7 +240,11 @@ public void enhance(Set enhancements) { // So, we need to reset to default before we process this new set. // LOOK this seems bogus if (orgDataType != null) { - setDataType(orgDataType); + // only reset if original DataType and current DataType are compatible (both + // are numeric or both are not numeric). + if (orgDataType.isNumeric() == dataType.isNumeric()) { + setDataType(orgDataType); + } } createEnhancements(); diff --git a/cdm/core/src/test/data/ncml/coords/nc/string_coord.nc b/cdm/core/src/test/data/ncml/coords/nc/string_coord.nc new file mode 100644 index 0000000000..56d8e8d299 Binary files /dev/null and b/cdm/core/src/test/data/ncml/coords/nc/string_coord.nc differ diff --git a/cdm/core/src/test/data/ncml/coords/string_coord.ncml b/cdm/core/src/test/data/ncml/coords/string_coord.ncml new file mode 100644 index 0000000000..9d9662b335 --- /dev/null +++ b/cdm/core/src/test/data/ncml/coords/string_coord.ncml @@ -0,0 +1,8 @@ + + + + + 0 1 + + \ No newline at end of file diff --git a/cdm/core/src/test/java/ucar/nc2/dataset/TestCoordinateAxisDataType.java b/cdm/core/src/test/java/ucar/nc2/dataset/TestCoordinateAxisDataType.java new file mode 100644 index 0000000000..d78a07f630 --- /dev/null +++ b/cdm/core/src/test/java/ucar/nc2/dataset/TestCoordinateAxisDataType.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2026 University Corporation for Atmospheric Research/Unidata + * See LICENSE for license information. + */ + +package ucar.nc2.dataset; + +import static com.google.common.truth.Truth.assertThat; + +import java.io.IOException; + +import org.junit.Test; +import ucar.ma2.DataType; +import ucar.nc2.Variable; +import ucar.unidata.util.test.TestDir; + +public class TestCoordinateAxisDataType { + + String testFile = TestDir.cdmLocalTestDataDir + "/ncml/coords/string_coord.ncml"; + + @Test + public void testDataTypeChangeOldApi() throws IOException { + try (NetcdfDataset ncdOld = NetcdfDataset.openDataset(testFile)) { + checkDataType(ncdOld); + } + } + + @Test + public void testDataTypeChangeNewApi() throws IOException { + String testFile = TestDir.cdmLocalTestDataDir + "/ncml/coords/string_coord.ncml"; + try (NetcdfDataset ncdNew = NetcdfDatasets.openDataset(testFile)) { + checkDataType(ncdNew); + } + } + + private void checkDataType(NetcdfDataset ncd) { + Variable var = ncd.findVariable("var"); + assertThat(var != null).isTrue(); + CoordinateAxis1D axis = (CoordinateAxis1D) var; + assertThat(axis.getOriginalDataType()).isEqualTo(DataType.INT); + assertThat(axis.getDataType()).isEqualTo(DataType.STRING); + assertThat(axis.isNumeric()).isFalse(); + } + +}