Changeset 491 for trunk/src/find_field_indices.m
- Timestamp:
- Jul 11, 2012, 2:56:40 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/find_field_indices.m
r477 r491 2 2 % group the variables into 'fields' with common dimensions 3 3 %------------------------------------------------------------------------ 4 % function [CellVarIndex,NbDim, VarType,errormsg]=find_field_indices(Data)4 % function [CellVarIndex,NbDim,CellVarType,errormsg]=find_field_indices(Data) 5 5 % 6 6 % OUTPUT: … … 8 8 % CellvarIndex{i} represents a set of variables with the same dimensions 9 9 % NbDim: array with the length of CellVarIndex, giving its space dimension 10 % VarType: cell array of structures with fields10 % CellVarType: cell array of structures with fields 11 11 % .coord_x, y, z: indices (in .ListVarname) of variables representing unstructured coordinates x, y, z 12 12 % .vector_x,_y,_z: indices of variables giving the vector components x, y, z … … 47 47 % but WITHOUT ANY WARRANTY; without even the implied warranty of 48 48 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 49 % GNU General Public License (file UVMAT/COPYING.txt) for more details.for input in proj_field and plot_field 50 % group the variables into 'fields' with common dimensions 51 %------------------------------------------------------------------------ 52 % function [CellVarIndex,NbDim,CellVarType,errormsg]=find_field_indices(Data) 53 % 54 % OUTPUT: 55 % CellVaxIndex: cell whose elements are arrays of indices in the list data.ListVarName 56 % CellvarIndex{i} represents a set of variables with the same dimensions 57 % NbDim: array with the length of CellVarIndex, giving its space dimension 58 % CellVarType: cell array of structures with fields 59 % .coord_x, y, z: indices (in .ListVarname) of variables representing unstructured coordinates x, y, z 60 % .vector_x,_y,_z: indices of variables giving the vector components x, y, z 61 % .warnflag: index of warnflag 62 % .errorflag: index of error flag 63 % .ancillary: indices of ancillary variables 64 % .image : B/W image, (behaves like scalar) 65 % .color : color image, the last index, which is not a coordinate variable, represent the 3 color components rgb 66 % .discrete: like scalar, but set of data points without continuity, represented as dots in a usual plot, instead of continuous lines otherwise 67 % .scalar: scalar field (default) 68 % .coord: vector of indices of coordinate variables corresponding to matrix dimensions 69 % errormsg: error message 70 % 71 % INPUT: 72 % Data: structure representing fields, output of check_field_structure 73 % .ListVarName: cell array listing the names (cahr strings) of the variables 74 % .VarDimName: cell array of cells containing the set of dimension names for each variable of .ListVarName 75 % .VarAttribute: cell array of structures containing the variable attributes: 76 % .VarAttribute{ilist}.key=value, where ilist is the variable 77 % index, key is the name of the attribute, value its value (char string or number) 78 % 79 % HELP: 80 % to get the dimensions of arrays common to the field #icell 81 % VarIndex=CellVarIndex{icell}; % list of variable indices 82 % DimIndex=Data.VarDimIndex{VarIndex(1)} % list of dimensions for each variable in the cell #icell 83 % 84 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 85 % Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org. 86 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 87 % This file is part of the toolbox UVMAT. 88 % 89 % UVMAT is free software; you can redistribute it and/or modify 90 % it under the terms of the GNU General Public License as published by 91 % the Free Software Foundation; either version 2 of the License, or 92 % (at your option) any later version. 93 % 94 % UVMAT is distributed in the hope that it will be useful, 95 % but WITHOUT ANY WARRANTY; without even the implied warranty of 96 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 49 97 % GNU General Public License (file UVMAT/COPYING.txt) for more details. 50 98 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 51 99 52 function [CellVarIndex,NbDim, VarType,errormsg]=find_field_indices(Data)100 function [CellVarIndex,NbDim,CellVarType,errormsg]=find_field_indices(Data) 53 101 CellVarIndex={}; 54 102 55 103 NbDim=[]; 56 VarType=[];104 CellVarType=[]; 57 105 errormsg=[]; 58 106 nbvar=numel(Data.ListVarName);%number of field variables 59 107 icell=0; 108 109 NbDim=[]; 60 110 ivardim=0; 61 111 VarDimIndex=[]; … … 64 114 errormsg='missing .VarDimName'; 65 115 return 116 end 117 118 %% role of variables 119 Role=mat2cell(blanks(nbvar),1,ones(1,nbvar));%initialize a cell array of nbvar blanks 120 Role=regexprep(Role,' ','scalar'); % Role set to 'scalar' by default 121 if isfield(Data,'VarAttribute') 122 for ivar=1:numel(Data.VarAttribute) 123 if isfield(Data.VarAttribute{ivar},'Role') 124 Role{ivar}=Data.VarAttribute{ivar}.Role; 125 end 126 end 66 127 end 67 128 … … 90 151 %look for dimension variables 91 152 if numel(DimCell)==1% if the variable has a single dimension 92 Role=''; 93 if isfield(Data,'VarAttribute') && length(Data.VarAttribute)>=ivar && isfield(Data.VarAttribute{ivar},'Role') 94 Role=Data.VarAttribute{ivar}.Role; 95 end 96 if strcmp(DimCell{1},Data.ListVarName{ivar}) || strcmp(Role,'dimvar') 153 if strcmp(DimCell{1},Data.ListVarName{ivar}) || strcmp(Role{ivar},'dimvar') 97 154 ivardim=ivardim+1; 98 155 VarDimIndex(ivardim)=ivar;%index of the variable … … 103 160 104 161 %% find the spatial dimensions and vector components 105 ListRole={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z',' warnflag','errorflag',...106 'ancillary','image','color','discrete','scalar','coord_tps'}; 162 ListRole={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','vector_x_tps','vector_y_tps','warnflag','errorflag',... 163 'ancillary','image','color','discrete','scalar','coord_tps'};% rmq vector_x_tps and vector_y_tps to be replaced by vector_x and vector_y 107 164 NbDim=zeros(size(CellVarIndex));%default 108 if isfield(Data,'VarAttribute'); 109 VarAttribute=Data.VarAttribute; 110 else 111 VarAttribute={}; 112 end 165 166 for ilist=1:numel(ListRole) 167 VarType.(ListRole{ilist})=find(strcmp(ListRole{ilist},Role)); 168 end 169 %look for tps coordinates 170 if ~isempty(VarType.coord_tps) 171 VarType.subrange_tps=[]; 172 VarType.nbsites_tps=[]; 173 for ifield=1:numel(VarType.coord_tps) 174 select(ifield)=0; 175 DimCell=Data.VarDimName{VarType.coord_tps(ifield)}; 176 if numel(DimCell)==3 177 for ivardim=1:numel(Data.VarDimName) 178 if strcmp(Data.VarDimName{ivardim},DimCell{3}) 179 VarType.nbsites_tps=[VarType.nbsites_tps ivardim]; 180 select(ifield)=select(ifield)+1; 181 elseif strcmp(Data.VarDimName{ivardim}{1},DimCell{2}) && strcmp(Data.VarDimName{ivardim}{3},DimCell{3}) 182 VarType.subrange_tps=[VarType.subrange_tps ivardim]; 183 select(ifield)=select(ifield)+1; 184 end 185 end 186 end 187 end 188 VarType.coord_tps=VarType.coord_tps(select==2); 189 VarType.subrange_tps=VarType.subrange_tps(select==2); 190 VarType.nbsites_tps=VarType.nbsites_tps(select==2); 191 end 192 193 index_remove=[]; 194 CellVarType=cell(1,length(CellVarIndex)); 113 195 for icell=1:length(CellVarIndex) 114 for ilist=1:numel(ListRole) 115 eval(['ivar_' ListRole{ilist} '=[];']) 116 end 117 VarIndex=CellVarIndex{icell};%set of variable indices with the same dim 118 DimCell=Data.VarDimName{VarIndex(1)};% list of dimensions for each variable in the cell #icell 119 test_2D=0; 120 for ivar=VarIndex 121 if length(VarAttribute)>=ivar 122 if isfield(VarAttribute{ivar},'Role') 123 role=VarAttribute{ivar}.Role; 124 switch role 125 case ListRole 126 eval(['ivar_' role '=[ivar_' role ' ivar];']) 127 otherwise 128 ivar_scalar=[ivar_scalar ivar];%variables are consiered as 'scalar' by default (in the absence of attribute 'Role') 196 VarIndex=CellVarIndex{icell};%set of variable indices with the same dim 197 check_remove=0; 198 for ifield=1:numel(VarType.coord_tps) 199 if isequal(VarIndex,VarType.coord_tps(ifield))||isequal(VarIndex,VarType.subrange_tps(ifield))||isequal(VarIndex,VarType.nbsites_tps(ifield)) 200 index_remove=[index_remove icell];% removes Coord_tps as field cell 201 check_remove=1; 202 end 203 end 204 205 if ~check_remove 206 for ilist=1:numel(ListRole) 207 CellVarType{icell}.(ListRole{ilist})=VarIndex(find(strcmp(ListRole{ilist},Role(VarIndex)))); 208 end 209 DimCell=Data.VarDimName{VarIndex(1)};% list of dimensions for each variable in the cell #icell 210 if numel(CellVarType{icell}.coord_x)>1 || numel(CellVarType{icell}.coord_y)>1 || numel(CellVarType{icell}.coord_z)>1 211 errormsg='multiply defined coordinates in the same cell'; 212 return 213 end 214 if numel(CellVarType{icell}.errorflag)>1 215 errormsg='multiply defined error flag in the same cell'; 216 return 217 end 218 if numel(CellVarType{icell}.warnflag)>1 219 errormsg='multiply defined warning flag in the same cell'; 220 return 221 end 222 test_coord=0; 223 % look for unstructured coordinates 224 if numel(VarIndex)>1 225 if ~isempty(CellVarType{icell}.coord_z) 226 NbDim(icell)=3; 227 test_coord=1; 228 elseif ~isempty(CellVarType{icell}.coord_y) 229 NbDim(icell)=2; 230 test_coord=1; 231 elseif ~isempty(CellVarType{icell}.coord_x) 232 NbDim(icell)=1; 233 test_coord=1; 234 end 235 end 236 % look for coordinates variables 237 coord=zeros(1,numel(DimCell));%default 238 % if NbDim(icell)==0 && ~isempty(VarDimName)% no unstructured coordinate found 239 if ~test_coord && ~isempty(VarDimName) 240 for idim=1:numel(DimCell) %loop on the dimensions of the variables in cell #icell 241 for ivardim=1:numel(VarDimName) 242 if strcmp(VarDimName{ivardim},DimCell{idim}) 243 coord(idim)=VarDimIndex(ivardim); 244 break 245 end 129 246 end 130 else 131 ivar_scalar=[ivar_scalar ivar];% variable considered as scalar in the absence of Role attribute 132 end 133 if isfield(VarAttribute{ivar},'Coord_2') 134 test_2D=1; %obsolete convention 135 end 136 else 137 ivar_scalar=[ivar_scalar ivar];%variables are consiered as 'scalar' by default (in the absence of attribute 'Role') 138 end 139 end 140 for ilist=1:numel(ListRole) 141 eval(['VarType{icell}.' ListRole{ilist} '=ivar_' ListRole{ilist} ';']) 142 end 143 if numel(ivar_coord_x)>1 || numel(ivar_coord_y)>1 || numel(ivar_coord_z)>1 144 errormsg='multiply defined coordinates in the same cell'; 145 return 146 end 147 if numel(ivar_errorflag)>1 148 errormsg='multiply defined error flag in the same cell'; 149 return 150 end 151 if numel(ivar_warnflag)>1 152 errormsg='multiply defined warning flag in the same cell'; 153 return 154 end 155 test_coord=0; 156 if numel(VarIndex)>1 157 if ~isempty(ivar_coord_z) 158 NbDim(icell)=3; 159 test_coord=1; 160 elseif ~isempty(ivar_coord_y) 161 NbDim(icell)=2; 162 test_coord=1; 163 elseif ~isempty(ivar_coord_x) 164 NbDim(icell)=1; 165 test_coord=1; 166 end 167 end 168 % look at coordinates variables 169 coord=zeros(1,numel(DimCell));%default 170 % if NbDim(icell)==0 && ~isempty(VarDimName)% no unstructured coordinate found 171 if ~test_coord && ~isempty(VarDimName) 172 for idim=1:numel(DimCell) %loop on the dimensions of the variables in cell #icell 173 for ivardim=1:numel(VarDimName) 174 if strcmp(VarDimName{ivardim},DimCell{idim}) 175 coord(idim)=VarDimIndex(ivardim); 176 break 177 end 178 end 179 end 180 NbDim(icell)=numel(find(coord)); 181 end 182 VarType{icell}.coord=coord; 183 if NbDim(icell)==0 && test_2D %look at attributes Coord_1, coord_2 (obsolete convention) 184 NbDim(icell)=2; 185 end 186 %look for tps data 187 if ~isempty(VarType{icell}.coord_tps) 188 VarType{icell}.var_tps=[]; 189 tps_dimnames=Data.VarDimName{VarType{icell}.coord_tps}; 190 if length(tps_dimnames)==3 191 for ilist=1:length(Data.VarDimName) 192 if strcmp(tps_dimnames{1},Data.VarDimName{ilist}{1}) && strcmp(tps_dimnames{3},Data.VarDimName{ilist}{2})% identify the variables corresponding to the tps site coordinates coord_tps 193 VarType{icell}.var_tps=[VarType{icell}.var_tps ilist]; 194 elseif length(Data.VarDimName{ilist})==1 && strcmp(tps_dimnames{3},Data.VarDimName{ilist}{1})% identify the variable corresponding to nbsites 195 VarType{icell}.nbsites_tps= ilist; 196 elseif length(Data.VarDimName{ilist})==3 && strcmp(tps_dimnames{2},Data.VarDimName{ilist}{1})&& strcmp(tps_dimnames{3},Data.VarDimName{ilist}{3})% identify the variable subrange 197 VarType{icell}.subrange_tps= ilist; 198 end 199 end 200 end 201 NbDim(icell)=2; 202 end 203 end 247 end 248 NbDim(icell)=numel(find(coord)); 249 end 250 CellVarType{icell}.coord=coord; 251 %look for tps data 252 if ~isempty(VarType.coord_tps) 253 for ilist=1:numel(VarType.coord_tps) 254 tps_dimnames=Data.VarDimName{VarType.coord_tps(ilist)}; 255 if length(tps_dimnames)==3 && strcmp(tps_dimnames{1},DimCell{1}) && strcmp(tps_dimnames{3},DimCell{2}) 256 CellVarIndex{icell}=[CellVarIndex{icell} VarType.coord_tps(ilist) VarType.nbsites_tps(ilist) VarType.subrange_tps(ilist)]; 257 CellVarType{icell}.coord_tps=VarType.coord_tps(ilist); 258 CellVarType{icell}.nbsites_tps=VarType.nbsites_tps(ilist); 259 CellVarType{icell}.subrange_tps=VarType.subrange_tps(ilist); 260 NbDim(icell)=size(Data.(Data.ListVarName{VarType.coord_tps(ilist)}),2); 261 end 262 end 263 end 264 end 265 end 266 if ~isempty(index_remove) 267 CellVarIndex(index_remove)=[]; 268 CellVarType(index_remove)=[]; 269 NbDim(index_remove)=[]; 270 end
Note: See TracChangeset
for help on using the changeset viewer.