source: trunk/src/test_netcdf.m @ 1156

Last change on this file since 1156 was 1127, checked in by g7moreau, 11 months ago

Update Joel email

File size: 3.1 KB
Line 
1%=======================================================================
2% Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
3%   http://www.legi.grenoble-inp.fr
4%   Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr
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
19% program to test struct2nc for creating netcdf files
20%% creating a simple netcdf file
21Data.ListVarName={'A'};% matrix A to record
22Data.VarDimName={{'ny','nx'}};
23[x,y] = meshgrid(-2:.2:2);
24Data.A = x.*exp(-x.^2-y.^2);
25err=struct2nc('test_1.nc',Data);
26if isempty(err)
27    display('test_1.nc written')
28end
29%
30%% reading the simple netcdf file
31DataOut=nc2struct('test_1.nc');
32display( 'reading the simple netcdf file:')
33display(DataOut)
34
35%% writing a documented set of fields in a netcdf file
36Data.ListGlobalAttribute={'title','Time','TimeUnit'};
37Data.title='test field for netcdf';
38Data.Time=15;
39Data.TimeUnit='s';
40Data.ListVarName={'X','Y','U','V','coord_y','coord_x','A'};%  cell array containing the names of the fields to record
41Data.VarDimName={'nbvec','nbvec','nbvec','nbvec','coord_y','coord_x',{'coord_y','coord_x'}};
42Data.VarAttribute{1}.longname='abscissa';
43Data.VarAttribute{2}.longname='ordinate';
44Data.VarAttribute{3}.longname='x velocity component';
45Data.VarAttribute{4}.longname='y velocity component';
46Data.VarAttribute{5}.longname='interpolated abscissa';
47Data.VarAttribute{6}.longname='interpolated ordinate';
48Data.VarAttribute{7}.longname='stream function';
49Data.VarAttribute{1}.Role='coord_x';
50Data.VarAttribute{2}.Role='coord_y';
51Data.VarAttribute{3}.Role='vector_x';
52Data.VarAttribute{4}.Role='vector_y';
53
54nbvec=200;
55Data.X=4*rand(1,nbvec)-2;%random set of abscissa between -2 and 2
56Data.Y=4*rand(1,nbvec)-2;%random set of ordinates between -2 and 2
57Psi=exp(-Data.X.^2-Data.Y.^2);
58Data.U=Data.Y.*Psi;
59Data.V=-Data.X.*Psi;
60Data.coord_y=[-2 2];
61Data.coord_x=[-2 2];
62Data.A =exp(-x.^2-y.^2);
63err=struct2nc('test_2.nc',Data);
64if isempty(err)
65    display('test_2.nc written')
66else
67    msgbox_uvmat('ERROR',err)
68end
69
70%% fast reading of global attributes
71DataOut_global=nc2struct('test_2.nc','ListGlobalAttribute','title','Time');
72display('fast reading of global attributes:')
73display(DataOut_global)
74
75%% reading of the whole netcdf file
76DataOut_all=nc2struct('test_2.nc');
77display('reading of the whole netcdf file:')
78display(DataOut_all)
79
80%% reading only the field A, coord_y, coord_x  only
81DataOut_A=nc2struct('test_2.nc',{'coord_y','coord_x','A'});
82display('reading only the field A, coord_y, coord_x  only:')
83display(DataOut_A)
Note: See TracBrowser for help on using the repository browser.