Changeset 404 for trunk/src/struct2nc.m
- Timestamp:
- Apr 30, 2012, 6:46:45 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/struct2nc.m
r382 r404 42 42 return 43 43 end 44 hhh=which('netcdf.create');% look for built-in matlab netcdf library 45 46 %USE OF built-in matlab netcdf library 47 if ~isequal(hhh,'') 48 FilePath=fileparts(flname); 49 if ~strcmp(FilePath,'') && ~exist(FilePath,'dir') 50 errormsg=['directory ' FilePath ' needs to be created']; 51 return 52 end 53 [Data,errormsg]=check_field_structure(Data);%check the validity of the input field structure 54 if ~isempty(errormsg) 55 errormsg=['error in struct2nc:invalid input structure_' errormsg]; 56 return 57 end 58 ListVarName=Data.ListVarName; 59 nc=netcdf.create(flname,'NC_CLOBBER');%,'clobber'); %create the netcdf file with name flname 60 %write global constants 61 if isfield(Data,'ListGlobalAttribute') 62 keys=Data.ListGlobalAttribute; 63 for iattr=1:length(keys) 64 if isfield(Data,keys{iattr}) 65 testvar=0; 66 for ivar=1:length(ListVarName)% eliminate possible global attributes with the same name as a variable 67 if isequal(ListVarName{ivar}, keys{iattr}) 68 testvar=1; 69 break 70 end 44 FilePath=fileparts(flname); 45 if ~strcmp(FilePath,'') && ~exist(FilePath,'dir') 46 errormsg=['directory ' FilePath ' needs to be created']; 47 return 48 end 49 % [Data,errormsg]=check_field_structure(Data);%check the validity of the input field structure 50 [errormsg,ListDimName,DimValue,VarDimIndex]=check_field_structure(Data); 51 if ~isempty(errormsg) 52 errormsg=['error in struct2nc:invalid input structure_' errormsg]; 53 return 54 end 55 ListVarName=Data.ListVarName; 56 nc=netcdf.create(flname,'NC_CLOBBER');%,'clobber'); %create the netcdf file with name flname 57 %write global constants 58 if isfield(Data,'ListGlobalAttribute') 59 keys=Data.ListGlobalAttribute; 60 for iattr=1:length(keys) 61 if isfield(Data,keys{iattr}) 62 testvar=0; 63 for ivar=1:length(ListVarName)% eliminate possible global attributes with the same name as a variable 64 if isequal(ListVarName{ivar}, keys{iattr}) 65 testvar=1; 66 break 71 67 end 72 if ~testvar73 eval(['cte=Data.' keys{iattr} ';'])74 if (ischar(cte) ||isnumeric(cte)) && ~isempty(cte)%&& ~isequal(cte,'')75 %write constant only if it is numeric or char string, and not empty76 netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),keys{iattr},cte)77 end68 end 69 if ~testvar 70 eval(['cte=Data.' keys{iattr} ';']) 71 if (ischar(cte) ||isnumeric(cte)) && ~isempty(cte)%&& ~isequal(cte,'') 72 %write constant only if it is numeric or char string, and not empty 73 netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),keys{iattr},cte) 78 74 end 79 75 end 80 76 end 81 77 end 82 %create dimensions 83 dimid=zeros(1,length(Data.ListDimName)); 84 for idim=1:length(Data.ListDimName) 85 dimid(idim) = netcdf.defDim(nc,Data.ListDimName{idim},Data.DimValue(idim)); 86 end87 VarAttribute={}; %default 88 testattr=0; 89 if isfield(Data,'VarAttribute') 90 VarAttribute=Data.VarAttribute; 91 testattr=1;92 end93 varid=zeros(1,length(Data.ListVarName)); 94 for ivar=1:length(ListVarName) 95 varid(ivar)=netcdf.defVar(nc,ListVarName{ivar},'nc_double',dimid(Data.VarDimIndex{ivar}));%define variable 96 end97 %write variable attributes 98 if testattr99 for ivar=1:min(numel(VarAttribute),numel(ListVarName)) 100 if isstruct(VarAttribute{ivar})101 attr_names=fields(VarAttribute{ivar});102 for iattr=1:length(attr_names)103 eval(['attr_val=VarAttribute{ivar}.' attr_names{iattr} ';']);104 if ~isempty(attr_names{iattr})&& ~isempty(attr_val)105 netcdf.putAtt(nc,varid(ivar),attr_names{iattr},attr_val);106 end78 end 79 %create dimensions 80 dimid=zeros(1,length(ListDimName)); 81 for idim=1:length(ListDimName) 82 dimid(idim) = netcdf.defDim(nc,ListDimName{idim},DimValue(idim)); 83 end 84 VarAttribute={}; %default 85 testattr=0; 86 if isfield(Data,'VarAttribute') 87 VarAttribute=Data.VarAttribute; 88 testattr=1; 89 end 90 varid=zeros(1,length(Data.ListVarName)); 91 for ivar=1:length(ListVarName) 92 varid(ivar)=netcdf.defVar(nc,ListVarName{ivar},'nc_double',dimid(VarDimIndex{ivar}));%define variable 93 end 94 %write variable attributes 95 if testattr 96 for ivar=1:min(numel(VarAttribute),numel(ListVarName)) 97 if isstruct(VarAttribute{ivar}) 98 attr_names=fields(VarAttribute{ivar}); 99 for iattr=1:length(attr_names) 100 eval(['attr_val=VarAttribute{ivar}.' attr_names{iattr} ';']); 101 if ~isempty(attr_names{iattr})&& ~isempty(attr_val) 102 netcdf.putAtt(nc,varid(ivar),attr_names{iattr},attr_val); 107 103 end 108 104 end 109 105 end 110 106 end 111 netcdf.endDef(nc); %put in data mode 112 for ivar=1:length(ListVarName) 113 if isfield(Data,ListVarName{ivar}) 114 VarVal=Data.(ListVarName{ivar}); 115 %varval=values of the current variable 116 VarDimIndex=Data.VarDimIndex{ivar}; %indices of the variable dimensions in the list of dimensions 117 VarDimName=Data.VarDimName{ivar}; 118 if ischar(VarDimName) 119 VarDimName={VarDimName}; 120 end 121 siz=size(VarVal); 122 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. 123 testline=isequal(length(siz),2) && isequal(siz(1),1)&& isequal(siz(2), Data.DimValue(VarDimIndex));%matlab vector 124 testcolumn=isequal(length(siz),2) && isequal(siz(1), Data.DimValue(VarDimIndex))&& isequal(siz(2),1);%matlab column vector 125 % if ~testrange && ~testline && ~testcolumn && ~isequal(siz,Data.DimValue(VarDimIndex)) 107 end 108 netcdf.endDef(nc); %put in data mode 109 for ivar=1:length(ListVarName) 110 if isfield(Data,ListVarName{ivar}) 111 VarVal=Data.(ListVarName{ivar}); 112 %varval=values of the current variable 113 % VarDimIndex=Data.VarDimIndex{ivar}; %indices of the variable dimensions in the list of dimensions 114 VarDimName=Data.VarDimName{ivar}; 115 if ischar(VarDimName) 116 VarDimName={VarDimName}; 117 end 118 siz=size(VarVal); 119 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. 120 testline=isequal(length(siz),2) && isequal(siz(1),1)&& isequal(siz(2), DimValue(VarDimIndex{ivar}));%matlab vector 121 testcolumn=isequal(length(siz),2) && isequal(siz(1), DimValue(VarDimIndex{ivar}))&& isequal(siz(2),1);%matlab column vector 122 % if ~testrange && ~testline && ~testcolumn && ~isequal(siz,DimValue(VarDimIndex)) 126 123 % errormsg=['wrong dimensions declared for ' ListVarName{ivar} ' in struct2nc.m']; 127 124 % break 128 125 % end 129 if testline || testrange 130 if testrange 131 VarVal=linspace(VarVal(1),VarVal(2),Data.DimValue(VarDimIndex));% restitute the whole array of coordinate values 132 end 133 netcdf.putVar(nc,varid(ivar), double(VarVal')); 134 else 135 netcdf.putVar(nc,varid(ivar), double(VarVal)); 136 end 137 end 126 if testline || testrange 127 if testrange 128 VarVal=linspace(VarVal(1),VarVal(2),DimValue(VarDimIndex{ivar}));% restitute the whole array of coordinate values 129 end 130 netcdf.putVar(nc,varid(ivar), double(VarVal')); 131 else 132 netcdf.putVar(nc,varid(ivar), double(VarVal)); 133 end 138 134 end 139 netcdf.close(nc)140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%141 %OLD netcdf toolbox142 else143 errormsg=struct2nc_toolbox(flname,Data);144 135 end 136 netcdf.close(nc) 145 137 138
Note: See TracChangeset
for help on using the changeset viewer.