Skip to content

Commit

Permalink
Handle undefined values when converting
Browse files Browse the repository at this point in the history
In DssDataWriter handle the condition that a value is undefined when converting. In this case the value should not be converted but remain undefined.
  • Loading branch information
Tom Brauer committed Nov 7, 2024
1 parent 48dadd3 commit 51aede0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import java.nio.file.Path;

import static hec.heclib.util.Heclib.UNDEFINED_FLOAT;

public class RasterUtils {
static {
GdalRegister.getInstance();
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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());
Expand All @@ -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");
}

Expand All @@ -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);
Expand Down

This file was deleted.

0 comments on commit 51aede0

Please sign in to comment.