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 library
|
---|
45 |
|
---|
46 | %USE OF built-in 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 | netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),keys{iattr},cte)
|
---|
72 | else
|
---|
73 | errormsg='global attributes must be characters or numbers';
|
---|
74 | return
|
---|
75 | end
|
---|
76 | end
|
---|
77 | end
|
---|
78 | end
|
---|
79 | end
|
---|
80 | %create dimensions
|
---|
81 | dimid=[];
|
---|
82 | for idim=1:length(Data.ListDimName)
|
---|
83 | dimid(idim) = netcdf.defDim(nc,Data.ListDimName{idim},Data.DimValue(idim));
|
---|
84 | end
|
---|
85 | VarAttribute={}; %default
|
---|
86 | testattr=0;
|
---|
87 | if isfield(Data,'VarAttribute')
|
---|
88 | VarAttribute=Data.VarAttribute;
|
---|
89 | testattr=1;
|
---|
90 | end
|
---|
91 | varid=[];
|
---|
92 | for ivar=1:length(ListVarName)
|
---|
93 | varid(ivar)=netcdf.defVar(nc,ListVarName{ivar},'double',dimid(Data.VarDimIndex{ivar}));%define variable
|
---|
94 | end
|
---|
95 | %write variable attributes
|
---|
96 | if testattr
|
---|
97 | for ivar=1:length(VarAttribute)
|
---|
98 | if isstruct(VarAttribute{ivar})
|
---|
99 | attr_names=fields(VarAttribute{ivar});
|
---|
100 | for iattr=1:length(attr_names)
|
---|
101 | eval(['attr_val=VarAttribute{ivar}.' attr_names{iattr} ';']);
|
---|
102 | if ~isempty(attr_names{iattr})&& ~isempty(attr_val)
|
---|
103 | netcdf.putAtt(nc,varid(ivar),attr_names{iattr},attr_val);
|
---|
104 | end
|
---|
105 | end
|
---|
106 | end
|
---|
107 | end
|
---|
108 | end
|
---|
109 | netcdf.endDef(nc); %put in data mode
|
---|
110 | for ivar=1:length(ListVarName)
|
---|
111 | if isfield(Data,ListVarName{ivar})
|
---|
112 | eval(['VarVal=Data.' ListVarName{ivar} ';'])%varval=values of the current variable
|
---|
113 | VarDimIndex=Data.VarDimIndex{ivar}; %indices of the variable dimensions in the list of dimensions
|
---|
114 | siz=size(VarVal);
|
---|
115 | VarDimName=Data.VarDimName{ivar};%NEW
|
---|
116 | if ischar(VarDimName)%NEW
|
---|
117 | VarDimName={VarDimName};%NEW
|
---|
118 | end%NEW
|
---|
119 | testrange=(numel(VarDimName)==1 && strcmp(VarDimName{1},ListVarName{ivar}) && numel(VarVal)==2); %NEW
|
---|
120 | testline=isequal(length(siz),2) && isequal(siz(1),1)&& isequal(siz(2), Data.DimValue(VarDimIndex));
|
---|
121 | testcolumn=isequal(length(siz),2) && isequal(siz(1), Data.DimValue(VarDimIndex))&& isequal(siz(2),1);
|
---|
122 | if ~testline && ~testcolumn && ~isequal(siz,Data.DimValue(VarDimIndex))
|
---|
123 | errormsg=['wrong dimensions declared for ' ListVarName{ivar} ' in struct2nc.m'];
|
---|
124 | break
|
---|
125 | end
|
---|
126 | if testline || testrange
|
---|
127 | if testrange
|
---|
128 | VarVal=linspace(VarVal(1),VarVal(2),Data.DimValue(VarDimIndex));
|
---|
129 | end
|
---|
130 | %nc{ListVarName{ivar}}=ncfloat(Data.ListDimName(VarDimIndex));%vector of x coordinates
|
---|
131 | netcdf.putVar(nc,varid(ivar), double(VarVal'));
|
---|
132 | else
|
---|
133 | netcdf.putVar(nc,varid(ivar), double(VarVal));
|
---|
134 | %nc{ListVarName{ivar}}(:) = VarVal;
|
---|
135 | end
|
---|
136 |
|
---|
137 | end
|
---|
138 | end
|
---|
139 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
140 | %OLD netcdf toolbox
|
---|
141 | else
|
---|
142 | errormsg=struct2nc_toolbox(flname,Data);
|
---|
143 | end
|
---|
144 |
|
---|