source: trunk/src/find_field_indices.m @ 399

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

implementation of thin plate interpolation (proj on planes with mode 'filter'), rationalisation of variable formats in civ_matlab

File size: 8.8 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={};
60if ~isfield(Data,'VarDimName')
61    errormsg='missing .VarDimName';
62    return
63end
64
65%% loop on the list of variables, group them by common dimensions
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
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','coord_tps'};
104NbDim=zeros(size(CellVarIndex));%default
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_errorflag)>1
145        errormsg='multiply defined error flag in the same cell';
146        return
147    end
148    if numel(ivar_warnflag)>1
149        errormsg='multiply defined warning flag in the same cell';
150        return
151    end
152    test_coord=0;
153    if numel(VarIndex)>1     
154        if ~isempty(ivar_coord_z)
155            NbDim(icell)=3;
156            test_coord=1;
157        elseif ~isempty(ivar_coord_y)
158            NbDim(icell)=2;
159            test_coord=1;
160        elseif ~isempty(ivar_coord_x)
161            NbDim(icell)=1;
162            test_coord=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    if  ~test_coord && ~isempty(VarDimName)
169        for idim=1:numel(DimCell)   %loop on the dimensions of the variables in cell #icell
170            for ivardim=1:numel(VarDimName)
171                if strcmp(VarDimName{ivardim},DimCell{idim})
172                    coord(idim)=VarDimIndex(ivardim);
173                    break
174                end
175            end
176        end
177        NbDim(icell)=numel(find(coord)); 
178    end 
179    VarType{icell}.coord=coord;
180    if NbDim(icell)==0 && test_2D %look at attributes Coord_1, coord_2 (obsolete convention)
181        NbDim(icell)=2;
182    end
183    %look for tps data
184    if ~isempty(VarType{icell}.coord_tps)
185        VarType{icell}.var_tps=[];
186        tps_dimnames=Data.VarDimName{VarType{icell}.coord_tps};
187        if length(tps_dimnames)==3
188            for ilist=1:length(Data.VarDimName)
189                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
190                    VarType{icell}.var_tps=[VarType{icell}.var_tps ilist];
191                elseif length(Data.VarDimName{ilist})==1 && strcmp(tps_dimnames{3},Data.VarDimName{ilist}{1})% identify the variable corresponding to nbsites
192                    VarType{icell}.nbsites_tps= ilist;
193                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
194                    VarType{icell}.subrange_tps= ilist;
195                end
196            end
197        end
198        NbDim(icell)=2;
199    end 
200end
Note: See TracBrowser for help on using the repository browser.