Changeset 943 for trunk/src/struct2nc.m
 Timestamp:
 May 2, 2016, 9:49:22 AM (7 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/src/struct2nc.m
r938 r943 1 1 % 'struct2nc': create a netcdf file from a Matlab structure 2 2 % 3 % errormsg=struct2nc(flname,Data )3 % errormsg=struct2nc(flname,Data,action) 4 4 % 5 5 % OUTPUT: … … 16 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 17 % (requested) .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName 18 % action: if ='keep_open', don't close the file after creation, keep it open for further data input 18 19 19 20 %======================================================================= … … 35 36 %======================================================================= 36 37 37 function [errormsg,nc]=struct2nc(flname,Data,action )38 function [errormsg,nc]=struct2nc(flname,Data,action,ListDimName,DimValue,VarDimIndex) 38 39 nc=[]; 39 %if ~ischar(flname)40 %errormsg='invalid input for the netcf file name';41 %return42 %end40 if ~ischar(flname) 41 errormsg='invalid input for the netcf file name'; 42 return 43 end 43 44 if ~exist('Data','var') 44 45 errormsg='no data input for the netcdf file'; 45 46 return 46 47 end 47 if ~exist('action','var')48 action='one_input'; %fill the file with data and close it49 end50 48 51 49 52 50 %% check the validity of the input field structure 51 if ~ (exist('action','var') && strcmp(action,'keep_open')) 53 52 [errormsg,ListDimName,DimValue,VarDimIndex]=check_field_structure(Data); 54 53 if ~isempty(errormsg) … … 56 55 return 57 56 end 57 end 58 58 ListVarName=Data.ListVarName; 59 59 60 60 %% create the netcdf file with name flname in format NETCDF4 61 if ischar(flname)61 % if ischar(flname) 62 62 FilePath=fileparts(flname); 63 63 if ~strcmp(FilePath,'') && ~exist(FilePath,'dir') … … 69 69 cmode = bitor(cmode, netcdf.getConstant('CLOBBER')); 70 70 nc = netcdf.create(flname, cmode); 71 else72 nc=flname;73 end71 % else 72 % nc=flname; 73 % end 74 74 75 75 %% write global constants … … 113 113 for ivar=1:length(ListVarName) 114 114 if isfield(Data,ListVarName{ivar}) 115 VarClass =class(Data.(ListVarName{ivar}));115 VarClass{ivar}=class(Data.(ListVarName{ivar})); 116 116 VarType=''; 117 switch VarClass 117 switch VarClass{ivar} 118 118 case {'single','double'} 119 119 VarType='nc_float'; % store all floating reals as single … … 146 146 147 147 %% fill the variables with input data 148 for ivar=1:length(ListVarName) 149 if ~isnan(varid(ivar)) 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 159 %testcolumn=isequal(length(siz),2) && isequal(siz(1), DimValue(VarDimIndex{ivar}))&& isequal(siz(2),1);%matlab column vector 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 168 end 169 end 170 if strcmp(action,'one_input') 171 netcdf.close(nc) 148 if ~(exist('action','var') && strcmp(action,'keep_open')) 149 for ivar=1:length(ListVarName) 150 if ~isnan(varid(ivar)) 151 VarVal=Data.(ListVarName{ivar}); 152 if strcmp(VarClass{ivar},'single') 153 VarVal=single(VarVal); 154 end 155 %varval=values of the current variable 156 VarDimName=Data.VarDimName{ivar}; 157 if ischar(VarDimName) 158 VarDimName={VarDimName}; 159 end 160 siz=size(VarVal); 161 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. 162 testline=isequal(length(siz),2) && isequal(siz(1),1)&& isequal(siz(2), DimValue(VarDimIndex{ivar}));%matlab vector 163 %testcolumn=isequal(length(siz),2) && isequal(siz(1), DimValue(VarDimIndex{ivar}))&& isequal(siz(2),1);%matlab column vector 164 if testline  testrange 165 if testrange 166 VarVal=linspace(VarVal(1),VarVal(2),DimValue(VarDimIndex{ivar}));% restitute the whole array of coordinate values from the first and last values 167 end 168 netcdf.putVar(nc,varid(ivar), VarVal'); 169 else 170 netcdf.putVar(nc,varid(ivar), VarVal); 171 end 172 end 173 end 174 end 175 netcdf.close(nc) 172 176 [success,errormsg] = fileattrib(flname ,'+w');% allow writing access for the group of users 173 end174 175 177 %'check_field_structure': check the validity of the field struture representation consistant with the netcdf format 176 178 %
Note: See TracChangeset
for help on using the changeset viewer.