diff --git a/src/lib/src/main/java/gov/usgs/earthquake/nshmp/netcdf/NetcdfDataFiles.java b/src/lib/src/main/java/gov/usgs/earthquake/nshmp/netcdf/NetcdfDataFiles.java new file mode 100644 index 0000000000000000000000000000000000000000..959baa7cb84ac92c647f7bb4c045d3443fced86b --- /dev/null +++ b/src/lib/src/main/java/gov/usgs/earthquake/nshmp/netcdf/NetcdfDataFiles.java @@ -0,0 +1,76 @@ +package gov.usgs.earthquake.nshmp.netcdf; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import gov.usgs.earthquake.nshmp.gmm.NehrpSiteClass; + +/** + * Read in all NetCDF files associated with a {@link NetcdfDataType}, + * holds {@link Netcdf} objects associated with each NetCDF file. + * + * @author U.S. Geological Survey + */ +public abstract class NetcdfDataFiles<T extends Netcdf<?>> extends ArrayList<T> { + private final Path netcdfPath; + private final NetcdfDataType dataType; + + public NetcdfDataFiles(Path netcdfPath, NetcdfDataType dataType) { + super(); + this.netcdfPath = netcdfPath; + this.dataType = dataType; + } + + /** + * Returns the path to the directory where all NetCDF files are. + */ + public Path netcdfPath() { + return netcdfPath; + } + + /** + * Returns the data type of the NetCDF files. + */ + public NetcdfDataType dataType() { + return dataType; + } + + /** + * Returns the set of NEHRP site classes for all data files. + */ + public Set<NehrpSiteClass> siteClasses() { + return stream() + .flatMap(netcdf -> netcdf.netcdfData().siteClasses().stream()) + .sorted() + .collect(Collectors.toSet()); + } + + /** + * Find NetCDF files (.nc) with specific {@link NetcdfDataType}. + * + * @param netcdfPath Path to NetCDF files + * @param dataType The data type to filter by + * @throws IOException + */ + protected Stream<Path> walkFiles(Path netcdfPath, NetcdfDataType dataType) throws IOException { + return Files.walk(netcdfPath) + .filter(file -> file.getFileName().toString().endsWith(".nc")) + .filter(file -> NetcdfDataType.getDataType(file) == dataType); + } + + /** + * Returns the list of {@link Netcdf} objects associated with a NetCDF file + * and {@link NetcdfDataType}. + * + * @param netcdfPath The path to NetCDF files + * @param dataType The data type to read in + * @throws IOException + */ + protected abstract List<T> readFiles(Path netcdfPath, NetcdfDataType dataType); +}