Package ucar.nc2.write
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); }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
NetcdfFormatWriter.Builder
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
abort()
Abort writing to this file.int
appendStructureData(Structure s, StructureData sdata)
static NetcdfFormatWriter.Builder
builder()
Obtain a Builder to set custom optionslong
calcSize()
void
close()
close the file.static NetcdfFileFormat
convertToNetcdfFileFormat(NetcdfFileWriter.Version version)
static NetcdfFileWriter.Version
convertToNetcdfFileWriterVersion(NetcdfFileFormat format)
static NetcdfFormatWriter.Builder
createNewNetcdf3(String location)
Create a new Netcdf3 file.static NetcdfFormatWriter.Builder
createNewNetcdf4(NetcdfFileFormat format, String location, Nc4Chunking chunker)
Create a new Netcdf4 file.Dimension
findDimension(String dimName)
Attribute
findGlobalAttribute(String attName)
Variable
findVariable(String fullNameEscaped)
void
flush()
Flush anything written to disk.NetcdfFileFormat
getFormat()
NetcdfFile
getOutputFile()
static NetcdfFormatWriter.Builder
openExisting(String location)
Open an existing Netcdf file for writing data.void
updateAttribute(Variable v2, Attribute att)
Update the value of an existing attribute.void
write(String varName, int[] origin, Array values)
Write data to the named variable.void
write(String varName, Array values)
Write data to the named variable, origin assumed to be 0.void
write(Variable v, int[] origin, Array values)
Write data to the given variable.void
write(Variable v, Array values)
Write data to the given variable, origin assumed to be 0.void
writeStringDataToChar(Variable v, int[] origin, Array values)
Write String data to a CHAR variable.void
writeStringDataToChar(Variable v, Array values)
Write String data to a CHAR variable, origin assumed to be 0.
-
-
-
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
-
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()
-
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 tovalues
- write this array; must be same type and rank as Variable- Throws:
IOException
- if I/O errorInvalidRangeException
- 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 toorigin
- offset within the variable to start writing.values
- write this array; must be same type and rank as Variable- Throws:
IOException
- if I/O errorInvalidRangeException
- 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 toorigin
- offset within the variable to start writing.values
- write this array; must be same type and rank as Variable- Throws:
IOException
- if I/O errorInvalidRangeException
- 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 tovalues
- write this array; must be ArrayObject of String- Throws:
IOException
- if I/O errorInvalidRangeException
- 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 toorigin
- offset to start writing, ignore the strlen dimension.values
- write this array; must be ArrayObject of String- Throws:
IOException
- if I/O errorInvalidRangeException
- 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 attributeatt
- 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 interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
abort
public void abort() throws IOException
Abort writing to this file. The file is closed.- Throws:
IOException
-
-