source: trunk/src/struct2nc.m @ 849

Last change on this file since 849 was 811, checked in by g7moreau, 7 years ago
  • Update header OUPUT -> OUTPUT and so on...
File size: 11.2 KB
Line 
1% 'struct2nc': create a netcdf file from a Matlab structure
2%---------------------------------------------------------------------
3% errormsg=struct2nc(flname,Data)
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
19%=======================================================================
20% Copyright 2008-2014, LEGI UMR 5519 / CNRS UJF G-INP, Grenoble, France
21%   http://www.legi.grenoble-inp.fr
22%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
23%
24%     This file is part of the toolbox UVMAT.
25%
26%     UVMAT is free software; you can redistribute it and/or modify
27%     it under the terms of the GNU General Public License as published
28%     by the Free Software Foundation; either version 2 of the license,
29%     or (at your option) any later version.
30%
31%     UVMAT is distributed in the hope that it will be useful,
32%     but WITHOUT ANY WARRANTY; without even the implied warranty of
33%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34%     GNU General Public License (see LICENSE.txt) for more details.
35%=======================================================================
36
37function errormsg=struct2nc(flname,Data)
38if ~ischar(flname)
39    errormsg='invalid input for the netcf file name';
40    return
41end
42if ~exist('Data','var')
43     errormsg='no data  input for the netcdf file';
44    return
45end
46FilePath=fileparts(flname);
47if ~strcmp(FilePath,'') && ~exist(FilePath,'dir')
48    errormsg=['directory ' FilePath ' needs to be created'];
49    return
50end
51%check the validity of the input field structure
52[errormsg,ListDimName,DimValue,VarDimIndex]=check_field_structure(Data);
53if ~isempty(errormsg)
54    errormsg=['error in struct2nc:invalid input structure_' errormsg];
55    return
56end
57ListVarName=Data.ListVarName;
58nc=netcdf.create(flname,'NC_CLOBBER');%,'clobber'); %create the netcdf file with name flname   
59%write global constants
60if isfield(Data,'ListGlobalAttribute')
61    keys=Data.ListGlobalAttribute;
62    for iattr=1:length(keys)
63        if isfield(Data,keys{iattr})
64             testvar=0;
65            for ivar=1:length(ListVarName)% eliminate possible global attributes with the same name as a variable
66                if isequal(ListVarName{ivar}, keys{iattr})
67                    testvar=1;
68                    break
69                end
70            end
71            if ~testvar               
72                eval(['cte=Data.' keys{iattr} ';'])
73                if (ischar(cte) ||isnumeric(cte)) &&  ~isempty(cte)%&& ~isequal(cte,'')
74                    %write constant only if it is numeric or char string, and not empty
75                    netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),keys{iattr},cte)
76                end
77            end
78        end
79    end
80end
81%create dimensions
82dimid=zeros(1,length(ListDimName));
83for idim=1:length(ListDimName)
84     dimid(idim) = netcdf.defDim(nc,ListDimName{idim},DimValue(idim));
85end
86VarAttribute={}; %default
87testattr=0;
88if isfield(Data,'VarAttribute')
89    VarAttribute=Data.VarAttribute;
90    testattr=1;
91end
92varid=zeros(1,length(Data.ListVarName));
93for ivar=1:length(ListVarName)
94    varid(ivar)=netcdf.defVar(nc,ListVarName{ivar},'nc_double',dimid(VarDimIndex{ivar}));%define variable 
95end
96 %write variable attributes
97if testattr
98    for ivar=1:min(numel(VarAttribute),numel(ListVarName)) 
99        if isstruct(VarAttribute{ivar})
100            attr_names=fields(VarAttribute{ivar});
101            for iattr=1:length(attr_names)
102                attr_val=VarAttribute{ivar}.(attr_names{iattr});
103                if ~isempty(attr_names{iattr})&& ~isempty(attr_val)&&~iscell(attr_val)
104                    netcdf.putAtt(nc,varid(ivar),attr_names{iattr},attr_val);
105                end
106            end
107        end
108    end
109end
110netcdf.endDef(nc); %put in data mode
111for ivar=1:length(ListVarName)
112    if isfield(Data,ListVarName{ivar})
113        VarVal=Data.(ListVarName{ivar});
114        %varval=values of the current variable
115%        VarDimIndex=Data.VarDimIndex{ivar}; %indices of the variable dimensions in the list of dimensions
116        VarDimName=Data.VarDimName{ivar};
117        if ischar(VarDimName)
118            VarDimName={VarDimName};
119        end
120        siz=size(VarVal);
121        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.
122        testline=isequal(length(siz),2) && isequal(siz(1),1)&& isequal(siz(2), DimValue(VarDimIndex{ivar}));%matlab vector
123        testcolumn=isequal(length(siz),2) && isequal(siz(1), DimValue(VarDimIndex{ivar}))&& isequal(siz(2),1);%matlab column vector
124%             if ~testrange && ~testline && ~testcolumn && ~isequal(siz,DimValue(VarDimIndex))
125%                 errormsg=['wrong dimensions declared for ' ListVarName{ivar} ' in struct2nc.m'];
126%                 break
127%             end
128        if testline || testrange
129            if testrange
130                VarVal=linspace(VarVal(1),VarVal(2),DimValue(VarDimIndex{ivar}));% restitute the whole array of coordinate values
131            end
132            netcdf.putVar(nc,varid(ivar), double(VarVal'));
133        else
134            netcdf.putVar(nc,varid(ivar), double(VarVal));
135        end     
136    end
137end
138netcdf.close(nc)
139
140
141%'check_field_structure': check the validity of the field struture representation consistant with the netcdf format
142%------------------------------------------------------------------------
143% [errormsg,ListDimName,DimValue,VarDimIndex]=check_field_structure(Data)
144%
145% OUTPUT:
146% errormsg: error message which is not empty when the input structure does not have the right form
147% ListDimName: list of dimension names (cell array of cahr strings)
148% DimValue: list of dimension values (numerical array with the same dimension as ListDimName)
149% VarDimIndex: cell array of dimension index (in the list ListDimName) for each element of Data.ListVarName
150%
151% INPUT:
152% Data:   structure containing
153%         (optional) .ListGlobalAttribute: cell listing the names of the global attributes
154%                    .Att_1,Att_2... : values of the global attributes
155%         (requested)  .ListVarName: list of variable names to select (cell array of  char strings {'VarName1', 'VarName2',...} )
156%         (requested)  .VarDimName: list of dimension names for each element of .ListVarName (cell array of string cells)                         
157%         (requested) .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
158
159
160function [errormsg,ListDimName,DimValue,VarDimIndex]=check_field_structure(Data)
161errormsg='';
162ListDimName={};
163DimValue=[]; %default
164VarDimIndex={};
165if ~isstruct(Data)
166    errormsg='input field is not a structure';
167    return
168end
169if isfield(Data,'ListVarName') && iscell(Data.ListVarName)
170    nbfield=numel(Data.ListVarName);
171else
172    errormsg='input field does not contain the cell array of variable names .ListVarName';
173    return
174end
175%check dimension names
176if (isfield(Data,'VarDimName') && iscell(Data.VarDimName))
177    if  numel(Data.VarDimName)~=nbfield
178       errormsg=' .ListVarName and .VarDimName have different lengths';
179        return
180    end
181else
182    errormsg='input field does not contain the  cell array of dimension names .VarDimName';
183    return
184end
185nbdim=0;
186ListDimName={};
187
188%% main loop on the list of variables
189VarDimIndex=cell(1,nbfield);
190for ivar=1:nbfield
191    VarName=Data.ListVarName{ivar};
192    if ~isfield(Data,VarName)
193        errormsg=['the listed variable ' VarName ' is not found'];
194        return
195    end
196    sizvar=size(Data.(VarName));% sizvar = dimension of variable
197    DimCell=Data.VarDimName{ivar};
198    if ischar(DimCell)
199        DimCell={DimCell};%case of a single dimension name, defined by a string
200    elseif ~iscell(DimCell)
201        errormsg=['wrong format for .VarDimName{' num2str(ivar) ' (must be the cell of dimension names of the variable ' VarName];
202        return       
203    end
204    nbcoord=numel(sizvar);%nbre of coordinates for variable named VarName
205    testrange=0;
206    if numel(DimCell)==0
207        errormsg=['empty declared dimension .VarDimName{' num2str(ivar) '} for ' VarName];
208        return
209    elseif numel(DimCell)==1% one dimension declared
210        if nbcoord==2
211            if sizvar(1)==1
212                sizvar(1)=sizvar(2);
213            elseif sizvar(2)==1
214            else
215                errormsg=['1 dimension declared in .VarDimName{' num2str(ivar) '} inconsistent with the nbre of dimensions =2 of the variable ' VarName];
216                return
217            end
218            if sizvar(1)==2 && isequal(VarName,DimCell{1})
219                testrange=1;% test for a dimension variable representing a range
220            end
221        else
222            errormsg=['1 dimension declared in .VarDimName{' num2str(ivar) '} inconsistent with the nbre of dimensions =' num2str(nbcoord) ' of the variable ' VarName];
223            return
224        end
225    else
226        if numel(DimCell)>nbcoord
227            sizvar(nbcoord+1:numel(DimCell))=1;% case of singleton dimensions (not seen by the function size)
228        elseif nbcoord > numel(DimCell)
229            errormsg=['nbre of declared dimensions in .VarDimName{' num2str(ivar) '} smaller than the nbre of dimensions =' num2str(nbcoord) ' of the variable ' VarName];
230            return
231        end
232    end
233    DimIndex=[];
234    for idim=1:numel(DimCell) %loop on the coordinates of variable #ivar
235        DimName=DimCell{idim};
236        iprev=find(strcmp(DimName,ListDimName),1);%look for dimension name DimName in the current list
237        if isempty(iprev)% append the dimension name to the current list
238            nbdim=nbdim+1;
239            RangeTest(nbdim)=0; %default
240            if sizvar(idim)==2 && strcmp(DimName,VarName)%case of a coordinate defined by the two end values (regular spacing)
241                RangeTest(nbdim)=1; %to be updated for a later variable 
242            end
243            DimValue(nbdim)=sizvar(idim);
244            ListDimName{nbdim}=DimName;
245            DimIndex=[DimIndex nbdim];
246        else % DimName is detected in the current list of dimension names
247            if ~isequal(DimValue(iprev),sizvar(idim))
248                if isequal(DimValue(iprev),2)&& RangeTest(iprev)  % the dimension has been already detected as a range [min max]
249                    DimValue(iprev)=sizvar(idim); %update with actual value
250                elseif ~testrange               
251                    errormsg=['dimension declaration inconsistent with the size =[' num2str(sizvar) '] for ' VarName];
252                    return
253                end
254            end
255            DimIndex=[DimIndex iprev];
256        end
257    end
258    VarDimIndex{ivar}=DimIndex;
259end
Note: See TracBrowser for help on using the repository browser.