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='invalid input for the netcf file name'; |
---|
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 | if ~isempty(errormsg) |
---|
55 | errormsg=['invalid input structure:' errormsg]; |
---|
56 | return |
---|
57 | end |
---|
58 | ListVarName=Data.ListVarName; |
---|
59 | nc=netcdf.create(flname,'NC_CLOBBER');%,'clobber'); %create the netcdf file with name flname |
---|
60 | %write global constants |
---|
61 | if isfield(Data,'ListGlobalAttribute') |
---|
62 | keys=Data.ListGlobalAttribute; |
---|
63 | for iattr=1:length(keys) |
---|
64 | if isfield(Data,keys{iattr}) |
---|
65 | testvar=0; |
---|
66 | for ivar=1:length(ListVarName)% eliminate possible global attributes with the same name as a variable |
---|
67 | if isequal(ListVarName{ivar}, keys{iattr}) |
---|
68 | testvar=1; |
---|
69 | break |
---|
70 | end |
---|
71 | end |
---|
72 | if ~testvar |
---|
73 | eval(['cte=Data.' keys{iattr} ';']) |
---|
74 | if (ischar(cte) ||isnumeric(cte)) && ~isempty(cte)%&& ~isequal(cte,'') |
---|
75 | %write constant only if it is numeric or char string, and not empty |
---|
76 | netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),keys{iattr},cte) |
---|
77 | end |
---|
78 | end |
---|
79 | end |
---|
80 | end |
---|
81 | end |
---|
82 | %create dimensions |
---|
83 | dimid=zeros(1,length(Data.ListDimName)); |
---|
84 | for idim=1:length(Data.ListDimName) |
---|
85 | dimid(idim) = netcdf.defDim(nc,Data.ListDimName{idim},Data.DimValue(idim)); |
---|
86 | end |
---|
87 | VarAttribute={}; %default |
---|
88 | testattr=0; |
---|
89 | if isfield(Data,'VarAttribute') |
---|
90 | VarAttribute=Data.VarAttribute; |
---|
91 | testattr=1; |
---|
92 | end |
---|
93 | varid=zeros(1,length(Data.ListVarName)); |
---|
94 | for ivar=1:length(ListVarName) |
---|
95 | varid(ivar)=netcdf.defVar(nc,ListVarName{ivar},'nc_double',dimid(Data.VarDimIndex{ivar}));%define variable |
---|
96 | end |
---|
97 | %write variable attributes |
---|
98 | if testattr |
---|
99 | for ivar=1:min(numel(VarAttribute),numel(ListVarName)) |
---|
100 | if isstruct(VarAttribute{ivar}) |
---|
101 | attr_names=fields(VarAttribute{ivar}); |
---|
102 | for iattr=1:length(attr_names) |
---|
103 | eval(['attr_val=VarAttribute{ivar}.' attr_names{iattr} ';']); |
---|
104 | if ~isempty(attr_names{iattr})&& ~isempty(attr_val) |
---|
105 | netcdf.putAtt(nc,varid(ivar),attr_names{iattr},attr_val); |
---|
106 | end |
---|
107 | end |
---|
108 | end |
---|
109 | end |
---|
110 | end |
---|
111 | netcdf.endDef(nc); %put in data mode |
---|
112 | for ivar=1:length(ListVarName) |
---|
113 | if isfield(Data,ListVarName{ivar}) |
---|
114 | eval(['VarVal=Data.' ListVarName{ivar} ';'])%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), Data.DimValue(VarDimIndex));%matlab vector |
---|
123 | testcolumn=isequal(length(siz),2) && isequal(siz(1), Data.DimValue(VarDimIndex))&& isequal(siz(2),1);%matlab column vector |
---|
124 | if ~testrange && ~testline && ~testcolumn && ~isequal(siz,Data.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),Data.DimValue(VarDimIndex));% 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 |
---|
137 | end |
---|
138 | netcdf.close(nc) |
---|
139 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
140 | %OLD netcdf toolbox |
---|
141 | else |
---|
142 | errormsg=struct2nc_toolbox(flname,Data); |
---|
143 | end |
---|
144 | |
---|