source: trunk/src/find_field_cells.m @ 569

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

many corrections; introduction of browse_data to scan the whole set of data, used also to replicate data in calibration mode

File size: 16.0 KB
RevLine 
[514]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%------------------------------------------------------------------------
[530]4% function  [CellInfo,NbDim,errormsg]=find_field_cells(Data)
[514]5%
6% OUTPUT:
[530]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   
[516]21%      .FieldRequest= 'interp_lin', 'interp_tps' indicate whether lin interpolation  or derivatives (tps) is needed to calculate the requested field
[530]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
[514]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
[530]56function [CellInfo,NbDim,errormsg]=find_field_cells(Data)
[535]57
[530]58NbDim=0;
[535]59errormsg='';
[530]60if ~isfield(Data,'ListVarName'), errormsg='the list of variables .ListVarName is missing';return;end
61if ~isfield(Data,'VarDimName'), errormsg='the list of dimensions .VarDimName is missing';return;end
[514]62nbvar=numel(Data.ListVarName);%number of field variables
[530]63if ~isequal(numel(Data.VarDimName),nbvar), errormsg='.ListVarName and .VarDimName have unequal length';return;end
[535]64% check the existence of variable data
65check_var=1;
66for ilist=1:numel(Data.ListVarName)
67    if ~isfield(Data,Data.ListVarName{ilist})
68        check_var=0;% dimensions of data defined, data not needed for this function
69        break
[530]70    end
71end
[535]72if ~check_var &&  ~(isfield(Data,'ListDimName')&& isfield(Data,'DimValue')&&isequal(numel(Data.ListDimName),numel(Data.DimValue)))
73    errormsg=['missing variable or values of dimensions' Data.ListVarName{ilist}];
74    return
75end
[514]76
77
[515]78%% role of variables and list of requested operations
[530]79%ListRole={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','vector_x_tps','vector_y_tps','warnflag','errorflag',...
80%   'ancillary','image','color','discrete','scalar','coord_tps'};% rmq vector_x_tps and vector_y_tps to be replaced by vector_x and vector_y
[514]81Role=num2cell(blanks(nbvar));%initialize a cell array of nbvar blanks
[515]82FieldRequest=regexprep(Role,' ',''); % fieldRequest set to '' by default
83Operation=cell(size(Role)); % fieldRequest set to {} by default
84CheckSub=zeros(size(Role));% =1 for fields to substract
[514]85Role=regexprep(Role,' ','scalar'); % Role set to 'scalar' by default
86if isfield(Data,'VarAttribute')
87    for ivar=1:numel(Data.VarAttribute)
88        if isfield(Data.VarAttribute{ivar},'Role')
89            Role{ivar}=Data.VarAttribute{ivar}.Role;
90        end
[515]91        if isfield(Data.VarAttribute{ivar},'FieldRequest')
92            FieldRequest{ivar}=Data.VarAttribute{ivar}.FieldRequest;
93        end
94        if isfield(Data.VarAttribute{ivar},'Operation')
95            Operation{ivar}=Data.VarAttribute{ivar}.Operation;
96        end
97        if isfield(Data.VarAttribute{ivar},'CheckSub')
98            CheckSub(ivar)=Data.VarAttribute{ivar}.CheckSub;
99        end
[514]100    end
101end
102
[530]103%% find scattered (unstructured) coordinates
104ivar_coord_x=find(strcmp('coord_x',Role));
105% VarDimCell=cell(numel(ivar_coord_x));
106check_select=zeros(1,nbvar);
107CellInfo=cell(1,numel(ivar_coord_x));
108NbDim=zeros(1,numel(ivar_coord_x));
109for icell=1:numel(ivar_coord_x)
110    DimCell=Data.VarDimName{ivar_coord_x(icell)};
111    if ischar(DimCell),DimCell={DimCell};end
112    check_cell=zeros(numel(DimCell),nbvar);
113    for idim=1:numel(DimCell)
114        for ivar=1:nbvar
115            check_cell(idim,ivar)=max(strcmp(DimCell{idim},Data.VarDimName{ivar}));
116        end
[514]117    end
[530]118    check_cell=sum(check_cell,1)==numel(DimCell);%logical array=1 for variables belonging to the current cell
119    VarIndex=find(check_cell);
120    if ~(numel(VarIndex)==1 && numel(DimCell)==1)% exclude case of isolated coord_x variable (treated later)
121        if ~(numel(VarIndex)==1 && numel(DimCell)>1)% a variable is associated to coordinate
122            CellInfo{icell}.CoordIndex=ivar_coord_x(icell);
123            % size of coordinate var
[535]124            if check_var
125                CellInfo{icell}.CoordSize=numel(Data.(Data.ListVarName{ivar_coord_x(icell)}));
126            else
[530]127                for idim=1:numel(DimCell)
128                 check_index= strcmp(DimCell{idim},Data.ListDimName);
129                 CellInfo{icell}.CoordSize(idim)=Data.DimValue(check_index);
130                end
131                CellInfo{icell}.CoordSize=prod(CellInfo{icell}.CoordSize);
132            end
133            ind_y=find(strcmp('coord_y',Role(VarIndex)));
134            if numel(VarIndex)==2||isempty(ind_y)% no variable, except possibly y
135                NbDim(icell)=1;
136            else
137                CellInfo{icell}.CoordType='scattered';
138                ind_z=find(strcmp('coord_z',Role(VarIndex)));
139                if numel(VarIndex)==3||isempty(ind_z)% no z variable, except possibly as a fct z(x,y)
140                    CellInfo{icell}.CoordIndex=[VarIndex(ind_y) CellInfo{icell}.CoordIndex];
141                    NbDim(icell)=2;
142                else
143                    CellInfo{icell}.CoordIndex=[VarIndex(ind_z) CellInfo{icell}.CoordIndex];
144                    NbDim(icell)=3;
145                end
146            end
[514]147        end
[530]148        CellInfo{icell}.VarIndex=VarIndex;
149        check_select=check_select|check_cell;
[514]150    end
[530]151end
152
153%% look for tps coordinates
[535]154ivar_remain=find(~check_select);% indices of remaining variables (not already selected)
[530]155check_coord_tps= strcmp('coord_tps',Role(~check_select));
[535]156ivar_tps=ivar_remain(check_coord_tps);% variable indices corresponding to tps coordinates
[530]157for icell_tps=1:numel(ivar_tps)
[535]158    DimCell=Data.VarDimName{ivar_tps(icell_tps)};% dimension names for the current tps coordinate variable
159    icell=numel(CellInfo)+icell_tps; % new field cell index
160    CellInfo{icell}.CoordIndex=ivar_tps(icell_tps);% index of the  tps coordinate variable
[546]161    %CellInfo{icell}.VarIndex_subrange_tps=[];
162    %CellInfo{icell}.VarIndex_nbsites_tps=[];
[530]163    if numel(DimCell)==3
164        VarDimName=Data.VarDimName(~check_select);
165        for ivardim=1:numel(VarDimName)
166            if strcmp(VarDimName{ivardim},DimCell{3})
167                CellInfo{icell}.NbSite_tps= ivar_remain(ivardim);
168                check_cell(ivar_remain(ivardim))=1;% nbre of sites for each tps subdomain
169            elseif strcmp(VarDimName{ivardim}{1},DimCell{2}) && strcmp(VarDimName{ivardim}{3},DimCell{3})
170                CellInfo{icell}.SubRange_tps=ivar_remain(ivardim);
171                check_cell(ivar_remain(ivardim))=1;% subrange definiton for tps
172            elseif strcmp(VarDimName{ivardim}{1},DimCell{1}) && strcmp(VarDimName{ivardim}{2},DimCell{3})
173                check_cell(ivar_remain(ivardim))=1;% variable
174            end
175        end
[514]176    end
[530]177    CellInfo{icell}.CoordType='tps';
178    CellInfo{icell}.VarIndex=find(check_cell);
[535]179    if check_var
180        NbDim(icell)=size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),2);
181        CellInfo{icell}.CoordSize=size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),1)*size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),3);
182    else
[530]183        check_index_1= strcmp(DimCell{1},Data.ListDimName);
184        check_index_2= strcmp(DimCell{2},Data.ListDimName);
185        check_index_3= strcmp(DimCell{3},Data.ListDimName);
186        NbDim(icell)=Data.DimValue(check_index_2);
187        CellInfo{icell}.CoordSize=Data.DimValue(check_index_1)*Data.DimValue(check_index_3);
[515]188    end
[530]189    check_select=check_select|check_cell;
[514]190end
191
[535]192%% look for coordinate variables and corresponding gridded data:
193% coordinate variables are variables associated with a single dimension, defining the coordinate values
194% two cases: 1)the coordiante variable represents the set of coordiante values
195%            2)the coordinate variable contains only two elements, representing the coordinate bounds for the dimension with the same name as the cordinate
196ivar_remain=find(~check_select);% indices of remaining variables, not already taken into account
197ListVarName=Data.ListVarName(~check_select);%list of remaining variables
[530]198VarDimName=Data.VarDimName(~check_select);%dimensions of remaining variables
[535]199check_coord= cellfun(@numel,VarDimName)==1|cellfun(@ischar,VarDimName)==1;% find remaining variables with a single dimension
200ListCoordIndex=ivar_remain(check_coord);% indices of remaining variables with a single dimension
201ListCoordName=Data.ListVarName(ListCoordIndex);% corresponding names of remaining variables with a single dimension
202ListDimName=Data.VarDimName(ListCoordIndex);% dimension names of remaining variables with a single dimension
203
204%remove redondant variables -> keep only one variable per dimension
[530]205check_keep=logical(ones(size(ListDimName)));
206for idim=1:numel(ListDimName)
[535]207    prev_ind=strcmp(ListDimName{idim},ListDimName(1:idim-1));% check whether the dimension is already taken into account
[530]208    if ~isempty(prev_ind)
[535]209        if strcmp(ListCoordName{idim},ListDimName{idim}) %variable with the same name as the coordinate taken in priority
[530]210            check_keep(prev_ind)=0;
211        else
212           check_keep(idim)=0;
[514]213        end
214    end
215end
[535]216ListCoordIndex=ListCoordIndex(check_keep);% list of coordinate variable indices
217ListCoordName=ListCoordName(check_keep);% list of coordinate variable names
218ListDimName=ListDimName(check_keep);% list of coordinate dimension names
[514]219
[535]220% determine dimension sizes
221CoordSize=zeros(size(ListCoordIndex));
[530]222for ilist=1:numel(ListCoordIndex)
223    if iscell(ListDimName{ilist})
224        ListDimName(ilist)=ListDimName{ilist};%transform cell to string
[514]225    end
[535]226    if check_var% if the list of dimensions has been directly defined, no variable data available
227        CoordSize(ilist)=numel(Data.(ListCoordName{ilist}));% number of elements in the variable corresponding to the dimension #ilist
[530]228    else
[535]229        check_index= strcmp(ListDimName{ilist},Data.ListDimName);% find the  index in the list of dimensions
230        CoordSize(ilist)=Data.DimValue(check_index);% find the  corresponding dimension value
[530]231    end
[535]232    if CoordSize(ilist)==2% case of uniform grid coordinate defined by lower and upper bounds only
233        ListDimName{ilist}=ListCoordName{ilist};% replace the dimension name by the coordinate variable name
[514]234    end
[530]235end
[535]236
237% group the remaining variables in cells sharing the same coordinate variables
[530]238NewCellInfo={};
239NewCellDimIndex={};
240NewNbDim=[];
[535]241for ivardim=1:numel(VarDimName) % loop at the list of remaining variables
242    DimCell=VarDimName{ivardim};% dimension names of the current variable
[530]243    if ischar(DimCell), DimCell={DimCell}; end %transform char to cell if needed
244    DimIndices=[];
245    for idim=1:numel(DimCell)
[535]246        ind_dim=find(strcmp(DimCell{idim},ListDimName));%find the dim index in the list of coordinate variables
[530]247        if ~isempty(ind_dim)
248            DimIndices=[DimIndices ind_dim]; %update the list of coord dimensions included in DimCell
[535]249            if check_var && CoordSize(ind_dim)==2 % determine the size of the coordinate in case of coordiante variable limited to lower and upper bounds
[543]250                if isvector(Data.(ListVarName{ivardim}))
[535]251                    if numel(Data.(ListVarName{ivardim}))>2
252                        CoordSize(ind_dim)=numel(Data.(ListVarName{ivardim}));
253                    end
254                else
255                    CoordSize(ind_dim)=size(Data.(ListVarName{ivardim}),idim);
256                end
257            end
[514]258        end
[530]259    end
[535]260    % look for cells of variables with the same coordinate variables
[530]261    check_previous=0;
262    for iprev=1:numel(NewCellInfo)
263        if isequal(DimIndices,NewCellDimIndex{iprev})
264            check_previous=1;
265            NewCellInfo{iprev}.VarIndex=[NewCellInfo{iprev}.VarIndex ivar_remain(ivardim)];%append the current variable index to the found field cell
266            break
[514]267        end
[530]268    end
[535]269    % create a new cell if no previous one contains the coordinate variables
[530]270    if ~check_previous
271        nbcell=numel(NewCellInfo)+1;
272        NewCellDimIndex{nbcell}=DimIndices;
273        NewCellInfo{nbcell}.VarIndex=ivar_remain(ivardim);% create a new field cell with the current variable index
274        NewNbDim(nbcell)=numel(DimIndices);
275        NewCellInfo{nbcell}.CoordType='grid';
276        NewCellInfo{nbcell}.CoordSize=CoordSize;
277        NewCellInfo{nbcell}.CoordIndex=ListCoordIndex(DimIndices);
278    end
279end
280NbDim=[NbDim NewNbDim];
281CellInfo=[CellInfo NewCellInfo];
[569]282%
283% %% suppress empty cells
284% check_empty=cellfun(@isempty,CellInfo);
285% CellInfo(check_empty)=[];
286% NbDim(check_empty)=[];
[530]287
[569]288%% suppress empty cells or cells with a single coordinate variable
289check_remove=false(size(CellInfo));
290for icell=1:numel(check_remove)
291    if isempty(CellInfo{icell})||(numel(CellInfo{icell}.VarIndex)==1 && check_coord(icell))
292        check_remove(icell)=1;
293    end
294end
295CellInfo(check_remove)=[];
296NbDim(check_remove)=[];
[530]297
298%% document roles of non-coordinate variables
299% ListRole={'vector_x','vector_y','vector_z','vector_x_tps','vector_y_tps','warnflag','errorflag',...
300%    'ancillary','image','color','discrete','scalar'};% except coord,coord_x,_y,_z,Coord_tps already taken, into account
301for icell=1:numel(CellInfo)
302    VarIndex=CellInfo{icell}.VarIndex;
303    for ivar=VarIndex
304        if isfield(CellInfo{icell},['VarIndex_' Role{ivar}])
305            CellInfo{icell}.(['VarIndex_' Role{ivar}])=[CellInfo{icell}.(['VarIndex_' Role{ivar}]) ivar];
306        else
307            CellInfo{icell}.(['VarIndex_' Role{ivar}])= ivar;
[514]308        end
[530]309        if ~isempty(FieldRequest{ivar})
310            CellInfo{icell}.FieldRequest=FieldRequest{ivar};
[514]311        end
[530]312        if ~isempty(Operation{ivar})
313            CellInfo{icell}.Operation=Operation{ivar};
[514]314        end
[530]315        if CheckSub(ivar)==1
316            CellInfo{icell}.CheckSub=1;
[514]317        end
318    end
319end
Note: See TracBrowser for help on using the repository browser.