Changeset 919 for trunk/src/struct2nc.m
 Timestamp:
 Jun 26, 2015, 5:14:45 PM (8 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/src/struct2nc.m
r913 r919 49 49 return 50 50 end 51 %check the validity of the input field structure 51 52 %% check the validity of the input field structure 52 53 [errormsg,ListDimName,DimValue,VarDimIndex]=check_field_structure(Data); 53 54 if ~isempty(errormsg) … … 56 57 end 57 58 ListVarName=Data.ListVarName; 58 %nc=netcdf.create(flname,'NC_CLOBBER');%,'clobber'); %create the netcdf file with name flname 59 60 %% create the netcdf file with name flname in format NETCDF4 59 61 cmode = netcdf.getConstant('NETCDF4'); 60 62 cmode = bitor(cmode, netcdf.getConstant('CLASSIC_MODEL')); 61 63 cmode = bitor(cmode, netcdf.getConstant('CLOBBER')); 62 nc = netcdf.create(flname, cmode) 63 64 % write global constants64 nc = netcdf.create(flname, cmode); 65 66 %% write global constants 65 67 if isfield(Data,'ListGlobalAttribute') 66 68 keys=Data.ListGlobalAttribute; … … 84 86 end 85 87 end 86 %create dimensions 88 89 %% create the dimensions 87 90 dimid=zeros(1,length(ListDimName)); 88 91 for idim=1:length(ListDimName) … … 95 98 testattr=1; 96 99 end 97 varid=zeros(1,length(Data.ListVarName)); 100 101 102 %% create the variables 103 varid=nan(1,length(Data.ListVarName)); 98 104 for ivar=1:length(ListVarName) 99 varid(ivar)=netcdf.defVar(nc,ListVarName{ivar},'nc_double',dimid(VarDimIndex{ivar}));%define variable 100 end 101 %write variable attributes 105 if isfield(Data,ListVarName{ivar}) 106 VarClass=class(Data.(ListVarName{ivar})); 107 VarType=''; 108 switch VarClass 109 case {'single','double'} 110 VarType='nc_float'; % store all floating reals as single 111 case {'uint8','int16','uint16','int32','uint32','int64','uint64'} 112 VarType='nc_int'; 113 case 'logical' 114 VarType='nc_byte'; 115 end 116 if ~isempty(VarType) 117 varid(ivar)=netcdf.defVar(nc,ListVarName{ivar},VarType,dimid(VarDimIndex{ivar}));%define variable 118 end 119 end 120 end 121 122 %% write variable attributes 102 123 if testattr 103 124 for ivar=1:min(numel(VarAttribute),numel(ListVarName)) 104 if isstruct(VarAttribute{ivar}) 125 if isstruct(VarAttribute{ivar}) && ~isnan(varid(ivar)) 105 126 attr_names=fields(VarAttribute{ivar}); 106 127 for iattr=1:length(attr_names) … … 114 135 end 115 136 netcdf.endDef(nc); %put in data mode 137 138 %% fill the variables with input data 116 139 for ivar=1:length(ListVarName) 117 if isfield(Data,ListVarName{ivar})140 if ~isnan(varid(ivar)) 118 141 VarVal=Data.(ListVarName{ivar}); 119 142 %varval=values of the current variable 120 % VarDimIndex=Data.VarDimIndex{ivar}; %indices of the variable dimensions in the list of dimensions121 143 VarDimName=Data.VarDimName{ivar}; 122 144 if ischar(VarDimName) … … 126 148 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. 127 149 testline=isequal(length(siz),2) && isequal(siz(1),1)&& isequal(siz(2), DimValue(VarDimIndex{ivar}));%matlab vector 128 testcolumn=isequal(length(siz),2) && isequal(siz(1), DimValue(VarDimIndex{ivar}))&& isequal(siz(2),1);%matlab column vector 129 % if ~testrange && ~testline && ~testcolumn && ~isequal(siz,DimValue(VarDimIndex)) 130 % errormsg=['wrong dimensions declared for ' ListVarName{ivar} ' in struct2nc.m']; 131 % break 132 % end 150 %testcolumn=isequal(length(siz),2) && isequal(siz(1), DimValue(VarDimIndex{ivar}))&& isequal(siz(2),1);%matlab column vector 133 151 if testline  testrange 134 152 if testrange
Note: See TracChangeset
for help on using the changeset viewer.