[106] | 1 | % 'struct2nc': create a netcdf file from a Matlab structure |
---|
| 2 | %--------------------------------------------------------------------- |
---|
| 3 | % errormsg=struct2nc(flname,Data) |
---|
| 4 | % |
---|
[811] | 5 | % OUTPUT: |
---|
[106] | 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 | |
---|
[809] | 19 | %======================================================================= |
---|
[924] | 20 | % Copyright 2008-2016, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
[809] | 21 | % http://www.legi.grenoble-inp.fr |
---|
| 22 | % Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr |
---|
| 23 | % |
---|
[106] | 24 | % This file is part of the toolbox UVMAT. |
---|
[809] | 25 | % |
---|
[106] | 26 | % UVMAT is free software; you can redistribute it and/or modify |
---|
[809] | 27 | % it under the terms of the GNU General Public License as published |
---|
| 28 | % by the Free Software Foundation; either version 2 of the license, |
---|
| 29 | % or (at your option) any later version. |
---|
| 30 | % |
---|
[106] | 31 | % UVMAT is distributed in the hope that it will be useful, |
---|
| 32 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 33 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
[809] | 34 | % GNU General Public License (see LICENSE.txt) for more details. |
---|
| 35 | %======================================================================= |
---|
[106] | 36 | |
---|
[930] | 37 | function [errormsg,nc]=struct2nc(flname,Data,action) |
---|
| 38 | nc=[]; |
---|
| 39 | % if ~ischar(flname) |
---|
| 40 | % errormsg='invalid input for the netcf file name'; |
---|
| 41 | % return |
---|
| 42 | % end |
---|
[106] | 43 | if ~exist('Data','var') |
---|
| 44 | errormsg='no data input for the netcdf file'; |
---|
| 45 | return |
---|
| 46 | end |
---|
[930] | 47 | if ~exist('action','var') |
---|
| 48 | action='one_input'; %fill the file with data and close it |
---|
[404] | 49 | end |
---|
[919] | 50 | |
---|
[930] | 51 | |
---|
[919] | 52 | %% check the validity of the input field structure |
---|
[404] | 53 | [errormsg,ListDimName,DimValue,VarDimIndex]=check_field_structure(Data); |
---|
| 54 | if ~isempty(errormsg) |
---|
| 55 | errormsg=['error in struct2nc:invalid input structure_' errormsg]; |
---|
| 56 | return |
---|
| 57 | end |
---|
| 58 | ListVarName=Data.ListVarName; |
---|
[919] | 59 | |
---|
| 60 | %% create the netcdf file with name flname in format NETCDF4 |
---|
[930] | 61 | if ischar(flname) |
---|
| 62 | FilePath=fileparts(flname); |
---|
| 63 | if ~strcmp(FilePath,'') && ~exist(FilePath,'dir') |
---|
| 64 | errormsg=['directory ' FilePath ' needs to be created']; |
---|
| 65 | return |
---|
| 66 | end |
---|
| 67 | cmode = netcdf.getConstant('NETCDF4'); |
---|
| 68 | cmode = bitor(cmode, netcdf.getConstant('CLASSIC_MODEL')); |
---|
| 69 | cmode = bitor(cmode, netcdf.getConstant('CLOBBER')); |
---|
| 70 | nc = netcdf.create(flname, cmode); |
---|
| 71 | else |
---|
| 72 | nc=flname; |
---|
| 73 | end |
---|
[913] | 74 | |
---|
[919] | 75 | %% write global constants |
---|
[404] | 76 | if isfield(Data,'ListGlobalAttribute') |
---|
| 77 | keys=Data.ListGlobalAttribute; |
---|
| 78 | for iattr=1:length(keys) |
---|
| 79 | if isfield(Data,keys{iattr}) |
---|
| 80 | testvar=0; |
---|
| 81 | for ivar=1:length(ListVarName)% eliminate possible global attributes with the same name as a variable |
---|
| 82 | if isequal(ListVarName{ivar}, keys{iattr}) |
---|
| 83 | testvar=1; |
---|
| 84 | break |
---|
[106] | 85 | end |
---|
[404] | 86 | end |
---|
| 87 | if ~testvar |
---|
| 88 | eval(['cte=Data.' keys{iattr} ';']) |
---|
| 89 | if (ischar(cte) ||isnumeric(cte)) && ~isempty(cte)%&& ~isequal(cte,'') |
---|
| 90 | %write constant only if it is numeric or char string, and not empty |
---|
| 91 | netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),keys{iattr},cte) |
---|
[106] | 92 | end |
---|
| 93 | end |
---|
| 94 | end |
---|
| 95 | end |
---|
[404] | 96 | end |
---|
[919] | 97 | |
---|
| 98 | %% create the dimensions |
---|
[404] | 99 | dimid=zeros(1,length(ListDimName)); |
---|
| 100 | for idim=1:length(ListDimName) |
---|
| 101 | dimid(idim) = netcdf.defDim(nc,ListDimName{idim},DimValue(idim)); |
---|
| 102 | end |
---|
| 103 | VarAttribute={}; %default |
---|
| 104 | testattr=0; |
---|
| 105 | if isfield(Data,'VarAttribute') |
---|
| 106 | VarAttribute=Data.VarAttribute; |
---|
| 107 | testattr=1; |
---|
| 108 | end |
---|
[919] | 109 | |
---|
| 110 | |
---|
| 111 | %% create the variables |
---|
| 112 | varid=nan(1,length(Data.ListVarName)); |
---|
[404] | 113 | for ivar=1:length(ListVarName) |
---|
[919] | 114 | if isfield(Data,ListVarName{ivar}) |
---|
| 115 | VarClass=class(Data.(ListVarName{ivar})); |
---|
| 116 | VarType=''; |
---|
| 117 | switch VarClass |
---|
| 118 | case {'single','double'} |
---|
| 119 | VarType='nc_float'; % store all floating reals as single |
---|
| 120 | case {'uint8','int16','uint16','int32','uint32','int64','uint64'} |
---|
| 121 | VarType='nc_int'; |
---|
| 122 | case 'logical' |
---|
| 123 | VarType='nc_byte'; |
---|
| 124 | end |
---|
| 125 | if ~isempty(VarType) |
---|
| 126 | varid(ivar)=netcdf.defVar(nc,ListVarName{ivar},VarType,dimid(VarDimIndex{ivar}));%define variable |
---|
| 127 | end |
---|
| 128 | end |
---|
[404] | 129 | end |
---|
[919] | 130 | |
---|
| 131 | %% write variable attributes |
---|
[404] | 132 | if testattr |
---|
| 133 | for ivar=1:min(numel(VarAttribute),numel(ListVarName)) |
---|
[919] | 134 | if isstruct(VarAttribute{ivar}) && ~isnan(varid(ivar)) |
---|
[404] | 135 | attr_names=fields(VarAttribute{ivar}); |
---|
| 136 | for iattr=1:length(attr_names) |
---|
[526] | 137 | attr_val=VarAttribute{ivar}.(attr_names{iattr}); |
---|
| 138 | if ~isempty(attr_names{iattr})&& ~isempty(attr_val)&&~iscell(attr_val) |
---|
[404] | 139 | netcdf.putAtt(nc,varid(ivar),attr_names{iattr},attr_val); |
---|
[106] | 140 | end |
---|
| 141 | end |
---|
| 142 | end |
---|
| 143 | end |
---|
[404] | 144 | end |
---|
| 145 | netcdf.endDef(nc); %put in data mode |
---|
[919] | 146 | |
---|
| 147 | %% fill the variables with input data |
---|
[404] | 148 | for ivar=1:length(ListVarName) |
---|
[919] | 149 | if ~isnan(varid(ivar)) |
---|
[404] | 150 | VarVal=Data.(ListVarName{ivar}); |
---|
| 151 | %varval=values of the current variable |
---|
| 152 | VarDimName=Data.VarDimName{ivar}; |
---|
| 153 | if ischar(VarDimName) |
---|
| 154 | VarDimName={VarDimName}; |
---|
| 155 | end |
---|
| 156 | siz=size(VarVal); |
---|
| 157 | 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. |
---|
| 158 | testline=isequal(length(siz),2) && isequal(siz(1),1)&& isequal(siz(2), DimValue(VarDimIndex{ivar}));%matlab vector |
---|
[919] | 159 | %testcolumn=isequal(length(siz),2) && isequal(siz(1), DimValue(VarDimIndex{ivar}))&& isequal(siz(2),1);%matlab column vector |
---|
[404] | 160 | if testline || testrange |
---|
| 161 | if testrange |
---|
| 162 | VarVal=linspace(VarVal(1),VarVal(2),DimValue(VarDimIndex{ivar}));% restitute the whole array of coordinate values |
---|
| 163 | end |
---|
| 164 | netcdf.putVar(nc,varid(ivar), double(VarVal')); |
---|
| 165 | else |
---|
| 166 | netcdf.putVar(nc,varid(ivar), double(VarVal)); |
---|
| 167 | end |
---|
[106] | 168 | end |
---|
| 169 | end |
---|
[930] | 170 | if strcmp(action,'one_input') |
---|
[404] | 171 | netcdf.close(nc) |
---|
[938] | 172 | [success,errormsg] = fileattrib(flname ,'+w');% allow writing access for the group of users |
---|
[930] | 173 | end |
---|
[106] | 174 | |
---|
[530] | 175 | %'check_field_structure': check the validity of the field struture representation consistant with the netcdf format |
---|
| 176 | %------------------------------------------------------------------------ |
---|
[674] | 177 | % [errormsg,ListDimName,DimValue,VarDimIndex]=check_field_structure(Data) |
---|
[530] | 178 | % |
---|
| 179 | % OUTPUT: |
---|
| 180 | % errormsg: error message which is not empty when the input structure does not have the right form |
---|
[674] | 181 | % ListDimName: list of dimension names (cell array of cahr strings) |
---|
| 182 | % DimValue: list of dimension values (numerical array with the same dimension as ListDimName) |
---|
| 183 | % VarDimIndex: cell array of dimension index (in the list ListDimName) for each element of Data.ListVarName |
---|
[530] | 184 | % |
---|
| 185 | % INPUT: |
---|
| 186 | % Data: structure containing |
---|
| 187 | % (optional) .ListGlobalAttribute: cell listing the names of the global attributes |
---|
| 188 | % .Att_1,Att_2... : values of the global attributes |
---|
| 189 | % (requested) .ListVarName: list of variable names to select (cell array of char strings {'VarName1', 'VarName2',...} ) |
---|
| 190 | % (requested) .VarDimName: list of dimension names for each element of .ListVarName (cell array of string cells) |
---|
| 191 | % (requested) .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | function [errormsg,ListDimName,DimValue,VarDimIndex]=check_field_structure(Data) |
---|
| 195 | errormsg=''; |
---|
| 196 | ListDimName={}; |
---|
| 197 | DimValue=[]; %default |
---|
| 198 | VarDimIndex={}; |
---|
| 199 | if ~isstruct(Data) |
---|
| 200 | errormsg='input field is not a structure'; |
---|
| 201 | return |
---|
| 202 | end |
---|
| 203 | if isfield(Data,'ListVarName') && iscell(Data.ListVarName) |
---|
| 204 | nbfield=numel(Data.ListVarName); |
---|
| 205 | else |
---|
| 206 | errormsg='input field does not contain the cell array of variable names .ListVarName'; |
---|
| 207 | return |
---|
| 208 | end |
---|
| 209 | %check dimension names |
---|
| 210 | if (isfield(Data,'VarDimName') && iscell(Data.VarDimName)) |
---|
| 211 | if numel(Data.VarDimName)~=nbfield |
---|
| 212 | errormsg=' .ListVarName and .VarDimName have different lengths'; |
---|
| 213 | return |
---|
| 214 | end |
---|
| 215 | else |
---|
| 216 | errormsg='input field does not contain the cell array of dimension names .VarDimName'; |
---|
| 217 | return |
---|
| 218 | end |
---|
| 219 | nbdim=0; |
---|
| 220 | ListDimName={}; |
---|
| 221 | |
---|
| 222 | %% main loop on the list of variables |
---|
[674] | 223 | VarDimIndex=cell(1,nbfield); |
---|
[530] | 224 | for ivar=1:nbfield |
---|
| 225 | VarName=Data.ListVarName{ivar}; |
---|
| 226 | if ~isfield(Data,VarName) |
---|
| 227 | errormsg=['the listed variable ' VarName ' is not found']; |
---|
| 228 | return |
---|
| 229 | end |
---|
| 230 | sizvar=size(Data.(VarName));% sizvar = dimension of variable |
---|
| 231 | DimCell=Data.VarDimName{ivar}; |
---|
| 232 | if ischar(DimCell) |
---|
| 233 | DimCell={DimCell};%case of a single dimension name, defined by a string |
---|
| 234 | elseif ~iscell(DimCell) |
---|
| 235 | errormsg=['wrong format for .VarDimName{' num2str(ivar) ' (must be the cell of dimension names of the variable ' VarName]; |
---|
[674] | 236 | return |
---|
[530] | 237 | end |
---|
| 238 | nbcoord=numel(sizvar);%nbre of coordinates for variable named VarName |
---|
| 239 | testrange=0; |
---|
| 240 | if numel(DimCell)==0 |
---|
| 241 | errormsg=['empty declared dimension .VarDimName{' num2str(ivar) '} for ' VarName]; |
---|
| 242 | return |
---|
| 243 | elseif numel(DimCell)==1% one dimension declared |
---|
| 244 | if nbcoord==2 |
---|
| 245 | if sizvar(1)==1 |
---|
| 246 | sizvar(1)=sizvar(2); |
---|
| 247 | elseif sizvar(2)==1 |
---|
| 248 | else |
---|
| 249 | errormsg=['1 dimension declared in .VarDimName{' num2str(ivar) '} inconsistent with the nbre of dimensions =2 of the variable ' VarName]; |
---|
| 250 | return |
---|
| 251 | end |
---|
| 252 | if sizvar(1)==2 && isequal(VarName,DimCell{1}) |
---|
| 253 | testrange=1;% test for a dimension variable representing a range |
---|
| 254 | end |
---|
| 255 | else |
---|
| 256 | errormsg=['1 dimension declared in .VarDimName{' num2str(ivar) '} inconsistent with the nbre of dimensions =' num2str(nbcoord) ' of the variable ' VarName]; |
---|
| 257 | return |
---|
| 258 | end |
---|
| 259 | else |
---|
| 260 | if numel(DimCell)>nbcoord |
---|
| 261 | sizvar(nbcoord+1:numel(DimCell))=1;% case of singleton dimensions (not seen by the function size) |
---|
| 262 | elseif nbcoord > numel(DimCell) |
---|
| 263 | errormsg=['nbre of declared dimensions in .VarDimName{' num2str(ivar) '} smaller than the nbre of dimensions =' num2str(nbcoord) ' of the variable ' VarName]; |
---|
| 264 | return |
---|
| 265 | end |
---|
| 266 | end |
---|
| 267 | DimIndex=[]; |
---|
| 268 | for idim=1:numel(DimCell) %loop on the coordinates of variable #ivar |
---|
| 269 | DimName=DimCell{idim}; |
---|
| 270 | iprev=find(strcmp(DimName,ListDimName),1);%look for dimension name DimName in the current list |
---|
| 271 | if isempty(iprev)% append the dimension name to the current list |
---|
| 272 | nbdim=nbdim+1; |
---|
| 273 | RangeTest(nbdim)=0; %default |
---|
| 274 | if sizvar(idim)==2 && strcmp(DimName,VarName)%case of a coordinate defined by the two end values (regular spacing) |
---|
| 275 | RangeTest(nbdim)=1; %to be updated for a later variable |
---|
| 276 | end |
---|
| 277 | DimValue(nbdim)=sizvar(idim); |
---|
| 278 | ListDimName{nbdim}=DimName; |
---|
| 279 | DimIndex=[DimIndex nbdim]; |
---|
| 280 | else % DimName is detected in the current list of dimension names |
---|
| 281 | if ~isequal(DimValue(iprev),sizvar(idim)) |
---|
| 282 | if isequal(DimValue(iprev),2)&& RangeTest(iprev) % the dimension has been already detected as a range [min max] |
---|
| 283 | DimValue(iprev)=sizvar(idim); %update with actual value |
---|
| 284 | elseif ~testrange |
---|
| 285 | errormsg=['dimension declaration inconsistent with the size =[' num2str(sizvar) '] for ' VarName]; |
---|
| 286 | return |
---|
| 287 | end |
---|
| 288 | end |
---|
| 289 | DimIndex=[DimIndex iprev]; |
---|
| 290 | end |
---|
| 291 | end |
---|
| 292 | VarDimIndex{ivar}=DimIndex; |
---|
[801] | 293 | end |
---|