[809] | 1 | %======================================================================= |
---|
[1126] | 2 | % Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
[809] | 3 | % http://www.legi.grenoble-inp.fr |
---|
[1127] | 4 | % Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr |
---|
[809] | 5 | % |
---|
| 6 | % This file is part of the toolbox UVMAT. |
---|
| 7 | % |
---|
| 8 | % UVMAT is free software; you can redistribute it and/or modify |
---|
| 9 | % it under the terms of the GNU General Public License as published |
---|
| 10 | % by the Free Software Foundation; either version 2 of the license, |
---|
| 11 | % or (at your option) any later version. |
---|
| 12 | % |
---|
| 13 | % UVMAT is distributed in the hope that it will be useful, |
---|
| 14 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | % GNU General Public License (see LICENSE.txt) for more details. |
---|
| 17 | %======================================================================= |
---|
| 18 | |
---|
[142] | 19 | % program to test struct2nc for creating netcdf files |
---|
| 20 | %% creating a simple netcdf file |
---|
| 21 | Data.ListVarName={'A'};% matrix A to record |
---|
| 22 | Data.VarDimName={{'ny','nx'}}; |
---|
| 23 | [x,y] = meshgrid(-2:.2:2); |
---|
| 24 | Data.A = x.*exp(-x.^2-y.^2); |
---|
| 25 | err=struct2nc('test_1.nc',Data); |
---|
| 26 | if isempty(err) |
---|
| 27 | display('test_1.nc written') |
---|
| 28 | end |
---|
| 29 | % |
---|
| 30 | %% reading the simple netcdf file |
---|
| 31 | DataOut=nc2struct('test_1.nc'); |
---|
| 32 | display( 'reading the simple netcdf file:') |
---|
| 33 | display(DataOut) |
---|
| 34 | |
---|
| 35 | %% writing a documented set of fields in a netcdf file |
---|
| 36 | Data.ListGlobalAttribute={'title','Time','TimeUnit'}; |
---|
| 37 | Data.title='test field for netcdf'; |
---|
| 38 | Data.Time=15; |
---|
| 39 | Data.TimeUnit='s'; |
---|
| 40 | Data.ListVarName={'X','Y','U','V','coord_y','coord_x','A'};% cell array containing the names of the fields to record |
---|
| 41 | Data.VarDimName={'nbvec','nbvec','nbvec','nbvec','coord_y','coord_x',{'coord_y','coord_x'}}; |
---|
| 42 | Data.VarAttribute{1}.longname='abscissa'; |
---|
| 43 | Data.VarAttribute{2}.longname='ordinate'; |
---|
| 44 | Data.VarAttribute{3}.longname='x velocity component'; |
---|
| 45 | Data.VarAttribute{4}.longname='y velocity component'; |
---|
| 46 | Data.VarAttribute{5}.longname='interpolated abscissa'; |
---|
| 47 | Data.VarAttribute{6}.longname='interpolated ordinate'; |
---|
| 48 | Data.VarAttribute{7}.longname='stream function'; |
---|
[225] | 49 | Data.VarAttribute{1}.Role='coord_x'; |
---|
| 50 | Data.VarAttribute{2}.Role='coord_y'; |
---|
| 51 | Data.VarAttribute{3}.Role='vector_x'; |
---|
| 52 | Data.VarAttribute{4}.Role='vector_y'; |
---|
[142] | 53 | |
---|
| 54 | nbvec=200; |
---|
| 55 | Data.X=4*rand(1,nbvec)-2;%random set of abscissa between -2 and 2 |
---|
| 56 | Data.Y=4*rand(1,nbvec)-2;%random set of ordinates between -2 and 2 |
---|
| 57 | Psi=exp(-Data.X.^2-Data.Y.^2); |
---|
| 58 | Data.U=Data.Y.*Psi; |
---|
| 59 | Data.V=-Data.X.*Psi; |
---|
| 60 | Data.coord_y=[-2 2]; |
---|
| 61 | Data.coord_x=[-2 2]; |
---|
| 62 | Data.A =exp(-x.^2-y.^2); |
---|
| 63 | err=struct2nc('test_2.nc',Data); |
---|
| 64 | if isempty(err) |
---|
| 65 | display('test_2.nc written') |
---|
| 66 | else |
---|
| 67 | msgbox_uvmat('ERROR',err) |
---|
| 68 | end |
---|
| 69 | |
---|
| 70 | %% fast reading of global attributes |
---|
| 71 | DataOut_global=nc2struct('test_2.nc','ListGlobalAttribute','title','Time'); |
---|
| 72 | display('fast reading of global attributes:') |
---|
| 73 | display(DataOut_global) |
---|
| 74 | |
---|
| 75 | %% reading of the whole netcdf file |
---|
| 76 | DataOut_all=nc2struct('test_2.nc'); |
---|
| 77 | display('reading of the whole netcdf file:') |
---|
| 78 | display(DataOut_all) |
---|
| 79 | |
---|
| 80 | %% reading only the field A, coord_y, coord_x only |
---|
| 81 | DataOut_A=nc2struct('test_2.nc',{'coord_y','coord_x','A'}); |
---|
| 82 | display('reading only the field A, coord_y, coord_x only:') |
---|
| 83 | display(DataOut_A) |
---|