From 30d99d1edc66a189ae0a3b413f8e652a8dc85b0e Mon Sep 17 00:00:00 2001 From: Brandon Clayton <bclayton@usgs.gov> Date: Thu, 21 Jul 2022 09:28:43 -0600 Subject: [PATCH] aashto netcdf data files class --- .../netcdf/NetcdfDataFilesGroundMotions.java | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/aashto/src/main/java/gov/usgs/earthquake/nshmp/netcdf/NetcdfDataFilesGroundMotions.java diff --git a/src/aashto/src/main/java/gov/usgs/earthquake/nshmp/netcdf/NetcdfDataFilesGroundMotions.java b/src/aashto/src/main/java/gov/usgs/earthquake/nshmp/netcdf/NetcdfDataFilesGroundMotions.java new file mode 100644 index 0000000..3fcaf21 --- /dev/null +++ b/src/aashto/src/main/java/gov/usgs/earthquake/nshmp/netcdf/NetcdfDataFilesGroundMotions.java @@ -0,0 +1,77 @@ +package gov.usgs.earthquake.nshmp.netcdf; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import gov.usgs.earthquake.nshmp.geo.Location; +import gov.usgs.earthquake.nshmp.netcdf.reader.NetcdfUtils; +import gov.usgs.earthquake.nshmp.netcdf.reader.NetcdfUtils.Key; + +/** + * Read in and parse all AASHTO NetCDF files for a particular year. + * + * @author U.S. Geological Survey + */ +public class NetcdfDataFilesGroundMotions extends NetcdfDataFiles<NetcdfGroundMotions> { + + private int aashtoYear; + + public NetcdfDataFilesGroundMotions(Path netcdfPath, int aashtoYear) { + super(netcdfPath, NetcdfDataType.GROUND_MOTIONS); + this.aashtoYear = aashtoYear; + addAll(readFiles(netcdfPath, dataType())); + } + + /** + * Returns the aastho year of the NetCDF files. + */ + public int aashtoYear() { + return aashtoYear; + } + + /** + * Returns the {@link NetcdfGroundMotions} for a given site. + * + * @param location The site to get the NetCDF data + */ + public NetcdfGroundMotions netcdf(Location location) { + return stream() + .filter(netcdf -> netcdf.netcdfData().contains(location)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException( + String.format( + "Longitude %s and latitude %s not found in data sets.", + location.longitude, + location.latitude))); + } + + @Override + protected List<NetcdfGroundMotions> readFiles(Path netcdfPath, NetcdfDataType dataType) { + try { + List<NetcdfGroundMotions> data = walkFiles(netcdfPath, dataType) + .map(NetcdfGroundMotions::new) + .collect(Collectors.toList()); + + if (data.isEmpty()) { + throw new FileNotFoundException( + String.format("Failed to find AASHTO %s NetCDF files", aashtoYear())); + } + + return List.copyOf(data); + } catch (IOException e) { + throw new RuntimeException("Failed to read NetCDF directory " + netcdfPath, e); + } + } + + @Override + protected Stream<Path> walkFiles(Path netcdfPath, NetcdfDataType dataType) throws IOException { + return super.walkFiles(netcdfPath, dataType) + .filter(file -> NetcdfUtils.readAttribute( + Key.YEAR, + file).getNumericValue().intValue() == aashtoYear()); + } +} -- GitLab