Skip to content

Commit 51aede0

Browse files
author
Tom Brauer
committed
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.
1 parent 48dadd3 commit 51aede0

3 files changed

Lines changed: 27 additions & 56 deletions

File tree

vortex-api/src/main/java/mil/army/usace/hec/vortex/geo/RasterUtils.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import java.nio.file.Path;
1313

14+
import static hec.heclib.util.Heclib.UNDEFINED_FLOAT;
15+
1416
public class RasterUtils {
1517
static {
1618
GdalRegister.getInstance();
@@ -87,4 +89,13 @@ public static float[] flipVertically (float[] data, int nx) {
8789

8890
return flipped;
8991
}
92+
93+
public static float[] convert(float[] data, float conversionFactor, float noDataValue) {
94+
float[] convertedData = new float[data.length];
95+
for (int i = 0; i < data.length; i++) {
96+
float value = data[i];
97+
convertedData[i] = Float.compare(noDataValue, value) == 0 ? noDataValue : value * conversionFactor;
98+
}
99+
return convertedData;
100+
}
90101
}

vortex-api/src/main/java/mil/army/usace/hec/vortex/io/DssDataWriter.java

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import hec.heclib.grid.GriddedData;
99
import hec.heclib.util.HecTime;
1010
import hec.heclib.util.HecTimeArray;
11-
import hec.heclib.util.Heclib;
1211
import hec.hecmath.HecMath;
1312
import hec.io.DataContainer;
1413
import hec.io.TimeSeriesContainer;
@@ -29,8 +28,8 @@
2928
import java.util.logging.Level;
3029
import java.util.logging.Logger;
3130
import java.util.stream.Collectors;
32-
import java.util.stream.IntStream;
3331

32+
import static hec.heclib.util.Heclib.UNDEFINED_FLOAT;
3433
import static javax.measure.MetricPrefix.MILLI;
3534
import static mil.army.usace.hec.vortex.VortexVariable.*;
3635
import static systems.uom.common.USCustomary.FAHRENHEIT;
@@ -69,8 +68,9 @@ public void write() {
6968

7069
double noDataValue = grid.noDataValue();
7170
for (int i = 0; i < data.length; i++) {
72-
if (Double.compare(data[i], noDataValue) == 0 || Double.isNaN(data[i])) {
73-
data[i] = Heclib.UNDEFINED_FLOAT;
71+
float value = data[i];
72+
if (Double.compare(value, noDataValue) == 0 || Double.isNaN(value) || Double.isInfinite(value)) {
73+
data[i] = UNDEFINED_FLOAT;
7474
}
7575
}
7676

@@ -115,12 +115,7 @@ public void write() {
115115
conversion = 1;
116116
}
117117

118-
for (int i = 0; i < data.length; i++) {
119-
if (data[i] == Heclib.UNDEFINED_FLOAT)
120-
continue;
121-
122-
data[i] *= conversion;
123-
}
118+
float[] convertedData = RasterUtils.convert(data, conversion, UNDEFINED_FLOAT);
124119

125120
gridInfo.setDataUnits("MM");
126121
gridInfo.setDataType(DssDataType.PER_CUM.value());
@@ -133,25 +128,26 @@ public void write() {
133128
}
134129
}
135130

136-
write(data, gridInfo, dssPathname);
131+
write(convertedData, gridInfo, dssPathname);
137132

138133
} else if (cPart.equals("PRECIPITATION") && units.equals(METRE)) {
139-
float[] convertedData = new float[data.length];
140-
IntStream.range(0, data.length).forEach(i -> convertedData[i] = data[i] * 1000);
141-
134+
float[] convertedData = RasterUtils.convert(data, 1000, UNDEFINED_FLOAT);
142135
gridInfo.setDataUnits("MM");
143136

144137
write(convertedData, gridInfo, dssPathname);
145138
} else if (units.equals(FAHRENHEIT) || units.equals(KELVIN) || units.equals(CELSIUS)) {
146139
float[] convertedData = new float[data.length];
147140
if (units.equals(FAHRENHEIT)) {
148-
IntStream.range(0, data.length).forEach(i -> convertedData[i] = data[i]);
141+
System.arraycopy(data, 0, convertedData, 0, data.length);
149142
gridInfo.setDataUnits("DEG F");
150143
} else if (units.equals(KELVIN)) {
151-
IntStream.range(0, data.length).forEach(i -> convertedData[i] = (float) (data[i] - 273.15));
144+
for (int i = 0; i < data.length; i++) {
145+
float value = data[i];
146+
convertedData[i] = Float.compare(UNDEFINED_FLOAT, value) == 0 ? UNDEFINED_FLOAT : (float) (data[i] - 273.15);
147+
}
152148
gridInfo.setDataUnits("DEG C");
153149
} else if (units.equals(CELSIUS)) {
154-
IntStream.range(0, data.length).forEach(i -> convertedData[i] = data[i]);
150+
System.arraycopy(data, 0, convertedData, 0, data.length);
155151
gridInfo.setDataUnits("DEG C");
156152
}
157153

@@ -165,23 +161,17 @@ public void write() {
165161

166162
write(convertedData, gridInfo, dssPathname);
167163
} else if (cPart.equals("HUMIDITY") && units.equals(ONE)) {
168-
float[] convertedData = new float[data.length];
169-
IntStream.range(0, data.length).forEach(i -> convertedData[i] = data[i] * 100);
170-
164+
float[] convertedData = RasterUtils.convert(data, 100, UNDEFINED_FLOAT);
171165
gridInfo.setDataUnits("%");
172166

173167
write(convertedData, gridInfo, dssPathname);
174168
} else if (units.equals(ONE.divide(INCH.multiply(1000)))) {
175-
float[] convertedData = new float[data.length];
176-
IntStream.range(0, data.length).forEach(i -> convertedData[i] = data[i] / 1000);
177-
169+
float[] convertedData = RasterUtils.convert(data, 1E-3f, UNDEFINED_FLOAT);
178170
gridInfo.setDataUnits("IN");
179171

180172
write(convertedData, gridInfo, dssPathname);
181173
} else if (units.equals(PASCAL)) {
182-
float[] convertedData = new float[data.length];
183-
IntStream.range(0, data.length).forEach(i -> convertedData[i] = data[i] / 1000);
184-
174+
float[] convertedData = RasterUtils.convert(data, 1E-3f, UNDEFINED_FLOAT);
185175
gridInfo.setDataUnits("KPA");
186176

187177
write(convertedData, gridInfo, dssPathname);

vortex-api/src/main/java/mil/army/usace/hec/vortex/util/MatrixUtils.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)