source: trunk/src/find_field_cells.m @ 526

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

various bug repairs and cleaning

File size: 11.7 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,CellVarType,errormsg]=find_field_cells(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% CellVarType: 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%
22%      .FieldRequest= 'interp_lin', 'interp_tps' indicate whether lin interpolation  or derivatives (tps) is needed to calculate the requested field
23%      .FieldNames = cell of fields to calculate from the fied cell
24%
25% errormsg: error message
26%   
27% INPUT:
28% Data: structure representing fields, output of check_field_structure
29%            .ListVarName: cell array listing the names (cahr strings) of the variables
30%            .VarDimName: cell array of cells containing the set of dimension names for each variable of .ListVarName
31%            .VarAttribute: cell array of structures containing the variable attributes:
32%                     .VarAttribute{ilist}.key=value, where ilist is the variable
33%                     index, key is the name of the attribute, value its value (char string or number)
34%
35% HELP:
36% to get the dimensions of arrays common to the field #icell
37%         VarIndex=CellVarIndex{icell}; % list of variable indices
38%         DimIndex=Data.VarDimIndex{VarIndex(1)} % list of dimensions for each variable in the cell #icell
39%
40%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
41%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
42%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
43%     This file is part of the toolbox UVMAT.
44%
45%     UVMAT is free software; you can redistribute it and/or modify
46%     it under the terms of the GNU General Public License as published by
47%     the Free Software Foundation; either version 2 of the License, or
48%     (at your option) any later version.
49%
50%     UVMAT is distributed in the hope that it will be useful,
51%     but WITHOUT ANY WARRANTY; without even the implied warranty of
52%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
53%     GNU General Public License (file UVMAT/COPYING.txt) for more details.for input in proj_field and plot_field
54%    group the variables  into 'fields' with common dimensions
55
56function [CellVarIndex,NbDim,CellVarType,errormsg]=find_field_cells(Data)
57CellVarIndex={};
58
59CellVarType=[];
60errormsg=[];
61nbvar=numel(Data.ListVarName);%number of field variables
62icell=0;
63
64NbDim=[];
65VarDimIndex=[];
66VarDimName={};
67if ~isfield(Data,'VarDimName')
68    errormsg='missing .VarDimName';
69    return
70end
71
72%% role of variables and list of requested operations
73Role=num2cell(blanks(nbvar));%initialize a cell array of nbvar blanks
74FieldRequest=regexprep(Role,' ',''); % fieldRequest set to '' by default
75Operation=cell(size(Role)); % fieldRequest set to {} by default
76CheckSub=zeros(size(Role));% =1 for fields to substract
77Role=regexprep(Role,' ','scalar'); % Role set to 'scalar' by default
78if isfield(Data,'VarAttribute')
79    for ivar=1:numel(Data.VarAttribute)
80        if isfield(Data.VarAttribute{ivar},'Role')
81            Role{ivar}=Data.VarAttribute{ivar}.Role;
82        end
83        if isfield(Data.VarAttribute{ivar},'FieldRequest')
84            FieldRequest{ivar}=Data.VarAttribute{ivar}.FieldRequest;
85        end
86        if isfield(Data.VarAttribute{ivar},'Operation')
87            Operation{ivar}=Data.VarAttribute{ivar}.Operation;
88        end
89        if isfield(Data.VarAttribute{ivar},'CheckSub')
90            CheckSub(ivar)=Data.VarAttribute{ivar}.CheckSub;
91        end
92    end
93end
94
95%% loop on the list of variables, group them by common dimensions
96CellVarType=cell(1,length(CellVarIndex));
97for ivar=1:nbvar
98    if ischar(Data.VarDimName{ivar})
99        Data.VarDimName{ivar}=Data.VarDimName(ivar);%transform char chain into cell
100    end
101    DimCell=Data.VarDimName{ivar}; %dimensions associated with the variable #ivar
102    testnewcell=1;
103    for icell_prev=1:numel(CellVarIndex)%detect whether the dimensions of ivar fit with an existing cell
104        PrevVarIndex=CellVarIndex{icell_prev};%list of variable indices in cell # icell_prev
105        PrevDimCell=Data.VarDimName{PrevVarIndex(1)};%list of corresponding variable names
106        if isequal(PrevDimCell,DimCell)
107            CellVarIndex{icell_prev}=[CellVarIndex{icell_prev} ivar];% add variable index #ivar to the cell #icell_prev
108            testnewcell=0; %existing cell detected
109            break
110        end
111    end
112    if testnewcell
113        icell=icell+1;
114        CellVarIndex{icell}=ivar;%put the current variable index in the new cell
115        NbDim(icell)=numel(DimCell);%default   
116        CellVarType{icell}=[];
117    end
118    if ~isempty(FieldRequest{ivar})
119       CellVarType{icell}.FieldRequest=FieldRequest{ivar};
120    end
121    if ~isempty(Operation{ivar})
122       CellVarType{icell}.Operation=Operation{ivar};
123    end
124    if CheckSub(ivar)
125    CellVarType{icell}.CheckSub=1;
126    end
127end
128
129%% find dimension variables
130checksinglecell=cellfun(@numel,CellVarIndex)==1 & NbDim==1;% find isolated cells with a single dimension
131ind_dim_var_cell=find(checksinglecell);
132%CoordType(ind_dim_var_cell)='dim_var';% to be used in output
133%VarDimIndex=cell(size(ind_dim_var_cell));
134VarDimName=cell(size(ind_dim_var_cell));
135for icoord=1:numel(ind_dim_var_cell)
136    VarDimIndex(icoord)=CellVarIndex{ind_dim_var_cell(icoord)};
137    VarDimName{icoord}=Data.VarDimName{VarDimIndex(icoord)}{1};
138end
139
140%% find the spatial dimensions and vector components
141ListRole={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','vector_x_tps','vector_y_tps','warnflag','errorflag',...
142    'ancillary','image','color','discrete','scalar','coord_tps'};% rmq vector_x_tps and vector_y_tps to be replaced by vector_x and vector_y
143
144for ilist=1:numel(ListRole)
145    VarType.(ListRole{ilist})=find(strcmp(ListRole{ilist},Role));
146end
147%look for tps coordinates
148if ~isempty(VarType.coord_tps)
149    VarType.subrange_tps=[];
150    VarType.nbsites_tps=[];
151    select=zeros(1,numel(VarType.coord_tps));
152    for ifield=1:numel(VarType.coord_tps)
153        DimCell=Data.VarDimName{VarType.coord_tps(ifield)};
154        if numel(DimCell)==3
155            for ivardim=1:numel(Data.VarDimName)
156                if strcmp(Data.VarDimName{ivardim},DimCell{3})
157                    VarType.nbsites_tps=[VarType.nbsites_tps ivardim];
158                    select(ifield)=select(ifield)+1;
159                elseif strcmp(Data.VarDimName{ivardim}{1},DimCell{2}) && strcmp(Data.VarDimName{ivardim}{3},DimCell{3})
160                    VarType.subrange_tps=[VarType.subrange_tps ivardim];
161                    select(ifield)=select(ifield)+1;
162                end
163            end
164        end
165    end
166    VarType.coord_tps=VarType.coord_tps(select==2);
167    VarType.subrange_tps=VarType.subrange_tps(select==2);
168    VarType.nbsites_tps=VarType.nbsites_tps(select==2);
169end
170
171index_remove=[];
172for icell=1:length(CellVarIndex)
173    if checksinglecell(icell)
174        continue
175    end
176    VarIndex=CellVarIndex{icell};%set of variable indices with the same dim
177    check_remove=0;
178    for ifield=1:numel(VarType.coord_tps)
179        if isequal(VarIndex,VarType.coord_tps(ifield))||isequal(VarIndex,VarType.subrange_tps(ifield))||isequal(VarIndex,VarType.nbsites_tps(ifield))
180            index_remove=[index_remove icell];% removes Coord_tps as field cell
181            check_remove=1;
182        end
183    end
184   
185    if ~check_remove
186        for ilist=1:numel(ListRole)
187            CellVarType{icell}.(ListRole{ilist})=VarIndex(find(strcmp(ListRole{ilist},Role(VarIndex))));
188        end
189        DimCell=Data.VarDimName{VarIndex(1)};% list of dimensions for each variable in the cell #icell
190        if numel(CellVarType{icell}.coord_x)>1 || numel(CellVarType{icell}.coord_y)>1 || numel(CellVarType{icell}.coord_z)>1
191            errormsg='multiply defined coordinates  in the same cell';
192            return
193        end
194        % case of x cordinate marked as a dimension variable (var name=dimension name)
195        if isempty(CellVarType{icell}.coord_x)
196            var_dim_index=find(strcmp(DimCell{1},Data.ListVarName(VarIndex)));
197            if ~isempty(var_dim_index)
198                CellVarType{icell}.coord_x=VarIndex(var_dim_index);
199            end
200        end         
201        if numel(CellVarType{icell}.errorflag)>1
202            errormsg='multiply defined error flag in the same cell';
203            return
204        end
205        if numel(CellVarType{icell}.warnflag)>1
206            errormsg='multiply defined warning flag in the same cell';
207            return
208        end
209        test_coord=0;
210        % look for unstructured coordinates
211        if numel(VarIndex)>1
212            if ~isempty(CellVarType{icell}.coord_z)
213                NbDim(icell)=3;
214                test_coord=1;
215            elseif ~isempty(CellVarType{icell}.coord_y)
216                NbDim(icell)=2;
217                test_coord=1;
218            elseif ~isempty(CellVarType{icell}.coord_x)
219                NbDim(icell)=1;
220                test_coord=1;
221            elseif numel(DimCell)==1
222                NbDim(icell)=0;% set of data without coordinates
223            end
224        end
225        % look for coordinates variables
226        coord=zeros(1,numel(DimCell));%default
227        %     if NbDim(icell)==0 && ~isempty(VarDimName)% no unstructured coordinate found
228        if  ~test_coord && ~isempty(VarDimName)
229            for idim=1:numel(DimCell)   %loop on the dimensions of the variables in cell #icell
230                ind_coord=find(strcmp(DimCell{idim},VarDimName));
231                if ~isempty(ind_coord)
232                    coord(idim)=VarDimIndex(ind_coord);
233                end
234            end
235            NbDim(icell)=numel(find(coord));
236        end
237        CellVarType{icell}.coord=coord;
238        %look for tps data
239        if ~isempty(VarType.coord_tps)
240            for ilist=1:numel(VarType.coord_tps)
241            tps_dimnames=Data.VarDimName{VarType.coord_tps(ilist)};
242            if length(tps_dimnames)==3 && strcmp(tps_dimnames{1},DimCell{1}) && strcmp(tps_dimnames{3},DimCell{2})
243                CellVarIndex{icell}=[CellVarIndex{icell} VarType.coord_tps(ilist) VarType.nbsites_tps(ilist) VarType.subrange_tps(ilist)];
244                CellVarType{icell}.coord_tps=VarType.coord_tps(ilist);
245                CellVarType{icell}.nbsites_tps=VarType.nbsites_tps(ilist);
246                CellVarType{icell}.subrange_tps=VarType.subrange_tps(ilist);
247                if isfield(Data,'ListDimName')
248                    dim_index=find(strcmp(tps_dimnames{2},Data.ListDimName));
249                    NbDim(icell)=Data.DimValue(dim_index);
250                else
251                NbDim(icell)=size(Data.(Data.ListVarName{VarType.coord_tps(ilist)}),2);
252                end
253            end
254            end
255        end
256    end
257end
258if ~isempty(index_remove)
259    CellVarIndex(index_remove)=[];
260    CellVarType(index_remove)=[];
261    NbDim(index_remove)=[];
262end
Note: See TracBrowser for help on using the repository browser.