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