Main Content

netcdf.open

Open netCDF data source

Description

ncid = netcdf.open(source) opens the source for read-only access and returns a netCDF identifier in ncid. The specified source can be the name of a netCDF file, the URL of an OPeNDAP netCDF data source, or the HTTP URL of a remote netCDF file (with the #mode=bytes suffix for byte-range reading).

example

ncid = netcdf.open(source,mode) opens source with the type of access specified by mode. Mode values are 'WRITE', 'SHARE', or 'NOWRITE'.

example

[actualChunksize,ncid] = netcdf.open(source,mode,chunksize) opens an existing netCDF data source, with a specified chunksize. The chunksize parameter enables I/O performance tuning.

Examples

collapse all

Open the sample NetCDF file example.nc with read-only access, read a variable, and then close the file. The netcdf.open function returns a file identifier.

ncid = netcdf.open('example.nc')
ncid = 
65536

Read the data associated with the variable avagadros_number in the example file, using the netcdf.getVar function. The second input to netcdf.getVar is the variable ID, which is a zero-based index that identifies the variable. The avagadros_number variable has the index value 0.

A_number = netcdf.getVar(ncid,0)
A_number = 
6.0221e+23

Close the NetCDF file.

netcdf.close(ncid)

Open a NetCDF file and assign a value to a variable, and then display it.

Create the NetCDF file myexample.nc containing a variable pi.

nccreate('myexample.nc','pi')

Open the file for writing and write the value 3.1416 to the variable pi.

ncid = netcdf.open('myexample.nc','WRITE')
ncid = 65536
varInd = 0; % index of the first variable in the file
varValue = 3.1416; 
netcdf.putVar(ncid,varInd,varValue);

Read and display the value of the variable pi from the file.

netcdf.getVar(ncid,varInd)
ans = 3.1416

Close the file.

netcdf.close(ncid);

Input Arguments

collapse all

Name of the netCDF data source, specified as a string scalar or character vector. The source argument can be one of these values:

  • The path of a local netCDF source

  • The OPeNDAP URL of a remote OPeNDAP netCDF data source

  • The HTTP URL of a remote netCDF source, with #mode=bytes appended to the end of the URL to enable byte-range reading

    Note

    Byte-range reading is slower than other methods for reading from remote sources. For more details about byte-range reading, see the netCDF documentation.

Example: "myNetCDFfile.nc"

Example: "http://host_name/netcdf_filename#mode=bytes"

Access type, specified as a character vector or string scalar, or a numeric value. The mode input can be one of these values.

Value Description
'WRITE'Read-write access
'SHARE'Synchronous file updates
'NOWRITE'Read-only access

You also can specify mode with a numeric value. Retrieve the numeric value for mode by using netcdf.getConstant. For example, netcdf.getConstant('WRITE') returns the numeric value for mode. Use these numeric values when you want to specify a bitwise-OR of several modes.

Data Types: char | string | double

Chunk size parameter for performance tuning, specified as an integer.

The chunksize parameter controls the space-versus-time tradeoff, memory that the netcdf.open function allocates in the netCDF library versus the number of system calls. Because of internal requirements, the actual value of the chunk size used by the netcdf.open function can be different than the input value you specify. For the actual value used by the function, see the actualChunksize argument.

Example: 1024

Example: 8192

Data Types: double

Output Arguments

collapse all

File identifier of an open netCDF file or OPeNDAP netCDF data source, returned as an integer.

Data Types: double

Actual chunk size used by the netcdf.open function, returned as an integer.

Data Types: double

Algorithms

This function corresponds to the nc_open and nc__open functions in the netCDF library C API. To use this function, you should be familiar with the netCDF programming paradigm.

Version History

Introduced in R2011a

expand all