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