'struct2nc': create a netcdf file from a Matlab structure --------------------------------------------------------------------- error=struct2nc(flname,Data) OUPUT: error=error message, =0: default, no error INPUT: flname: name of the netcdf file to create (must end with the extension '.nc') Data: structure containing all the information of the netcdf file (or netcdf object) with fields: .ListGlobalAttribute: cell listing the names of the global attributes (note that a global atribute with the same name as a variable is excluded) .Att_1,Att_2... : values of the global attributes .ListDimName: cell listing the names of the array dimensions .DimValue: array dimension values (Matlab vector with the same length as .ListDimName .ListVarName: cell listing the names of the variables .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName .VarAttribute: cell of structures s containing names and values of variable attributes (s.name=value) for each variable of .ListVarName .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA This file is part of the toolbox UVMAT. UVMAT is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. UVMAT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License (file UVMAT/COPYING.txt) for more details. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
0001 %'struct2nc': create a netcdf file from a Matlab structure 0002 %--------------------------------------------------------------------- 0003 % error=struct2nc(flname,Data) 0004 % 0005 %OUPUT: 0006 %error=error message, =0: default, no error 0007 %INPUT: 0008 %flname: name of the netcdf file to create (must end with the extension '.nc') 0009 % Data: structure containing all the information of the netcdf file (or netcdf object) 0010 % with fields: 0011 % .ListGlobalAttribute: cell listing the names of the global attributes (note that a global atribute with the same name as a variable is excluded) 0012 % .Att_1,Att_2... : values of the global attributes 0013 % .ListDimName: cell listing the names of the array dimensions 0014 % .DimValue: array dimension values (Matlab vector with the same length as .ListDimName 0015 % .ListVarName: cell listing the names of the variables 0016 % .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName 0017 % .VarAttribute: cell of structures s containing names and values of variable attributes (s.name=value) for each variable of .ListVarName 0018 % .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName 0019 % 0020 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 0021 % Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org. 0022 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 0023 % This file is part of the toolbox UVMAT. 0024 % 0025 % UVMAT is free software; you can redistribute it and/or modify 0026 % it under the terms of the GNU General Public License as published by 0027 % the Free Software Foundation; either version 2 of the License, or 0028 % (at your option) any later version. 0029 % 0030 % UVMAT is distributed in the hope that it will be useful, 0031 % but WITHOUT ANY WARRANTY; without even the implied warranty of 0032 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0033 % GNU General Public License (file UVMAT/COPYING.txt) for more details. 0034 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 0035 0036 function errormsg=struct2nc(flname,Data) 0037 0038 hhh=which('netcdf.create');% look for built-in matlab library 0039 0040 %USE OF built-in netcdf library 0041 if ~isequal(hhh,'') 0042 FilePath=fileparts(flname); 0043 if ~exist(FilePath,'dir') 0044 errormsg=['directory ' FilePath ' needs to be created']; 0045 return 0046 end 0047 [Data,errormsg]=check_field_structure(Data); 0048 ListVarName=Data.ListVarName; 0049 nc=netcdf.create(flname,'NC_CLOBBER');%,'clobber'); %create the netcdf file with name flname 0050 %write global constants 0051 if isfield(Data,'ListGlobalAttribute') 0052 keys=Data.ListGlobalAttribute; 0053 for iattr=1:length(keys) 0054 if isfield(Data,keys{iattr}) 0055 testvar=0; 0056 for ivar=1:length(ListVarName)% eliminate possible global attributes with the same name as a variable 0057 if isequal(ListVarName{ivar}, keys{iattr}) 0058 testvar=1; 0059 break 0060 end 0061 end 0062 if ~testvar 0063 eval(['cte=Data.' keys{iattr} ';']) 0064 if (ischar(cte) ||isnumeric(cte)) && ~isempty(cte)&& ~isequal(cte,'') 0065 netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),keys{iattr},cte) 0066 else 0067 errormsg='global attributes must be characters or numbers'; 0068 return 0069 end 0070 end 0071 end 0072 end 0073 end 0074 %create dimensions 0075 dimid=[]; 0076 for idim=1:length(Data.ListDimName) 0077 dimid(idim) = netcdf.defDim(nc,Data.ListDimName{idim},Data.DimValue(idim)); 0078 end 0079 VarAttribute={}; %default 0080 testattr=0; 0081 if isfield(Data,'VarAttribute') 0082 VarAttribute=Data.VarAttribute; 0083 testattr=1; 0084 end 0085 varid=[]; 0086 for ivar=1:length(ListVarName) 0087 varid(ivar)=netcdf.defVar(nc,ListVarName{ivar},'double',dimid(Data.VarDimIndex{ivar}));%define variable 0088 end 0089 %write variable attributes 0090 if testattr 0091 for ivar=1:length(VarAttribute) 0092 if isstruct(VarAttribute{ivar}) 0093 attr_names=fields(VarAttribute{ivar}); 0094 for iattr=1:length(attr_names) 0095 eval(['attr_val=VarAttribute{ivar}.' attr_names{iattr} ';']); 0096 netcdf.putAtt(nc,varid(ivar),attr_names{iattr},attr_val); 0097 end 0098 end 0099 end 0100 end 0101 netcdf.endDef(nc); %put in data mode 0102 for ivar=1:length(ListVarName) 0103 if isfield(Data,ListVarName{ivar}) 0104 eval(['VarVal=Data.' ListVarName{ivar} ';'])%varval=values of the current variable 0105 VarDimIndex=Data.VarDimIndex{ivar}; %indices of the variable dimensions in the list of dimensions 0106 siz=size(VarVal); 0107 VarDimName=Data.VarDimName{ivar};%NEW 0108 if ischar(VarDimName)%NEW 0109 VarDimName={VarDimName};%NEW 0110 end%NEW 0111 testrange=(numel(VarDimName)==1 && strcmp(VarDimName{1},ListVarName{ivar}) && numel(VarVal)==2); %NEW 0112 testline=isequal(length(siz),2) && isequal(siz(1),1)&& isequal(siz(2), Data.DimValue(VarDimIndex)); 0113 testcolumn=isequal(length(siz),2) && isequal(siz(1), Data.DimValue(VarDimIndex))&& isequal(siz(2),1); 0114 if ~testline && ~testcolumn && ~isequal(siz,Data.DimValue(VarDimIndex)) 0115 errormsg=['wrong dimensions declared for ' ListVarName{ivar} ' in struct2nc.m']; 0116 break 0117 end 0118 if testline || testrange%NEW 0119 if testrange 0120 VarVal=linspace(VarVal(1),VarVal(2),Data.DimValue(VarDimIndex));%NEW 0121 end 0122 %nc{ListVarName{ivar}}=ncfloat(Data.ListDimName(VarDimIndex));%vector of x coordinates 0123 netcdf.putVar(nc,varid(ivar), VarVal'); 0124 else 0125 netcdf.putVar(nc,varid(ivar), VarVal); 0126 %nc{ListVarName{ivar}}(:) = VarVal; 0127 end 0128 0129 end 0130 end 0131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0132 %OLD netcdf toolbox 0133 else 0134 errormsg=struct2nc_toolbox(flname,Data); 0135 end 0136