Package ucar.nc2
Class NetcdfFile
java.lang.Object
ucar.nc2.NetcdfFile
- All Implemented Interfaces:
Closeable,AutoCloseable,ucar.nc2.util.cache.FileCacheable
- Direct Known Subclasses:
NetcdfDataset
Read-only scientific datasets that are accessible through the netCDF API.
Immutable after setImmutable() is called. Reading data is not
thread-safe because of the use of RandomAccessFile.
Using this class's Builder scheme to create a NetcdfFile object could, for
example, be accomplished as follows, using a try/finally block to ensure that the
NetcdfFile is closed when done.
NetcdfFile ncfile = null;
try {
ncfile = NetcdfFile.builder().setLocation(fileName).build();
// do stuff
} finally {
if (ncfile != null) {
ncfile.close();
}
}
More conveniently, a NetcdfFile object may be created using one of the static methods
in NetcdfFiles:
NetcdfFile ncfile = null;
try {
ncfile = NetcdfFiles.open(fileName);
// do stuff
} finally {
if (ncfile != null) {
ncfile.close();
}
}
Or better yet, use try-with-resources:
try (NetcdfFile ncfile = NetcdfFiles.open(fileName)) {
// do stuff
}
Naming
Each object has a name (aka "full name") that is unique within the entire netcdf file, and a "short name" that is unique within the parent group. These coincide for objects in the root group, and so are backwards compatible with version 3 files.- Variable: group1/group2/varname
- Structure member Variable: group1/group2/varname.s1.s2
- Group Attribute: group1/group2@attName
- Variable Attribute: group1/group2/varName@attName
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classNetcdfFile.Builder<T extends NetcdfFile.Builder<T>>A builder of NetcdfFile objects. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic NetcdfFile.Builder<?>builder()Get Builder for this class.voidclose()Close all resources (files, sockets, etc) associated with this file.findAttribute(String fullNameEscaped) Find an attribute, with the specified (escaped full) name.findDimension(String fullName) Finds a Dimension with the specified full name.findGlobalAttribute(String attName) Look up an Attribute by (short) name in the root Group or nested Groups, exact match.Look up an Attribute by (short) name in the root Group or nested Groups, ignore case.Find a Group, with the specified (full) name.findVariable(String fullNameEscaped) Find a Variable, with the specified (escaped full) name.Show debug / underlying implementation detailsvoidGet a human-readable description for this file type.Get the file type id for the underlying data source.Get the version of this file type.com.google.common.collect.ImmutableList<Attribute>Returns the set of global attributes associated with this file, which are the attributes associated with the root group, or any subgroup.getId()Get the globally unique dataset identifier, if it exists.Get the NetcdfFile location.Get the root group.getTitle()Get the human-readable title, if it exists.Return the unlimited (record) dimension, or null if not exist.com.google.common.collect.ImmutableList<Variable>Get all of the variables in the file, in all groups.booleanReturn true if this file has one or more unlimited (record) dimension.readSection(String variableSection) Read a variable using the given section specification.sendIospMessage(Object message) Generic way to send a "message" to the underlying IOSP.Turn into a mutable Builder.NcML representation of Netcdf header info, non stricttoString()CDL representation of Netcdf header info, non strictvoidwriteNcml(OutputStream os, String uri) Write the NcML representation: dont show coordinate valuesvoidWrite the NcML representation: dont show coordinate valuesMethods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface ucar.nc2.util.cache.FileCacheable
getLastModified, reacquire, release, setFileCache
-
Field Details
-
IOSP_MESSAGE_RANDOM_ACCESS_FILE
- See Also:
-
IOSP_MESSAGE_GET_NETCDF_FILE_FORMAT
- See Also:
-
-
Method Details
-
close
Close all resources (files, sockets, etc) associated with this file. If the underlying file was acquired, it will be released, otherwise closed. if isClosed() already, nothing will happen- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceucar.nc2.util.cache.FileCacheable- Throws:
IOException- if error when closing
-
getLocation
Get the NetcdfFile location. This is a URL, or a file pathname.- Specified by:
getLocationin interfaceucar.nc2.util.cache.FileCacheable- Returns:
- location URL or file pathname.
-
getId
Get the globally unique dataset identifier, if it exists.- Returns:
- id, or null if none.
-
getTitle
Get the human-readable title, if it exists.- Returns:
- title, or null if none.
-
getRootGroup
Get the root group.- Returns:
- root group
-
findGroup
Find a Group, with the specified (full) name. A full name should start with a '/'. For backwards compatibility, we accept full names that omit the leading '/'. An embedded '/' separates subgroup names.- Parameters:
fullName- eg "/group/subgroup/wantGroup". Null or empty string returns the root group.- Returns:
- Group or null if not found.
-
findVariable
Find a Variable, with the specified (escaped full) name. It may possibly be nested in multiple groups and/or structures. An embedded "." is interpreted as structure.member. An embedded "/" is interpreted as group/variable. If the name actually has a ".", you must escape it (call NetcdfFiles.makeValidPathName(varname)) Any other chars may also be escaped, as they are removed before testing.- Parameters:
fullNameEscaped- eg "/group/subgroup/name1.name2.name".- Returns:
- Variable or null if not found.
-
findDimension
Finds a Dimension with the specified full name. It may be nested in multiple groups. An embedded "/" is interpreted as a group separator. A leading slash indicates the root group. That slash may be omitted, but thefullNamewill be treated as if it were there. In other words, the first name token infullNameis treated as the short name of a Group or Dimension, relative to the root group.- Parameters:
fullName- Dimension full name, e.g. "/group/subgroup/dim".- Returns:
- the Dimension or
nullif it wasn't found.
-
hasUnlimitedDimension
public boolean hasUnlimitedDimension()Return true if this file has one or more unlimited (record) dimension.- Returns:
- if this file has an unlimited Dimension(s)
-
getUnlimitedDimension
Return the unlimited (record) dimension, or null if not exist. If there are multiple unlimited dimensions, it will return the first one.- Returns:
- the unlimited Dimension, or null if none.
-
getVariables
Get all of the variables in the file, in all groups. Alternatively, use groups. -
getGlobalAttributes
Returns the set of global attributes associated with this file, which are the attributes associated with the root group, or any subgroup. Alternatively, use groups. -
findGlobalAttribute
Look up an Attribute by (short) name in the root Group or nested Groups, exact match.- Parameters:
attName- the name of the attribute- Returns:
- the first Group attribute with given name, or null if not found
-
findGlobalAttributeIgnoreCase
Look up an Attribute by (short) name in the root Group or nested Groups, ignore case.- Parameters:
name- the name of the attribute- Returns:
- the first group attribute with given Attribute name, ignoronmg case, or null if not found
-
findAttribute
Find an attribute, with the specified (escaped full) name. It may possibly be nested in multiple groups and/or structures. An embedded "." is interpreted as structure.member. An embedded "/" is interpreted as group/group or group/variable. An embedded "@" is interpreted as variable@attribute If the name actually has a ".", you must escape it (call NetcdfFiles.makeValidPathName(varname)) Any other chars may also be escaped, as they are removed before testing.- Parameters:
fullNameEscaped- eg "@attName", "/group/subgroup/@attName" or "/group/subgroup/varname.name2.name@attName"- Returns:
- Attribute or null if not found.
-
toString
CDL representation of Netcdf header info, non strict -
toNcml
NcML representation of Netcdf header info, non strict -
writeNcml
Write the NcML representation: dont show coordinate values- Parameters:
os- : write to this OutputStream. Will be closed at end of the method.uri- use this for the url attribute; if null use getLocation(). // ??- Throws:
IOException- if error
-
writeNcml
Write the NcML representation: dont show coordinate values- Parameters:
writer- : write to this Writer, should have encoding of UTF-8. Will be closed at end of the method.uri- use this for the url attribute; if null use getLocation().- Throws:
IOException- if error
-
sendIospMessage
Generic way to send a "message" to the underlying IOSP. This message is sent after the file is open. To affect the creation of the file, use a factory method like NetcdfFile.open(). In ver6, IOSP_MESSAGE_ADD_RECORD_STRUCTURE, IOSP_MESSAGE_REMOVE_RECORD_STRUCTURE will not work here.- Parameters:
message- iosp specific message- Returns:
- iosp specific return, may be null
-
readSection
Read a variable using the given section specification. The result is always an array of the type of the innermost variable. Its shape is the accumulation of all the shapes of its parent structures.- Parameters:
variableSection- the constraint expression.- Returns:
- data requested
- Throws:
IOException- if errorInvalidRangeException- if variableSection is invalid- See Also:
-
getDetailInfo
Show debug / underlying implementation details -
getDetailInfo
-
getFileTypeId
Get the file type id for the underlying data source.- Returns:
- registered id of the file type
- See Also:
-
- "https://www.unidata.ucar.edu/software/netcdf-java/formats/FileTypes.html"
-
getFileTypeDescription
Get a human-readable description for this file type.- Returns:
- description of the file type
- See Also:
-
- "https://www.unidata.ucar.edu/software/netcdf-java/formats/FileTypes.html"
-
getFileTypeVersion
Get the version of this file type.- Returns:
- version of the file type
- See Also:
-
- "https://www.unidata.ucar.edu/software/netcdf-java/formats/FileTypes.html"
-
toBuilder
Turn into a mutable Builder. Can use toBuilder().build() to copy. -
builder
Get Builder for this class. Allows subclassing.- See Also:
-
- "https://community.oracle.com/blogs/emcmanus/2010/10/24/using-builder-pattern-subclasses"
-