source: trunk/src/find_field_cells.m @ 533

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

new conventions for find_field_cells .

File size: 14.0 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  [CellInfo,NbDim,errormsg]=find_field_cells(Data)
5%
6% OUTPUT:
7% CellInfo: cell of structures describing field cells
8%     .CoordType:  type of coordinates for each field cell = 'scattered','grid','tps';
9%     .CoordIndex: array of the indices of the variables representing the coordinates (in the order z,y,x)
10%     .CoordSize: array of the nbre of values for each  coordinate in a grid, nbre of points in the unstructured case
11%     .NbSite_tps:
12%     .SubRange_tps
13%     .VarIndex: arrays of the variable indices in the field cell
14%     .VarIndex_ancillary: indices of ancillary variables
15%              _color : color image, the last index, which is not a coordinate variable, represent the 3 color components rgb
16%              _discrete: like scalar, but set of data points without continuity, represented as dots in a usual plot, instead of continuous lines otherwise
17%              _errorflag: index of error flag
18%              _image   : B/W image, (behaves like scalar)
19%              _vector_x,_y,_z: indices of variables giving the vector components x, y, z
20%              _warnflag: index of warnflag   
21%      .FieldRequest= 'interp_lin', 'interp_tps' indicate whether lin interpolation  or derivatives (tps) is needed to calculate the requested field
22%      .Operation = operation to be performed to finalise the field cell after projection
23%      .SubCheck=0 /1 indicate that the field must be substracted (second  entry in uvmat)
24% NbDim: array with the length of CellVarIndex, giving the space dimension of each field cell
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 [CellInfo,NbDim,errormsg]=find_field_cells(Data)
57% CellVarIndex={};
58% CellInfo={}
59NbDim=0;
60% CellVarType=[];
61errormsg=[];
62if ~isfield(Data,'ListVarName'), errormsg='the list of variables .ListVarName is missing';return;end
63if ~isfield(Data,'VarDimName'), errormsg='the list of dimensions .VarDimName is missing';return;end
64nbvar=numel(Data.ListVarName);%number of field variables
65if ~isequal(numel(Data.VarDimName),nbvar), errormsg='.ListVarName and .VarDimName have unequal length';return;end
66if isfield(Data,'ListDimName')&& isfield(Data,'DimValue')&&isequal(numel(Data.ListDimName),numel(Data.DimValue))
67    check_dim=1;% dimensions of data defined, data not needed for this function
68else
69    check_dim=0;
70    for ilist=1:numel(Data.ListVarName)
71        if ~isfield(Data,Data.ListVarName{ilist})
72            errormsg=['missing variable ' Data.ListVarName{ilist}];
73            return
74        end
75    end
76end
77icell=0;
78
79% NbDim=[];
80% VarDimIndex=[];
81% VarDimName={};
82
83%% role of variables and list of requested operations
84%ListRole={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','vector_x_tps','vector_y_tps','warnflag','errorflag',...
85%   'ancillary','image','color','discrete','scalar','coord_tps'};% rmq vector_x_tps and vector_y_tps to be replaced by vector_x and vector_y
86Role=num2cell(blanks(nbvar));%initialize a cell array of nbvar blanks
87FieldRequest=regexprep(Role,' ',''); % fieldRequest set to '' by default
88Operation=cell(size(Role)); % fieldRequest set to {} by default
89CheckSub=zeros(size(Role));% =1 for fields to substract
90Role=regexprep(Role,' ','scalar'); % Role set to 'scalar' by default
91if isfield(Data,'VarAttribute')
92    for ivar=1:numel(Data.VarAttribute)
93        if isfield(Data.VarAttribute{ivar},'Role')
94            Role{ivar}=Data.VarAttribute{ivar}.Role;
95        end
96        if isfield(Data.VarAttribute{ivar},'FieldRequest')
97            FieldRequest{ivar}=Data.VarAttribute{ivar}.FieldRequest;
98        end
99        if isfield(Data.VarAttribute{ivar},'Operation')
100            Operation{ivar}=Data.VarAttribute{ivar}.Operation;
101        end
102        if isfield(Data.VarAttribute{ivar},'CheckSub')
103            CheckSub(ivar)=Data.VarAttribute{ivar}.CheckSub;
104        end
105    end
106end
107
108%% find scattered (unstructured) coordinates
109ivar_coord_x=find(strcmp('coord_x',Role));
110% VarDimCell=cell(numel(ivar_coord_x));
111check_select=zeros(1,nbvar);
112CellInfo=cell(1,numel(ivar_coord_x));
113NbDim=zeros(1,numel(ivar_coord_x));
114for icell=1:numel(ivar_coord_x)
115    DimCell=Data.VarDimName{ivar_coord_x(icell)};
116    if ischar(DimCell),DimCell={DimCell};end
117    check_cell=zeros(numel(DimCell),nbvar);
118    for idim=1:numel(DimCell)
119        for ivar=1:nbvar
120            check_cell(idim,ivar)=max(strcmp(DimCell{idim},Data.VarDimName{ivar}));
121        end
122    end
123    check_cell=sum(check_cell,1)==numel(DimCell);%logical array=1 for variables belonging to the current cell
124    VarIndex=find(check_cell);
125    if ~(numel(VarIndex)==1 && numel(DimCell)==1)% exclude case of isolated coord_x variable (treated later)
126        if ~(numel(VarIndex)==1 && numel(DimCell)>1)% a variable is associated to coordinate
127            CellInfo{icell}.CoordIndex=ivar_coord_x(icell);
128            % size of coordinate var
129            if check_dim
130                for idim=1:numel(DimCell)
131                 check_index= strcmp(DimCell{idim},Data.ListDimName);
132                 CellInfo{icell}.CoordSize(idim)=Data.DimValue(check_index);
133                end
134                CellInfo{icell}.CoordSize=prod(CellInfo{icell}.CoordSize);
135            else
136                CellInfo{icell}.CoordSize=numel(Data.(Data.ListVarName{ivar_coord_x(icell)}));
137            end
138            ind_y=find(strcmp('coord_y',Role(VarIndex)));
139            if numel(VarIndex)==2||isempty(ind_y)% no variable, except possibly y
140                NbDim(icell)=1;
141            else
142                CellInfo{icell}.CoordType='scattered';
143                ind_z=find(strcmp('coord_z',Role(VarIndex)));
144                if numel(VarIndex)==3||isempty(ind_z)% no z variable, except possibly as a fct z(x,y)
145                    CellInfo{icell}.CoordIndex=[VarIndex(ind_y) CellInfo{icell}.CoordIndex];
146                    NbDim(icell)=2;
147                else
148                    CellInfo{icell}.CoordIndex=[VarIndex(ind_z) CellInfo{icell}.CoordIndex];
149                    NbDim(icell)=3;
150                end
151            end
152        end
153        CellInfo{icell}.VarIndex=VarIndex;
154        check_select=check_select|check_cell;
155    end
156end
157
158%% look for tps coordinates
159ivar_remain=find(~check_select);
160check_coord_tps= strcmp('coord_tps',Role(~check_select));
161ivar_tps=ivar_remain(check_coord_tps);
162for icell_tps=1:numel(ivar_tps)
163    DimCell=Data.VarDimName{ivar_tps(icell_tps)};
164    icell=numel(CellInfo)+icell_tps;
165    CellInfo{icell}.CoordIndex=ivar_tps(icell);
166    CellInfo{icell}.VarIndex_subrange_tps=[];
167    CellInfo{icell}.VarIndex_nbsites_tps=[];
168    if numel(DimCell)==3
169        VarDimName=Data.VarDimName(~check_select);
170        for ivardim=1:numel(VarDimName)
171            if strcmp(VarDimName{ivardim},DimCell{3})
172                CellInfo{icell}.NbSite_tps= ivar_remain(ivardim);
173                check_cell(ivar_remain(ivardim))=1;% nbre of sites for each tps subdomain
174            elseif strcmp(VarDimName{ivardim}{1},DimCell{2}) && strcmp(VarDimName{ivardim}{3},DimCell{3})
175                CellInfo{icell}.SubRange_tps=ivar_remain(ivardim);
176                check_cell(ivar_remain(ivardim))=1;% subrange definiton for tps
177            elseif strcmp(VarDimName{ivardim}{1},DimCell{1}) && strcmp(VarDimName{ivardim}{2},DimCell{3})
178                check_cell(ivar_remain(ivardim))=1;% variable
179            end
180        end
181    end
182    CellInfo{icell}.CoordType='tps';
183    CellInfo{icell}.VarIndex=find(check_cell);
184    if check_dim
185        check_index_1= strcmp(DimCell{1},Data.ListDimName);
186        check_index_2= strcmp(DimCell{2},Data.ListDimName);
187        check_index_3= strcmp(DimCell{3},Data.ListDimName);
188        NbDim(icell)=Data.DimValue(check_index_2);
189        CellInfo{icell}.CoordSize=Data.DimValue(check_index_1)*Data.DimValue(check_index_3);
190    else
191        NbDim(icell)=size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),2);
192        CellInfo{icell}.CoordSize=size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),1)*size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),3);
193    end
194    check_select=check_select|check_cell;
195end
196
197%% look for dimension variables and corresponding gridded data
198ivar_remain=find(~check_select);
199VarDimName=Data.VarDimName(~check_select);%dimensions of remaining variables
200check_coord= cellfun(@numel,VarDimName)==1|cellfun(@ischar,VarDimName)==1;% find variables with a single dimension
201ListCoordIndex=ivar_remain(check_coord);
202ListCoordName=Data.ListVarName(ListCoordIndex);
203ListDimName=Data.VarDimName(ListCoordIndex);
204%remove redondant values
205check_keep=logical(ones(size(ListDimName)));
206for idim=1:numel(ListDimName)
207    prev_ind=strcmp(ListDimName{idim},ListDimName(1:idim-1));
208    if ~isempty(prev_ind)
209        if strcmp(ListCoordName{idim},ListDimName{idim}) %coordinate variable
210            check_keep(prev_ind)=0;
211        else
212           check_keep(idim)=0;
213        end
214    end
215end
216ListCoordIndex=ListCoordIndex(check_keep);
217ListCoordName=ListCoordName(check_keep);
218ListDimName=ListDimName(check_keep);
219
220CoordSize=[];
221for ilist=1:numel(ListCoordIndex)
222    if iscell(ListDimName{ilist})
223        ListDimName(ilist)=ListDimName{ilist};%transform cell to string
224    end
225    if check_dim% if the list of dimensions is directly defined
226        check_index= strcmp(ListDimName{ilist},Data.ListDimName);
227        DimValue=Data.DimValue(check_index);
228    else
229        DimValue=numel(Data.(ListCoordName{ilist}));
230    end
231    if DimValue==2% case of uniform grid coordinate defined by lower and upper bounds only
232        ListDimName{ilist}=ListCoordName{ilist};% look for dimensions with name equal to coordinate for
233        if check_dim
234            check_index= strcmp(ListCoordName{ilist},Data.ListDimName);
235            CoordSize(ilist)=Data.DimValue(check_index);
236        else
237            CoordSize(ilist)=numel(Data.(ListCoordName{ilist}));
238        end
239    else
240        CoordSize(ilist)=DimValue;
241    end
242end
243NewCellInfo={};
244NewCellDimIndex={};
245NewNbDim=[];
246for ivardim=1:numel(VarDimName) % loop on the list of remaining variables
247    DimCell=VarDimName{ivardim};% dimension names of the current variable
248    if ischar(DimCell), DimCell={DimCell}; end %transform char to cell if needed
249    DimIndices=[];
250    for idim=1:numel(DimCell)
251        ind_dim=find(strcmp(DimCell{idim},ListDimName));%find the dim index in the list of coord dimensions
252        if ~isempty(ind_dim)
253            DimIndices=[DimIndices ind_dim]; %update the list of coord dimensions included in DimCell
254        end
255    end
256    check_previous=0;
257    for iprev=1:numel(NewCellInfo)
258        if isequal(DimIndices,NewCellDimIndex{iprev})
259            check_previous=1;
260            NewCellInfo{iprev}.VarIndex=[NewCellInfo{iprev}.VarIndex ivar_remain(ivardim)];%append the current variable index to the found field cell
261            break
262        end
263    end
264    if ~check_previous
265        nbcell=numel(NewCellInfo)+1;
266        NewCellDimIndex{nbcell}=DimIndices;
267        NewCellInfo{nbcell}.VarIndex=ivar_remain(ivardim);% create a new field cell with the current variable index
268        NewNbDim(nbcell)=numel(DimIndices);
269        NewCellInfo{nbcell}.CoordType='grid';
270        NewCellInfo{nbcell}.CoordSize=CoordSize;
271        NewCellInfo{nbcell}.CoordIndex=ListCoordIndex(DimIndices);
272    end
273end
274NbDim=[NbDim NewNbDim];
275CellInfo=[CellInfo NewCellInfo];
276
277%% suppress empty cells
278check_empty=cellfun(@isempty,CellInfo);
279%check_empty=cellfun(@isempty,CellVarIndex);
280CellInfo(check_empty)=[];
281
282% CellVarIndex(check_empty)=[];
283NbDim(check_empty)=[];
284% CoordType(check_empty)=[];
285% VarRole(check_empty)=[];
286
287%% document roles of non-coordinate variables
288% ListRole={'vector_x','vector_y','vector_z','vector_x_tps','vector_y_tps','warnflag','errorflag',...
289%    'ancillary','image','color','discrete','scalar'};% except coord,coord_x,_y,_z,Coord_tps already taken, into account
290for icell=1:numel(CellInfo)
291    VarIndex=CellInfo{icell}.VarIndex;
292    for ivar=VarIndex
293        if isfield(CellInfo{icell},['VarIndex_' Role{ivar}])
294            CellInfo{icell}.(['VarIndex_' Role{ivar}])=[CellInfo{icell}.(['VarIndex_' Role{ivar}]) ivar];
295        else
296            CellInfo{icell}.(['VarIndex_' Role{ivar}])= ivar;
297        end
298        if ~isempty(FieldRequest{ivar})
299            CellInfo{icell}.FieldRequest=FieldRequest{ivar};
300        end
301        if ~isempty(Operation{ivar})
302            CellInfo{icell}.Operation=Operation{ivar};
303        end
304        if CheckSub(ivar)==1
305            CellInfo{icell}.CheckSub=1;
306        end
307    end
308end
Note: See TracBrowser for help on using the repository browser.