Class NetCDFServiceImpl

java.lang.Object
loci.common.services.AbstractService
loci.formats.services.NetCDFServiceImpl
All Implemented Interfaces:
com.esotericsoftware.kryo.KryoSerializable, loci.common.services.Service, NetCDFService

public class NetCDFServiceImpl extends loci.common.services.AbstractService implements NetCDFService, com.esotericsoftware.kryo.KryoSerializable
Utility class for working with NetCDF/HDF files. Uses reflection to call the NetCDF Java library.
  • Field Details

    • NO_NETCDF_MSG

      public static final String NO_NETCDF_MSG
    • currentFile

      private String currentFile
    • attributeList

      private Vector<String> attributeList
    • variableList

      private Vector<String> variableList
    • netCDFFile

      private ucar.nc2.NetcdfFile netCDFFile
      NetCDF file instance.
    • root

      private ucar.nc2.Group root
      Root of the NetCDF file.
  • Constructor Details

    • NetCDFServiceImpl

      public NetCDFServiceImpl()
      Default constructor.
  • Method Details

    • setFile

      public void setFile(String file) throws IOException
      Description copied from interface: NetCDFService
      Initializes the service on a given file path.
      Specified by:
      setFile in interface NetCDFService
      Parameters:
      file - Path to initialize the service with.
      Throws:
      IOException - If there is an error initializing the service with file.
    • getFile

      public String getFile()
      Description copied from interface: NetCDFService
      Retrieves the current initialized file path.
      Specified by:
      getFile in interface NetCDFService
      Returns:
      Current initialized file path or null if the service has yet to be initialized or is closed.
    • getAttributeList

      public Vector<String> getAttributeList()
      Description copied from interface: NetCDFService
      Retrieves an exhaustive list of the HDF paths for all attributes in the HDF document.
      Specified by:
      getAttributeList in interface NetCDFService
      Returns:
      Collection of attribute paths.
    • getVariableList

      public Vector<String> getVariableList()
      Description copied from interface: NetCDFService
      Retrieves an exhaustive list of the HDF paths for all variables in the HDF document.
      Specified by:
      getVariableList in interface NetCDFService
      Returns:
      Collection of variable paths.
    • getAttributeValue

      public String getAttributeValue(String path)
      Description copied from interface: NetCDFService
      Retrieves an attribute's value.
      Specified by:
      getAttributeValue in interface NetCDFService
      Parameters:
      path - HDF path to the attribute.
      Returns:
      String value or null if the attribute is not a string.
    • getVariableValue

      public Object getVariableValue(String name) throws loci.common.services.ServiceException
      Description copied from interface: NetCDFService
      Retrieves a variable's value.
      Specified by:
      getVariableValue in interface NetCDFService
      Parameters:
      name - HDF path to the variable.
      Returns:
      The Java one-dimensional array representation of the variable's values.
      Throws:
      loci.common.services.ServiceException - If there is an error with the range of values or reading from the file.
      See Also:
      • Array.copyTo1DJavaArray()
    • getArray

      public Object getArray(String path, int[] origin, int[] shape) throws loci.common.services.ServiceException
      Description copied from interface: NetCDFService
      Retrieves an HDF path's values.
      Specified by:
      getArray in interface NetCDFService
      Parameters:
      path - HDF path to the values.
      origin - Array specifying the starting index. If null, assume all zeroes.
      shape - Array specifying the extents in each dimension. This becomes the shape of the returned Array.
      Returns:
      The Java n-dimensional array representation of the path's values.
      Throws:
      loci.common.services.ServiceException - If there is an error with the range of values or reading from the file.
      See Also:
      • Variable.read(int[], int[])
      • Array.copyToNDJavaArray()
    • getVariableAttributes

      public Hashtable<String,Object> getVariableAttributes(String name)
      Description copied from interface: NetCDFService
      Retrieves all of a variable's attributes.
      Specified by:
      getVariableAttributes in interface NetCDFService
      Parameters:
      name - HDF path to the variable.
      Returns:
      Hash table of attributes to Java one-dimensional array representations of the attribute's values.
      See Also:
      • Array.copyTo1DJavaArray()
    • getDimension

      public int getDimension(String name)
      Description copied from interface: NetCDFService
      Retrieves the length of a dimension.
      Specified by:
      getDimension in interface NetCDFService
      Parameters:
      name - HDF path to the dimension.
      Returns:
      Length of the dimension.
    • close

      public void close() throws IOException
      Description copied from interface: NetCDFService
      Closes and resets the service.
      Specified by:
      close in interface NetCDFService
      Throws:
      IOException - If there is an error closing the file.
    • parseAttributesAndVariables

      private void parseAttributesAndVariables(List<ucar.nc2.Group> groups)
      Recursively parses attribute and variable paths, filling attributeList and variableList.
      Parameters:
      groups - List of groups to recursively parse.
    • getGroup

      private ucar.nc2.Group getGroup(String path)
      Retrieves a group based on its fully qualified path.
      Parameters:
      path - Fully qualified path to the group.
      Returns:
      Group or root if the group cannot be found.
    • getDirectory

      private String getDirectory(String path)
    • getName

      private String getName(String path)
    • arrayToString

      private String arrayToString(ucar.ma2.Array values)
    • init

      private void init() throws IOException
      Throws:
      IOException
    • read

      public void read(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Input in)
      Specified by:
      read in interface com.esotericsoftware.kryo.KryoSerializable
    • write

      public void write(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Output out)
      Specified by:
      write in interface com.esotericsoftware.kryo.KryoSerializable