Skip to content

Commit

Permalink
fix failing tests and persist scale/offset attributes via dataset bui…
Browse files Browse the repository at this point in the history
…lders
  • Loading branch information
haileyajohnson committed Nov 28, 2023
1 parent 62e8bfa commit 573adf4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
9 changes: 8 additions & 1 deletion cdm/core/src/main/java/ucar/nc2/dataset/VariableDS.java
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ protected VariableDS(Builder<?> builder, Group parentGroup) {

this.orgFileTypeId = builder.orgFileTypeId;
this.enhanceProxy = new EnhancementsImpl(this, builder.units, builder.getDescription());
this.scaleOffset = builder.scaleOffset;

createEnhancements();

Expand All @@ -859,7 +860,9 @@ private void createEnhancements() {
this.dataType = unsignedConversion != null ? unsignedConversion.getOutType() : dataType;
}
if (this.enhanceMode.contains(Enhance.ApplyScaleOffset) && (dataType.isNumeric() || dataType == DataType.CHAR)) {
this.scaleOffset = ScaleOffset.createFromVariable(this);
if (this.scaleOffset == null) {
this.scaleOffset = ScaleOffset.createFromVariable(this);
}
this.dataType = scaleOffset != null ? scaleOffset.getScaledOffsetType() : this.dataType;
}
Attribute standardizerAtt = findAttribute(CDM.STANDARDIZE);
Expand Down Expand Up @@ -903,6 +906,7 @@ protected Builder<?> addLocalFieldsToBuilder(Builder<? extends Builder<?>> build
builder.setOriginalVariable(this.orgVar).setOriginalDataType(this.orgDataType).setOriginalName(this.orgName)
.setOriginalFileTypeId(this.orgFileTypeId).setEnhanceMode(this.enhanceMode).setUnits(this.enhanceProxy.units)
.setDesc(this.enhanceProxy.desc);
builder.scaleOffset = this.scaleOffset;
if (this.coordSysNames != null) {
this.coordSysNames.stream().forEach(s -> builder.addCoordinateSystemName(s));
}
Expand Down Expand Up @@ -948,6 +952,8 @@ public static abstract class Builder<T extends Builder<T>> extends Variable.Buil
private boolean fillValueIsMissing = NetcdfDataset.fillValueIsMissing;
private boolean missingDataIsMissing = NetcdfDataset.missingDataIsMissing;

private ScaleOffset scaleOffset;

private boolean built;

protected abstract T self();
Expand Down Expand Up @@ -1044,6 +1050,7 @@ public T copyFrom(VariableDS.Builder<?> builder) {
setFillValueIsMissing(builder.fillValueIsMissing);
setInvalidDataIsMissing(builder.invalidDataIsMissing);
setMissingDataIsMissing(builder.missingDataIsMissing);
this.scaleOffset = builder.scaleOffset;
this.orgVar = builder.orgVar;
this.orgDataType = builder.orgDataType;
this.orgFileTypeId = builder.orgFileTypeId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static java.lang.Float.NaN;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand Down Expand Up @@ -85,7 +86,6 @@ public void testWrite() throws Exception {

Array readEnhanced;

// read the packed form, enhance using scale/offset, compare to original
try (NetcdfDataset ncd = NetcdfDatasets.openDataset(filename)) {
VariableDS vs = (VariableDS) ncd.findVariable("packed");
vs.removeEnhancement(Enhance.ConvertMissing);
Expand Down Expand Up @@ -133,15 +133,14 @@ private void doSubset(String filename) throws IOException, InvalidRangeException
}
}


// Asserts that "scale_factor" is applied to "_FillValue".
// This test demonstrated the bug in https://github.com/Unidata/thredds/issues/1065.
@Test
public void testScaledFillValue() throws URISyntaxException, IOException {
File testResource = new File(getClass().getResource("testScaledFillValue.ncml").toURI());

// LOOK removeEnhancement does not work in new
try (NetcdfDataset ncd = NetcdfDataset.openDataset(testResource.getAbsolutePath(), true, null)) {
try (NetcdfDataset ncd = NetcdfDatasets.openDataset(testResource.getAbsolutePath(), true, null)) {
VariableDS fooVar = (VariableDS) ncd.findVariable("foo");

double expectedFillValue = .99999;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public void testStandaloneNoEnhanceDataset() throws IOException {
Variable scaledvar = ncfile.findVariable("scaledvar");
assertThat((Object) scaledvar).isNotNull();
assertThat(scaledvar.getDataType()).isEqualTo(DataType.SHORT);
assertThat(scaledvar.attributes().hasAttribute("scale_factor")).isFalse();
assertThat(scaledvar.attributes().hasAttribute("scale_factor")).isTrue();
assertThat(scaledvar.attributes().findAttributeDouble("scale_factor", 1.0)).isEqualTo(2.0);
assertThat(scaledvar.readScalarShort()).isEqualTo(1);
}
}
Expand Down Expand Up @@ -107,8 +108,6 @@ public void testStandaloneDoubleEnhanceDataset() throws IOException {
Variable scaledvar = ncfile.findVariable("scaledvar");
assertThat((Object) scaledvar).isNotNull();
assertThat(scaledvar.getDataType()).isEqualTo(DataType.FLOAT);
assertThat(scaledvar.attributes().hasAttribute("scale_factor")).isTrue();
assertThat(scaledvar.attributes().findAttributeDouble("scale_factor", 1.0)).isEqualTo(2.0);
assertThat(scaledvar.readScalarFloat()).isEqualTo(12.0f);
}
}
Expand Down

0 comments on commit 573adf4

Please sign in to comment.