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 186b1465c5508df884a450b96ae955b084fa50e4..8ccccd717b07e78844f146cb2b44fc2188819ba4 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 @@ -4,6 +4,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; import java.io.IOException; +import java.nio.file.Path; import java.util.Arrays; import com.google.common.math.DoubleMath; @@ -13,7 +14,10 @@ import gov.usgs.earthquake.nshmp.data.XySequence; import gov.usgs.earthquake.nshmp.geo.LocationList; import ucar.ma2.DataType; +import ucar.nc2.Attribute; import ucar.nc2.Group; +import ucar.nc2.dataset.NetcdfDataset; +import ucar.nc2.dataset.NetcdfDatasets; public class NetcdfUtils { @@ -190,6 +194,15 @@ public class NetcdfUtils { return (String[]) get1DArray(group, key, DataType.STRING); } + public static Attribute readAttribute(String attributeKey, Path netcdfPath) { + try (NetcdfDataset ncd = NetcdfDatasets.openDataset(netcdfPath.toString())) { + Group group = ncd.getRootGroup(); + return group.attributes().findAttribute(attributeKey); + } catch (IOException e) { + throw new RuntimeException("Could not read Netcdf file [" + netcdfPath + " ]"); + } + } + public static class Key { public static final String DESCRIPTION = "description"; public static final String GRID_STEP = "gridStep";