source: trunk/src/struct2nc_toolbox.m @ 985

Last change on this file since 985 was 977, checked in by g7moreau, 4 years ago
  • Update Copyright 2008-2017 notice
File size: 5.8 KB
Line 
1%'struct2nc_toolbox': create a netcdf file from a Matlab structure: use of netcdf toolbox
2%---------------------------------------------------------------------
3% errormsg=struct2nc_toolbox(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%    .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
20
21%=======================================================================
22% Copyright 2008-2017, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
23%   http://www.legi.grenoble-inp.fr
24%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
25%
26%     This file is part of the toolbox UVMAT.
27%
28%     UVMAT is free software; you can redistribute it and/or modify
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%
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
36%     GNU General Public License (see LICENSE.txt) for more details.
37%=======================================================================
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
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);
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
104        if testline || testrange
105           dimname=Data.ListDimName{VarDimIndex};
106           if testrange
107               VarVal=linspace(VarVal(1),VarVal(2),Data.DimValue(VarDimIndex));
108           end
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
115    end
116end
117%write variable attributes
118if testattr
119    for ivar=1:min(numel(VarAttribute),numel(ListVarName))  %loop on the attributes of variable ivar
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 ;'])
128                end
129            end
130        end
131    end
132 end
133
134
135close(nc);
Note: See TracBrowser for help on using the repository browser.