Package ucar.nc2
Class NetcdfFileWriteable
- java.lang.Object
-
- ucar.nc2.NetcdfFile
-
- ucar.nc2.NetcdfFileWriteable
-
- All Implemented Interfaces:
Closeable,AutoCloseable,ucar.nc2.util.cache.FileCacheable
public class NetcdfFileWriteable extends ucar.nc2.NetcdfFileDeprecated.use NetcdfFileWriterCreate/Write netCDF-3 formatted files.When a file is first created, it is in is "define mode", where the header objects (Dimensions, Attributes and Variables) may be added, deleted and modified, but no data may be written. Once create() is called, you can no longer modify the header, but you can now write data. An existing file is opened in write mode.
If setRedefine(true) is called, the file goes into define mode, and header objects can be changed. When setRedefine(false) is called, the new header is written, and the old file data is copied to the new file. This can be quite costly.
- See Also:
NetcdfFile
-
-
Field Summary
-
Fields inherited from class ucar.nc2.NetcdfFile
cache, dimensions, gattributes, id, iosp, IOSP_MESSAGE_ADD_RECORD_STRUCTURE, IOSP_MESSAGE_CONVERT_RECORD_STRUCTURE, IOSP_MESSAGE_GET_NETCDF_FILE_FORMAT, IOSP_MESSAGE_RANDOM_ACCESS_FILE, IOSP_MESSAGE_REMOVE_RECORD_STRUCTURE, location, rootGroup, title, variables
-
-
Constructor Summary
Constructors Modifier Constructor Description NetcdfFileWriteable()Deprecated.use createNew(String filename, boolean fill)NetcdfFileWriteable(String location)Deprecated.use openExisting(String filename, boolean fill)NetcdfFileWriteable(String location, boolean fill)Deprecated.use createNew(String filename, boolean fill)protectedNetcdfFileWriteable(ucar.nc2.iosp.IOServiceProviderWriter iospw, ucar.unidata.io.RandomAccessFile raf, String location, boolean fill, boolean isExisting)Deprecated.Open or create a new Netcdf file, put it into define mode to allow writing, using the provided IOSP and RAF.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description ucar.nc2.DimensionaddDimension(String dimName, int length)Deprecated.Add a Dimension to the file.ucar.nc2.DimensionaddDimension(String dimName, int length, boolean isShared, boolean isUnlimited, boolean isVariableLength)Deprecated.Add a Dimension to the file.ucar.nc2.AttributeaddGlobalAttribute(String name, Number value)Deprecated.Add a Global attribute of type Number to the file.ucar.nc2.AttributeaddGlobalAttribute(String name, String value)Deprecated.Add a Global attribute of type String to the file.ucar.nc2.AttributeaddGlobalAttribute(String name, ucar.ma2.Array values)Deprecated.Add a Global attribute of type Array to the file.ucar.nc2.AttributeaddGlobalAttribute(ucar.nc2.Attribute att)Deprecated.Add a Global attribute to the file.ucar.nc2.VariableaddStringVariable(String varName, List<ucar.nc2.Dimension> dims, int max_strlen)Deprecated.Add a variable with DataType = String to the file.ucar.nc2.DimensionaddUnlimitedDimension(String dimName)Deprecated.Add an unlimited Dimension to the file.ucar.nc2.VariableaddVariable(String varName, Class componentType, ucar.nc2.Dimension[] dims)Deprecated.use addVariable(String varName, DataType dataType, ArrayList dims);ucar.nc2.VariableaddVariable(String varName, ucar.ma2.DataType dataType, String dims)Deprecated.Add a variable to the file.ucar.nc2.VariableaddVariable(String shortName, ucar.ma2.DataType dataType, List<ucar.nc2.Dimension> dims)Deprecated.Add a variable to the file.ucar.nc2.VariableaddVariable(String varName, ucar.ma2.DataType dataType, ucar.nc2.Dimension[] dims)Deprecated.Add a variable to the file.voidaddVariableAttribute(String varName, String attName, Number value)Deprecated.Add an attribute of type Number to the named Variable.voidaddVariableAttribute(String varName, String attName, String value)Deprecated.Add an attribute of type String to the named Variable.voidaddVariableAttribute(String varName, String attName, ucar.ma2.Array value)Deprecated.Add an attribute of type Array to the named Variable.voidaddVariableAttribute(String varName, ucar.nc2.Attribute att)Deprecated.Add an attribute to the named Variable.voidclose()Deprecated.close the file.voidcreate()Deprecated.After you have added all of the Dimensions, Variables, and Attributes, call create() to actually create the file.static NetcdfFileWriteablecreateNew(String location)Deprecated.Create a new Netcdf file, with fill mode true.static NetcdfFileWriteablecreateNew(String location, boolean fill)Deprecated.Create a new Netcdf file, put it into define mode.ucar.nc2.AttributedeleteGlobalAttribute(String attName)Deprecated.Delete a global Attribute.ucar.nc2.AttributedeleteVariableAttribute(String varName, String attName)Deprecated.Delete a variable Attribute.voidflush()Deprecated.Flush anything written to disk.StringgetFileTypeDescription()Deprecated.StringgetFileTypeId()Deprecated.booleanisDefineMode()Deprecated.Is the file in define mode, which allows objects to be added and changed?static NetcdfFileWriteableopenExisting(String location)Deprecated.Open an existing Netcdf file for writing data.static NetcdfFileWriteableopenExisting(String location, boolean fill)Deprecated.Open an existing Netcdf file for writing data.ucar.nc2.DimensionrenameDimension(String oldName, String newName)Deprecated.Rename a Dimension.ucar.nc2.AttributerenameGlobalAttribute(String oldName, String newName)Deprecated.Rename a global Attribute.ucar.nc2.VariablerenameVariable(String oldName, String newName)Deprecated.Rename a Variable.ucar.nc2.AttributerenameVariableAttribute(String varName, String attName, String newName)Deprecated.Rename a variable Attribute.voidsetExtraHeaderBytes(int extraHeaderBytes)Deprecated.Set extra bytes to reserve in the header.voidsetFill(boolean fill)Deprecated.Set the fill flag: call before calling create() or doing any data writing.voidsetLargeFile(boolean isLargeFile)Deprecated.Set if this should be a "large file" (64-bit offset) format.voidsetLength(long size)Deprecated.Preallocate the file size, for efficiency.voidsetName(String filename)Deprecated.use NetcdfFileWriteable.createNew(String filename);booleansetRedefineMode(boolean redefineMode)Deprecated.Set the redefine mode.voidupdateAttribute(ucar.nc2.Variable v2, ucar.nc2.Attribute att)Deprecated.Update the value of an existing attribute.voidwrite(String fullNameEsc, int[] origin, ucar.ma2.Array values)Deprecated.Write data to the named variable.voidwrite(String fullNameEsc, ucar.ma2.Array values)Deprecated.Write data to the named variable, origin assumed to be 0.voidwriteStringData(String fullNameEsc, int[] origin, ucar.ma2.Array values)Deprecated.Write String data to a CHAR variable.voidwriteStringData(String varName, ucar.ma2.Array values)Deprecated.Write String data to a CHAR variable, origin assumed to be 0.-
Methods inherited from class ucar.nc2.NetcdfFile
addAttribute, addAttribute, addDimension, addGroup, addLocalFieldsToBuilder, addStringVariable, addVariable, addVariable, addVariableAttribute, builder, canonicalizeUriString, canOpen, empty, findAttribute, findAttValueIgnoreCase, findDimension, findGlobalAttribute, findGlobalAttributeIgnoreCase, findGroup, findVariable, findVariable, findVariableByAttribute, finish, getCacheName, getDetailInfo, getDetailInfo, getDimensions, getFileTypeVersion, getGlobalAttributes, getId, getIosp, getLastModified, getLocation, getRootGroup, getStructureIterator, getTitle, getUnlimitedDimension, getVariables, hasUnlimitedDimension, iospDeRegister, iospRegistered, makeFullName, makeFullName, makeFullNameSectionSpec, makeFullNameWithString, makeNameUnescaped, makeRecordStructure, makeValidCDLName, makeValidCdmObjectName, makeValidPathName, makeValidSectionSpecName, open, open, open, open, open, open, openInMemory, openInMemory, openInMemory, openInMemory, reacquire, read, readArrays, readAttributeDouble, readAttributeInteger, readData, readSection, readToByteChannel, readToOutputStream, registerIOProvider, registerIOProvider, registerIOProvider, registerIOProviderPreferred, release, removeDimension, removeRecordStructure, removeVariable, sendIospMessage, setCacheName, setDebugFlags, setFileCache, setId, setImmutable, setLocation, setProperty, setRootGroup, setTitle, showCached, showProxies, syncExtend, toBuilder, toNcml, toString, toStringDebug, writeCDL, writeCDL, writeCDL, writeNcml, writeNcml
-
-
-
-
Constructor Detail
-
NetcdfFileWriteable
protected NetcdfFileWriteable(ucar.nc2.iosp.IOServiceProviderWriter iospw, ucar.unidata.io.RandomAccessFile raf, String location, boolean fill, boolean isExisting) throws IOExceptionDeprecated.Open or create a new Netcdf file, put it into define mode to allow writing, using the provided IOSP and RAF.- Parameters:
iospw- IO service provider to use, if null use standardraf- Random access file to use, may be null if iospw islocation- open a new file at this locationfill- set fill modeisExisting- true if file already exists- Throws:
IOException- on I/O error
-
NetcdfFileWriteable
public NetcdfFileWriteable(String location, boolean fill)
Deprecated.use createNew(String filename, boolean fill)Create a new Netcdf file, put it into define mode.- Parameters:
location- open a new file at this locationfill- set fill mode
-
NetcdfFileWriteable
public NetcdfFileWriteable()
Deprecated.use createNew(String filename, boolean fill)Open a new Netcdf file, put it into define mode.
-
NetcdfFileWriteable
public NetcdfFileWriteable(String location) throws IOException
Deprecated.use openExisting(String filename, boolean fill)Open an existing Netcdf file for writing data.- Parameters:
location- open an existing file at this location- Throws:
IOException- on read error
-
-
Method Detail
-
openExisting
public static NetcdfFileWriteable openExisting(String location) throws IOException
Deprecated.Open an existing Netcdf file for writing data. Fill mode is true. 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
-
openExisting
public static NetcdfFileWriteable openExisting(String location, boolean fill) throws IOException
Deprecated.Open an existing Netcdf file for writing data. Cannot add new objects, you can only read/write data to existing Variables. Setting fill = false is more efficient, use when you know you will write all data.- Parameters:
location- name of existing file to open.fill- if true, the data is first written with fill values.- Returns:
- existing file that can be written to
- Throws:
IOException- on I/O error
-
createNew
public static NetcdfFileWriteable createNew(String location) throws IOException
Deprecated.Create a new Netcdf file, with fill mode true.- Parameters:
location- name of new file to open; if it exists, will overwrite it.- Returns:
- new file that can be written to
- Throws:
IOException- on I/O error
-
createNew
public static NetcdfFileWriteable createNew(String location, boolean fill) throws IOException
Deprecated.Create a new Netcdf file, put it into define mode. Make calls to addXXX(), then when all objects are added, call create(). You cannot read or write data until create() is called. Setting fill = false is more efficient, use when you know you will write all data.- Parameters:
location- name of new file to open; if it exists, will overwrite it.fill- if true, the data is first written with fill values. Leave false if you expect to write all data values, set to true if you want to be sure that unwritten data values have the fill value in it. (default is false)- Returns:
- new file that can be written to
- Throws:
IOException- on I/O error
-
setFill
public void setFill(boolean fill)
Deprecated.Set the fill flag: call before calling create() or doing any data writing. If true, the data is first written with fill values. Default is fill = false. Leave false if you expect to write all data values, set to true if you want to be sure that unwritten data values have the fill value in it.- Parameters:
fill- set fill mode true or false
-
setLength
public void setLength(long size)
Deprecated.Preallocate the file size, for efficiency. Must be in define mode Must call before create() to have any affect.- Parameters:
size- if set to > 0, set length of file to this upon creation - this (usually) pre-allocates contiguous storage.
-
setLargeFile
public void setLargeFile(boolean isLargeFile)
Deprecated.Set if this should be a "large file" (64-bit offset) format. Must be in define mode- Parameters:
isLargeFile- true if large file
-
setExtraHeaderBytes
public void setExtraHeaderBytes(int extraHeaderBytes)
Deprecated.Set extra bytes to reserve in the header. This can prevent rewriting the entire file on redefine. Must be in define mode- Parameters:
extraHeaderBytes- # bytes extra for the header
-
isDefineMode
public boolean isDefineMode()
Deprecated.Is the file in define mode, which allows objects to be added and changed?- Returns:
- true if the file in define mode
-
addDimension
public ucar.nc2.Dimension addDimension(String dimName, int length)
Deprecated.Add a Dimension to the file. Must be in define mode.- Parameters:
dimName- name of dimensionlength- size of dimension.- Returns:
- the created dimension
-
addDimension
public ucar.nc2.Dimension addDimension(String dimName, int length, boolean isShared, boolean isUnlimited, boolean isVariableLength)
Deprecated.Add a Dimension to the file. Must be in define mode.- Parameters:
dimName- name of dimensionlength- size of dimension.isShared- if dimension is sharedisUnlimited- if dimension is unlimitedisVariableLength- if dimension is variable length- Returns:
- the created dimension
-
addUnlimitedDimension
public ucar.nc2.Dimension addUnlimitedDimension(String dimName)
Deprecated.Add an unlimited Dimension to the file. Must be in define mode.- Parameters:
dimName- name of unlimited dimension- Returns:
- the created dimension
-
renameDimension
public ucar.nc2.Dimension renameDimension(String oldName, String newName)
Deprecated.Rename a Dimension. Must be in define mode.- Parameters:
oldName- existing dimension has this namenewName- rename to this- Returns:
- renamed dimension, or null if not found
-
addGlobalAttribute
public ucar.nc2.Attribute addGlobalAttribute(ucar.nc2.Attribute att)
Deprecated.Add a Global attribute to the file. Must be in define mode.- Parameters:
att- the attribute.- Returns:
- the created attribute
-
addGlobalAttribute
public ucar.nc2.Attribute addGlobalAttribute(String name, String value)
Deprecated.Add a Global attribute of type String to the file. Must be in define mode.- Parameters:
name- name of attribute.value- value of atribute.- Returns:
- the created attribute
-
addGlobalAttribute
public ucar.nc2.Attribute addGlobalAttribute(String name, Number value)
Deprecated.Add a Global attribute of type Number to the file. Must be in define mode.- Parameters:
name- name of attribute.value- must be of type Float, Double, Integer, Short or Byte- Returns:
- the created attribute
-
addGlobalAttribute
public ucar.nc2.Attribute addGlobalAttribute(String name, ucar.ma2.Array values)
Deprecated.Add a Global attribute of type Array to the file. Must be in define mode.- Parameters:
name- name of attribute.values- Array of values- Returns:
- the created attribute
-
deleteGlobalAttribute
public ucar.nc2.Attribute deleteGlobalAttribute(String attName)
Deprecated.Delete a global Attribute. Must be in define mode.- Parameters:
attName- existing Attribute has this name- Returns:
- deleted Attribute, or null if not found
-
renameGlobalAttribute
public ucar.nc2.Attribute renameGlobalAttribute(String oldName, String newName)
Deprecated.Rename a global Attribute. Must be in define mode.- Parameters:
oldName- existing Attribute has this namenewName- rename to this- Returns:
- renamed Attribute, or null if not found
-
addVariable
public ucar.nc2.Variable addVariable(String varName, ucar.ma2.DataType dataType, ucar.nc2.Dimension[] dims)
Deprecated.Add a variable to the file. Must be in define mode.- Parameters:
varName- name of Variable, must be unique with the file.dataType- type of underlying elementdims- array of Dimensions for the variable, must already have been added. Use an array of length 0 for a scalar variable.- Returns:
- the Variable that has been added
-
addVariable
public ucar.nc2.Variable addVariable(String varName, ucar.ma2.DataType dataType, String dims)
Deprecated.Add a variable to the file. Must be in define mode.- Parameters:
varName- name of Variable, must be unique with the file.dataType- type of underlying elementdims- names of Dimensions for the variable, blank seperated. Must already have been added. Use an empty string for a scalar variable.- Returns:
- the Variable that has been added
-
addVariable
public ucar.nc2.Variable addVariable(String shortName, ucar.ma2.DataType dataType, List<ucar.nc2.Dimension> dims)
Deprecated.Add a variable to the file. Must be in define mode.- Parameters:
shortName- name of Variable, must be unique with the file.dataType- type of underlying elementdims- list of Dimensions for the variable, must already have been added. Use a list of length 0 for a scalar variable.- Returns:
- the Variable that has been added
-
addStringVariable
public ucar.nc2.Variable addStringVariable(String varName, List<ucar.nc2.Dimension> dims, int max_strlen)
Deprecated.Add a variable with DataType = String to the file. Must be in define mode. The variable will be stored in the file as a CHAR variable. A new dimension with name "varName_strlen" is automatically added, with length max_strlen.- Parameters:
varName- name of Variable, must be unique within the file.dims- list of Dimensions for the variable, must already have been added. Use a list of length 0 for a scalar variable. Do not include the string length dimension.max_strlen- maximum string length.- Returns:
- the Variable that has been added
-
renameVariable
public ucar.nc2.Variable renameVariable(String oldName, String newName)
Deprecated.Rename a Variable. Must be in define mode.- Parameters:
oldName- existing Variable has this namenewName- rename to this- Returns:
- renamed Variable, or null if not found
-
addVariableAttribute
public void addVariableAttribute(String varName, ucar.nc2.Attribute att)
Deprecated.Add an attribute to the named Variable. Must be in define mode.- Parameters:
varName- name of variable. must already have been added to the file.att- Attribute to add.
-
addVariableAttribute
public void addVariableAttribute(String varName, String attName, String value)
Deprecated.Add an attribute of type String to the named Variable. Must be in define mode.- Parameters:
varName- name of variable. must already have been added to the file.attName- name of attribute.value- String value of atribute.
-
addVariableAttribute
public void addVariableAttribute(String varName, String attName, Number value)
Deprecated.Add an attribute of type Number to the named Variable. Must be in define mode.- Parameters:
varName- name of variable. IllegalArgumentException if not valid name.attName- name of attribute.value- must be of type Float, Double, Integer, Short or Byte
-
addVariableAttribute
public void addVariableAttribute(String varName, String attName, ucar.ma2.Array value)
Deprecated.Add an attribute of type Array to the named Variable. Must be in define mode.- Parameters:
varName- name of variable. IllegalArgumentException if not valid name.attName- name of attribute.value- Array of valkues
-
deleteVariableAttribute
public ucar.nc2.Attribute deleteVariableAttribute(String varName, String attName)
Deprecated.Delete a variable Attribute. Must be in define mode.- Parameters:
varName- existing Variable nameattName- existing Attribute has this name- Returns:
- deleted Attribute, or null if not found
-
renameVariableAttribute
public ucar.nc2.Attribute renameVariableAttribute(String varName, String attName, String newName)
Deprecated.Rename a variable Attribute. Must be in define mode.- Parameters:
varName- existing Variable nameattName- existing Attribute has this namenewName- rename to this- Returns:
- renamed Attribute, or null if not found
-
updateAttribute
public void updateAttribute(ucar.nc2.Variable v2, ucar.nc2.Attribute att) throws IOExceptionDeprecated.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.- Parameters:
v2- variable, or null for global attributeatt- replace with this value- Throws:
IOException- if I/O error
-
create
public void create() throws IOExceptionDeprecated.After you have added all of the Dimensions, Variables, and Attributes, call create() to actually create the file. You must be in define mode. After this call, you are no longer in define mode.- Throws:
IOException- if I/O error
-
setRedefineMode
public boolean setRedefineMode(boolean redefineMode) throws IOExceptionDeprecated.Set the redefine mode. Designed to emulate nc_redef (redefineMode = true) and nc_enddef (redefineMode = false)- Parameters:
redefineMode- start or end define mode- Returns:
- true if it had to rewrite the entire file, false if it wrote the header in place
- Throws:
IOException- on read/write error
-
write
public void write(String fullNameEsc, ucar.ma2.Array values) throws IOException, ucar.ma2.InvalidRangeException
Deprecated.Write data to the named variable, origin assumed to be 0. Must not be in define mode.- Parameters:
fullNameEsc- name of variable. IllegalArgumentException if variable name does not exist.values- write this array; must be same type and rank as Variable- Throws:
IOException- if I/O errorucar.ma2.InvalidRangeException- if values Array has illegal shape
-
write
public void write(String fullNameEsc, int[] origin, ucar.ma2.Array values) throws IOException, ucar.ma2.InvalidRangeException
Deprecated.Write data to the named variable. Must not be in define mode.- Parameters:
fullNameEsc- full, escaped name of variable. IllegalArgumentException if variable name does not exist.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 errorucar.ma2.InvalidRangeException- if values Array has illegal shape
-
writeStringData
public void writeStringData(String varName, ucar.ma2.Array values) throws IOException, ucar.ma2.InvalidRangeException
Deprecated.Write String data to a CHAR variable, origin assumed to be 0. Must not be in define mode.- Parameters:
varName- name of variable, must be of type CHAR.values- write this array; must be ArrayObject of String- Throws:
IOException- if I/O errorucar.ma2.InvalidRangeException- if values Array has illegal shape
-
writeStringData
public void writeStringData(String fullNameEsc, int[] origin, ucar.ma2.Array values) throws IOException, ucar.ma2.InvalidRangeException
Deprecated.Write String data to a CHAR variable. Must not be in define mode.- Parameters:
fullNameEsc- name of variable, must be of type CHAR.origin- offset to start writing, ignore the strlen dimension.values- write this array; must be ArrayObject of String- Throws:
IOException- if I/O errorucar.ma2.InvalidRangeException- if values Array has illegal shape
-
flush
public void flush() throws IOExceptionDeprecated.Flush anything written to disk.- Throws:
IOException- if I/O error
-
close
public void close() throws IOExceptionDeprecated.close the file.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceucar.nc2.util.cache.FileCacheable- Overrides:
closein classucar.nc2.NetcdfFile- Throws:
IOException- if I/O error
-
getFileTypeId
public String getFileTypeId()
Deprecated.- Overrides:
getFileTypeIdin classucar.nc2.NetcdfFile
-
getFileTypeDescription
public String getFileTypeDescription()
Deprecated.- Overrides:
getFileTypeDescriptionin classucar.nc2.NetcdfFile
-
setName
public void setName(String filename)
Deprecated.use NetcdfFileWriteable.createNew(String filename);Set the filename of a new file to be created: call before calling create().- Parameters:
filename- name of new file to create.
-
addVariable
public ucar.nc2.Variable addVariable(String varName, Class componentType, ucar.nc2.Dimension[] dims)
Deprecated.use addVariable(String varName, DataType dataType, ArrayList dims);Add a variable to the file. Must be in define mode.- Parameters:
varName- name of Variable, must be unique with the file.componentType- type of underlying element: String, double or Double, etc.dims- array of Dimensions for the variable, must already have been added.- Returns:
- the Varible added
-
-