Class NetcdfFormatWriter

java.lang.Object
ucar.nc2.write.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 Details

    • 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
    • builder

      public static NetcdfFormatWriter.Builder builder()
      Obtain a Builder to set custom options
    • convertToNetcdfFileWriterVersion

      public static NetcdfFileWriter.Version convertToNetcdfFileWriterVersion(NetcdfFileFormat format)
    • convertToNetcdfFileFormat

      public static NetcdfFileFormat convertToNetcdfFileFormat(NetcdfFileWriter.Version version)
    • getOutputFile

      public NetcdfFile getOutputFile()
    • getFormat

      public NetcdfFileFormat getFormat()
    • 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, Array values) throws IOException, InvalidRangeException
      Write data to the given variable, origin assumed to be 0.
      Parameters:
      v - variable to write to
      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, Array values) throws IOException, InvalidRangeException
      Write data to the named variable, origin assumed to be 0.
      Throws:
      IOException
      InvalidRangeException
    • 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, Array values) throws IOException, InvalidRangeException
      Write String data to a CHAR variable, origin assumed to be 0.
      Parameters:
      v - variable to write to
      values - write this array; must be ArrayObject of String
      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
    • appendStructureData

      public int appendStructureData(Structure s, StructureData sdata) throws IOException, InvalidRangeException
      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. 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
    • flush

      public void flush() throws IOException
      Flush anything written to disk.
      Throws:
      IOException
    • close

      public void close() throws IOException
      close the file.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • abort

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