Home - Alexander-Barth/octave-netcdf GitHub Wiki

This package aims to implement the netcdf interface of MATLAB in GNU Octave. It is an alternative to the deprecated octcdf toolbox.

For new code, it is recommended to use this package instead of octcdf. If you want use to a even more high-level and object-oriented interface, I would suggest you to use the package ncArray (which works with both the octcdf and netcdf package in octave as well as with matlab).

Requirements

Your should have the netcdf 4.1 library (or later) installed (including development headers) and the nc-config script in your path. This function should return the complete path of nc-config:

which nc-config

If this is not the case, then you have to install netcdf 4 (preferably using your package manager). Under Debian/Ubuntu you can install NetCDF using the following command:

sudo apt-get install libnetcdf-dev

If NetCDF is installed in a non-standard location, you must set the variables PATH and LD\_LIBRARY\_PATH. If you compile netCDF from source, be aware that:

  • octave-netcdf requires netcdf4 features. Therefore do not use the option --disable-netcdf4 when configuring netCDF and make sure that you have the HDF5 libraries installed. For example under Debian/Ubuntu you can install HDF5 using the following command:
sudo apt-get install libhdf5-dev
  • if you are only building static libraries, you must use the configure option --with-pic on an AMD/Intel 64-bit processor.

Installation

Fedora users can simply install the octave-netcdf package using their package manager:

dnf install octave-netcdf

Check your software repos if octave-netcdf is not already included.

Otherwise, inside octave, you can download and install the package by issuing:

 pkg install -verbose -forge -auto netcdf

Double-check if netcdf uses the correct netCDF library. If you did not use the -auto flag, you must load the package explicitly (include this in your .octaverc or must do this at every octave session):

 pkg load netcdf

Test the package:

 test_netcdf

This should return:

Using NetCDF library version "4.1.1 of Nov  7 2011 11:35:16 $"
run test_netcdf_constant..............   OK  
run test_netcdf_create................   OK  
run test_netcdf_low_level_interface...   OK  
run test_netcdf_datatypes.............   OK  
run test_netcdf_scalar_variable.......   OK  
run test_netcdf_attributes............   OK  
run test_netcdf_high_level_interface..   OK  
run test_netcdf_ncwriteschema.........   OK  
run test_netcdf_ncwriteschema_unlim...   OK  
run test_netcdf_ncwriteschema_chunking   OK  
run test_netcdf_ncwriteschema_group...   OK

Low-level interface

GNU octave does not yet support namespace. One need to use the function "import_netcdf" at the beginning of every function and script which uses the low-level interface in GNU octave to use the functions with a dot (e.g. netcdf.open) instead of an underscore (e.g. netcdf_open). For portability, it is recommended to use the high-level interface. An implementation of the following function is available:

netcdf_getConstant
netcdf_getConstantNames
netcdf_inqLibVers
netcdf_setDefaultFormat
netcdf_setChunkCache
netcdf_getChunkCache
netcdf_create
netcdf_open
netcdf_abort
netcdf_sync
netcdf_setFill
netcdf_inq
netcdf_inqUnlimDims
netcdf_inqFormat
netcdf_defDim
netcdf_renameDim
netcdf_defVar
netcdf_renameVar
netcdf_defVarFill
netcdf_inqVarFill
netcdf_defVarDeflate
netcdf_inqVarDeflate
netcdf_defVarChunking
netcdf_inqVarChunking
netcdf_defVarFletcher32
netcdf_inqVarFletcher32
netcdf_endDef
netcdf_reDef
netcdf_putVar
netcdf_getVar
netcdf_close
netcdf_inqAttName
netcdf_inqAttID
netcdf_inqAtt
netcdf_getAtt
netcdf_putAtt
netcdf_copyAtt
netcdf_renameAtt
netcdf_delAtt
netcdf_inqVarID
netcdf_inqVarIDs
netcdf_inqVar
netcdf_inqDim
netcdf_inqDimID
netcdf_inqDimIDs
netcdf_defGrp
netcdf_inqGrps
netcdf_inqGrpName
netcdf_inqGrpNameFull
netcdf_inqGrpParent
netcdf_inqGrpFullNcid
netcdf_inqNcid

High-level interface

An implementation of the following function exist:

nccreate     
ncdisp       
ncinfo       
ncreadatt    
ncread       
ncwriteatt   
ncwrite      
ncwriteschema

The octcdf package (version 1.1.6) currently includes a rudimentary implementation of ncread, ncwrite and ncinfo which behaves similarly to the matlab equivalent functions. These functions will be removed in octcdf as they have been re-implemented in the netcdf package with a much higher degree of compatibility and with the ability to use netcdf4 features. The functions defined in octcdf might shadow the functions in the netcdf package. If you use both packages (netcdf and octcdf version 1.1.6), it is therefore important to load the octcdf package before the netcdf package.

Right:

>> pkg load octcdf netcdf 
>> which ncinfo
'ncinfo' is a function from the file /home/abarth/octave/netcdf-1.0.0/ncinfo.m

Wrong:

>> pkg load netcdf octcdf
>> which ncinfo
'ncinfo' is a function from the file /home/abarth/octave/octcdf-1.1.6/ncinfo.m

Example

Oddities of the matlab netcdf interface

The function netcdf.inqVarFletcher32 returns the string 'NOCHECKSUM' or 'FLETCHER32' (upper-case) while similar functions like netcdf.inqVarChunking return strings (storage is 'contiguous' or 'chunked') in lower case (in matlab 2013a). According to the matlab documentation all these function should return strings in upper-case. The octave interface mimics currently this odd behavior. In any case, it is better to compare the output of these functions with strcmpi (which is case insensitive).