source: trunk/src/struct2nc.m @ 1195

Last change on this file since 1195 was 1195, checked in by sommeria, 5 days ago

format of PIV data made more compact/4

File size: 12.5 KB
Line 
1% 'struct2nc': create a netcdf file from a Matlab structure
2%---------------------------------------------------------------------
3% errormsg=struct2nc(flname,Data,action)
4%
5% OUTPUT:
6% errormsg=error message, =[]: default, no error
7%
8% INPUT:
9% flname: name of the netcdf file to create (must end with the extension '.nc')
10%  Data: structure containing all the information of the netcdf file (or netcdf object)
11%           with fields:
12%       (optional) .ListGlobalAttribute: list (cell array of character strings) of the names of the global attributes Att_1, Att_2...
13%                  .Att_1,Att_2...: values of the global attributes
14%      (requested) .ListVarName: list of the variable names Var_1, Var_2....(cell array of character strings).
15%      (requested) .VarDimName: list of dimension names for each element of .ListVarName (cell array of string cells)
16%       (optional) .VarAttribute: cell array of structures of the form .VarAttribute{ivar}.key=value, defining an attribute key name and value for the variable #ivar
17%      (requested) .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
18% action: if ='keep_open', don't close the file after creation, keep it open for further data input
19
20%=======================================================================
21% Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
22%   http://www.legi.grenoble-inp.fr
23%   Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr
24%
25%     This file is part of the toolbox UVMAT.
26%
27%     UVMAT is free software; you can redistribute it and/or modify
28%     it under the terms of the GNU General Public License as published
29%     by the Free Software Foundation; either version 2 of the license,
30%     or (at your option) any later version.
31%
32%     UVMAT is distributed in the hope that it will be useful,
33%     but WITHOUT ANY WARRANTY; without even the implied warranty of
34%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35%     GNU General Public License (see LICENSE.txt) for more details.
36%=======================================================================
37
38function [errormsg,nc]=struct2nc(flname,Data,action,ListDimName,DimValue,VarDimIndex)
39nc=[];
40errormsg='';
41if ~ischar(flname)
42    errormsg='invalid input for the netcf file name';
43    return
44end
45FilePath=fileparts(flname);
46if ~strcmp(FilePath,'') && ~exist(FilePath,'dir')
47    errormsg=['directory ' FilePath ' needs to be created'];
48    return
49end
50if ~exist('Data','var')
51     errormsg='no data  input for the netcdf file';
52    return
53end
54
55%% check the validity of the input field structure
56if ~ (exist('action','var') && strcmp(action,'keep_open'))
57    [errormsg,ListDimName,DimValue,VarDimIndex]=check_field_structure(Data);
58    if ~isempty(errormsg)
59        errormsg=['error in struct2nc:invalid input structure_' errormsg];
60        return
61    end
62end
63ListVarName=Data.ListVarName;
64
65%% create the netcdf file with name flname in format NETCDF4
66cmode = netcdf.getConstant('NETCDF4');
67cmode = bitor(cmode, netcdf.getConstant('CLASSIC_MODEL'));
68cmode = bitor(cmode, netcdf.getConstant('CLOBBER'));
69nc = netcdf.create(flname, cmode);
70netcdf.setFill(nc,'NOFILL')
71
72%% write global constants
73if isfield(Data,'ListGlobalAttribute')
74    keys=Data.ListGlobalAttribute;
75    for iattr=1:length(keys)
76        if isfield(Data,keys{iattr})
77             testvar=0;
78            for ivar=1:length(ListVarName)% eliminate possible global attributes with the same name as a variable
79                if isequal(ListVarName{ivar}, keys{iattr})
80                    testvar=1;
81                    break
82                end
83            end
84            if ~testvar               
85                cte=Data.(keys{iattr});
86                if (ischar(cte) ||isnumeric(cte)) &&  ~isempty(cte)%&& ~isequal(cte,'')
87                    %write constant only if it is numeric or char string, and not empty
88                    netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),keys{iattr},cte)
89                end
90            end
91        end
92    end
93end
94
95%% create the dimensions
96dimid=zeros(1,length(ListDimName));
97for idim=1:length(ListDimName)
98     dimid(idim) = netcdf.defDim(nc,ListDimName{idim},DimValue(idim));
99end
100VarAttribute={}; %default
101testattr=0;
102if isfield(Data,'VarAttribute')
103    VarAttribute=Data.VarAttribute;
104    testattr=1;
105end
106
107
108%% create the variables
109varid=nan(1,length(Data.ListVarName));
110VarClass=cell(1,length(ListVarName));
111for ivar=1:length(ListVarName)
112    if isfield(Data,ListVarName{ivar})
113        VarClass{ivar}=class(Data.(ListVarName{ivar}));
114        VarType='';
115        switch VarClass{ivar}
116            case {'single','double','int32','uint32','int64','uint64'}
117                VarType='nc_float'; % store all floating reals as single
118            case {'int16','uint16'}
119                  VarType='nc_short';
120            case {'int8','uint8'}
121                VarType='nc_byte';
122            case 'logical'
123                VarType='nc_byte';
124                Data.(ListVarName{ivar})=uint8(Data.(ListVarName{ivar}));
125        end
126        if ~isempty(VarType)
127            varid(ivar)=netcdf.defVar(nc,ListVarName{ivar},VarType,dimid(VarDimIndex{ivar}));%define variable
128        end
129    end
130end
131
132%% write variable attributes
133if testattr
134    for ivar=1:min(numel(VarAttribute),numel(ListVarName)) 
135        if isstruct(VarAttribute{ivar}) && ~isnan(varid(ivar))
136            attr_names=fields(VarAttribute{ivar});
137            for iattr=1:length(attr_names)
138                attr_val=VarAttribute{ivar}.(attr_names{iattr});
139                if ~isempty(attr_names{iattr})&& ~isempty(attr_val)&&~iscell(attr_val)
140                    netcdf.putAtt(nc,varid(ivar),attr_names{iattr},attr_val);
141                end
142            end
143        end
144    end
145end
146netcdf.endDef(nc); %put in data mode
147
148%% fill the variables with input data except in mode 'keep_open' (variables will be filled later)
149if ~(exist('action','var') && strcmp(action,'keep_open'))
150    for ivar=1:length(ListVarName)
151        if ~isnan(varid(ivar))
152            VarVal=Data.(ListVarName{ivar});
153            if strcmp(VarClass{ivar},'single')
154                VarVal=single(VarVal);
155            end
156            %varval=values of the current variable
157            VarDimName=Data.VarDimName{ivar};
158            if ischar(VarDimName)
159                VarDimName={VarDimName};
160            end
161            siz=size(VarVal);
162            testrange=(numel(VarDimName)==1 && strcmp(VarDimName{1},ListVarName{ivar}) && numel(VarVal)==2);% case of a coordinate defined on a regular mesh by the first and last values.
163            testline=isequal(length(siz),2) && isequal(siz(1),1)&& isequal(siz(2), DimValue(VarDimIndex{ivar}));%matlab vector
164            %testcolumn=isequal(length(siz),2) && isequal(siz(1), DimValue(VarDimIndex{ivar}))&& isequal(siz(2),1);%matlab column vector
165            if testline || testrange
166                if testrange
167                    VarVal=linspace(VarVal(1),VarVal(2),DimValue(VarDimIndex{ivar}));% restitute the whole array of coordinate values from the first and last values
168                end
169                netcdf.putVar(nc,varid(ivar), VarVal');
170            else
171                netcdf.putVar(nc,varid(ivar), VarVal);
172            end
173        end
174    end
175end
176if ~ (exist('action','var') && strcmp(action,'keep_open'))
177 netcdf.close(nc)
178[success,errormsg] = fileattrib(flname ,'+w');% allow writing access for the group of users
179end
180%'check_field_structure': check the validity of the field struture representation consistant with the netcdf format
181%------------------------------------------------------------------------
182% [errormsg,ListDimName,DimValue,VarDimIndex]=check_field_structure(Data)
183%
184% OUTPUT:
185% errormsg: error message which is not empty when the input structure does not have the right form
186% ListDimName: list of dimension names (cell array of cahr strings)
187% DimValue: list of dimension values (numerical array with the same dimension as ListDimName)
188% VarDimIndex: cell array of dimension index (in the list ListDimName) for each element of Data.ListVarName
189%
190% INPUT:
191% Data:   structure containing
192%         (optional) .ListGlobalAttribute: cell listing the names of the global attributes
193%                    .Att_1,Att_2... : values of the global attributes
194%         (requested)  .ListVarName: list of variable names to select (cell array of  char strings {'VarName1', 'VarName2',...} )
195%         (requested)  .VarDimName: list of dimension names for each element of .ListVarName (cell array of string cells)                         
196%         (requested) .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
197
198
199function [errormsg,ListDimName,DimValue,VarDimIndex]=check_field_structure(Data)
200errormsg='';
201ListDimName={};
202DimValue=[]; %default
203VarDimIndex={};
204if ~isstruct(Data)
205    errormsg='input field is not a structure';
206    return
207end
208if isfield(Data,'ListVarName') && iscell(Data.ListVarName)
209    nbfield=numel(Data.ListVarName);
210else
211    errormsg='input field does not contain the cell array of variable names .ListVarName';
212    return
213end
214%check dimension names
215if (isfield(Data,'VarDimName') && iscell(Data.VarDimName))
216    if  numel(Data.VarDimName)~=nbfield
217       errormsg=' .ListVarName and .VarDimName have different lengths';
218        return
219    end
220else
221    errormsg='input field does not contain the  cell array of dimension names .VarDimName';
222    return
223end
224nbdim=0;
225ListDimName={};
226
227%% main loop on the list of variables
228VarDimIndex=cell(1,nbfield);
229for ivar=1:nbfield
230    VarName=Data.ListVarName{ivar};
231    if ~isfield(Data,VarName)
232        errormsg=['the listed variable ' VarName ' is not found'];
233        return
234    end
235    sizvar=size(Data.(VarName));% sizvar = dimension of variable
236    DimCell=Data.VarDimName{ivar};
237    if ischar(DimCell)
238        DimCell={DimCell};%case of a single dimension name, defined by a string
239    elseif ~iscell(DimCell)
240        errormsg=['wrong format for .VarDimName{' num2str(ivar) ' (must be the cell of dimension names of the variable ' VarName];
241        return       
242    end
243    nbcoord=numel(sizvar);%nbre of coordinates for variable named VarName
244    testrange=0;
245    if numel(DimCell)==0
246        errormsg=['empty declared dimension .VarDimName{' num2str(ivar) '} for ' VarName];
247        return
248    elseif numel(DimCell)==1% one dimension declared
249        if nbcoord==2
250            if sizvar(1)==1
251                sizvar(1)=sizvar(2);
252            elseif sizvar(2)==1
253            else
254                errormsg=['1 dimension declared in .VarDimName{' num2str(ivar) '} inconsistent with the nbre of dimensions =2 of the variable ' VarName];
255                return
256            end
257            if sizvar(1)==2 && isequal(VarName,DimCell{1})
258                testrange=1;% test for a dimension variable representing a range
259            end
260        else
261            errormsg=['1 dimension declared in .VarDimName{' num2str(ivar) '} inconsistent with the nbre of dimensions =' num2str(nbcoord) ' of the variable ' VarName];
262            return
263        end
264    else
265        if numel(DimCell)>nbcoord
266            sizvar(nbcoord+1:numel(DimCell))=1;% case of singleton dimensions (not seen by the function size)
267        elseif nbcoord > numel(DimCell)
268            errormsg=['nbre of declared dimensions in .VarDimName{' num2str(ivar) '} smaller than the nbre of dimensions =' num2str(nbcoord) ' of the variable ' VarName];
269            return
270        end
271    end
272    DimIndex=[];
273    for idim=1:numel(DimCell) %loop on the coordinates of variable #ivar
274        DimName=DimCell{idim};
275        iprev=find(strcmp(DimName,ListDimName),1);%look for dimension name DimName in the current list
276        if isempty(iprev)% append the dimension name to the current list
277            nbdim=nbdim+1;
278            RangeTest(nbdim)=0; %default
279            if sizvar(idim)==2 && strcmp(DimName,VarName)%case of a coordinate defined by the two end values (regular spacing)
280                RangeTest(nbdim)=1; %to be updated for a later variable 
281            end
282            DimValue(nbdim)=sizvar(idim);
283            ListDimName{nbdim}=DimName;
284            DimIndex=[DimIndex nbdim];
285        else % DimName is detected in the current list of dimension names
286            if ~isequal(DimValue(iprev),sizvar(idim))
287                if isequal(DimValue(iprev),2)&& RangeTest(iprev)  % the dimension has been already detected as a range [min max]
288                    DimValue(iprev)=sizvar(idim); %update with actual value
289                elseif ~testrange               
290                    errormsg=['dimension declaration inconsistent with the size =[' num2str(sizvar) '] for ' VarName];
291                    return
292                end
293            end
294            DimIndex=[DimIndex iprev];
295        end
296    end
297    VarDimIndex{ivar}=DimIndex;
298end
Note: See TracBrowser for help on using the repository browser.