Write a slice at a time in a NetCDF file

From GHER

Jump to: navigation, search

Sometimes the data to be processed is so large that it cannot be loaded entirely in memory. If the processing can be made with on slices, then the NetCDF library allows to save the data one slice at a time. The following example shows how this can be done in Octave or Matlab. (For Octave you need the package from octave-forge):

% Example of how to write a slice at a time for a netcdf variable

% name of the netcdf file
filename = 'test.nc';

% number of elements in longitude and latitude dimensions
imax = 10;
jmax = 10;

% time origin (for units)
torigin = datenum(2000,1,1);

% the number of elements for unlimited dimensions and has the special value inf
% The dimension can also have a fixed size.
% Use the additional option 'Format','64bit' to create a netcdf file compatible 
% with the library version 3.6

nccreate(filename,'temperature','Dimensions',{'lon',imax,'lat',jmax,'time',inf});

% the dimensions time was already created in the previous call. Its size
% does not have to be repeated here.
nccreate(filename,'time','Dimensions',{'time'});

% format 31 is 'yyyy-mm-dd HH:MM:SS'
ncwriteatt(filename,'time','units',['days since ' datestr(torigin,31)]);

% loop over slices
for i = 1:31
    % create some random data (or load it from somewhere)
    data = randn(imax,jmax);

    % assume we have daily data for January 2014
    time = datenum(2014,1,1) + i-1;

    % [1 1 i] are the start indices where to write the data
    start = [1 1 i];

    % write a slice of data
    ncwrite(filename,'temperature',data,[1 1 i]);

    % write the corresponding time
    ncwrite(filename,'time',time - torigin,i);    
end


disp('You can check the file with the command "ncdump -v time -t test.nc".')
Personal tools