source: trunk/src/private/find_field_indices.m @ 8

Last change on this file since 8 was 8, checked in by gostiaux, 11 years ago
File size: 7.5 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 [DimVarIndex,CellVarIndex,NbDim,VarType]=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
21%   
22%INPUT:
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
27%
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
32%
33%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
34%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
35%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
36%     This file is part of the toolbox UVMAT.
37%
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.
42%
43%     UVMAT is distributed in the hope that it will be useful,
44%     but WITHOUT ANY WARRANTY; without even the implied warranty of
45%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
47%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
48
49function [CellVarIndex,NbDim,VarType,errormsg]=find_field_indices(Data)
50CellVarIndex={};
51NbDim=[];
52VarType=[];
53errormsg=[];
54nbvar=numel(Data.ListVarName);%number of field variables
55icell=0;
56ivardim=0;
57VarDimIndex=[];
58VarDimName={};
59
60if ~isfield(Data,'VarDimName')
61    errormsg='missing .VarDimName';
62    return
63end
64
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};
75        PrevDimName=Data.VarDimName{PrevVarIndex(1)};
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
86   
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
99end
100
101% find the spatial dimensions and vector components
102ListRole={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','warnflag','errorflag',...
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 ~isempty(ivar_coord_z)
157        NbDim(icell)=3;
158    elseif ~isempty(ivar_coord_y)
159        NbDim(icell)=2;
160    elseif ~isempty(ivar_coord_x)
161        NbDim(icell)=1;
162    end
163           
164    % look at coordinates variables 
165    coord=zeros(1,numel(DimCell));%default
166    if NbDim(icell)==0 && ~isempty(VarDimName)% no unstructured coordinate found
167        for idim=1:numel(DimCell)   %loop on the dimensions of the variables in cell #icell
168            for ivardim=1:numel(VarDimName)
169                if strcmp(VarDimName{ivardim},DimCell{idim})
170                    coord(idim)=VarDimIndex(ivardim);
171                    break
172                end
173            end
174        end
175        NbDim(icell)=numel(find(coord)); 
176    end 
177    VarType{icell}.coord=coord;
178    if NbDim(icell)==0 && test_2D %look at attributes Coord_1, coord_2 (obsolete convention)
179        NbDim(icell)=2;
180    end
181end
Note: See TracBrowser for help on using the repository browser.