]> Creatis software - CreaPhase.git/blob - octave_packages/octcdf-1.1.4/example_netcdf.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / octcdf-1.1.4 / example_netcdf.m
1 %% Copyright (C) 2005 Alexander Barth
2 %%
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.
7 %%
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.
12 %%
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/>.
15
16 % Example for creating and reading a netcdf file
17
18
19 % create some variables to store them in a netcdf file
20
21 latitude = -90:1:90;
22 longitude = -179:1:180;
23 [y,x] = meshgrid(pi/180 * latitude,pi/180 * longitude);
24 temp = cos(2*x) .* cos(y);
25
26 %---------------------------------------%
27 %                                       %
28 % write data to a netcdf file           %
29 %                                       %
30 %---------------------------------------%
31
32 % create netcdf file called example.nc
33
34 nc = netcdf('example.nc','c');
35
36 % define the dimension longitude and latitude of size
37 % 360 and 181 respectively.
38
39 nc('longitude') = 360;
40 nc('latitude') = 181;
41
42 % coordinate variable longitude
43
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
48
49 % coordinate variable latitude
50
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
55
56 % variable temp
57
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
64
65                                                       % define a global string attribute
66 nc.history = 'netcdf file created by example_netcdf.m in octave';
67 nc.title = 'sample file';
68
69 close(nc)                                             % close netcdf file and all changes are written to disk
70
71
72 disp(['example.nc file created. You might now inspect this file with the shell command "ncdump -h example.nc"']);
73
74
75 %---------------------------------------%
76 %                                       %
77 % read data from a netcdf file          %
78 %                                       %
79 %---------------------------------------%
80
81 nc = netcdf('example.nc','r');                       % open netcdf file example.nc in read-only
82
83 n = nc('longitude');                                 % get the length of the dimension longitude
84
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
88
89 global_history = nc.history;                         % retrieve the global attribute history