Changeset 128 for trunk/src/struct2nc.m
 Timestamp:
 Nov 21, 2010, 10:06:11 PM (13 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/src/struct2nc.m
r107 r128 42 42 return 43 43 end 44 hhh=which('netcdf.create');% look for builtin matlab library44 hhh=which('netcdf.create');% look for builtin matlab netcdf library 45 45 46 %USE OF builtin netcdf library46 %USE OF builtin matlab netcdf library 47 47 if ~isequal(hhh,'') 48 48 FilePath=fileparts(flname); … … 68 68 if ~testvar 69 69 eval(['cte=Data.' keys{iattr} ';']) 70 if (ischar(cte) isnumeric(cte)) && ~isempty(cte)&& ~isequal(cte,'') 70 if (ischar(cte) isnumeric(cte)) && ~isempty(cte)%&& ~isequal(cte,'') 71 %write constant only if it is numeric or char string, and not empty 71 72 netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),keys{iattr},cte) 72 else73 errormsg='global attributes must be characters or numbers';74 return75 73 end 76 74 end … … 79 77 end 80 78 %create dimensions 81 dimid= [];79 dimid=zeros(1,length(Data.ListDimName)); 82 80 for idim=1:length(Data.ListDimName) 83 dimid(idim) = netcdf.defDim(nc,Data.ListDimName{idim},Data.DimValue(idim)); 81 dimid(idim) = netcdf.defDim(nc,Data.ListDimName{idim},Data.DimValue(idim)); 84 82 end 85 83 VarAttribute={}; %default … … 89 87 testattr=1; 90 88 end 91 varid= [];89 varid=zeros(1,length(Data.ListVarName)); 92 90 for ivar=1:length(ListVarName) 93 varid(ivar)=netcdf.defVar(nc,ListVarName{ivar},' double',dimid(Data.VarDimIndex{ivar}));%define variable91 varid(ivar)=netcdf.defVar(nc,ListVarName{ivar},'nc_double',dimid(Data.VarDimIndex{ivar}));%define variable 94 92 end 95 93 %write variable attributes … … 117 115 VarDimName={VarDimName}; 118 116 end 119 testrange=(numel(VarDimName)==1 && strcmp(VarDimName{1},ListVarName{ivar}) && numel(VarVal)==2); 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); 117 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. 118 testline=isequal(length(siz),2) && isequal(siz(1),1)&& isequal(siz(2), Data.DimValue(VarDimIndex));%matlab vector 119 testcolumn=isequal(length(siz),2) && isequal(siz(1), Data.DimValue(VarDimIndex))&& isequal(siz(2),1);%matlab column vector 122 120 if ~testrange && ~testline && ~testcolumn && ~isequal(siz,Data.DimValue(VarDimIndex)) 123 121 errormsg=['wrong dimensions declared for ' ListVarName{ivar} ' in struct2nc.m']; … … 126 124 if testline  testrange 127 125 if testrange 128 VarVal=linspace(VarVal(1),VarVal(2),Data.DimValue(VarDimIndex)); 126 VarVal=linspace(VarVal(1),VarVal(2),Data.DimValue(VarDimIndex));% restitute the whole array of coordinate values 129 127 end 130 %nc{ListVarName{ivar}}=ncfloat(Data.ListDimName(VarDimIndex));%vector of x coordinates 131 netcdf.putVar(nc,varid(ivar), double(VarVal')); 128 netcdf.putVar(nc,varid(ivar), double(VarVal')); 132 129 else 133 130 netcdf.putVar(nc,varid(ivar), double(VarVal)); 134 %nc{ListVarName{ivar}}(:) = VarVal; 135 end 136 131 end 137 132 end 138 133 end 134 netcdf.close(nc) 139 135 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 140 136 %OLD netcdf toolbox
Note: See TracChangeset
for help on using the changeset viewer.