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
9 changes: 7 additions & 2 deletions cdm/core/src/main/java/ucar/nc2/dataset/VariableDS.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -239,7 +240,11 @@ public void enhance(Set<Enhance> 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();
Expand Down
Binary file not shown.
8 changes: 8 additions & 0 deletions cdm/core/src/test/data/ncml/coords/string_coord.ncml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<netcdf xmlns="https://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2"
location="./nc/string_coord.nc">
<!-- https://github.com/Unidata/netcdf-java/issues/1540 -->
<!-- CoordinateAxis1D should be of type String in both old and new API -->
<variable name="var" type="String" shape="dim">
<values>0 1</values>
</variable>
</netcdf>
Original file line number Diff line number Diff line change
@@ -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();
}

}