source: trunk/src/find_field_indices.m @ 233

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

various bug repairs

File size: 8.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
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
[85]75        PrevVarIndex=CellVarIndex{icell_prev};%list of variable indices in cell # icell_prev
76        PrevDimName=Data.VarDimName{PrevVarIndex(1)};%list of corresponding variable names
[8]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
[89]156    %NbDim(icell)=0;% nbre of space dimensions
[187]157%     NbDim(icell)=numel(DimCell);
158    NbDim(icell)=0;
[89]159    test_coord=0;
160    if numel(VarIndex)>1     
[85]161        if ~isempty(ivar_coord_z)
162            NbDim(icell)=3;
[89]163            test_coord=1;
[85]164        elseif ~isempty(ivar_coord_y)
165            NbDim(icell)=2;
[89]166            test_coord=1;
[85]167        elseif ~isempty(ivar_coord_x)
168            NbDim(icell)=1;
[89]169            test_coord=1;
[85]170        end
171    end
[8]172    % look at coordinates variables 
173    coord=zeros(1,numel(DimCell));%default
[89]174%     if NbDim(icell)==0 && ~isempty(VarDimName)% no unstructured coordinate found
175    if  ~test_coord && ~isempty(VarDimName)
[8]176        for idim=1:numel(DimCell)   %loop on the dimensions of the variables in cell #icell
177            for ivardim=1:numel(VarDimName)
178                if strcmp(VarDimName{ivardim},DimCell{idim})
179                    coord(idim)=VarDimIndex(ivardim);
180                    break
181                end
182            end
183        end
184        NbDim(icell)=numel(find(coord)); 
185    end 
186    VarType{icell}.coord=coord;
187    if NbDim(icell)==0 && test_2D %look at attributes Coord_1, coord_2 (obsolete convention)
188        NbDim(icell)=2;
189    end
190end
Note: See TracBrowser for help on using the repository browser.