source: trunk/src/find_field_indices.m @ 444

Last change on this file since 444 was 411, checked in by sommeria, 12 years ago

bugs corrected in uvmat: fixed x/y and calc_field for the new PIV data

File size: 9.0 KB
RevLine 
[8]1%'find_file_indices': test field structure for input in proj_field and plot_field
[89]2%    group the variables  into 'fields' with common dimensions
3%------------------------------------------------------------------------
[105]4% function  [CellVarIndex,NbDim,VarType,errormsg]=find_field_indices(Data)
[8]5%
[89]6% OUTPUT:
[8]7% CellVaxIndex: cell whose elements are arrays of indices in the list data.ListVarName 
8%              CellvarIndex{i} represents a set of variables with the same dimensions
9% NbDim: array with the length of CellVarIndex, giving its  space dimension
10% VarType: cell array of structures with fields
11%      .coord_x, y, z: indices (in .ListVarname) of variables representing  unstructured coordinates x, y, z
12%      .vector_x,_y,_z: indices of variables giving the vector components x, y, z
13%      .warnflag: index of warnflag
14%      .errorflag: index of error flag
15%      .ancillary: indices of ancillary variables
16%      .image   : B/W image, (behaves like scalar)
17%      .color : color image, the last index, which is not a coordinate variable, represent the 3 color components rgb
18%      .discrete: like scalar, but set of data points without continuity, represented as dots in a usual plot, instead of continuous lines otherwise
19%      .scalar: scalar field (default)
[89]20%      .coord: vector of indices of coordinate variables corresponding to matrix dimensions
[105]21% errormsg: error message
[8]22%   
[89]23% INPUT:
[8]24% Data: structure representing fields, output of check_field_structure
[404]25%            .ListVarName: cell array listing the names (cahr strings) of the variables
26%            .VarDimName: cell array of cells containing the set of dimension names for each variable of .ListVarName
27%            .VarAttribute: cell array of structures containing the variable attributes:
28%                     .VarAttribute{ilist}.key=value, where ilist is the variable
29%                     index, key is the name of the attribute, value its value (char string or number)
[8]30%
31% HELP:
32% to get the dimensions of arrays common to the field #icell
33%         VarIndex=CellVarIndex{icell}; % list of variable indices
34%         DimIndex=Data.VarDimIndex{VarIndex(1)} % list of dimensions for each variable in the cell #icell
35%
36%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
37%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
38%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
39%     This file is part of the toolbox UVMAT.
40%
41%     UVMAT is free software; you can redistribute it and/or modify
42%     it under the terms of the GNU General Public License as published by
43%     the Free Software Foundation; either version 2 of the License, or
44%     (at your option) any later version.
45%
46%     UVMAT is distributed in the hope that it will be useful,
47%     but WITHOUT ANY WARRANTY; without even the implied warranty of
48%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
49%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
50%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
51
52function [CellVarIndex,NbDim,VarType,errormsg]=find_field_indices(Data)
53CellVarIndex={};
[411]54
[8]55NbDim=[];
56VarType=[];
57errormsg=[];
58nbvar=numel(Data.ListVarName);%number of field variables
59icell=0;
60ivardim=0;
61VarDimIndex=[];
62VarDimName={};
63if ~isfield(Data,'VarDimName')
64    errormsg='missing .VarDimName';
65    return
66end
67
[399]68%% loop on the list of variables, group them by common dimensions
[8]69for ivar=1:nbvar
70    DimCell=Data.VarDimName{ivar}; %dimensions associated with the variable #ivar
71    if ischar(DimCell)
72        DimCell={DimCell};
73        Data.VarDimName{ivar}={Data.VarDimName{ivar}};%transform char chain into cell
74    end
75    testnewcell=1;
76    for icell_prev=1:numel(CellVarIndex)%detect whether the dimensions of ivar fit with an existing cell
[85]77        PrevVarIndex=CellVarIndex{icell_prev};%list of variable indices in cell # icell_prev
78        PrevDimName=Data.VarDimName{PrevVarIndex(1)};%list of corresponding variable names
[8]79        if isequal(PrevDimName,DimCell)
80            CellVarIndex{icell_prev}=[CellVarIndex{icell_prev} ivar];% add variable index #ivar to the cell #icell_prev
81            testnewcell=0; %existing cell detected
82            break
83        end
84    end
85    if testnewcell
86        icell=icell+1;
87        CellVarIndex{icell}=ivar;%put the current variabl index in the new cell
88    end
[399]89   
[8]90    %look for dimension variables
91    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')
97            ivardim=ivardim+1;
98            VarDimIndex(ivardim)=ivar;%index of the variable
99            VarDimName{ivardim}=DimCell{1};%name of the dimension
100        end
101    end
102end
103
[399]104%% find the spatial dimensions and vector components
[8]105ListRole={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','warnflag','errorflag',...
[397]106    'ancillary','image','color','discrete','scalar','coord_tps'};
[399]107NbDim=zeros(size(CellVarIndex));%default
[411]108if isfield(Data,'VarAttribute');
109    VarAttribute=Data.VarAttribute;
110else
111    VarAttribute={};
112end
[8]113for 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')
129                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
[89]155    test_coord=0;
156    if numel(VarIndex)>1     
[85]157        if ~isempty(ivar_coord_z)
158            NbDim(icell)=3;
[89]159            test_coord=1;
[85]160        elseif ~isempty(ivar_coord_y)
161            NbDim(icell)=2;
[89]162            test_coord=1;
[85]163        elseif ~isempty(ivar_coord_x)
164            NbDim(icell)=1;
[89]165            test_coord=1;
[85]166        end
167    end
[8]168    % look at coordinates variables 
169    coord=zeros(1,numel(DimCell));%default
[89]170%     if NbDim(icell)==0 && ~isempty(VarDimName)% no unstructured coordinate found
171    if  ~test_coord && ~isempty(VarDimName)
[8]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)
[411]184        NbDim(icell)=2;     
[8]185    end
[399]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 
[8]203end
Note: See TracBrowser for help on using the repository browser.