source: trunk/src/find_field_cells.m @ 743

Last change on this file since 743 was 690, checked in by sommeria, 11 years ago

representation of ancillary data as table improved.

File size: 16.8 KB
Line 
1%'find_field_cells': analyse the field structure for input in uvmat functions, grouping the variables  into 'fields' with common coordinates
2%------------------------------------------------------------------------
3% function  [CellInfo,NbDim,errormsg]=find_field_cells(Data)
4%
5% OUTPUT:
6% CellInfo: cell of structures describing field cells
7%     .CoordType:  type of coordinates for each field cell = 'scattered','grid','tps';
8%     .CoordIndex: array of the indices of the variables representing the coordinates (in the order z,y,x)
9%     .CoordSize: array of the nbre of values for each  coordinate in a grid, nbre of points in the unstructured case
10%     .NbCentres_tps:
11%     .SubRange_tps
12%     .VarIndex: arrays of the variable indices in the field cell
13%     .VarIndex_ancillary: indices of ancillary variables
14%              _color : color image, the last index, which is not a coordinate variable, represent the 3 color components rgb
15%              _discrete: like scalar, but set of data points without continuity, represented as dots in a usual plot, instead of continuous lines otherwise
16%              _errorflag: index of error flag
17%              _image   : B/W image, (behaves like scalar)
18%              _vector_x,_y,_z: indices of variables giving the vector components x, y, z
19%              _warnflag: index of warnflag   
20%      .ProjModeRequest= 'interp_lin', 'interp_tps' indicate whether lin interpolation  or derivatives (tps) is needed to calculate the requested field
21%      .FieldName = operation to be performed to finalise the field cell after projection
22%      .SubCheck=0 /1 indicate that the field must be substracted (second  entry in uvmat)
23% NbDim: array with the length of CellVarIndex, giving the space dimension of each field cell
24% errormsg: error message
25%   
26% INPUT:
27% Data: structure representing fields, output of check_field_structure
28%            .ListGlobalAttributes
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%            .Attr1, .Attr2....
35%        case of actual data:
36%            .Var1, .Var2...
37%        case of data structure, without the  data themselves
38%            .LisDimName: list of dimension names
39%            .DimValue: list of corresponding dimension values
40% HELP:
41% to get the dimensions of arrays common to the field #icell
42%         VarIndex=CellVarIndex{icell}; % list of variable indices
43%         DimIndex=Data.VarDimIndex{VarIndex(1)} % list of dimensions for each variable in the cell #icell
44%
45%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
46%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
47%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
48%     This file is part of the toolbox UVMAT.
49%
50%     UVMAT is free software; you can redistribute it and/or modify
51%     it under the terms of the GNU General Public License as published by
52%     the Free Software Foundation; either version 2 of the License, or
53%     (at your option) any later version.
54%
55%     UVMAT is distributed in the hope that it will be useful,
56%     but WITHOUT ANY WARRANTY; without even the implied warranty of
57%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
58%     GNU General Public License (file UVMAT/COPYING.txt) for more details.for input in proj_field and plot_field
59%    group the variables  into 'fields' with common dimensions
60
61function [CellInfo,NbDim,errormsg]=find_field_cells(Data)
62CellInfo={};
63NbDim=0;
64errormsg='';
65if ~isfield(Data,'ListVarName'), errormsg='the list of variables .ListVarName is missing';return;end
66if ~isfield(Data,'VarDimName'), errormsg='the list of dimensions .VarDimName is missing';return;end
67nbvar=numel(Data.ListVarName);%number of variables in the field structure
68if ~isequal(numel(Data.VarDimName),nbvar), errormsg='.ListVarName and .VarDimName have unequal length';return;end
69% check the existence of variable data
70check_var=1;
71for ilist=1:numel(Data.ListVarName)
72    if ~isfield(Data,Data.ListVarName{ilist})
73        check_var=0;% dimensions of data defined, data not needed for this function
74        break
75    end
76end
77if ~check_var &&  ~(isfield(Data,'ListDimName')&& isfield(Data,'DimValue')&&isequal(numel(Data.ListDimName),numel(Data.DimValue)))
78    errormsg=['missing variable or values of dimensions' Data.ListVarName{ilist}];
79    return
80end
81
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
87ProjModeRequest=regexprep(Role,' ',''); % fieldRequest set to '' by default
88FieldName=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},'ProjModeRequest')
97            ProjModeRequest{ivar}=Data.VarAttribute{ivar}.ProjModeRequest;
98        end
99        if isfield(Data.VarAttribute{ivar},'FieldName')
100            FieldName{ivar}=Data.VarAttribute{ivar}.FieldName;
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));%find variables with Role='coord_x'
110check_select=false(1,nbvar);
111check_coord=false(1,nbvar);
112CellInfo=cell(1,numel(ivar_coord_x));
113NbDim=zeros(1,numel(ivar_coord_x));
114% loop on unstructured coordinate x -> different field cells
115for icell=1:numel(ivar_coord_x)
116    DimCell=Data.VarDimName{ivar_coord_x(icell)};% cell of dimension names for ivar_coord_x(icell)
117    if ischar(DimCell),DimCell={DimCell};end % transform char to cell for a single dimension
118    % look for variables sharing dimension(s) with ivar_coord_x(icell)
119    check_cell=zeros(numel(DimCell),nbvar);
120    for idim=1:numel(DimCell)
121        for ivar=1:nbvar
122            check_cell(idim,ivar)=max(strcmp(DimCell{idim},Data.VarDimName{ivar}));
123        end
124    end
125    check_cell=sum(check_cell,1)==numel(DimCell);%logical array=1 for variables belonging to the current cell
126    VarIndex=find(check_cell);% list of detected variable indices
127    if ~(numel(VarIndex)==1 && numel(DimCell)==1)% exclude case of isolated coord_x variable (treated later)
128        if ~(numel(VarIndex)==1 && numel(DimCell)>1)% a variable is associated to coordinate
129            CellInfo{icell}.CoordIndex=ivar_coord_x(icell);
130            % size of coordinate var
131            if check_var
132                CellInfo{icell}.CoordSize=numel(Data.(Data.ListVarName{ivar_coord_x(icell)}));
133            else
134                for idim=1:numel(DimCell)
135                    check_index= strcmp(DimCell{idim},Data.ListDimName);
136                    CellInfo{icell}.CoordSize(idim)=Data.DimValue(check_index);
137                end
138                CellInfo{icell}.CoordSize=prod(CellInfo{icell}.CoordSize);
139            end
140            ind_y=find(strcmp('coord_y',Role(VarIndex)));
141            if numel(VarIndex)==2||isempty(ind_y)% no variable, except possibly y
142                NbDim(icell)=1;
143            else
144                CellInfo{icell}.CoordType='scattered';
145                ind_z=find(strcmp('coord_z',Role(VarIndex)));
146                if numel(VarIndex)==3||isempty(ind_z)% no z variable, except possibly as a fct z(x,y)
147                    CellInfo{icell}.CoordIndex=[VarIndex(ind_y) CellInfo{icell}.CoordIndex];
148                    NbDim(icell)=2;
149                else
150                    CellInfo{icell}.CoordIndex=[VarIndex(ind_z) CellInfo{icell}.CoordIndex];
151                    NbDim(icell)=3;
152                end
153            end
154        end
155        CellInfo{icell}.VarIndex=VarIndex;
156        check_select=check_select|check_cell;
157    end
158end
159
160%% look for tps coordinates
161ivar_remain=find(~check_select);% indices of remaining variables (not already selected)
162check_coord_tps= strcmp('coord_tps',Role(~check_select));
163ivar_tps=ivar_remain(check_coord_tps);% variable indices corresponding to tps coordinates
164
165% loop on the tps coordinate sets
166for icell_tps=1:numel(ivar_tps)
167    check_cell=zeros(1,nbvar);% =1 for the variables selected in the current cell
168    check_cell(ivar_tps(icell_tps))=1;% mark the coordinate variable as selected
169    DimCell=Data.VarDimName{ivar_tps(icell_tps)};% dimension names for the current tps coordinate variable
170    icell=numel(CellInfo)+icell_tps; % new field cell index
171    CellInfo{icell}.CoordIndex=ivar_tps(icell_tps);% index of the  tps coordinate variable
172    if numel(DimCell)==3
173        VarDimName=Data.VarDimName(~check_select);
174        for ivardim=1:numel(VarDimName)
175            if strcmp(VarDimName{ivardim},DimCell{3})
176                CellInfo{icell}.NbCentres_tps= ivar_remain(ivardim);% nbre of sites for each tps subdomain
177                check_cell(ivar_remain(ivardim))=1;% mark the variable as selected
178            elseif strcmp(VarDimName{ivardim}{1},DimCell{2}) && numel(VarDimName{ivardim})>=3 && strcmp(VarDimName{ivardim}{3},DimCell{3})
179                CellInfo{icell}.SubRange_tps=ivar_remain(ivardim);% subrange definiton for tps
180                check_cell(ivar_remain(ivardim))=1;% mark the variable as selected
181            elseif strcmp(VarDimName{ivardim}{1},DimCell{1}) && strcmp(VarDimName{ivardim}{2},DimCell{3})% variable
182                check_cell(ivar_remain(ivardim))=1;% mark the variable as selected
183            end
184        end
185    end
186    CellInfo{icell}.CoordType='tps';
187    CellInfo{icell}.VarIndex=find(check_cell);
188    if check_var
189        NbDim(icell)=size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),2);
190        CellInfo{icell}.CoordSize=size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),1)*size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),3);
191    else
192        check_index_1= strcmp(DimCell{1},Data.ListDimName);
193        check_index_2= strcmp(DimCell{2},Data.ListDimName);
194        NbDim(icell)=Data.DimValue(check_index_2);
195        if numel(DimCell)>=3
196        check_index_3= strcmp(DimCell{3},Data.ListDimName); 
197        CellInfo{icell}.CoordSize=Data.DimValue(check_index_1)*Data.DimValue(check_index_3);
198        end
199    end
200    check_select=check_select|check_cell;
201end
202
203%% look for coordinate variables and corresponding gridded data:
204% coordinate variables are variables associated with a single dimension, defining the coordinate values
205% two cases: 1)the coordiante variable represents the set of coordiante values
206%            2)the coordinate variable contains only two elements, representing the coordinate bounds for the dimension with the same name as the cordinate
207ivar_remain=find(~check_select);% indices of remaining variables, not already taken into account
208ListVarName=Data.ListVarName(~check_select);%list of remaining variables
209VarDimName=Data.VarDimName(~check_select);%dimensions of remaining variables
210check_coord_select= cellfun(@numel,VarDimName)==1|cellfun(@ischar,VarDimName)==1;% find remaining variables with a single dimension
211check_coord_select=check_coord_select & ~strcmp('ancillary',Role(~check_select));% do not select ancillary variables as coordinates
212%check_coord(~check_select)=check_coord_select;
213ListCoordIndex=ivar_remain(check_coord_select);% indices of remaining variables with a single dimension
214ListCoordName=ListVarName(check_coord_select);% corresponding names of remaining variables with a single dimension
215ListDimName=VarDimName(check_coord_select);% dimension names of remaining variables with a single dimension
216
217%remove redondant variables -> keep only one variable per dimension
218check_keep=logical(ones(size(ListDimName)));
219for idim=1:numel(ListDimName)
220    prev_ind=strcmp(ListDimName{idim},ListDimName(1:idim-1));% check whether the dimension is already taken into account
221    if ~isempty(prev_ind)
222        if strcmp(ListCoordName{idim},ListDimName{idim}) %variable with the same name as the coordinate taken in priority
223            check_keep(prev_ind)=0;
224        else
225           check_keep(idim)=0;
226        end
227    end
228end
229ListCoordIndex=ListCoordIndex(check_keep);% list of coordinate variable indices
230ListCoordName=ListCoordName(check_keep);% list of coordinate variable names
231ListDimName=ListDimName(check_keep);% list of coordinate dimension names
232
233% determine dimension sizes
234CoordSize=zeros(size(ListCoordIndex));
235for ilist=1:numel(ListCoordIndex)
236    if iscell(ListDimName{ilist})
237        ListDimName(ilist)=ListDimName{ilist};%transform cell to string
238    end
239    if check_var% if the list of dimensions has been directly defined, no variable data available
240        CoordSize(ilist)=numel(Data.(ListCoordName{ilist}));% number of elements in the variable corresponding to the dimension #ilist
241    else
242        check_index= strcmp(ListDimName{ilist},Data.ListDimName);% find the  index in the list of dimensions
243        CoordSize(ilist)=Data.DimValue(check_index);% find the  corresponding dimension value
244    end
245    if CoordSize(ilist)==2% case of uniform grid coordinate defined by lower and upper bounds only
246        ListDimName{ilist}=ListCoordName{ilist};% replace the dimension name by the coordinate variable name
247    end
248end
249
250%% group the remaining variables in cells sharing the same coordinate variables
251NewCellInfo={};
252NewCellDimIndex={};
253NewNbDim=[];
254for ivardim=1:numel(VarDimName) % loop at the list of remaining variables
255    DimCell=VarDimName{ivardim};% dimension names of the current variable
256    if ischar(DimCell), DimCell={DimCell}; end %transform char to cell if needed
257    DimIndices=[];
258    for idim=1:numel(DimCell)
259        ind_dim=find(strcmp(DimCell{idim},ListDimName));%find the dim index in the list of coordinate variables
260        if ~isempty(ind_dim)
261            DimIndices=[DimIndices ind_dim]; %update the list of coord dimensions included in DimCell
262            if check_var && CoordSize(ind_dim)==2 % determine the size of the coordinate in case of coordiante variable limited to lower and upper bounds
263                if isvector(Data.(ListVarName{ivardim}))
264                    if numel(Data.(ListVarName{ivardim}))>2
265                        CoordSize(ind_dim)=numel(Data.(ListVarName{ivardim}));
266                    end
267                else
268                    CoordSize(ind_dim)=size(Data.(ListVarName{ivardim}),idim);
269                end
270            end
271        end
272    end
273    % look for cells of variables with the same coordinate variables
274    check_previous=0;
275    for iprev=1:numel(NewCellInfo)
276        if isequal(DimIndices,NewCellDimIndex{iprev})
277            check_previous=1;
278            NewCellInfo{iprev}.VarIndex=[NewCellInfo{iprev}.VarIndex ivar_remain(ivardim)];%append the current variable index to the found field cell
279            break
280        end
281    end
282    % create a new cell if no previous one contains the coordinate variables
283    if ~check_previous
284        nbcell=numel(NewCellInfo)+1;
285        NewCellDimIndex{nbcell}=DimIndices;
286        NewCellInfo{nbcell}.VarIndex=ivar_remain(ivardim);% create a new field cell with the current variable index
287        NewNbDim(nbcell)=numel(DimIndices);
288        NewCellInfo{nbcell}.CoordType='grid';
289        NewCellInfo{nbcell}.CoordSize=CoordSize;
290        NewCellInfo{nbcell}.CoordIndex=ListCoordIndex(DimIndices);
291    end
292end
293NbDim=[NbDim NewNbDim];
294CellInfo=[CellInfo NewCellInfo];
295
296%% suppress empty cells or cells with a single coordinate variable
297check_remove=false(size(CellInfo));
298for icell=1:numel(check_remove)
299    if isempty(CellInfo{icell})||(numel(CellInfo{icell}.VarIndex)==1 && check_coord(icell))
300        check_remove(icell)=1;
301    end
302end
303CellInfo(check_remove)=[];
304NbDim(check_remove)=[];
305
306%% document roles of non-coordinate variables
307for icell=1:numel(CellInfo)
308    VarIndex=CellInfo{icell}.VarIndex;
309    for ivar=VarIndex
310        if isfield(CellInfo{icell},['VarIndex_' Role{ivar}])
311            CellInfo{icell}.(['VarIndex_' Role{ivar}])=[CellInfo{icell}.(['VarIndex_' Role{ivar}]) ivar];
312        else
313            CellInfo{icell}.(['VarIndex_' Role{ivar}])= ivar;
314        end
315        if ~isempty(ProjModeRequest{ivar})
316            CellInfo{icell}.ProjModeRequest=ProjModeRequest{ivar};
317        end
318        if ~isempty(FieldName{ivar})
319            CellInfo{icell}.FieldName=FieldName{ivar};
320        end
321        if CheckSub(ivar)==1
322            CellInfo{icell}.CheckSub=1;
323        end
324    end
325end
Note: See TracBrowser for help on using the repository browser.