source: trunk/src/find_field_indices.m @ 146

Last change on this file since 146 was 105, checked in by sommeria, 14 years ago

Bug fix

File size: 8.0 KB
Line 
1%'find_file_indices': test field structure for input in proj_field and plot_field
2%    group the variables  into 'fields' with common dimensions
3%------------------------------------------------------------------------
4% function  [CellVarIndex,NbDim,VarType,errormsg]=find_field_indices(Data)
5%
6% OUTPUT:
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)
20%      .coord: vector of indices of coordinate variables corresponding to matrix dimensions
21% errormsg: error message
22%   
23% INPUT:
24% Data: structure representing fields, output of check_field_structure
25%            .ListDimName: cell listing the names of the array dimensions
26%            .ListVarName: cell listing the names of the variables
27%            .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName
28%
29% HELP:
30% to get the dimensions of arrays common to the field #icell
31%         VarIndex=CellVarIndex{icell}; % list of variable indices
32%         DimIndex=Data.VarDimIndex{VarIndex(1)} % list of dimensions for each variable in the cell #icell
33%
34%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
35%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
36%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
37%     This file is part of the toolbox UVMAT.
38%
39%     UVMAT is free software; you can redistribute it and/or modify
40%     it under the terms of the GNU General Public License as published by
41%     the Free Software Foundation; either version 2 of the License, or
42%     (at your option) any later version.
43%
44%     UVMAT is distributed in the hope that it will be useful,
45%     but WITHOUT ANY WARRANTY; without even the implied warranty of
46%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
47%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
48%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
49
50function [CellVarIndex,NbDim,VarType,errormsg]=find_field_indices(Data)
51CellVarIndex={};
52NbDim=[];
53VarType=[];
54errormsg=[];
55nbvar=numel(Data.ListVarName);%number of field variables
56icell=0;
57ivardim=0;
58VarDimIndex=[];
59VarDimName={};
60
61if ~isfield(Data,'VarDimName')
62    errormsg='missing .VarDimName';
63    return
64end
65
66%loop on the list of variables
67for ivar=1:nbvar
68    DimCell=Data.VarDimName{ivar}; %dimensions associated with the variable #ivar
69    if ischar(DimCell)
70        DimCell={DimCell};
71        Data.VarDimName{ivar}={Data.VarDimName{ivar}};%transform char chain into cell
72    end
73    testnewcell=1;
74    for icell_prev=1:numel(CellVarIndex)%detect whether the dimensions of ivar fit with an existing cell
75        PrevVarIndex=CellVarIndex{icell_prev};%list of variable indices in cell # icell_prev
76        PrevDimName=Data.VarDimName{PrevVarIndex(1)};%list of corresponding variable names
77        if isequal(PrevDimName,DimCell)
78            CellVarIndex{icell_prev}=[CellVarIndex{icell_prev} ivar];% add variable index #ivar to the cell #icell_prev
79            testnewcell=0; %existing cell detected
80            break
81        end
82    end
83    if testnewcell
84        icell=icell+1;
85        CellVarIndex{icell}=ivar;%put the current variabl index in the new cell
86    end
87   
88    %look for dimension variables
89    if numel(DimCell)==1% if the variable has a single dimension
90        Role='';
91        if isfield(Data,'VarAttribute') && length(Data.VarAttribute)>=ivar && isfield(Data.VarAttribute{ivar},'Role')
92            Role=Data.VarAttribute{ivar}.Role;
93        end
94        if strcmp(DimCell{1},Data.ListVarName{ivar}) || strcmp(Role,'dimvar')
95            ivardim=ivardim+1;
96            VarDimIndex(ivardim)=ivar;%index of the variable
97            VarDimName{ivardim}=DimCell{1};%name of the dimension
98        end
99    end
100end
101
102% find the spatial dimensions and vector components
103ListRole={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','warnflag','errorflag',...
104    'ancillary','image','color','discrete','scalar'};
105for icell=1:length(CellVarIndex)
106    for ilist=1:numel(ListRole)
107        eval(['ivar_' ListRole{ilist} '=[];'])
108    end
109    VarIndex=CellVarIndex{icell};%set of variable indices with the same dim
110    DimCell=Data.VarDimName{VarIndex(1)};% list of dimensions for each variable in the cell #icell
111    if isfield(Data,'VarAttribute');
112        VarAttribute=Data.VarAttribute;
113    else
114        VarAttribute={};
115    end
116    test_2D=0;
117    for ivar=VarIndex
118        if length(VarAttribute)>=ivar
119            if isfield(VarAttribute{ivar},'Role')
120                role=VarAttribute{ivar}.Role;
121                switch role
122                    case ListRole
123                        eval(['ivar_' role '=[ivar_' role ' ivar];'])
124                    otherwise
125                       ivar_scalar=[ivar_scalar ivar];%variables are consiered as 'scalar' by default (in the absence of attribute 'Role')
126                end
127            else
128              ivar_scalar=[ivar_scalar ivar];% variable considered as scalar in the absence of Role attribute 
129            end
130            if isfield(VarAttribute{ivar},'Coord_2')
131                test_2D=1; %obsolete convention
132            end
133        else
134            ivar_scalar=[ivar_scalar ivar];%variables are consiered as 'scalar' by default (in the absence of attribute 'Role')
135        end
136    end
137    for ilist=1:numel(ListRole)
138        eval(['VarType{icell}.' ListRole{ilist} '=ivar_' ListRole{ilist} ';'])
139    end   
140    if numel(ivar_coord_x)>1 || numel(ivar_coord_y)>1 || numel(ivar_coord_z)>1
141        errormsg='multiply defined coordinates  in the same cell';
142        return
143    end
144    if numel(ivar_vector_x)>1 || numel(ivar_vector_y)>1 || numel(ivar_vector_z)>1
145        errormsg='multiply defined vector components in the same cell';
146        return
147    end 
148    if numel(ivar_errorflag)>1
149        errormsg='multiply defined error flag in the same cell';
150        return
151    end
152    if numel(ivar_warnflag)>1
153        errormsg='multiply defined warning flag in the same cell';
154        return
155    end
156    %NbDim(icell)=0;% nbre of space dimensions
157    NbDim(icell)=numel(DimCell);
158    test_coord=0;
159    if numel(VarIndex)>1     
160        if ~isempty(ivar_coord_z)
161            NbDim(icell)=3;
162            test_coord=1;
163        elseif ~isempty(ivar_coord_y)
164            NbDim(icell)=2;
165            test_coord=1;
166        elseif ~isempty(ivar_coord_x)
167            NbDim(icell)=1;
168            test_coord=1;
169        end
170    end
171    % look at coordinates variables 
172    coord=zeros(1,numel(DimCell));%default
173%     if NbDim(icell)==0 && ~isempty(VarDimName)% no unstructured coordinate found
174    if  ~test_coord && ~isempty(VarDimName)
175        for idim=1:numel(DimCell)   %loop on the dimensions of the variables in cell #icell
176            for ivardim=1:numel(VarDimName)
177                if strcmp(VarDimName{ivardim},DimCell{idim})
178                    coord(idim)=VarDimIndex(ivardim);
179                    break
180                end
181            end
182        end
183        NbDim(icell)=numel(find(coord)); 
184    end 
185    VarType{icell}.coord=coord;
186    if NbDim(icell)==0 && test_2D %look at attributes Coord_1, coord_2 (obsolete convention)
187        NbDim(icell)=2;
188    end
189end
Note: See TracBrowser for help on using the repository browser.