1 | % 'struct2nc': create a netcdf file from a Matlab structure |
---|
2 | %--------------------------------------------------------------------- |
---|
3 | % errormsg=struct2nc(flname,Data) |
---|
4 | % |
---|
5 | % OUPUT: |
---|
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 | %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
---|
20 | % Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org. |
---|
21 | %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
---|
22 | % This file is part of the toolbox UVMAT. |
---|
23 | % |
---|
24 | % UVMAT is free software; you can redistribute it and/or modify |
---|
25 | % it under the terms of the GNU General Public License as published by |
---|
26 | % the Free Software Foundation; either version 2 of the License, or |
---|
27 | % (at your option) any later version. |
---|
28 | % |
---|
29 | % UVMAT is distributed in the hope that it will be useful, |
---|
30 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
31 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
32 | % GNU General Public License (file UVMAT/COPYING.txt) for more details. |
---|
33 | %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
---|
34 | |
---|
35 | function errormsg=struct2nc(flname,Data) |
---|
36 | if ~ischar(flname) |
---|
37 | errormsg='no name input for the netcf file'; |
---|
38 | return |
---|
39 | end |
---|
40 | if ~exist('Data','var') |
---|
41 | errormsg='no data input for the netcdf file'; |
---|
42 | return |
---|
43 | end |
---|
44 | hhh=which('netcdf.create');% look for built-in matlab netcdf library |
---|
45 | |
---|
46 | %USE OF built-in matlab netcdf library |
---|
47 | if ~isequal(hhh,'') |
---|
48 | FilePath=fileparts(flname); |
---|
49 | if ~strcmp(FilePath,'') && ~exist(FilePath,'dir') |
---|
50 | errormsg=['directory ' FilePath ' needs to be created']; |
---|
51 | return |
---|
52 | end |
---|
53 | [Data,errormsg]=check_field_structure(Data);%check the validity of the input field structure |
---|
54 | ListVarName=Data.ListVarName; |
---|
55 | nc=netcdf.create(flname,'NC_CLOBBER');%,'clobber'); %create the netcdf file with name flname |
---|
56 | %write global constants |
---|
57 | if isfield(Data,'ListGlobalAttribute') |
---|
58 | keys=Data.ListGlobalAttribute; |
---|
59 | for iattr=1:length(keys) |
---|
60 | if isfield(Data,keys{iattr}) |
---|
61 | testvar=0; |
---|
62 | for ivar=1:length(ListVarName)% eliminate possible global attributes with the same name as a variable |
---|
63 | if isequal(ListVarName{ivar}, keys{iattr}) |
---|
64 | testvar=1; |
---|
65 | break |
---|
66 | end |
---|
67 | end |
---|
68 | if ~testvar |
---|
69 | eval(['cte=Data.' keys{iattr} ';']) |
---|
70 | if (ischar(cte) ||isnumeric(cte)) && ~isempty(cte)%&& ~isequal(cte,'') |
---|
71 | %write constant only if it is numeric or char string, and not empty |
---|
72 | netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),keys{iattr},cte) |
---|
73 | end |
---|
74 | end |
---|
75 | end |
---|
76 | end |
---|
77 | end |
---|
78 | %create dimensions |
---|
79 | dimid=zeros(1,length(Data.ListDimName)); |
---|
80 | for idim=1:length(Data.ListDimName) |
---|
81 | dimid(idim) = netcdf.defDim(nc,Data.ListDimName{idim},Data.DimValue(idim)); |
---|
82 | end |
---|
83 | VarAttribute={}; %default |
---|
84 | testattr=0; |
---|
85 | if isfield(Data,'VarAttribute') |
---|
86 | VarAttribute=Data.VarAttribute; |
---|
87 | testattr=1; |
---|
88 | end |
---|
89 | varid=zeros(1,length(Data.ListVarName)); |
---|
90 | for ivar=1:length(ListVarName) |
---|
91 | varid(ivar)=netcdf.defVar(nc,ListVarName{ivar},'nc_double',dimid(Data.VarDimIndex{ivar}));%define variable |
---|
92 | end |
---|
93 | %write variable attributes |
---|
94 | if testattr |
---|
95 | for ivar=1:min(numel(VarAttribute),numel(ListVarName)) |
---|
96 | if isstruct(VarAttribute{ivar}) |
---|
97 | attr_names=fields(VarAttribute{ivar}); |
---|
98 | for iattr=1:length(attr_names) |
---|
99 | eval(['attr_val=VarAttribute{ivar}.' attr_names{iattr} ';']); |
---|
100 | if ~isempty(attr_names{iattr})&& ~isempty(attr_val) |
---|
101 | netcdf.putAtt(nc,varid(ivar),attr_names{iattr},attr_val); |
---|
102 | end |
---|
103 | end |
---|
104 | end |
---|
105 | end |
---|
106 | end |
---|
107 | netcdf.endDef(nc); %put in data mode |
---|
108 | for ivar=1:length(ListVarName) |
---|
109 | if isfield(Data,ListVarName{ivar}) |
---|
110 | eval(['VarVal=Data.' ListVarName{ivar} ';'])%varval=values of the current variable |
---|
111 | VarDimIndex=Data.VarDimIndex{ivar}; %indices of the variable dimensions in the list of dimensions |
---|
112 | siz=size(VarVal); |
---|
113 | VarDimName=Data.VarDimName{ivar}; |
---|
114 | if ischar(VarDimName) |
---|
115 | VarDimName={VarDimName}; |
---|
116 | end |
---|
117 | 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. |
---|
118 | testline=isequal(length(siz),2) && isequal(siz(1),1)&& isequal(siz(2), Data.DimValue(VarDimIndex));%matlab vector |
---|
119 | testcolumn=isequal(length(siz),2) && isequal(siz(1), Data.DimValue(VarDimIndex))&& isequal(siz(2),1);%matlab column vector |
---|
120 | if ~testrange && ~testline && ~testcolumn && ~isequal(siz,Data.DimValue(VarDimIndex)) |
---|
121 | errormsg=['wrong dimensions declared for ' ListVarName{ivar} ' in struct2nc.m']; |
---|
122 | break |
---|
123 | end |
---|
124 | if testline || testrange |
---|
125 | if testrange |
---|
126 | VarVal=linspace(VarVal(1),VarVal(2),Data.DimValue(VarDimIndex));% restitute the whole array of coordinate values |
---|
127 | end |
---|
128 | netcdf.putVar(nc,varid(ivar), double(VarVal')); |
---|
129 | else |
---|
130 | netcdf.putVar(nc,varid(ivar), double(VarVal)); |
---|
131 | end |
---|
132 | end |
---|
133 | end |
---|
134 | netcdf.close(nc) |
---|
135 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
136 | %OLD netcdf toolbox |
---|
137 | else |
---|
138 | errormsg=struct2nc_toolbox(flname,Data); |
---|
139 | end |
---|
140 | |
---|