1 %% Copyright (C) 2005 Alexander Barth
3 %% This program is free software; you can redistribute it and/or modify
4 %% it under the terms of the GNU General Public License as published by
5 %% the Free Software Foundation; either version 2 of the License, or
6 %% (at your option) any later version.
8 %% This program is distributed in the hope that it will be useful,
9 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
10 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 %% GNU General Public License for more details.
13 %% You should have received a copy of the GNU General Public License
14 %% along with this program; If not, see <http://www.gnu.org/licenses/>.
16 % Example for creating and reading a netcdf file
19 % create some variables to store them in a netcdf file
22 longitude = -179:1:180;
23 [y,x] = meshgrid(pi/180 * latitude,pi/180 * longitude);
24 temp = cos(2*x) .* cos(y);
26 %---------------------------------------%
28 % write data to a netcdf file %
30 %---------------------------------------%
32 % create netcdf file called example.nc
34 nc = netcdf('example.nc','c');
36 % define the dimension longitude and latitude of size
37 % 360 and 181 respectively.
39 nc('longitude') = 360;
42 % coordinate variable longitude
44 nc{'longitude'} = ncdouble('longitude'); % create a variable longitude of type double with
45 % 360 elements (dimension longitude).
46 nc{'longitude'}(:) = longitude; % store the octave variable longitude in the netcdf file
47 nc{'longitude'}.units = 'degrees_east'; % define a string attribute of the variable longitude
49 % coordinate variable latitude
51 nc{'latitude'} = ncdouble('latitude');; % create a variable latitude of type double with
52 % 181 elements (dimension latitude).
53 nc{'latitude'}(:) = latitude; % store the octave variable latitude in the netcdf file
54 nc{'latitude'}.units = 'degrees_north'; % define a string attribute of the variable latitude
58 nc{'temp'} = ncdouble('latitude','longitude'); % create a variable temp of type double of the size 360x181
59 % (dimension longitude and latitude).
60 nc{'temp'}(:) = temp; % store the octave variable temp in the netcdf file
61 nc{'temp'}.long_name = 'Temperature';
62 nc{'temp'}.units = 'degree Celsius'; % define a string attribute of the variable
63 nc{'temp'}.valid_range = [-10 40]; % define a vector of doubles attribute of the variable
65 % define a global string attribute
66 nc.history = 'netcdf file created by example_netcdf.m in octave';
67 nc.title = 'sample file';
69 close(nc) % close netcdf file and all changes are written to disk
72 disp(['example.nc file created. You might now inspect this file with the shell command "ncdump -h example.nc"']);
75 %---------------------------------------%
77 % read data from a netcdf file %
79 %---------------------------------------%
81 nc = netcdf('example.nc','r'); % open netcdf file example.nc in read-only
83 n = nc('longitude'); % get the length of the dimension longitude
85 temp = nc{'temp'}(:); % retrieve the netcdf variable temp
86 temp_units = nc{'temp'}.units; % retrieve the attribute units of variable temp
87 temp_valid_range = nc{'temp'}.valid_range; % retrieve the attribute valid_range of variable temp
89 global_history = nc.history; % retrieve the global attribute history