diff --git a/src/lib/src/main/java/gov/usgs/earthquake/nshmp/netcdf/reader/NetcdfUtils.java b/src/lib/src/main/java/gov/usgs/earthquake/nshmp/netcdf/reader/NetcdfUtils.java index d03775dd2fadd9c9a00baa1d7f79331f7d8c6e3a..186b1465c5508df884a450b96ae955b084fa50e4 100644 --- a/src/lib/src/main/java/gov/usgs/earthquake/nshmp/netcdf/reader/NetcdfUtils.java +++ b/src/lib/src/main/java/gov/usgs/earthquake/nshmp/netcdf/reader/NetcdfUtils.java @@ -1,6 +1,5 @@ package gov.usgs.earthquake.nshmp.netcdf.reader; -import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; @@ -9,17 +8,24 @@ import java.util.Arrays; import com.google.common.math.DoubleMath; +import gov.usgs.earthquake.nshmp.Maths; import gov.usgs.earthquake.nshmp.data.XySequence; -import gov.usgs.earthquake.nshmp.geo.Location; import gov.usgs.earthquake.nshmp.geo.LocationList; -import gov.usgs.earthquake.nshmp.netcdf.data.BoundingData; -import gov.usgs.earthquake.nshmp.netcdf.data.StaticData; import ucar.ma2.DataType; import ucar.nc2.Group; public class NetcdfUtils { + /** TODO: Have this be dynamic from NetCDF file */ + public static final int LOCATION_PRECISION = 3; + + /** TODO: Have this be dynamic from NetCDF file */ + public static final int GROUND_MOTION_PRECISION = 3; + + /** TODO: Have this be dynamic from NetCDF file */ + public static final int HAZARD_PRECISION = 5; + // Tolerance for longitude/latitude comparisons static final double LOCATION_TOLERANCE = 0.000001; @@ -76,38 +82,6 @@ public class NetcdfUtils { return calcFrac(a[i], a[i + 1], t); } - /** - * Check whether bounding ground motions contain the same: Site classes, IMTs - * per each site class, and ground motions. - * - * @param a static data A - * @param b static B - */ - static void checkBoundingGroundMotion( - StaticData<XySequence> a, - StaticData<XySequence> b) { - checkState(a.size() == b.size(), "Maps are not the same size"); - checkState(a.keySet().containsAll(b.keySet()), "Site classes do not match"); - a.keySet().forEach(key -> checkXySequence(a.get(key), b.get(key))); - } - - /** - * Checks bounding hazard maps contain the same: Site classes, IMTs per each - * site class, and ground motions per each IMT - * - * @param boundingData The bounding ground motions - */ - static void checkBoundingGroundMotions( - BoundingData<XySequence> boundingData, - Location location) { - checkArgument(boundingData.containsKey(location), "Location not in bounding hazards"); - boundingData.keySet().stream() - .filter(loc -> loc.equals(location)) - .forEach(key -> { - checkBoundingGroundMotion(boundingData.get(location), boundingData.get(key)); - }); - } - /** * Check that the X values are identical. * @@ -120,6 +94,14 @@ public class NetcdfUtils { "Hazard curves xValues are not the same"); } + static XySequence cleanData(XySequence data, int precision) { + var xs = + data.xValues().map(x -> Double.isNaN(x) ? x : Maths.roundToDigits(x, precision)).toArray(); + var ys = + data.yValues().map(y -> Double.isNaN(y) ? y : Maths.roundToDigits(y, precision)).toArray(); + return XySequence.create(xs, ys); + } + /** * Get a 1D array from a netCDF group. * @@ -208,33 +190,6 @@ public class NetcdfUtils { return (String[]) get1DArray(group, key, DataType.STRING); } - /* - * Linear interpolation of data values to a target point - */ - static StaticData<XySequence> linearInterpolateGroundMotions( - StaticData<XySequence> v1, - StaticData<XySequence> v2, - double frac) { - checkBoundingGroundMotion(v1, v2); - - var targetMap = StaticData.<XySequence> builder(); - - v1.keySet().forEach(siteClass -> { - var v1Data = v1.get(siteClass).yValues().toArray(); - var v2Data = v2.get(siteClass).yValues().toArray(); - var target = new double[v1Data.length]; - - for (int i = 0; i < v1Data.length; i++) { - target[i] = v1Data[i] * (1 - frac) + v2Data[i] * frac; - } - - var xValues = v1.get(siteClass).xValues().toArray(); - targetMap.put(siteClass, XySequence.create(xValues, target)); - }); - - return targetMap.build(); - } - public static class Key { public static final String DESCRIPTION = "description"; public static final String GRID_STEP = "gridStep";