Package ucar.nc2.write
Class NetcdfFormatWriter
java.lang.Object
ucar.nc2.write.NetcdfFormatWriter
- All Implemented Interfaces:
Closeable,AutoCloseable
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 -
Method Summary
Modifier and TypeMethodDescriptionvoidabort()Abort writing to this file.intappendStructureData(Structure s, StructureData sdata) static NetcdfFormatWriter.Builderbuilder()Obtain a Builder to set custom optionslongcalcSize()voidclose()close the file.static NetcdfFileFormatconvertToNetcdfFileFormat(ucar.nc2.NetcdfFileWriter.Version version) static ucar.nc2.NetcdfFileWriter.Versionstatic NetcdfFormatWriter.BuildercreateNewNetcdf3(String location) Create a new Netcdf3 file.static NetcdfFormatWriter.BuildercreateNewNetcdf4(NetcdfFileFormat format, String location, Nc4Chunking chunker) Create a new Netcdf4 file.findDimension(String dimName) findGlobalAttribute(String attName) findVariable(String fullNameEscaped) voidflush()Flush anything written to disk.static NetcdfFormatWriter.BuilderopenExisting(String location) Open an existing Netcdf file for writing data.voidupdateAttribute(Variable v2, Attribute att) Update the value of an existing attribute.voidWrite data to the named variable.voidWrite data to the named variable, origin assumed to be 0.voidWrite data to the given variable.voidWrite data to the given variable, origin assumed to be 0.voidwriteStringDataToChar(Variable v, int[] origin, Array values) Write String data to a CHAR variable.voidwriteStringDataToChar(Variable v, Array values) Write String data to a CHAR variable, origin assumed to be 0.
-
Method Details
-
openExisting
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
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
Obtain a Builder to set custom options -
convertToNetcdfFileWriterVersion
public static ucar.nc2.NetcdfFileWriter.Version convertToNetcdfFileWriterVersion(NetcdfFileFormat format) -
convertToNetcdfFileFormat
-
getOutputFile
-
getFormat
-
findVariable
-
findDimension
-
findGlobalAttribute
-
calcSize
public long calcSize() -
write
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
Write data to the named variable, origin assumed to be 0.- Throws:
IOExceptionInvalidRangeException
-
write
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:
IOExceptionInvalidRangeException
-
updateAttribute
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
Flush anything written to disk.- Throws:
IOException
-
close
close the file.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
abort
Abort writing to this file. The file is closed.- Throws:
IOException
-