Changeset 535 for trunk/src/find_field_cells.m
- Timestamp:
- Sep 3, 2012, 2:02:13 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/find_field_cells.m
r530 r535 55 55 56 56 function [CellInfo,NbDim,errormsg]=find_field_cells(Data) 57 % CellVarIndex={}; 58 % CellInfo={} 57 59 58 NbDim=0; 60 % CellVarType=[]; 61 errormsg=[]; 59 errormsg=''; 62 60 if ~isfield(Data,'ListVarName'), errormsg='the list of variables .ListVarName is missing';return;end 63 61 if ~isfield(Data,'VarDimName'), errormsg='the list of dimensions .VarDimName is missing';return;end 64 62 nbvar=numel(Data.ListVarName);%number of field variables 65 63 if ~isequal(numel(Data.VarDimName),nbvar), errormsg='.ListVarName and .VarDimName have unequal length';return;end 66 if isfield(Data,'ListDimName')&& isfield(Data,'DimValue')&&isequal(numel(Data.ListDimName),numel(Data.DimValue)) 67 check_dim=1;% dimensions of data defined, data not needed for this function 68 else 69 check_dim=0; 70 for ilist=1:numel(Data.ListVarName) 71 if ~isfield(Data,Data.ListVarName{ilist}) 72 errormsg=['missing variable ' Data.ListVarName{ilist}]; 73 return 74 end 75 end 76 end 77 icell=0; 78 79 % NbDim=[]; 80 % VarDimIndex=[]; 81 % VarDimName={}; 64 % check the existence of variable data 65 check_var=1; 66 for ilist=1:numel(Data.ListVarName) 67 if ~isfield(Data,Data.ListVarName{ilist}) 68 check_var=0;% dimensions of data defined, data not needed for this function 69 break 70 end 71 end 72 if ~check_var && ~(isfield(Data,'ListDimName')&& isfield(Data,'DimValue')&&isequal(numel(Data.ListDimName),numel(Data.DimValue))) 73 errormsg=['missing variable or values of dimensions' Data.ListVarName{ilist}]; 74 return 75 end 76 82 77 83 78 %% role of variables and list of requested operations … … 127 122 CellInfo{icell}.CoordIndex=ivar_coord_x(icell); 128 123 % size of coordinate var 129 if check_dim 124 if check_var 125 CellInfo{icell}.CoordSize=numel(Data.(Data.ListVarName{ivar_coord_x(icell)})); 126 else 130 127 for idim=1:numel(DimCell) 131 128 check_index= strcmp(DimCell{idim},Data.ListDimName); … … 133 130 end 134 131 CellInfo{icell}.CoordSize=prod(CellInfo{icell}.CoordSize); 135 else136 CellInfo{icell}.CoordSize=numel(Data.(Data.ListVarName{ivar_coord_x(icell)}));137 132 end 138 133 ind_y=find(strcmp('coord_y',Role(VarIndex))); … … 157 152 158 153 %% look for tps coordinates 159 ivar_remain=find(~check_select); 154 ivar_remain=find(~check_select);% indices of remaining variables (not already selected) 160 155 check_coord_tps= strcmp('coord_tps',Role(~check_select)); 161 ivar_tps=ivar_remain(check_coord_tps); 156 ivar_tps=ivar_remain(check_coord_tps);% variable indices corresponding to tps coordinates 162 157 for icell_tps=1:numel(ivar_tps) 163 DimCell=Data.VarDimName{ivar_tps(icell_tps)}; 164 icell=numel(CellInfo)+icell_tps; 165 CellInfo{icell}.CoordIndex=ivar_tps(icell );158 DimCell=Data.VarDimName{ivar_tps(icell_tps)};% dimension names for the current tps coordinate variable 159 icell=numel(CellInfo)+icell_tps; % new field cell index 160 CellInfo{icell}.CoordIndex=ivar_tps(icell_tps);% index of the tps coordinate variable 166 161 CellInfo{icell}.VarIndex_subrange_tps=[]; 167 162 CellInfo{icell}.VarIndex_nbsites_tps=[]; … … 182 177 CellInfo{icell}.CoordType='tps'; 183 178 CellInfo{icell}.VarIndex=find(check_cell); 184 if check_dim 179 if check_var 180 NbDim(icell)=size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),2); 181 CellInfo{icell}.CoordSize=size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),1)*size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),3); 182 else 185 183 check_index_1= strcmp(DimCell{1},Data.ListDimName); 186 184 check_index_2= strcmp(DimCell{2},Data.ListDimName); … … 188 186 NbDim(icell)=Data.DimValue(check_index_2); 189 187 CellInfo{icell}.CoordSize=Data.DimValue(check_index_1)*Data.DimValue(check_index_3); 190 else191 NbDim(icell)=size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),2);192 CellInfo{icell}.CoordSize=size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),1)*size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),3);193 188 end 194 189 check_select=check_select|check_cell; 195 190 end 196 191 197 %% look for dimension variables and corresponding gridded data 198 ivar_remain=find(~check_select); 192 %% look for coordinate variables and corresponding gridded data: 193 % coordinate variables are variables associated with a single dimension, defining the coordinate values 194 % two cases: 1)the coordiante variable represents the set of coordiante values 195 % 2)the coordinate variable contains only two elements, representing the coordinate bounds for the dimension with the same name as the cordinate 196 ivar_remain=find(~check_select);% indices of remaining variables, not already taken into account 197 ListVarName=Data.ListVarName(~check_select);%list of remaining variables 199 198 VarDimName=Data.VarDimName(~check_select);%dimensions of remaining variables 200 check_coord= cellfun(@numel,VarDimName)==1|cellfun(@ischar,VarDimName)==1;% find variables with a single dimension 201 ListCoordIndex=ivar_remain(check_coord); 202 ListCoordName=Data.ListVarName(ListCoordIndex); 203 ListDimName=Data.VarDimName(ListCoordIndex); 204 %remove redondant values 199 check_coord= cellfun(@numel,VarDimName)==1|cellfun(@ischar,VarDimName)==1;% find remaining variables with a single dimension 200 ListCoordIndex=ivar_remain(check_coord);% indices of remaining variables with a single dimension 201 ListCoordName=Data.ListVarName(ListCoordIndex);% corresponding names of remaining variables with a single dimension 202 ListDimName=Data.VarDimName(ListCoordIndex);% dimension names of remaining variables with a single dimension 203 204 %remove redondant variables -> keep only one variable per dimension 205 205 check_keep=logical(ones(size(ListDimName))); 206 206 for idim=1:numel(ListDimName) 207 prev_ind=strcmp(ListDimName{idim},ListDimName(1:idim-1)); 207 prev_ind=strcmp(ListDimName{idim},ListDimName(1:idim-1));% check whether the dimension is already taken into account 208 208 if ~isempty(prev_ind) 209 if strcmp(ListCoordName{idim},ListDimName{idim}) % coordinate variable209 if strcmp(ListCoordName{idim},ListDimName{idim}) %variable with the same name as the coordinate taken in priority 210 210 check_keep(prev_ind)=0; 211 211 else … … 214 214 end 215 215 end 216 ListCoordIndex=ListCoordIndex(check_keep); 217 ListCoordName=ListCoordName(check_keep); 218 ListDimName=ListDimName(check_keep); 219 220 CoordSize=[]; 216 ListCoordIndex=ListCoordIndex(check_keep);% list of coordinate variable indices 217 ListCoordName=ListCoordName(check_keep);% list of coordinate variable names 218 ListDimName=ListDimName(check_keep);% list of coordinate dimension names 219 220 % determine dimension sizes 221 CoordSize=zeros(size(ListCoordIndex)); 221 222 for ilist=1:numel(ListCoordIndex) 222 223 if iscell(ListDimName{ilist}) 223 224 ListDimName(ilist)=ListDimName{ilist};%transform cell to string 224 225 end 225 if check_dim% if the list of dimensions is directly defined 226 check_index= strcmp(ListDimName{ilist},Data.ListDimName); 227 DimValue=Data.DimValue(check_index); 226 if check_var% if the list of dimensions has been directly defined, no variable data available 227 CoordSize(ilist)=numel(Data.(ListCoordName{ilist}));% number of elements in the variable corresponding to the dimension #ilist 228 228 else 229 DimValue=numel(Data.(ListCoordName{ilist})); 230 end 231 if DimValue==2% case of uniform grid coordinate defined by lower and upper bounds only 232 ListDimName{ilist}=ListCoordName{ilist};% look for dimensions with name equal to coordinate for 233 if check_dim 234 check_index= strcmp(ListCoordName{ilist},Data.ListDimName); 235 CoordSize(ilist)=Data.DimValue(check_index); 236 else 237 CoordSize(ilist)=numel(Data.(ListCoordName{ilist})); 238 end 239 else 240 CoordSize(ilist)=DimValue; 241 end 242 end 229 check_index= strcmp(ListDimName{ilist},Data.ListDimName);% find the index in the list of dimensions 230 CoordSize(ilist)=Data.DimValue(check_index);% find the corresponding dimension value 231 end 232 if CoordSize(ilist)==2% case of uniform grid coordinate defined by lower and upper bounds only 233 ListDimName{ilist}=ListCoordName{ilist};% replace the dimension name by the coordinate variable name 234 % if check_var 235 % check_index= strcmp(ListCoordName{ilist},Data.ListDimName); 236 % CoordSize(ilist)=Data.DimValue(check_index); 237 % else 238 % CoordSize(ilist)=numel(Data.(ListCoordName{ilist})); 239 % % end 240 % else 241 % CoordSize(ilist)=DimValue; 242 end 243 end 244 245 % group the remaining variables in cells sharing the same coordinate variables 243 246 NewCellInfo={}; 244 247 NewCellDimIndex={}; 245 248 NewNbDim=[]; 246 for ivardim=1:numel(VarDimName) % loop onthe list of remaining variables247 DimCell=VarDimName{ivardim};% dimension names of the current variable 249 for ivardim=1:numel(VarDimName) % loop at the list of remaining variables 250 DimCell=VarDimName{ivardim};% dimension names of the current variable 248 251 if ischar(DimCell), DimCell={DimCell}; end %transform char to cell if needed 249 252 DimIndices=[]; 250 253 for idim=1:numel(DimCell) 251 ind_dim=find(strcmp(DimCell{idim},ListDimName));%find the dim index in the list of coord dimensions254 ind_dim=find(strcmp(DimCell{idim},ListDimName));%find the dim index in the list of coordinate variables 252 255 if ~isempty(ind_dim) 253 256 DimIndices=[DimIndices ind_dim]; %update the list of coord dimensions included in DimCell 254 end 255 end 257 if check_var && CoordSize(ind_dim)==2 % determine the size of the coordinate in case of coordiante variable limited to lower and upper bounds 258 if isvector(Data.(ListVarName{ivardim})) || iscolumn(Data.(ListVarName{ivardim})) 259 if numel(Data.(ListVarName{ivardim}))>2 260 CoordSize(ind_dim)=numel(Data.(ListVarName{ivardim})); 261 end 262 else 263 CoordSize(ind_dim)=size(Data.(ListVarName{ivardim}),idim); 264 end 265 end 266 end 267 end 268 % look for cells of variables with the same coordinate variables 256 269 check_previous=0; 257 270 for iprev=1:numel(NewCellInfo) … … 262 275 end 263 276 end 277 % create a new cell if no previous one contains the coordinate variables 264 278 if ~check_previous 265 279 nbcell=numel(NewCellInfo)+1;
Note: See TracChangeset
for help on using the changeset viewer.