Class NetcdfFormatWriter

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class NetcdfFormatWriter
    extends Object
    implements Closeable
    Creates new Netcdf 3/4 format files. Writes data to new or existing files.

    TODO make netcdf3 and netcdf4 will be seperate, so they can have different functionality.

     NetcdfFormatWriter.Builder writerb = NetcdfFormatWriter.createNewNetcdf3(testFile.getPath());
     writerb.addDimension(Dimension.builder().setName("vdim").setIsUnlimited(true).build());
     writerb.addVariable("v", DataType.BYTE, "vdim");
     try (NetcdfFormatWriter writer = writerb.build()) {
       writer.config().forVariable("v").withArray(dataArray).write();
     }
     
    • Method Detail

      • createNewNetcdf3

        public static NetcdfFormatWriter.Builder<?> createNewNetcdf3​(String location)
        Create a new Netcdf3 file.
        Parameters:
        location - name of new file to open; if it exists, will overwrite it.
      • createNewNetcdf4

        public static NetcdfFormatWriter.Builder<?> createNewNetcdf4​(String location)
        Create a new NetcdfFileFormat.NETCDF4 file, with default chunker.
        Parameters:
        location - name of new file to open; if it exists, will overwrite it.
      • createNewNetcdf4

        public static NetcdfFormatWriter.Builder<?> createNewNetcdf4​(NetcdfFileFormat format,
                                                                     String location,
                                                                     @Nullable
                                                                     Nc4Chunking chunker)
        Create a new Netcdf4 file.
        Parameters:
        format - One of the netcdf-4 NetcdfFileFormat.
        location - name of new file to open; if it exists, will overwrite it.
        chunker - used only for netcdf4, or null for default chunking algorithm
      • openExisting

        public static NetcdfFormatWriter.Builder<?> openExisting​(String location)
                                                          throws IOException
        Open an existing Netcdf format file for writing data. Cannot add new objects, you can only read/write data to existing Variables. TODO: allow changes to Netcdf-4 format.
        Parameters:
        location - name of existing NetCDF file to open.
        Returns:
        existing file that can be written to
        Throws:
        IOException
      • getRootGroup

        public Group getRootGroup()
        The output file's root group.
      • findVariable

        @Nullable
        public Variable findVariable​(String fullNameEscaped)
        Find the named Variable in the output file.
      • findDimension

        @Nullable
        public Dimension findDimension​(String dimName)
        Find the named Dimension in the output file.
      • findGlobalAttribute

        @Nullable
        public Attribute findGlobalAttribute​(String attName)
        Find the named global attribute in the output file.
      • calcSize

        public long calcSize()
        An estimate of the size of the file when written to disk. Ignores compression for netcdf4.
        Returns:
        estimated file size in bytes.
      • writeStringData

        public void writeStringData​(Variable v,
                                    Index origin,
                                    String data)
                             throws IOException,
                                    InvalidRangeException
        Write String value to a CHAR Variable. Truncated or zero extended as needed to fit into last dimension of v. Note that origin is not incremeted as in previous versions.
         Index index = Index.ofRank(v.getRank());
         writer.writeStringData(v, index, "This is the first string.");
         writer.writeStringData(v, index.incr(0), "Shorty");
         writer.writeStringData(v, index.incr(0), "This is too long so it will get truncated");
         
        Parameters:
        v - write to this variable, must be of type CHAR.
        origin - offset within the variable to start writing. The innermost dimension must be 0.
        data - The String to write.
        Throws:
        IOException
        InvalidRangeException
      • updateAttribute

        public void updateAttribute​(Variable v2,
                                    Attribute att)
                             throws IOException
        Update the value of an existing attribute. Attribute is found by name, which must match exactly. You cannot make an attribute longer, or change the number of values. For strings: truncate if longer, zero fill if shorter. Strings are padded to 4 byte boundaries, ok to use padding if it exists. For numerics: must have same number of values.

        This is really a netcdf-3 writing only, in particular supporting point feature writing. netcdf-4 attributes can be changed without rewriting.

        Parameters:
        v2 - variable, or null for global attribute
        att - replace with this value
        Throws:
        IOException - if I/O error
      • abort

        public void abort()
                   throws IOException
        Abort writing to this file. The file is closed.
        Throws:
        IOException