Class NCReader

All Implemented Interfaces:
GridSubsetReader, NCSD
Direct Known Subclasses:
ACSPONCCFReader, ACSPONCReader, CommonDataModelNCReader, CWCFNCReader, CWNCReader

public abstract class NCReader extends EarthDataReader implements GridSubsetReader, NCSD
The NCReader class is the base class for readers that use the Java NetCDF API to read and parse metadata. Supported file formats include NetCDF 3/4, HDF5, and OPeNDAP network connections.
Since:
3.2.0
Author:
Peter Hollemans
  • Field Details

    • dataset

      protected ucar.nc2.dataset.NetcdfDataset dataset
      The NetCDF dataset for accessing data.
  • Constructor Details

    • NCReader

      protected NCReader(String name) throws IOException
      Creates a new reader using the dataset at the specified location.
      Parameters:
      name - the file or network location.
      Throws:
      IOException - if an error occurred accessing the dataset.
  • Method Details

    • getDataset

      public ucar.nc2.dataset.NetcdfDataset getDataset()
      Description copied from interface: NCSD
      Gets the NetCDF scientific dataset.
      Specified by:
      getDataset in interface NCSD
    • getFilename

      public String getFilename()
      Description copied from interface: NCSD
      Gets the NetCDF scientific dataset file name.
      Specified by:
      getFilename in interface NCSD
    • initializeReader

      protected void initializeReader() throws IOException
      Performs reader initialization after the dataset has been opened.
      Throws:
      IOException - if an error occurred on initialization.
    • convertAttributeValue

      protected static Object convertAttributeValue(ucar.nc2.Attribute att, boolean asArray)
      Converts an attribute into a Java string, primitive array, or wrapped primitive.
      Parameters:
      att - the attribute to convert.
      asArray - the array flag, true to return single values as an array, false to return as a wrapped primitive.
    • getAttribute

      public Object getAttribute(String name)
      Gets a NetCDF global file attribute from this reader and converts it to a Java string, primitive array, or wrapped primitive.
      Parameters:
      name - the name of the attribute.
      Returns:
      the attribute value or null if none exists.
    • getAttributeAsArray

      public Object getAttributeAsArray(String name)
      Gets a NetCDF global file attribute from this reader and converts it to a primitive array. Strings are returned as an array of bytes, and arrays of length 1 are returned as-is (and not converted to a wrapped primitive).
      Parameters:
      name - the name of the attribute.
      Returns:
      the attribute value or null if none exists.
    • getAttribute

      public static Object getAttribute(ucar.nc2.NetcdfFile file, String name)
      Gets a NetCDF global file attribute and converts it to a Java string, primitive array, or wrapped primitive.
      Parameters:
      file - the NetCDF file to access.
      name - the name of the attribute.
      Returns:
      the attribute value or null if none exists.
    • getAttributeAsArray

      public static Object getAttributeAsArray(ucar.nc2.NetcdfFile file, String name)
      Gets a NetCDF global file attribute and converts it to a primitive array. Strings are returned as an array of bytes, and arrays of length 1 are returned as-is (and not converted to a wrapped primitive).
      Parameters:
      file - the NetCDF file to access.
      name - the name of the attribute.
      Returns:
      the attribute value or null if none exists.
    • getAttribute

      public static Object getAttribute(ucar.nc2.Variable var, String name)
      Gets a NetCDF variable attribute and converts it to a Java string, primitive array, or wrapped primitive.
      Parameters:
      var - the variable to access.
      name - the name of the attribute.
      Returns:
      the attribute value or null if none exists.
    • getAttributeAsArray

      public static Object getAttributeAsArray(ucar.nc2.Variable var, String name)
      Gets a NetCDF variable attribute and converts it to a primitive array. Strings are returned as an array of bytes, and arrays of length 1 are returned as-is (and not converted to a wrapped primitive).
      Parameters:
      var - the variable to access.
      name - the name of the attribute.
      Returns:
      the attribute value or null if none exists.
    • isNetwork

      public boolean isNetwork()
      Returns true if this reader is network-connected.
    • getActualVariable

      protected abstract DataVariable getActualVariable(int index) throws IOException
      Gets the actual variable with data. This method should be implemented in the child class and is only called if the variable is not already in the cache. See getVariable(int) for the required behaviour.
      Throws:
      IOException
    • getVariable

      public DataVariable getVariable(int index) throws IOException
      Description copied from class: EarthDataReader
      Creates a data variable object. The full data is read into the object.
      Specified by:
      getVariable in class EarthDataReader
      Parameters:
      index - the index of the variable to get. Indexing starts at 0.
      Returns:
      a data variable object with full data value array.
      Throws:
      IOException - if the data source had I/O errors.
      See Also:
    • close

      public void close() throws IOException
      Closes the reader.
      Specified by:
      close in class EarthDataReader
      Throws:
      IOException - if the data source had I/O errors.
    • getReferencedFile

      protected ucar.nc2.NetcdfFile getReferencedFile()
      Gets the NetCDF file referenced in this reader.
      Returns:
      the NetCDF file.
      Since:
      3.6.1
    • getGridSubset

      public Grid getGridSubset(String varName, int[] start, int[] stride, int[] length) throws IOException
      Description copied from interface: GridSubsetReader
      Reads a subset of a data grid. This is similar to the EarthDataReader.getVariable(String) method except that it reads only grid variables and is capable of returning just a subset of the data values. In some cases, such as across a network connection, it may be more efficient to access only a subset or subsampling of data in a variable.
      Specified by:
      getGridSubset in interface GridSubsetReader
      Parameters:
      varName - the variable name to access.
      start - the 2D starting data coordinates.
      stride - the 2D data stride.
      length - the total number of values to read in each dimension.
      Returns:
      the subset and/or subsampled grid variable.
      Throws:
      IOException - if the data source had I/O errors.
    • getCoordinateSystems

      public List<ucar.nc2.dataset.CoordinateSystem> getCoordinateSystems()
      Description copied from class: EarthDataReader
      Gets the NetCDF CDM style coordinate systems accessed by this reader.
      Overrides:
      getCoordinateSystems in class EarthDataReader
      Returns:
      the list of coordinate systems, possibly empty.
    • getVariablesForSystem

      public List<String> getVariablesForSystem(ucar.nc2.dataset.CoordinateSystem system)
      Description copied from class: EarthDataReader
      Gets the variable names for the specified NetCDF CDM style coordinate systems accessed by this reader.
      Overrides:
      getVariablesForSystem in class EarthDataReader
      Returns:
      the list of variable names, possibly empty.
    • getRawMetadata

      public Map<String,Object> getRawMetadata(int index) throws IOException
      Description copied from class: EarthDataReader
      Gets the detailed raw metadata for a variable.
      Overrides:
      getRawMetadata in class EarthDataReader
      Parameters:
      index - the index of the variable for raw metadata.
      Returns:
      the map of attribute name to attribute value.
      Throws:
      IOException - if the data source had I/O errors.
      See Also: