Skip to content
Snippets Groups Projects
Commit dfe35955 authored by Clayton, Brandon Scott's avatar Clayton, Brandon Scott
Browse files

Add prescision

parent 1af197bc
No related branches found
No related tags found
2 merge requests!109Production Release,!107AASHTO Updates
package gov.usgs.earthquake.nshmp.netcdf.reader; 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.checkNotNull;
import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Preconditions.checkState;
...@@ -9,17 +8,24 @@ import java.util.Arrays; ...@@ -9,17 +8,24 @@ import java.util.Arrays;
import com.google.common.math.DoubleMath; import com.google.common.math.DoubleMath;
import gov.usgs.earthquake.nshmp.Maths;
import gov.usgs.earthquake.nshmp.data.XySequence; 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.geo.LocationList;
import gov.usgs.earthquake.nshmp.netcdf.data.BoundingData;
import gov.usgs.earthquake.nshmp.netcdf.data.StaticData;
import ucar.ma2.DataType; import ucar.ma2.DataType;
import ucar.nc2.Group; import ucar.nc2.Group;
public class NetcdfUtils { 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 // Tolerance for longitude/latitude comparisons
static final double LOCATION_TOLERANCE = 0.000001; static final double LOCATION_TOLERANCE = 0.000001;
...@@ -76,38 +82,6 @@ public class NetcdfUtils { ...@@ -76,38 +82,6 @@ public class NetcdfUtils {
return calcFrac(a[i], a[i + 1], t); 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. * Check that the X values are identical.
* *
...@@ -120,6 +94,14 @@ public class NetcdfUtils { ...@@ -120,6 +94,14 @@ public class NetcdfUtils {
"Hazard curves xValues are not the same"); "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. * Get a 1D array from a netCDF group.
* *
...@@ -208,33 +190,6 @@ public class NetcdfUtils { ...@@ -208,33 +190,6 @@ public class NetcdfUtils {
return (String[]) get1DArray(group, key, DataType.STRING); 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 class Key {
public static final String DESCRIPTION = "description"; public static final String DESCRIPTION = "description";
public static final String GRID_STEP = "gridStep"; public static final String GRID_STEP = "gridStep";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment