source: trunk/src/struct2nc_toolbox.m @ 1109

Last change on this file since 1109 was 1107, checked in by g7moreau, 3 years ago

Update Copyright to 2022

File size: 5.8 KB
RevLine 
[8]1%'struct2nc_toolbox': create a netcdf file from a Matlab structure: use of netcdf toolbox
2%---------------------------------------------------------------------
3% errormsg=struct2nc_toolbox(flname,Data)
4%
[811]5% OUTPUT:
6% errormsg=error message, =[]: default, no error
[8]7%
[811]8% INPUT:
9% flname: name of the netcdf file to create (must end with the extension '.nc')
[8]10%  Data: structure containing all the information of the netcdf file (or netcdf object)
11%           with fields:
12%    .ListGlobalAttribute: cell listing the names of the global attributes (note that a global atribute with the same name as a variable is excluded)
13%        .Att_1,Att_2... : values of the global attributes
14%            .ListDimName: cell listing the names of the array dimensions
15%               .DimValue: array dimension values (Matlab vector with the same length as .ListDimName
16%            .ListVarName: cell listing the names of the variables
17%            .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName
18%           .VarAttribute: cell of structures s containing names and values of variable attributes (s.name=value) for each variable of .ListVarName
19%        .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
[809]20
21%=======================================================================
[1107]22% Copyright 2008-2022, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
[809]23%   http://www.legi.grenoble-inp.fr
24%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
[8]25%
26%     This file is part of the toolbox UVMAT.
[809]27%
[8]28%     UVMAT is free software; you can redistribute it and/or modify
[809]29%     it under the terms of the GNU General Public License as published
30%     by the Free Software Foundation; either version 2 of the license,
31%     or (at your option) any later version.
32%
[8]33%     UVMAT is distributed in the hope that it will be useful,
34%     but WITHOUT ANY WARRANTY; without even the implied warranty of
35%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
[809]36%     GNU General Public License (see LICENSE.txt) for more details.
37%=======================================================================
[8]38
39function errormsg=struct2nc_toolbox(flname,Data)
40
41FilePath=fileparts(flname);
42if ~strcmp(FilePath,'') &&~exist(FilePath,'dir')
43    errormsg=['directory ' FilePath ' needs to be created'];
44    return
45end
46[Data,errormsg]=check_field_structure(Data);
47if ~isempty(errormsg)
48    return
49end
50ListVarName=Data.ListVarName;
51nc=netcdf(flname,'clobber'); %create the netcdf file with name flname
52%write global constants
53if isfield(Data,'ListGlobalAttribute')
54    keys=Data.ListGlobalAttribute;
55    for iattr=1:length(keys)
56        if isfield(Data,keys{iattr})
57             testvar=0;
58            for ivar=1:length(ListVarName)% eliminate possible global attributes with the same name as a variable
59                if isequal(ListVarName{ivar}, keys{iattr})
60                    testvar=1;
61                    break
62                end
63            end
64            if ~testvar               
65                eval(['cte=Data.' keys{iattr} ';'])
66                if ischar(cte) && ~isequal(cte,'')
67                    eval(['nc.' keys{iattr} '=''' cte ''';']);
68                elseif isnumeric(cte)&& ~isempty(cte)
69                    eval(['nc.' keys{iattr} '= cte; ']);
70                else
71                    errormsg='global attributes must be characters or numbers';
72                    return
73                end
74            end
75        end
76    end
77end
78for idim=1:length(Data.ListDimName)
79    nc(Data.ListDimName{idim})=Data.DimValue(idim);%create dimensions
80end
81
82VarAttribute={}; %default
83testattr=0;
84if isfield(Data,'VarAttribute')
85    VarAttribute=Data.VarAttribute;
86    testattr=1;
87end
88for ivar=1:length(ListVarName)
89    if isfield(Data,ListVarName{ivar})
90        eval(['VarVal=Data.' ListVarName{ivar} ';'])%varval=values of the current variable
91        siz=size(VarVal);
92        VarDimIndex=Data.VarDimIndex{ivar}; %indices of the variable dimensions in the list of dimensions
[46]93        VarDimName=Data.VarDimName{ivar};
94        if ischar(VarDimName)
95            VarDimName={VarDimName};
96        end
97        testrange=(numel(VarDimName)==1 && strcmp(VarDimName{1},ListVarName{ivar}) && numel(VarVal)==2);
[8]98        testline=isequal(length(siz),2) & isequal(siz(1),1)& isequal(siz(2), Data.DimValue(VarDimIndex));
99        testcolumn=isequal(length(siz),2) & isequal(siz(1), Data.DimValue(VarDimIndex))& isequal(siz(2),1);
100        if ~testrange && ~testline && ~testcolumn && ~isequal(siz,Data.DimValue(VarDimIndex))
101            errormsg=['wrong dimensions declared for ' ListVarName{ivar} ' in struct2nc.m'];
102            break
103        end
[46]104        if testline || testrange
[8]105           dimname=Data.ListDimName{VarDimIndex};
[46]106           if testrange
107               VarVal=linspace(VarVal(1),VarVal(2),Data.DimValue(VarDimIndex));
108           end
[8]109           nc{ListVarName{ivar}}=ncfloat(dimname);%vector of x coordinates
110           nc{ListVarName{ivar}}(:) = VarVal'; 
111        else
112            nc{ListVarName{ivar}}=ncfloat(Data.ListDimName(VarDimIndex));%vector of x coordinates
113            nc{ListVarName{ivar}}(:) = VarVal;
114        end
[58]115    end
116end
117%write variable attributes
118if testattr
[107]119    for ivar=1:min(numel(VarAttribute),numel(ListVarName))  %loop on the attributes of variable ivar
[58]120        if isstruct(VarAttribute{ivar})
121            attr_names=fields(VarAttribute{ivar});
122            for iattr=1:length(attr_names)
123                eval(['attr_val=VarAttribute{ivar}.' attr_names{iattr} ';']);
124                if ischar(attr_val) && ~isequal(attr_val,'')
125                    eval(['nc{''' ListVarName{ivar} '''}.' attr_names{iattr} '=''' attr_val ''';'])
126                elseif isnumeric(attr_val)&& ~isempty(attr_val)
127                     eval(['nc{''' ListVarName{ivar} '''}.' attr_names{iattr} '=attr_val ;'])
[8]128                end
129            end
[58]130        end
[8]131    end
[58]132 end
[8]133
[58]134
[801]135close(nc);
Note: See TracBrowser for help on using the repository browser.