Class NetcdfFormatWriter

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class NetcdfFormatWriter
    extends Object
    implements Closeable
    Writes Netcdf 3 or 4 formatted files to disk. Note that there is no redefine mode. Once you call build(), you cannot add new metadata, you can only write data.
     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.write("v", dataArray);
     }
     
    • Method Detail

      • openExisting

        public static NetcdfFormatWriter.Builder openExisting​(String location)
        Open an existing Netcdf file for writing data. Cannot add new objects, you can only read/write data to existing Variables.
        Parameters:
        location - name of existing file to open.
        Returns:
        existing file that can be written to
        Throws:
        IOException - on I/O error
      • 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.
        Returns:
        new NetcdfFormatWriter
      • createNewNetcdf4

        public static NetcdfFormatWriter.Builder createNewNetcdf4​(NetcdfFileFormat format,
                                                                  String location,
                                                                  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
        Returns:
        new NetcdfFormatWriter
      • getOutputFile

        public NetcdfFile getOutputFile()
      • findVariable

        @Nullable
        public Variable findVariable​(String fullNameEscaped)
      • findDimension

        @Nullable
        public Dimension findDimension​(String dimName)
      • findGlobalAttribute

        @Nullable
        public Attribute findGlobalAttribute​(String attName)
      • calcSize

        public long calcSize()
      • write

        public void write​(Variable v,
                          int[] origin,
                          Array values)
                   throws IOException,
                          InvalidRangeException
        Write data to the given variable.
        Parameters:
        v - variable to write to
        origin - offset within the variable to start writing.
        values - write this array; must be same type and rank as Variable
        Throws:
        IOException - if I/O error
        InvalidRangeException - if values Array has illegal shape
      • write

        public void write​(String varName,
                          int[] origin,
                          Array values)
                   throws IOException,
                          InvalidRangeException
        Write data to the named variable.
        Parameters:
        varName - name of variable to write to
        origin - offset within the variable to start writing.
        values - write this array; must be same type and rank as Variable
        Throws:
        IOException - if I/O error
        InvalidRangeException - if values Array has illegal shape
      • writeStringDataToChar

        public void writeStringDataToChar​(Variable v,
                                          int[] origin,
                                          Array values)
                                   throws IOException,
                                          InvalidRangeException
        Write String data to a CHAR variable.
        Parameters:
        v - variable to write to
        origin - offset to start writing, ignore the strlen dimension.
        values - write this array; must be ArrayObject of String
        Throws:
        IOException - if I/O error
        InvalidRangeException - if values Array has illegal shape
      • 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. 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