source: trunk/src/test_netcdf.m @ 146

Last change on this file since 146 was 142, checked in by sommeria, 14 years ago

test_netcdf program created to test writting and reading netcdf files, replaces test_struct2nc

File size: 2.0 KB
Line 
1% program to test struct2nc for creating netcdf files
2%% creating a simple netcdf file
3Data.ListVarName={'A'};% matrix A to record
4Data.VarDimName={{'ny','nx'}};
5[x,y] = meshgrid(-2:.2:2);
6Data.A = x.*exp(-x.^2-y.^2);
7err=struct2nc('test_1.nc',Data);
8if isempty(err)
9    display('test_1.nc written')
10end
11%
12%% reading the simple netcdf file
13DataOut=nc2struct('test_1.nc');
14display( 'reading the simple netcdf file:')
15display(DataOut)
16
17%% writing a documented set of fields in a netcdf file
18Data.ListGlobalAttribute={'title','Time','TimeUnit'};
19Data.title='test field for netcdf';
20Data.Time=15;
21Data.TimeUnit='s';
22Data.ListVarName={'X','Y','U','V','coord_y','coord_x','A'};%  cell array containing the names of the fields to record
23Data.VarDimName={'nbvec','nbvec','nbvec','nbvec','coord_y','coord_x',{'coord_y','coord_x'}};
24Data.VarAttribute{1}.longname='abscissa';
25Data.VarAttribute{2}.longname='ordinate';
26Data.VarAttribute{3}.longname='x velocity component';
27Data.VarAttribute{4}.longname='y velocity component';
28Data.VarAttribute{5}.longname='interpolated abscissa';
29Data.VarAttribute{6}.longname='interpolated ordinate';
30Data.VarAttribute{7}.longname='stream function';
31
32nbvec=200;
33Data.X=4*rand(1,nbvec)-2;%random set of abscissa between -2 and 2
34Data.Y=4*rand(1,nbvec)-2;%random set of ordinates between -2 and 2
35Psi=exp(-Data.X.^2-Data.Y.^2);
36Data.U=Data.Y.*Psi;
37Data.V=-Data.X.*Psi;
38Data.coord_y=[-2 2];
39Data.coord_x=[-2 2];
40Data.A =exp(-x.^2-y.^2);
41err=struct2nc('test_2.nc',Data);
42if isempty(err)
43    display('test_2.nc written')
44else
45    msgbox_uvmat('ERROR',err)
46end
47
48%% fast reading of global attributes
49DataOut_global=nc2struct('test_2.nc','ListGlobalAttribute','title','Time');
50display('fast reading of global attributes:')
51display(DataOut_global)
52
53%% reading of the whole netcdf file
54DataOut_all=nc2struct('test_2.nc');
55display('reading of the whole netcdf file:')
56display(DataOut_all)
57
58%% reading only the field A, coord_y, coord_x  only
59DataOut_A=nc2struct('test_2.nc',{'coord_y','coord_x','A'});
60display('reading only the field A, coord_y, coord_x  only:')
61display(DataOut_A)
Note: See TracBrowser for help on using the repository browser.