From 51aede0e8a3db06e6136d25cf9766b60532c1a41 Mon Sep 17 00:00:00 2001 From: Tom Brauer Date: Wed, 6 Nov 2024 16:23:54 -0800 Subject: [PATCH] Handle undefined values when converting In DssDataWriter handle the condition that a value is undefined when converting. In this case the value should not be converted but remain undefined. --- .../usace/hec/vortex/geo/RasterUtils.java | 11 +++++ .../usace/hec/vortex/io/DssDataWriter.java | 42 +++++++------------ .../usace/hec/vortex/util/MatrixUtils.java | 30 ------------- 3 files changed, 27 insertions(+), 56 deletions(-) delete mode 100644 vortex-api/src/main/java/mil/army/usace/hec/vortex/util/MatrixUtils.java diff --git a/vortex-api/src/main/java/mil/army/usace/hec/vortex/geo/RasterUtils.java b/vortex-api/src/main/java/mil/army/usace/hec/vortex/geo/RasterUtils.java index d99f06af..a651a5b6 100644 --- a/vortex-api/src/main/java/mil/army/usace/hec/vortex/geo/RasterUtils.java +++ b/vortex-api/src/main/java/mil/army/usace/hec/vortex/geo/RasterUtils.java @@ -11,6 +11,8 @@ import java.nio.file.Path; +import static hec.heclib.util.Heclib.UNDEFINED_FLOAT; + public class RasterUtils { static { GdalRegister.getInstance(); @@ -87,4 +89,13 @@ public static float[] flipVertically (float[] data, int nx) { return flipped; } + + public static float[] convert(float[] data, float conversionFactor, float noDataValue) { + float[] convertedData = new float[data.length]; + for (int i = 0; i < data.length; i++) { + float value = data[i]; + convertedData[i] = Float.compare(noDataValue, value) == 0 ? noDataValue : value * conversionFactor; + } + return convertedData; + } } diff --git a/vortex-api/src/main/java/mil/army/usace/hec/vortex/io/DssDataWriter.java b/vortex-api/src/main/java/mil/army/usace/hec/vortex/io/DssDataWriter.java index c5693e96..6efe3d7f 100644 --- a/vortex-api/src/main/java/mil/army/usace/hec/vortex/io/DssDataWriter.java +++ b/vortex-api/src/main/java/mil/army/usace/hec/vortex/io/DssDataWriter.java @@ -8,7 +8,6 @@ import hec.heclib.grid.GriddedData; import hec.heclib.util.HecTime; import hec.heclib.util.HecTimeArray; -import hec.heclib.util.Heclib; import hec.hecmath.HecMath; import hec.io.DataContainer; import hec.io.TimeSeriesContainer; @@ -29,8 +28,8 @@ import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; -import java.util.stream.IntStream; +import static hec.heclib.util.Heclib.UNDEFINED_FLOAT; import static javax.measure.MetricPrefix.MILLI; import static mil.army.usace.hec.vortex.VortexVariable.*; import static systems.uom.common.USCustomary.FAHRENHEIT; @@ -69,8 +68,9 @@ public void write() { double noDataValue = grid.noDataValue(); for (int i = 0; i < data.length; i++) { - if (Double.compare(data[i], noDataValue) == 0 || Double.isNaN(data[i])) { - data[i] = Heclib.UNDEFINED_FLOAT; + float value = data[i]; + if (Double.compare(value, noDataValue) == 0 || Double.isNaN(value) || Double.isInfinite(value)) { + data[i] = UNDEFINED_FLOAT; } } @@ -115,12 +115,7 @@ public void write() { conversion = 1; } - for (int i = 0; i < data.length; i++) { - if (data[i] == Heclib.UNDEFINED_FLOAT) - continue; - - data[i] *= conversion; - } + float[] convertedData = RasterUtils.convert(data, conversion, UNDEFINED_FLOAT); gridInfo.setDataUnits("MM"); gridInfo.setDataType(DssDataType.PER_CUM.value()); @@ -133,25 +128,26 @@ public void write() { } } - write(data, gridInfo, dssPathname); + write(convertedData, gridInfo, dssPathname); } else if (cPart.equals("PRECIPITATION") && units.equals(METRE)) { - float[] convertedData = new float[data.length]; - IntStream.range(0, data.length).forEach(i -> convertedData[i] = data[i] * 1000); - + float[] convertedData = RasterUtils.convert(data, 1000, UNDEFINED_FLOAT); gridInfo.setDataUnits("MM"); write(convertedData, gridInfo, dssPathname); } else if (units.equals(FAHRENHEIT) || units.equals(KELVIN) || units.equals(CELSIUS)) { float[] convertedData = new float[data.length]; if (units.equals(FAHRENHEIT)) { - IntStream.range(0, data.length).forEach(i -> convertedData[i] = data[i]); + System.arraycopy(data, 0, convertedData, 0, data.length); gridInfo.setDataUnits("DEG F"); } else if (units.equals(KELVIN)) { - IntStream.range(0, data.length).forEach(i -> convertedData[i] = (float) (data[i] - 273.15)); + for (int i = 0; i < data.length; i++) { + float value = data[i]; + convertedData[i] = Float.compare(UNDEFINED_FLOAT, value) == 0 ? UNDEFINED_FLOAT : (float) (data[i] - 273.15); + } gridInfo.setDataUnits("DEG C"); } else if (units.equals(CELSIUS)) { - IntStream.range(0, data.length).forEach(i -> convertedData[i] = data[i]); + System.arraycopy(data, 0, convertedData, 0, data.length); gridInfo.setDataUnits("DEG C"); } @@ -165,23 +161,17 @@ public void write() { write(convertedData, gridInfo, dssPathname); } else if (cPart.equals("HUMIDITY") && units.equals(ONE)) { - float[] convertedData = new float[data.length]; - IntStream.range(0, data.length).forEach(i -> convertedData[i] = data[i] * 100); - + float[] convertedData = RasterUtils.convert(data, 100, UNDEFINED_FLOAT); gridInfo.setDataUnits("%"); write(convertedData, gridInfo, dssPathname); } else if (units.equals(ONE.divide(INCH.multiply(1000)))) { - float[] convertedData = new float[data.length]; - IntStream.range(0, data.length).forEach(i -> convertedData[i] = data[i] / 1000); - + float[] convertedData = RasterUtils.convert(data, 1E-3f, UNDEFINED_FLOAT); gridInfo.setDataUnits("IN"); write(convertedData, gridInfo, dssPathname); } else if (units.equals(PASCAL)) { - float[] convertedData = new float[data.length]; - IntStream.range(0, data.length).forEach(i -> convertedData[i] = data[i] / 1000); - + float[] convertedData = RasterUtils.convert(data, 1E-3f, UNDEFINED_FLOAT); gridInfo.setDataUnits("KPA"); write(convertedData, gridInfo, dssPathname); diff --git a/vortex-api/src/main/java/mil/army/usace/hec/vortex/util/MatrixUtils.java b/vortex-api/src/main/java/mil/army/usace/hec/vortex/util/MatrixUtils.java deleted file mode 100644 index 6823c1ba..00000000 --- a/vortex-api/src/main/java/mil/army/usace/hec/vortex/util/MatrixUtils.java +++ /dev/null @@ -1,30 +0,0 @@ -package mil.army.usace.hec.vortex.util; - -public class MatrixUtils { - - private MatrixUtils(){} - - public static float[][] arrayToMatrix(float[] array, int nx, int ny){ - int count = 0; - float[][] matrix = new float[ny][nx]; - for (int j = ny-1; j>=0; j--){ - for (int i = 0; i