Skip to content

Commit

Permalink
Merge branch 'maint-5.x' into maint-5.x_sigmet
Browse files Browse the repository at this point in the history
  • Loading branch information
haileyajohnson committed Mar 13, 2024
2 parents d17603c + 343758e commit 3707ddd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,21 @@ public static Optional<FeatureDatasetCoverage> openGrib(String endpoint) {

}

/**
* @deprecated Use openNcmlString(String, String)
*/
@Deprecated
public static Optional<FeatureDatasetCoverage> openNcmlString(String ncml) throws IOException {
NetcdfDataset ncd = NetcdfDatasets.openNcmlDataset(new StringReader(ncml), null, null);
return openNcmlString(ncml, null);
}

/**
* @param ncml the NcML as a String
* @param location the URL location string of the NcML document,
* or may be just a unique name for caching purposes (if null, aggregation cache will not be used).
*/
public static Optional<FeatureDatasetCoverage> openNcmlString(String ncml, String location) throws IOException {
NetcdfDataset ncd = NetcdfDatasets.openNcmlDataset(new StringReader(ncml), location, null);

DtCoverageDataset gds = new DtCoverageDataset(ncd);
if (!gds.getGrids().isEmpty()) {
Expand Down
12 changes: 6 additions & 6 deletions cdm/core/src/main/java/ucar/nc2/util/Misc.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,19 @@ public static boolean nearlyEquals(float a, float b) {
return nearlyEquals(a, b, defaultMaxRelativeDiffFloat);
}

/** AbsoluteDifference is less than maxAbsDiff. */
public static boolean nearlyEquals(float a, float b, float maxAbsDiff) {
return DoubleMath.fuzzyEquals(a, b, maxAbsDiff);
/** RelativeDifference is less than maxRelDiff. */
public static boolean nearlyEquals(float a, float b, float maxRelDiff) {
return relativeDifference(a, b) < maxRelDiff;
}

/** AbsoluteDifference is less than {@link #defaultMaxRelativeDiffDouble}. */
public static boolean nearlyEquals(double a, double b) {
return nearlyEquals(a, b, defaultMaxRelativeDiffDouble);
}

/** AbsoluteDifference is less than maxAbsDiff. */
public static boolean nearlyEquals(double a, double b, double maxAbsDiff) {
return DoubleMath.fuzzyEquals(a, b, maxAbsDiff);
/** RelativeDifference is less than maxRelDiff. */
public static boolean nearlyEquals(double a, double b, double maxRelDiff) {
return relativeDifference(a, b) < maxRelDiff;
}

/** AbsoluteDifference is less than maxAbsDiff. */
Expand Down
17 changes: 17 additions & 0 deletions cdm/core/src/test/java/ucar/nc2/filter/TestEnhancements.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static com.google.common.truth.Truth.assertThat;

import java.io.IOException;
import java.util.Arrays;

public class TestEnhancements {

Expand Down Expand Up @@ -75,6 +76,13 @@ public static void setUp() throws IOException, InvalidRangeException {
.addAttribute(new Attribute(CDM.FILL_VALUE, SIGNED_SCALED_FILL_VALUE))
.addAttribute(new Attribute(CDM.MISSING_VALUE, SIGNED_SCALED_MISSING_VALUE));

// short data with small scale and offsets
Array smallVals = Array.factory(DataType.FLOAT, new int[] {dataLen}, missingData);

builder.addVariable("smallVals", DataType.FLOAT, "dim").addAttribute(new Attribute(CDM.SCALE_FACTOR, 1e-12))
.addAttribute(new Attribute(CDM.ADD_OFFSET, 1e-12)).addAttribute(new Attribute(CDM.FILL_VALUE, 110));


// write data
NetcdfFormatWriter writer = builder.build();
writer.write(writer.findVariable("signedVar"), new int[1], signedData);
Expand All @@ -85,6 +93,7 @@ public static void setUp() throws IOException, InvalidRangeException {
writer.write(writer.findVariable("validMinMax"), new int[1], missingDataArray);
writer.write(writer.findVariable("validRange"), new int[1], missingDataArray);
writer.write(writer.findVariable("enhanceAll"), new int[1], enhanceAllArray);
writer.write(writer.findVariable("smallVals"), new int[1], smallVals);
writer.close();
ncd = NetcdfDatasets.openDataset(filePath);
}
Expand Down Expand Up @@ -152,4 +161,12 @@ public void testCombinedEnhancements() throws IOException {
Array data = v.read();
assertThat((double[]) data.copyTo1DJavaArray()).isEqualTo(expected);
}

@Test
public void testConvertMissingWithSmallScaleAndOffset() throws IOException {
Variable v = ncd.findVariable("smallVals");
Array data = v.read();
assertThat(Double.isNaN(data.getDouble(2))).isTrue();
assertThat(Double.isNaN(data.getDouble(1))).isFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void testAggNewOpenAsCoverage() throws IOException, InvalidRangeException
ncml = ncmlStream.collect(Collectors.joining());
}
assertThat(ncml).isNotNull();
Optional<FeatureDatasetCoverage> fdc = CoverageDatasetFactory.openNcmlString(ncml);
Optional<FeatureDatasetCoverage> fdc = CoverageDatasetFactory.openNcmlString(ncml, null);
assertThat(fdc.isPresent()).isTrue();
List<CoverageCollection> cc = fdc.get().getCoverageCollections();
checkCoverages(cc);
Expand Down

0 comments on commit 3707ddd

Please sign in to comment.