Changeset 140 for trunk/src/check_field_structure.m
- Timestamp:
- Nov 29, 2010, 9:55:54 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/check_field_structure.m
r89 r140 32 32 nbfield=numel(Data.ListVarName); 33 33 else 34 errormsg='input field does not contain the list of variables .ListVarNames';34 errormsg='input field does not contain the cell array of variable names .ListVarNames'; 35 35 return 36 36 end 37 37 %check dimension names 38 % definition by VarDimIndex (obsolete) 39 % if ~isfield(Data,'VarDimName') && isfield(Data,'VarDimIndex') && isfield(Data,'ListDimName')%old convention 40 % for ivar=1:nbfield 41 % DimCell=Data.VarDimIndex{ivar}; 42 % if isnumeric(DimCell) 43 % if DimCell <= numel(Data.ListDimName) 44 % Data.VarDimName{ivar}=Data.ListDimName(DimCell); 45 % else 46 % errormsg='dimension names not defined'; 47 % return 48 % end 49 % else 50 % errormsg='unrecognized format for .VarDimIndex'; 51 % end 52 % end 53 % end 54 if ~(isfield(Data,'VarDimName') && iscell(Data.VarDimName)) 55 errormsg='input field does not contain the list of dimensions .VarDimName'; 38 if (isfield(Data,'VarDimName') && iscell(Data.VarDimName)) 39 if numel(Data.VarDimName)~=nbfield 40 errormsg=' .ListVarName and .VarDimName have different lengths'; 41 return 42 end 43 else 44 errormsg='input field does not contain the cell array of dimension names .VarDimName'; 56 45 return 57 46 end … … 59 48 Data=rmfield(Data,'DimValue'); 60 49 end 61 if isfield(Data,'VarDimName') && iscell(Data.VarDimName) 62 nbdim=0; 63 if numel(Data.VarDimName)==nbfield 64 for ivar=1:nbfield 65 VarName=Data.ListVarName{ivar}; 66 if ~isfield(Data,VarName) 67 errormsg=['the listed variable ' VarName ' is not found']; 50 nbdim=0; 51 Data.ListDimName={}; 52 for ivar=1:nbfield 53 VarName=Data.ListVarName{ivar}; 54 if ~isfield(Data,VarName) 55 errormsg=['the listed variable ' VarName ' is not found']; 56 return 57 end 58 eval(['sizvar=size(Data.' VarName ');'])% sizvar = dimension of variable 59 DimCell=Data.VarDimName{ivar}; 60 if ischar(DimCell) 61 DimCell={DimCell};%case of a single dimension name, defined by a string 62 elseif ~iscell(DimCell) 63 errormsg=['wrong format for .VarDimName{' num2str(ivar) ' (must be the cell of dimension names of the variable ' VarName]; 64 return 65 end 66 nbcoord=numel(sizvar);%nbre of coordiantes for variable named VarName 67 if numel(DimCell)==0 68 errormsg=['empty declared dimension .VarDimName{' num2str(ivar) '} for ' VarName]; 69 return 70 elseif numel(DimCell)==1% one dimension declared 71 if nbcoord==2 72 if sizvar(1)==1 73 nbcoord=1; 74 sizvar(1)=sizvar(2); 75 elseif sizvar(2)==1 76 nbcoord=1; 77 else 78 errormsg=['1 dimension declared in .VarDimName{' num2str(ivar) '} inconsistent with the nbre of dimensions =2 of the variable ' VarName]; 68 79 return 69 else 70 eval(['sizvar=size(Data.' VarName ');'])% sizvar = dimension of variable 71 DimCell=Data.VarDimName{ivar}; 72 if ischar(DimCell) 73 DimCell={DimCell}; 74 elseif ~iscell(DimCell) 75 errormsg=['wrong format for .VarDimName{' num2str(ivar) ' (must be the cell of dimension names of the variable ' VarName]; 80 end 81 else 82 errormsg=['1 dimension declared in .VarDimName{' num2str(ivar) '} inconsistent with the nbre of dimensions =' num2str(nbcoord) ' of the variable ' VarName]; 83 return 84 end 85 else 86 if numel(DimCell)>nbcoord 87 DimCell=DimCell(end-nbcoord+1:end);%first singleton diemnsions omitted, 88 elseif nbcoord > numel(DimCell) 89 errormsg=['nbre of declared dimensions in .VarDimName{' num2str(ivar) '} smaller than the nbre of dimensions =' num2str(nbcoord) ' of the variable ' VarName]; 90 return 91 end 92 end 93 DimIndex=[]; 94 for idim=1:nbcoord %loop on the coordinates of variable #ivar 95 DimName=DimCell{idim}; 96 iprev=find(strcmp(DimName,Data.ListDimName),1);%look for dimension name DimName in the current list 97 if isempty(iprev)% append the dimension name to the current list 98 nbdim=nbdim+1; 99 if sizvar(idim)==2 && strcmp(DimName,VarName)%case of a coordinate defined by the two end values (regular spacing) 100 RangeTest(nbdim)=1; %to be updated for a later variable 101 end 102 Data.DimValue(nbdim)=sizvar(idim); 103 Data.ListDimName{nbdim}=DimName; 104 DimIndex=[DimIndex nbdim]; 105 else % DimName is detected in the current list of dimension names 106 if ~isequal(Data.DimValue(iprev),sizvar(idim)) 107 if isequal(Data.DimValue(iprev),2)&& RangeTest(iprev) % the dimension has been already detected as a range [min max] 108 Data.DimValue(iprev)=sizvar(idim); %update with actual value 109 else 110 errormsg=['dimension declaration inconsistent with the size =[' num2str(sizvar) '] for ' VarName]; 76 111 return 77 112 end 78 nbcoord=numel(sizvar);79 if numel(DimCell)==080 errormsg=['empty declared dimension .VarDimName{' num2str(ivar) '} for ' VarName];81 return82 elseif numel(DimCell)==1% one dimension declared83 if nbcoord==284 if sizvar(1)==185 nbcoord=1;86 sizvar(1)=sizvar(2);87 elseif sizvar(2)==188 nbcoord=1;89 else90 errormsg=['1 dimension declared in .VarDimName{' num2str(ivar) '} inconsistent with the nbre of dimensions =2 of the variable ' VarName];91 return92 end93 else94 errormsg=['1 dimension declared in .VarDimName{' num2str(ivar) '} inconsistent with the nbre of dimensions =' num2str(nbcoord) ' of the variable ' VarName];95 return96 end97 else98 if numel(DimCell)>nbcoord99 DimCell=DimCell(end-nbcoord+1:end);%first singleton diemnsions omitted,100 elseif nbcoord > numel(DimCell)101 errormsg=['nbre of declared dimensions in .VarDimName{' num2str(ivar) '} smaller than the nbre of dimensions =' num2str(nbcoord) ' of the variable ' VarName];102 return103 end104 end105 DimIndex=[];106 for idim=1:nbcoord %loop on the coordinates of variable #ivar107 DimName=DimCell{idim};108 testprev=0;109 for iprev=1:nbdim %check previously listed dimension names110 if strcmp(Data.ListDimName{iprev},DimName)111 if ~isequal(Data.DimValue(iprev),sizvar(idim))112 if isequal(Data.DimValue(iprev),0) % the dimension has been already detected as a range [min max]113 Data.DimValue(idim)=sizvar(idim); %update with actual value114 elseif sizvar(idim)==2 && strcmp(DimName,VarName)115 %case of a regularly spaced coordinate defined by the first and last values: dimension will be determined later116 else117 errormsg=['dimension declaration inconsistent with the size =[' num2str(sizvar) '] for ' VarName];118 return119 end120 end121 DimIndex=[DimIndex iprev];122 testprev=1;123 break124 end125 end126 if ~testprev % a new dimension is appended to the list127 nbdim=nbdim+1;128 if sizvar(idim)==2 && strcmp(DimName,VarName)129 Data.DimValue(nbdim)=0; %to be updated for a later variable130 % Data.VarType{ivar}='range';131 else132 Data.DimValue(nbdim)=sizvar(idim);133 end134 Data.ListDimName{nbdim}=DimName;135 DimIndex=[DimIndex nbdim];136 end137 end138 Data.VarDimIndex{ivar}=DimIndex;139 113 end 140 end 141 else 142 errormsg=' .ListVarNames and .VarDimName have different lengths'; 143 return 114 DimIndex=[DimIndex iprev]; 115 end 144 116 end 145 else 146 errormsg='input field does not contain the cell of dimension names .VarDimName for variables'; 147 return 117 Data.VarDimIndex{ivar}=DimIndex; 148 118 end 149 119 DataOut=Data;
Note: See TracChangeset
for help on using the changeset viewer.