public class NetcdfFile extends Object implements ucar.nc2.util.cache.FileCacheable, Closeable
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
Modifier and Type | Class and Description |
---|---|
static class |
NetcdfFile.Builder<T extends NetcdfFile.Builder<T>>
A builder of NetcdfFile objects.
|
Modifier and Type | Field and Description |
---|---|
static String |
IOSP_MESSAGE_ADD_RECORD_STRUCTURE |
static String |
IOSP_MESSAGE_CONVERT_RECORD_STRUCTURE |
static String |
IOSP_MESSAGE_RANDOM_ACCESS_FILE |
static String |
IOSP_MESSAGE_REMOVE_RECORD_STRUCTURE |
Constructor and Description |
---|
NetcdfFile(String filename)
Deprecated.
use NetcdfFile.open( location) or NetcdfDataset.openFile( location)
|
NetcdfFile(URL url)
Deprecated.
use NetcdfFile.open( http:location) or NetcdfDataset.openFile( http:location)
|
Modifier and Type | Method and Description |
---|---|
Attribute |
addAttribute(Group parent,
Attribute att)
Deprecated.
Use NetcdfFile.builder()
|
Attribute |
addAttribute(Group parent,
String name,
String value)
Deprecated.
Use NetcdfFile.builder()
|
Dimension |
addDimension(Group parent,
Dimension d)
Deprecated.
Use NetcdfFile.builder()
|
Group |
addGroup(Group parent,
Group g)
Deprecated.
Use NetcdfFile.builder()
|
Variable |
addStringVariable(Group g,
String shortName,
String dims,
int strlen)
Deprecated.
Use NetcdfFile.builder()
|
Variable |
addVariable(Group g,
String shortName,
DataType dtype,
String dims)
Deprecated.
Use NetcdfFile.builder()
|
Variable |
addVariable(Group g,
Variable v)
Deprecated.
Use NetcdfFile.builder()
|
Attribute |
addVariableAttribute(Variable v,
Attribute att)
Deprecated.
Use NetcdfFile.builder()
|
static NetcdfFile.Builder<?> |
builder()
Get Builder for this class.
|
static String |
canonicalizeUriString(String location)
Deprecated.
use NetcdfFiles.canonicalizeUriString
|
static boolean |
canOpen(String location)
Deprecated.
use NetcdfFiles.canOpen
|
void |
close()
Close all resources (files, sockets, etc) associated with this file.
|
void |
empty()
Deprecated.
|
Attribute |
findAttribute(String fullNameEscaped)
Find an attribute, with the specified (escaped full) name.
|
String |
findAttValueIgnoreCase(Variable v,
String attName,
String defaultValue)
Deprecated.
use getRootGroup() or Variable findAttValueIgnoreCase().
|
Dimension |
findDimension(String fullName)
Finds a Dimension with the specified full name.
|
Attribute |
findGlobalAttribute(String name)
Look up global Attribute by (full) name.
|
Attribute |
findGlobalAttributeIgnoreCase(String name)
Look up global Attribute by name, ignore case.
|
Group |
findGroup(String fullName)
Find a Group, with the specified (full) name.
|
Variable |
findVariable(Group g,
String shortName)
Find a Variable by short name, in the given group.
|
Variable |
findVariable(String fullNameEscaped)
Find a Variable, with the specified (escaped full) name.
|
Variable |
findVariableByAttribute(Group g,
String attName,
String attValue)
Look in the given Group and in its nested Groups for a Variable with a String valued Attribute with the given name
and value.
|
void |
finish()
Deprecated.
Use NetcdfFile.builder()
|
String |
getCacheName()
Deprecated.
|
String |
getDetailInfo()
Deprecated.
do not use
|
void |
getDetailInfo(Formatter f)
Deprecated.
do not use
|
List<Dimension> |
getDimensions()
Get the shared Dimensions used in this file.
|
String |
getFileTypeDescription()
Get a human-readable description for this file type.
|
String |
getFileTypeId()
Get the file type id for the underlying data source.
|
String |
getFileTypeVersion()
Get the version of this file type.
|
List<Attribute> |
getGlobalAttributes()
Deprecated.
use getRootGroup().getAttributeContainer()
|
String |
getId()
Get the globally unique dataset identifier, if it exists.
|
IOServiceProvider |
getIosp()
Deprecated.
do not use.
|
long |
getLastModified()
Deprecated.
|
String |
getLocation()
Get the NetcdfFile location.
|
Group |
getRootGroup()
Get the root group.
|
String |
getTitle()
Get the human-readable title, if it exists.
|
Dimension |
getUnlimitedDimension()
Return the unlimited (record) dimension, or null if not exist.
|
List<Variable> |
getVariables()
Get all of the variables in the file, in all groups.
|
boolean |
hasUnlimitedDimension()
Return true if this file has one or more unlimited (record) dimension.
|
static boolean |
iospDeRegister(Class iospClass)
Deprecated.
use NetcdfFiles.iospDeRegister
|
static boolean |
iospRegistered(Class iospClass)
Deprecated.
use NetcdfFiles.iospRegistered
|
static String |
makeNameUnescaped(String vname)
Deprecated.
use NetcdfFiles.makeNameUnescaped
|
static String |
makeValidCDLName(String vname)
Deprecated.
use NetcdfFiles.makeValidCDLName
|
static String |
makeValidCdmObjectName(String shortName)
Deprecated.
use NetcdfFiles.makeValidCdmObjectName
|
static String |
makeValidPathName(String vname)
Deprecated.
use NetcdfFiles.makeValidPathName
|
static String |
makeValidSectionSpecName(String vname)
Deprecated.
use NetcdfFiles.makeValidSectionSpecName
|
static NetcdfFile |
open(RandomAccessFile raf,
String location,
CancelTask cancelTask,
Object iospMessage)
Deprecated.
use NetcdfFiles.open
|
static NetcdfFile |
open(String location)
Deprecated.
use NetcdfFiles.open
|
static NetcdfFile |
open(String location,
CancelTask cancelTask)
Deprecated.
use NetcdfFiles.open
|
static NetcdfFile |
open(String location,
int buffer_size,
CancelTask cancelTask)
Deprecated.
use NetcdfFiles.open
|
static NetcdfFile |
open(String location,
int buffer_size,
CancelTask cancelTask,
Object iospMessage)
Deprecated.
use NetcdfFiles.open
|
static NetcdfFile |
open(String location,
String iospClassName,
int bufferSize,
CancelTask cancelTask,
Object iospMessage)
Deprecated.
use NetcdfFiles.open
|
static NetcdfFile |
openInMemory(String filename)
Deprecated.
use NetcdfFiles.openInMemory
|
static NetcdfFile |
openInMemory(String name,
byte[] data)
Deprecated.
use NetcdfFiles.openInMemory
|
static NetcdfFile |
openInMemory(String name,
byte[] data,
String iospClassName)
Deprecated.
use NetcdfFiles.openInMemory
|
static NetcdfFile |
openInMemory(URI uri)
Deprecated.
use NetcdfFiles.openInMemory
|
void |
reacquire() |
Array |
read(String variableSection,
boolean flatten)
Deprecated.
use readSection(), flatten=false no longer supported
|
List<Array> |
readArrays(List<Variable> variables)
Deprecated.
will be moved to DODSNetcdfFile in version 6.
|
double |
readAttributeDouble(Variable v,
String attName,
double defValue)
Deprecated.
use Group.attributes().findAttributeDouble
|
int |
readAttributeInteger(Variable v,
String attName,
int defValue)
Deprecated.
use Group.attributes().findAttributeInteger
|
Array |
readSection(String variableSection)
Read a variable using the given section specification.
|
static void |
registerIOProvider(Class iospClass)
Deprecated.
use NetcdfFiles.registerIOProvider
|
static void |
registerIOProvider(Class iospClass,
boolean last)
Deprecated.
use NetcdfFiles.registerIOProvider
|
static void |
registerIOProvider(String className)
Deprecated.
use NetcdfFiles.registerIOProvider
|
static void |
registerIOProviderPreferred(Class iospClass,
Class target)
Deprecated.
use NetcdfFiles.registerIOProvider
|
void |
release() |
boolean |
removeDimension(Group g,
String dimName)
Deprecated.
Use NetcdfFile.builder()
|
boolean |
removeVariable(Group g,
String varName)
Deprecated.
Use NetcdfFile.builder()
|
Object |
sendIospMessage(Object message)
Deprecated.
Use NetcdfFile.builder()
|
static void |
setDebugFlags(DebugFlags debugFlag)
debugging
|
void |
setFileCache(ucar.nc2.util.cache.FileCacheIF cache)
Deprecated.
|
void |
setId(String id)
Deprecated.
Use NetcdfFile.builder()
|
NetcdfFile |
setImmutable()
Deprecated.
Use NetcdfFile.builder()
|
void |
setLocation(String location)
Deprecated.
Use NetcdfFile.builder()
|
static void |
setProperty(String name,
String value)
Deprecated.
do not use
|
void |
setRootGroup(Group rootGroup)
Deprecated.
Use NetcdfFile.builder()
|
void |
setTitle(String title)
Deprecated.
Use NetcdfFile.builder()
|
boolean |
syncExtend()
Deprecated.
do not use
|
NetcdfFile.Builder<?> |
toBuilder()
Turn into a mutable Builder.
|
String |
toNcML(String url)
Deprecated.
use NcMLWriter
|
String |
toString()
CDL representation of Netcdf header info, non strict
|
void |
writeCDL(OutputStream out,
boolean strict)
Deprecated.
use CDLWriter
|
void |
writeCDL(PrintWriter pw,
boolean strict)
Deprecated.
use CDLWriter
|
void |
writeNcML(OutputStream os,
String uri)
Deprecated.
will move in ver 6
|
void |
writeNcML(Writer writer,
String uri)
Deprecated.
will move in ver 6
|
public static final String IOSP_MESSAGE_ADD_RECORD_STRUCTURE
public static final String IOSP_MESSAGE_CONVERT_RECORD_STRUCTURE
public static final String IOSP_MESSAGE_REMOVE_RECORD_STRUCTURE
public static final String IOSP_MESSAGE_RANDOM_ACCESS_FILE
@Deprecated public NetcdfFile(String filename) throws IOException
filename
- locationIOException
- if error@Deprecated public NetcdfFile(URL url) throws IOException
url
- HTTP URL locationIOException
- if errorpublic static void setDebugFlags(DebugFlags debugFlag)
debugFlag
- debug flags@Deprecated public static void registerIOProvider(String className) throws IllegalAccessException, InstantiationException, ClassNotFoundException
className
- Class that implements IOServiceProvider.IllegalAccessException
- if class is not accessible.InstantiationException
- if class doesnt have a no-arg constructor.ClassNotFoundException
- if class not found.@Deprecated public static void registerIOProvider(Class iospClass) throws IllegalAccessException, InstantiationException
iospClass
- Class that implements IOServiceProvider.IllegalAccessException
- if class is not accessible.InstantiationException
- if class doesnt have a no-arg constructor.ClassCastException
- if class doesnt implement IOServiceProvider interface.@Deprecated public static void registerIOProvider(Class iospClass, boolean last) throws IllegalAccessException, InstantiationException
iospClass
- Class that implements IOServiceProvider.last
- true=>insert at the end of the list; otherwise frontIllegalAccessException
- if class is not accessible.InstantiationException
- if class doesnt have a no-arg constructor.ClassCastException
- if class doesnt implement IOServiceProvider interface.@Deprecated public static void registerIOProviderPreferred(Class iospClass, Class target) throws IllegalAccessException, InstantiationException
iospClass
- Class that implements IOServiceProvider.target
- Class to overrideIllegalAccessException
- if class is not accessible.InstantiationException
- if class doesnt have a no-arg constructor.ClassCastException
- if class doesnt implement IOServiceProvider interface.@Deprecated public static boolean iospRegistered(Class iospClass)
iospClass
- Class for which to search@Deprecated public static boolean iospDeRegister(Class iospClass)
iospClass
- Class for which to search and remove@Deprecated public static void setProperty(String name, String value)
name
- name of propertyvalue
- value of property@Deprecated public static NetcdfFile open(String location) throws IOException
location
- location of file.IOException
- if error@Deprecated public static NetcdfFile open(String location, CancelTask cancelTask) throws IOException
location
- location of the file.cancelTask
- allow task to be cancelled; may be null.IOException
- if error@Deprecated public static NetcdfFile open(String location, int buffer_size, CancelTask cancelTask) throws IOException
location
- location of file.buffer_size
- RandomAccessFile buffer size, if <= 0, use default sizecancelTask
- allow task to be cancelled; may be null.IOException
- if error@Deprecated public static NetcdfFile open(String location, int buffer_size, CancelTask cancelTask, Object iospMessage) throws IOException
location
- location of file. This may be a
buffer_size
- RandomAccessFile buffer size, if <= 0, use default sizecancelTask
- allow task to be cancelled; may be null.iospMessage
- special iosp tweaking (sent before open is called), may be nullIOException
- if error@Deprecated public static boolean canOpen(String location) throws IOException
location
- same as openIOException
- on read error@Deprecated public static NetcdfFile open(String location, String iospClassName, int bufferSize, CancelTask cancelTask, Object iospMessage) throws ClassNotFoundException, IllegalAccessException, InstantiationException, IOException
location
- location of fileiospClassName
- fully qualified class name of the IOSP class to handle this filebufferSize
- RandomAccessFile buffer size, if <= 0, use default sizecancelTask
- allow task to be cancelled; may be null.iospMessage
- special iosp tweaking (sent before open is called), may be nullIOException
- if read errorClassNotFoundException
- cannat find iospClassName in thye class pathInstantiationException
- if class cannot be instantiatedIllegalAccessException
- if class is not accessible@Deprecated public static String canonicalizeUriString(String location)
"file:"
or "file://"
prefix from the location, if necessary. Also replaces
back slashes with forward slashes.location
- a URI string.@Deprecated public static NetcdfFile openInMemory(String name, byte[] data, String iospClassName) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException
name
- name of the dataset. Typically use the filename or URI.data
- in-memory netcdf fileiospClassName
- fully qualified class name of the IOSP class to handle this fileIOException
- if read errorClassNotFoundException
- cannat find iospClassName in the class pathInstantiationException
- if class cannot be instantiatedIllegalAccessException
- if class is not accessible@Deprecated public static NetcdfFile openInMemory(String name, byte[] data) throws IOException
name
- name of the dataset. Typically use the filename or URI.data
- in-memory netcdf fileIOException
- if error@Deprecated public static NetcdfFile openInMemory(String filename) throws IOException
filename
- location of CDM file, must be a local file.IOException
- if error reading file@Deprecated public static NetcdfFile openInMemory(URI uri) throws IOException
uri
- location of CDM file, must be accessible through url.toURL().openStream().IOException
- if error reading file@Deprecated public static NetcdfFile open(RandomAccessFile raf, String location, CancelTask cancelTask, Object iospMessage) throws IOException
raf
- The open raf, is not cloised by this method.location
- human readable locatoin of this dataset.cancelTask
- used to monitor user cancellation; may be null.iospMessage
- send this message to iosp; may be null.IOException
- if cannot open as a CDM NetCDF.public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in interface ucar.nc2.util.cache.FileCacheable
IOException
- if error when closingpublic void release() throws IOException
release
in interface ucar.nc2.util.cache.FileCacheable
IOException
public void reacquire() throws IOException
reacquire
in interface ucar.nc2.util.cache.FileCacheable
IOException
@Deprecated public void setFileCache(ucar.nc2.util.cache.FileCacheIF cache)
setFileCache
in interface ucar.nc2.util.cache.FileCacheable
@Deprecated public String getCacheName()
public String getLocation()
getLocation
in interface ucar.nc2.util.cache.FileCacheable
@Nullable public String getId()
@Nullable public String getTitle()
public Group getRootGroup()
@Nullable public Group findGroup(String fullName)
fullName
- eg "/group/subgroup/wantGroup". Null or empty string returns the root group.@Nullable public Variable findVariable(Group g, String shortName)
g
- A group in this file. Null for root group.shortName
- short name of the Variable.@Nullable public Variable findVariable(String fullNameEscaped)
fullNameEscaped
- eg "/group/subgroup/name1.name2.name".@Nullable public Variable findVariableByAttribute(Group g, String attName, String attValue)
g
- start with this Group, null for the root Group.attName
- look for an Attribuite with this name.attValue
- look for an Attribuite with this value.@Nullable public Dimension findDimension(String fullName)
fullName
will be treated as if it were there. In other words, the first name token in
fullName
is treated as the short name of a Group or Dimension, relative to the root group.fullName
- Dimension full name, e.g. "/group/subgroup/dim".null
if it wasn't found.public boolean hasUnlimitedDimension()
@Nullable public Dimension getUnlimitedDimension()
public List<Dimension> getDimensions()
If the dimensions are in a group, the dimension name will have the group name, in order to disambiguate the dimensions. This means that a Variable's dimensions will not match Dimensions in this list. Therefore it is better to get the shared Dimensions directly from the Groups.
public List<Variable> getVariables()
@Deprecated public List<Attribute> getGlobalAttributes()
@Nullable public Attribute findGlobalAttribute(String name)
name
- the name of the attribute@Nullable public Attribute findGlobalAttributeIgnoreCase(String name)
name
- the name of the attribute@Nullable public Attribute findAttribute(String fullNameEscaped)
fullNameEscaped
- eg "@attName", "/group/subgroup/@attName" or "/group/subgroup/varname.name2.name@attName"@Deprecated public String findAttValueIgnoreCase(Variable v, String attName, String defaultValue)
v
- the variable or null to look in the root group.attName
- the (full) name of the attribute, case insensitivedefaultValue
- return this if attribute not found@Deprecated public double readAttributeDouble(Variable v, String attName, double defValue)
@Deprecated public int readAttributeInteger(Variable v, String attName, int defValue)
public String toString()
@Deprecated public String toNcML(String url)
@Deprecated public void writeCDL(OutputStream out, boolean strict)
out
- write to this OutputStreamstrict
- if true, make it stricly CDL, otherwise, add a little extra info@Deprecated public void writeCDL(PrintWriter pw, boolean strict)
pw
- write to this PrintWriterstrict
- if true, make it stricly CDL, otherwise, add a little extra info@Deprecated public void writeNcML(OutputStream os, String uri) throws IOException
os
- : write to this OutputStream. Will be closed at end of the method.uri
- use this for the url attribute; if null use getLocation(). // ??IOException
- if errorNcMLWriter.writeToStream(org.jdom2.Element, java.io.OutputStream)
@Deprecated public void writeNcML(Writer writer, String uri) throws IOException
writer
- : write to this Writer, should have encoding of UTF-8 if applicable. Will be closed at end of the
method.uri
- use this for the url attribute; if null use getLocation().IOException
- if errorNcMLWriter.writeToWriter(org.jdom2.Element, java.io.Writer)
@Deprecated public boolean syncExtend() throws IOException
IOException
- if error@Deprecated public long getLastModified()
getLastModified
in interface ucar.nc2.util.cache.FileCacheable
@Deprecated public Attribute addAttribute(Group parent, Attribute att)
parent
- add to this group. If group is null, use root groupatt
- add this attribute@Deprecated public Attribute addAttribute(Group parent, String name, String value)
parent
- add to this group. If group is null, use root groupname
- attribute name, may not be nullvalue
- attribute value, may be null, in which case, do not addd@Deprecated public Group addGroup(Group parent, Group g)
parent
- add to this group. If group is null, use root groupg
- add this group@Deprecated public void setRootGroup(Group rootGroup)
@Deprecated public Dimension addDimension(Group parent, Dimension d)
parent
- add to this group. If group is null, use root groupd
- add this Dimension@Deprecated public boolean removeDimension(Group g, String dimName)
g
- remove from this group. If group is null, use root groupdimName
- name of Dimension to remove.@Deprecated public Variable addVariable(Group g, Variable v)
g
- add to this group. If group is null, use root groupv
- add this Variable@Deprecated public Variable addVariable(Group g, String shortName, DataType dtype, String dims)
g
- add to this group. If group is null, use root groupshortName
- short name of the Variabledtype
- data type of the Variabledims
- list of dimension names@Deprecated public Variable addStringVariable(Group g, String shortName, String dims, int strlen)
g
- add to this group. If group is null, use root groupshortName
- short name of the Variabledims
- list of dimension namesstrlen
- dimension length of the inner (fastest changing) dimension@Deprecated public boolean removeVariable(Group g, String varName)
g
- remove from this group. If group is null, use root groupvarName
- name of variable to remove.@Deprecated public Attribute addVariableAttribute(Variable v, Attribute att)
v
- add to this Variable.att
- add this attribute@Deprecated public Object sendIospMessage(Object message)
message
- iosp specific message
Special:
@Deprecated public void setId(String id)
id
- the id@Deprecated public void setTitle(String title)
title
- the title@Deprecated public void setLocation(String location)
location
- the location@Deprecated public NetcdfFile setImmutable()
@Deprecated public void empty()
@Deprecated public void finish()
public Array readSection(String variableSection) throws IOException, InvalidRangeException
variableSection
- the constraint expression.IOException
- if errorInvalidRangeException
- if variableSection is invalid@Deprecated public List<Array> readArrays(List<Variable> variables) throws IOException
variables
- List of type VariableIOException
- if read error@Deprecated public Array read(String variableSection, boolean flatten) throws IOException, InvalidRangeException
variableSection
- the constraint expression.flatten
- MUST BE TRUEIOException
- if errorInvalidRangeException
- if variableSection is invalid@Deprecated public String getDetailInfo()
@Deprecated public void getDetailInfo(Formatter f)
@Deprecated public IOServiceProvider getIosp()
public String getFileTypeId()
public String getFileTypeDescription()
public String getFileTypeVersion()
@Deprecated public static String makeValidCdmObjectName(String shortName)
shortName
- from this name@Deprecated public static String makeValidCDLName(String vname)
vname
- the name@Deprecated public static String makeValidPathName(String vname)
vname
- the name@Deprecated public static String makeValidSectionSpecName(String vname)
vname
- the name@Deprecated public static String makeNameUnescaped(String vname)
vname
- the escaped namepublic NetcdfFile.Builder<?> toBuilder()
public static NetcdfFile.Builder<?> builder()