source: trunk/src/find_field_indices.m @ 85

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

uvmat;fig: cleaning out of buttons for mask record
view_field, write_plot_param: improve plot by view_field (projection)
uvmat: bug fixing for yellow coloring of buttons, improve switch from velocity to images
find_field_indices: bug fix for NbDim?
name_generator: help comments improved
civ: bugs corrected for BATCH button colouring

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