source: trunk/src/test_netcdf.m @ 442

Last change on this file since 442 was 225, checked in by sommeria, 14 years ago

civ : bug in binary check repaired. Introduction of the option PIVlab for PIV (still under test)

File size: 2.2 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';
31Data.VarAttribute{1}.Role='coord_x';
32Data.VarAttribute{2}.Role='coord_y';
33Data.VarAttribute{3}.Role='vector_x';
34Data.VarAttribute{4}.Role='vector_y';
35
36nbvec=200;
37Data.X=4*rand(1,nbvec)-2;%random set of abscissa between -2 and 2
38Data.Y=4*rand(1,nbvec)-2;%random set of ordinates between -2 and 2
39Psi=exp(-Data.X.^2-Data.Y.^2);
40Data.U=Data.Y.*Psi;
41Data.V=-Data.X.*Psi;
42Data.coord_y=[-2 2];
43Data.coord_x=[-2 2];
44Data.A =exp(-x.^2-y.^2);
45err=struct2nc('test_2.nc',Data);
46if isempty(err)
47    display('test_2.nc written')
48else
49    msgbox_uvmat('ERROR',err)
50end
51
52%% fast reading of global attributes
53DataOut_global=nc2struct('test_2.nc','ListGlobalAttribute','title','Time');
54display('fast reading of global attributes:')
55display(DataOut_global)
56
57%% reading of the whole netcdf file
58DataOut_all=nc2struct('test_2.nc');
59display('reading of the whole netcdf file:')
60display(DataOut_all)
61
62%% reading only the field A, coord_y, coord_x  only
63DataOut_A=nc2struct('test_2.nc',{'coord_y','coord_x','A'});
64display('reading only the field A, coord_y, coord_x  only:')
65display(DataOut_A)
Note: See TracBrowser for help on using the repository browser.