source: trunk/src/find_field_cells.m @ 1061

Last change on this file since 1061 was 1061, checked in by g7moreau, 2 years ago
  • Update copyright to year 2019
File size: 32.5 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%              _histo: index of variable used as histogram
21%       .DimIndex
22%      .ProjModeRequest= 'interp_lin', 'interp_tps' indicate whether lin interpolation  or derivatives (tps) is needed to calculate the requested field
23%      .FieldName = operation to be performed to finalise the field cell after projection
24%      .SubCheck=0 /1 indicate that the field must be substracted (second  entry in uvmat)
25% NbDim: array with the length of CellVarIndex, giving the space dimension of each field cell
26% errormsg: error message
27%   
28% INPUT:
29% Data: structure representing fields, output of check_field_structure
30%            .ListGlobalAttributes
31%            .ListVarName: cell array listing the names (cahr strings) of the variables
32%            .VarDimName: cell array of cells containing the set of dimension names for each variable of .ListVarName
33%            .VarAttribute: cell array of structures containing the variable attributes:
34%                     .VarAttribute{ilist}.key=value, where ilist is the variable
35%                     index, key is the name of the attribute, value its value (char string or number)
36%            .Attr1, .Attr2....
37%        case of actual data:
38%            .Var1, .Var2...
39%        case of data structure, without the  data themselves
40%            .LisDimName: list of dimension names
41%            .DimValue: list of corresponding dimension values
42% HELP:
43% to get the dimensions of arrays common to the field #icell
44%         VarIndex=CellVarIndex{icell}; % list of variable indices
45%         DimIndex=Data.VarDimIndex{VarIndex(1)} % list of dimensions for each variable in the cell #icell
46
47%=======================================================================
48% Copyright 2008-2019, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
49%   http://www.legi.grenoble-inp.fr
50%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
51%
52%     This file is part of the toolbox UVMAT.
53%
54%     UVMAT is free software; you can redistribute it and/or modify
55%     it under the terms of the GNU General Public License as published
56%     by the Free Software Foundation; either version 2 of the license,
57%     or (at your option) any later version.
58%
59%     UVMAT is distributed in the hope that it will be useful,
60%     but WITHOUT ANY WARRANTY; without even the implied warranty of
61%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
62%     GNU General Public License (see LICENSE.txt) for more details.
63%=======================================================================
64
65%    group the variables  into 'fields' with common dimensions
66
67function [CellInfo,NbDim,errormsg]=find_field_cells(Data)
68CellInfo={};%default output
69NbDim=0;
70errormsg='';
71if ~isfield(Data,'ListVarName'), errormsg='the list of variables .ListVarName is missing';return;end
72if ~isfield(Data,'VarDimName'), errormsg='the list of dimensions .VarDimName is missing';return;end
73nbvar=numel(Data.ListVarName);%number of variables in the field structure
74if ~isequal(numel(Data.VarDimName),nbvar), errormsg='.ListVarName and .VarDimName have unequal length';return;end
75% check the existence of variable data
76check_var=1;
77for ilist=1:numel(Data.ListVarName)
78    if ~isfield(Data,Data.ListVarName{ilist})
79        check_var=0;% dimensions of array defined, but the corresponding array is not given
80        break
81    end
82end
83
84%% role of variables and list of requested operations
85%ListRole={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','vector_x_tps','vector_y_tps','warnflag','errorflag',...
86%   'ancillary','image','color','discrete','scalar','coord_tps'};% rmq vector_x_tps and vector_y_tps to be replaced by vector_x and vector_y
87Role=num2cell(blanks(nbvar));%initialize a cell array of nbvar blanks
88ProjModeRequest=regexprep(Role,' ',''); % fieldRequest set to '' by default
89FieldName=cell(size(Role)); % fieldRequest set to {} by default
90CheckSub=zeros(size(Role));% =1 for fields to substract
91%Role=regexprep(Role,' ','scalar'); % Role set to 'scalar' by default
92if isfield(Data,'VarAttribute')
93    for ivar=1:numel(Data.VarAttribute)
94        if isfield(Data.VarAttribute{ivar},'Role')
95            Role{ivar}=Data.VarAttribute{ivar}.Role;
96        end
97        if isfield(Data.VarAttribute{ivar},'ProjModeRequest')
98            ProjModeRequest{ivar}=Data.VarAttribute{ivar}.ProjModeRequest;
99        end
100        if isfield(Data.VarAttribute{ivar},'FieldName')
101            FieldName{ivar}=Data.VarAttribute{ivar}.FieldName;
102        end
103        if isfield(Data.VarAttribute{ivar},'CheckSub')
104            CheckSub(ivar)=Data.VarAttribute{ivar}.CheckSub;
105        end
106    end
107end
108
109%% detect  fields with different roles
110ind_scalar=find(strcmp('scalar',Role));
111ind_errorflag=find(strcmp('errorflag',Role));
112ind_image=find(strcmp('image',Role));
113ind_vector_x=[find(strcmp('vector_x',Role)) find(strcmp('vector_x_tps',Role))];
114if ~isempty(ind_vector_x)
115    ind_vector_y=[find(strcmp('vector_y',Role)) find(strcmp('vector_y_tps',Role))];
116    ind_vector_z=find(strcmp('vector_z',Role));
117    ind_warnflag=find(strcmp('warnflag',Role));
118    ind_ancillary=find(strcmp('ancillary',Role));
119end
120ind_discrete=find(strcmp('discrete',Role));
121ind_coord_x=[find(strcmp('coord_x',Role)) find(strcmp('histo',Role))];
122ind_coord_y=find(strcmp('coord_y',Role));
123ind_coord_z=find(strcmp('coord_z',Role));
124ind_histo=find(strcmp('histo',Role));
125ind_coord_tps=find(strcmp('coord_tps',Role));
126check_string=cellfun(@ischar,Data.VarDimName)==1;
127index_string=find(check_string);
128for ivar=index_string
129    Data.VarDimName{ivar}={Data.VarDimName{ivar}};%transform char strings into cells
130end
131check_coord_names= cellfun(@numel,Data.VarDimName)==1;
132check_coord_raster=false(size(check_coord_names));% check variables describing regular mesh (raster coordinates), from two values, min and max.
133if check_var
134    for ivar=find(check_coord_names)
135        if numel(Data.(Data.ListVarName{ivar}))==2
136            check_coord_raster(ivar)=true;
137        end
138    end
139else
140    for ivar=find(check_coord_names)
141        DimIndex=find(strcmp(Data.VarDimName{ivar},Data.ListDimName));
142        if Data.DimValue(DimIndex)==2
143            check_coord_raster(ivar)=true;
144        end
145    end
146end
147
148
149%% initate cells around each scalar field
150index_remove=[];
151cell_nbre=numel(ind_scalar)+numel(ind_vector_x);
152flag_remove=false(1,cell_nbre);
153NbDim=zeros(1,cell_nbre);
154index_coord_x=zeros(size(ind_coord_x));
155for icell=1:numel(ind_scalar)
156    CellInfo{icell}.VarType='scalar';
157    CellInfo{icell}.VarIndex_scalar=ind_scalar(icell);
158    CellInfo{icell}.VarIndex=ind_scalar(icell);
159    DimCell_var=Data.VarDimName{ind_scalar(icell)};% cell of dimension names for ivar_coord_x(icell)
160    %look for errorflag
161    for ivar=ind_errorflag
162        DimCell=Data.VarDimName{ivar};
163        if isequal(DimCell,DimCell_var)
164            CellInfo{icell}.VarIndex(2)=ivar;
165            CellInfo{icell}.VarIndex_errorflag=ivar;
166            break
167        end
168    end
169end
170
171%% initate cells around each vector field
172for icell=numel(ind_scalar)+1:cell_nbre
173    CellInfo{icell}.VarType='vector';
174    CellInfo{icell}.VarIndex(1)=ind_vector_x(icell-numel(ind_scalar));
175    CellInfo{icell}.VarIndex_vector_x=ind_vector_x(icell-numel(ind_scalar));
176    DimCell_var=Data.VarDimName{ind_vector_x(icell-numel(ind_scalar))};% cell of dimension names for ivar_coord_x(icell)
177    % look for the associated y vector component
178    nbvar=1;
179    for ivar=ind_vector_y
180        DimCell=Data.VarDimName{ivar};
181        if isequal(DimCell,DimCell_var)
182            CellInfo{icell}.VarIndex(2)=ivar;
183            nbvar=2;
184            CellInfo{icell}.VarIndex_vector_y=ivar;
185            break
186        end
187    end
188    if ~isfield(CellInfo{icell},'VarIndex_vector_y')
189        flag_remove(icell)=true;% no vector_y found , mark cell to remove
190    end
191    % look for the associated z vector component
192    for ivar=ind_vector_z
193        DimCell=Data.VarDimName{ivar};
194        if isequal(DimCell,DimCell_var)
195            CellInfo{icell}.VarIndex(3)=ivar;
196            nbvar=3;
197            break
198        end
199    end
200    %look for the vector color scalar (ancillary)
201    for ivar=ind_ancillary
202        DimCell=Data.VarDimName{ivar};
203        if isequal(DimCell,DimCell_var)
204            nbvar=nbvar+1;
205            CellInfo{icell}.VarIndex(nbvar)=ivar;
206            CellInfo{icell}.VarIndex_ancillary=ivar;
207            break
208        end
209    end
210    %look for warnflag
211    for ivar=ind_warnflag
212        DimCell=Data.VarDimName{ivar};
213        if isequal(DimCell,DimCell_var)
214            nbvar=nbvar+1;
215            CellInfo{icell}.VarIndex(nbvar)=ivar;
216            CellInfo{icell}.VarIndex_warnflag=ivar;
217            break
218        end
219    end
220    %look for errorflag
221    for ivar=ind_errorflag
222        DimCell=Data.VarDimName{ivar};
223        if isequal(DimCell,DimCell_var)
224            nbvar=nbvar+1;
225            CellInfo{icell}.VarIndex(nbvar)=ivar;
226            CellInfo{icell}.VarIndex_errorflag=ivar;
227            break
228        end
229    end
230end
231
232%% find coordinates for each cell around field variables, scalars or vectors
233for icell=1:cell_nbre
234    CellInfo{icell}.CoordType='';
235    ind_var=CellInfo{icell}.VarIndex(1);
236    DimCell_var=Data.VarDimName{ind_var};% cell of dimension names for ivar_coord_x(icell)
237    if ~check_var
238        for idim=1:numel(DimCell_var)
239            CellInfo{icell}.DimIndex(idim)=find(strcmp(DimCell_var{idim},Data.ListDimName));
240        end
241    end
242    %look for z scattered coordinates
243    if isempty(ind_coord_z)
244        NbDim(icell)=2;
245        CellInfo{icell}.CoordIndex=[0 0];
246    else
247        NbDim(icell)=3;
248        CellInfo{icell}.CoordIndex=[0 0 0];
249        for ivar=ind_coord_z
250            DimCell=Data.VarDimName{ivar};
251            if isequal(DimCell,DimCell_var)
252                CellInfo{icell}.CoordType='scattered';
253                CellInfo{icell}.CoordIndex(1)=ivar;
254                CellInfo{icell}.ZName=Data.ListVarName{ivar};
255                CellInfo{icell}.ZIndex=ivar;
256                break
257            end
258        end
259    end
260    % look for y coordinate
261    for ivar=ind_coord_y
262        % detect scattered y coordinates, variable with the same dimension(s) as the field variable considered
263        DimCell=Data.VarDimName{ivar};
264        if isequal(DimCell,DimCell_var)
265            CellInfo{icell}.CoordType='scattered';
266            CellInfo{icell}.CoordIndex(NbDim(icell)-1)=ivar;
267            CellInfo{icell}.YName=Data.ListVarName{ivar};
268            CellInfo{icell}.YIndex=ivar;
269            break
270        end
271    end
272   
273    %look for x coordinates
274    if strcmp(CellInfo{icell}.CoordType,'scattered')
275        for ivar=ind_coord_x
276            DimCell=Data.VarDimName{ivar};
277            if isequal(DimCell,DimCell_var)
278                CellInfo{icell}.CoordIndex(NbDim(icell))=ivar;
279                CellInfo{icell}.XName=Data.ListVarName{ivar};
280                CellInfo{icell}.XIndex=ivar;
281                break
282            end
283        end
284    end
285    if isfield(CellInfo{icell},'ZName')
286        if isfield(CellInfo{icell},'YName')&& isfield(CellInfo{icell},'XName')
287            continue %scattered coordinates OK
288        end
289    else
290        if isfield(CellInfo{icell},'YName')
291            if isfield(CellInfo{icell},'XName')
292                NbDim(icell)=2;
293                continue %scattered coordinates OK
294            end
295        else
296            if isfield(CellInfo{icell},'XName'); % only one coordinate x, switch vector field to 1D plot
297                for ind=1:numel(CellInfo{icell}.VarIndex)
298                    Role{CellInfo{icell}.VarIndex(ind)}='coord_y';
299                end
300                continue
301            end
302        end
303    end
304   
305    %look for grid  coordinates
306    if isempty(CellInfo{icell}.CoordType)
307        NbDim(icell)=numel(DimCell_var);
308        CellInfo{icell}.DimOrder=[];
309        if NbDim(icell)==3
310            if strcmp(DimCell_var{3},'rgb')
311                NbDim(icell)=2;% case of color images
312            else
313                %coord z
314                for ivar=ind_coord_z
315                    if check_coord_names(ivar)
316                        DimRank=find(strcmp(Data.VarDimName{ivar},DimCell_var));
317                        check_coord=~isempty(DimRank);
318                    elseif check_coord_raster(ivar)
319                        DimRank=find(strcmp(Data.ListVarName{ivar},DimCell_var));
320                        check_coord=~isempty(DimRank);
321                    end
322                    %                 check_coord= (check_coord_names(ivar) && strcmp(Data.VarDimName{ivar},DimCell_var{1}))||...% coord varbable
323                    %                     (check_coord_raster(ivar) && strcmp(Data.ListVarName{ivar},DimCell_var{1})); % rasrewr coord defined by min and max
324                    if check_coord
325                        CellInfo{icell}.CoordType='grid';
326                        CellInfo{icell}.CoordIndex(1)=ivar;
327                        CellInfo{icell}.ZName=Data.ListVarName{ivar};
328                        CellInfo{icell}.ZIndex=ivar;
329                        CellInfo{icell}.DimOrder=DimRank;
330                        break
331                    end
332                end
333            end
334        end
335        for ivar=ind_coord_y
336            if check_coord_names(ivar)
337                DimRank=find(strcmp(Data.VarDimName{ivar},DimCell_var));
338                check_coord=~isempty(DimRank);
339            elseif check_coord_raster(ivar)
340                DimRank=find(strcmp(Data.ListVarName{ivar},DimCell_var));
341                check_coord=~isempty(DimRank);
342            end
343            %             check_coord= (check_coord_names(ivar) && strcmp(Data.VarDimName{ivar},DimCell_var{NbDim(icell)-1}))||...% coord variable
344            %                 (check_coord_raster(ivar) && strcmp(Data.ListVarName{ivar},DimCell_var{NbDim(icell)-1})); % rasrewr coord defined by min and max
345            if check_coord
346                CellInfo{icell}.CoordType='grid';
347                CellInfo{icell}.CoordIndex(NbDim(icell)-1)=ivar;
348                CellInfo{icell}.YName=Data.ListVarName{ivar};
349                CellInfo{icell}.YIndex=ivar;
350                CellInfo{icell}.DimOrder=[CellInfo{icell}.DimOrder DimRank];
351                break
352            end
353        end
354        for ivar=ind_coord_x
355            if check_coord_names(ivar)
356                DimRank=find(strcmp(Data.VarDimName{ivar},DimCell_var));
357                check_coord=~isempty(DimRank);
358            elseif check_coord_raster(ivar)
359                DimRank=find(strcmp(Data.ListVarName{ivar},DimCell_var));
360                check_coord=~isempty(DimRank);
361            end
362            %             check_coord= (check_coord_names(ivar) && strcmp(Data.VarDimName{ivar},DimCell_var{NbDim(icell)}))||...% coord variable
363            %                 (check_coord_raster(ivar) && strcmp(Data.ListVarName{ivar},DimCell_var{NbDim(icell)})); % raster coord defined by min and max
364            if check_coord
365                CellInfo{icell}.CoordIndex(NbDim(icell))=ivar;
366                CellInfo{icell}.XName=Data.ListVarName{ivar};
367                CellInfo{icell}.XIndex=ivar;
368                CellInfo{icell}.DimOrder=[CellInfo{icell}.DimOrder DimRank];
369                break
370            end
371        end
372    end
373    %look for tps coordinates
374    for ivar=ind_coord_tps
375        DimCell=Data.VarDimName{ivar};
376        if  numel(DimCell)==3 && strcmp(DimCell{1},DimCell_var{1})
377            CellInfo{icell}.CoordType='tps';
378            CellInfo{icell}.CoordIndex=ivar;
379            if check_var
380                NbDim(icell)=size(Data.(Data.ListVarName{ivar}),2);
381            else
382                DimIndex=find(strcmp(Data.VarDimName{ivar},Data.ListDimName));
383                NbDim(icell)= Data.DimValue(DimIndex);
384            end
385            for ivardim=1:numel(Data.VarDimName)
386                if strcmp(Data.VarDimName{ivardim},DimCell{3})
387                    CellInfo{icell}.NbCentres_tps= ivardim;% nbre of sites for each tps subdomain
388                elseif strcmp(Data.VarDimName{ivardim}{1},DimCell{2}) && numel(Data.VarDimName{ivardim})>=3 && strcmp(Data.VarDimName{ivardim}{3},DimCell{3})
389                    CellInfo{icell}.SubRange_tps=ivardim;% subrange definiton for tps
390                end
391            end
392        end
393        break
394    end
395end
396
397%% get number of coordinate points for each cell
398if check_var
399    for icell=1:numel(CellInfo)
400        switch CellInfo{icell}.CoordType
401            case 'scattered'
402                CellInfo{icell}.CoordSize=numel(Data.(CellInfo{icell}.XName));
403            case 'grid'
404                VarName=Data.ListVarName{CellInfo{icell}.VarIndex(1)};
405                if NbDim(icell)==3
406                    CellInfo{icell}.CoordSize=[size(Data.(VarName),3) size(Data.(VarName),2) size(Data.(VarName),1)];
407                else
408                    CellInfo{icell}.CoordSize=[size(Data.(VarName),1) size(Data.(VarName),2)];
409                end
410            case 'tps'
411                NbDim(icell)=size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),2);
412                CellInfo{icell}.CoordSize=size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),1);
413        end
414    end
415else
416    for icell=1:numel(CellInfo)
417        CellInfo{icell}.CoordSize=size(Data.DimValue(CellInfo{icell}.DimIndex));
418    end
419end
420%
421% %% loop on the tps coordinate sets
422%
423%     for icell_tps=1:numel(ind_coord_tps)
424%         check_cell=zeros(1,nbvar);% =1 for the variables selected in the current cell
425%         check_cell(ivar_tps(icell_tps))=1;% mark the coordinate variable as selected
426%         DimCell=Data.VarDimName{ivar_tps(icell_tps)};% dimension names for the current tps coordinate variable
427%         icell=numel(CellInfo)+icell_tps; % new field cell index
428%         CellInfo{icell}.CoordIndex=ivar_tps(icell_tps);% index of the  tps coordinate variable
429%         if numel(DimCell)==3
430%             VarDimName=Data.VarDimName(~check_select);
431%             for ivardim=1:numel(VarDimName)
432%                 if strcmp(VarDimName{ivardim},DimCell{3})
433%                     CellInfo{icell}.NbCentres_tps= ivar_remain(ivardim);% nbre of sites for each tps subdomain
434%                     check_cell(ivar_remain(ivardim))=1;% mark the variable as selected
435%                 elseif strcmp(VarDimName{ivardim}{1},DimCell{2}) && numel(VarDimName{ivardim})>=3 && strcmp(VarDimName{ivardim}{3},DimCell{3})
436%                     CellInfo{icell}.SubRange_tps=ivar_remain(ivardim);% subrange definiton for tps
437%                     check_cell(ivar_remain(ivardim))=1;% mark the variable as selected
438%                 elseif strcmp(VarDimName{ivardim}{1},DimCell{1}) && strcmp(VarDimName{ivardim}{2},DimCell{3})% variable
439%                     check_cell(ivar_remain(ivardim))=1;% mark the variable as selected
440%                 end
441%             end
442%         end
443%         CellInfo{icell}.CoordType='tps';
444%         CellInfo{icell}.VarIndex=find(check_cell);
445%         if check_var
446%             NbDim(icell)=size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),2);
447%             CellInfo{icell}.CoordSize=size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),1)*size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),3);
448%         else
449%             check_index_1= strcmp(DimCell{1},Data.ListDimName);
450%             check_index_2= strcmp(DimCell{2},Data.ListDimName);
451%             NbDim(icell)=Data.DimValue(check_index_2);
452%             if numel(DimCell)>=3
453%                 check_index_3= strcmp(DimCell{3},Data.ListDimName);
454%                 CellInfo{icell}.CoordSize=Data.DimValue(check_index_1)*Data.DimValue(check_index_3);
455%             end
456%         end
457%         check_select=check_select|check_cell;
458%     end
459
460
461%% cell for ordinary plots
462iremove=false(1,numel(ind_coord_y));
463for ilist=1:numel(ind_coord_y)% remove the y coordinates which have been used yet in scalar or vector fields
464    for icell=1:numel(CellInfo)
465        if isfield(CellInfo{icell},'YIndex')&& isequal(CellInfo{icell}.YIndex,ind_coord_y(ilist))
466            iremove(ilist)=true;
467            continue
468        end
469    end
470end
471ind_coord_y(iremove)=[];
472if ~isempty(ind_coord_x)
473    y_nbre=zeros(1,numel(ind_coord_x));
474    for icell=1:numel(ind_coord_x)
475        Cell1DPlot{icell}.VarType='1DPlot';
476        Cell1DPlot{icell}.XIndex=ind_coord_x(icell);
477        Cell1DPlot{icell}.XName=Data.ListVarName{ind_coord_x(icell)};
478        Cell1DPlot{icell}.YIndex=[];
479        Cell1DPlot{icell}.YIndex_discrete=[];
480        DimCell_x=Data.VarDimName{ind_coord_x(icell)};
481        for ivar=[ind_coord_y ind_histo]
482            DimCell=Data.VarDimName{ivar};
483            if  numel(DimCell_x)==1 && strcmp(DimCell_x{1},DimCell{1})
484                y_nbre(icell)=y_nbre(icell)+1;
485                Cell1DPlot{icell}.YIndex(y_nbre(icell))=ivar;
486            end
487        end
488        for ivar=ind_discrete
489            DimCell=Data.VarDimName{ivar};
490            if  numel(DimCell)==1 && strcmp(DimCell_x{1},DimCell{1})
491                y_nbre(icell)=y_nbre(icell)+1;
492                Cell1DPlot{icell}.YIndex_discrete(y_nbre(icell))=ivar;
493            end
494        end
495    end
496    Cell1DPlot(y_nbre==0)=[];
497    CellInfo=[CellInfo Cell1DPlot];
498    NbDim=[NbDim ones(1,numel(Cell1DPlot))];
499end
500
501%% document roles of non-coordinate variables
502for icell=1:numel(CellInfo)
503    if isfield(CellInfo{icell},'VarIndex')
504        VarIndex=CellInfo{icell}.VarIndex;
505        for ivar=VarIndex
506            %         if isfield(CellInfo{icell},['VarIndex_' Role{ivar}])
507            %             CellInfo{icell}.(['VarIndex_' Role{ivar}])=[CellInfo{icell}.(['VarIndex_' Role{ivar}]) ivar];
508            %         else
509            %             CellInfo{icell}.(['VarIndex_' Role{ivar}])= ivar;
510            %         end
511            if ~isempty(ProjModeRequest{ivar})
512                CellInfo{icell}.ProjModeRequest=ProjModeRequest{ivar};
513            end
514            if ~isempty(FieldName{ivar})
515                CellInfo{icell}.FieldName=FieldName{ivar};
516            end
517            if CheckSub(ivar)==1
518                CellInfo{icell}.CheckSub=1;
519            end
520        end
521    end
522end
523% for icell=ind_coord_tps
524%     VarIndex=CellInfo{icell}.VarIndex;
525%     for ivar=VarIndex
526%         if isfield(CellInfo{icell},['VarIndex_' Role{ivar}])
527%             CellInfo{icell}.(['VarIndex_' Role{ivar}])=[CellInfo{icell}.(['VarIndex_' Role{ivar}]) ivar];
528%         else
529%             CellInfo{icell}.(['VarIndex_' Role{ivar}])= ivar;
530%         end
531%         if ~isempty(ProjModeRequest{ivar})
532%             CellInfo{icell}.ProjModeRequest=ProjModeRequest{ivar};
533%         end
534%         if ~isempty(FieldName{ivar})
535%             CellInfo{icell}.FieldName=FieldName{ivar};
536%         end
537%         if CheckSub(ivar)==1
538%             CellInfo{icell}.CheckSub=1;
539%         end
540%     end
541% end
542
543
544
545%
546% %% analyse vector fields
547% if ~isempty(ind_vector_x) && ~isempty(ind_vector_y)
548%     if numel(ind_vector_x)>1
549%         errormsg='multiply defined vector x component'
550%         return
551%     end
552%     DimCell_vec=Data.VarDimName{ind_vector_x};% cell of dimension names for ivar_coord_x(icell)
553%     if ischar(DimCell),DimCell={DimCell};end % transform char to cell for a single dimension
554%     DimCell_y=Data.VarDimName{ind_vector_y};% cell of dimension names for ivar_coord_x(icell)
555%     if ischar(DimCell_y),DimCell_y={DimCell_y};end % transform char to cell for a single dimension
556%     if ~isequal(DimCell,DimCell_y)
557%         errormsg='inconsistent x and y vector components';
558%         return
559%     end
560%     %look for coordinates
561%     for ivar=ind_coord_y
562%         DimCell=Data.VarDimName{ivar};
563%         if ischar(DimCell),DimCell={DimCell};end % transform char to cell for a single dimension
564%         if isequal(DimCell,DimCell_vec)
565%             CoordType='scattered';
566%             coordy=ivar;
567%         else
568%             if isempty(ind_coord_z) && strcmp(DimCell{1},DimCell_vec{1})
569%                 CoordType='grid';
570%                 coordy=ivar;
571%             elseif ~isempty(ind_coord_z) && strcmp(DimCell{1},DimCell_vec{2})
572%                 CoordType='grid';
573%                 coordy=ivar;
574%                 coordz=ind_coord_z;
575%             end
576%         end
577%         
578%         %% find scattered (unstructured) coordinates
579%         ivar_coord_x=find(strcmp('coord_x',Role));%find variables with Role='coord_x'
580%         check_select=false(1,nbvar);
581%         check_coord=false(1,nbvar);
582%         CellInfo=cell(1,numel(ivar_coord_x));
583%         NbDim=zeros(1,numel(ivar_coord_x));
584%         % loop on unstructured coordinate x -> different field cells
585%         for icell=1:numel(ivar_coord_x)
586%             DimCell=Data.VarDimName{ivar_coord_x(icell)};% cell of dimension names for ivar_coord_x(icell)
587%             if ischar(DimCell),DimCell={DimCell};end % transform char to cell for a single dimension
588%             % look for variables sharing dimension(s) with ivar_coord_x(icell)
589%             check_cell=zeros(numel(DimCell),nbvar);
590%             for idim=1:numel(DimCell);% for each variable with role coord_x, look at which other variables contain the same dimension
591%                 for ivar=1:nbvar
592%                     check_cell(idim,ivar)=max(strcmp(DimCell{idim},Data.VarDimName{ivar}));
593%                 end
594%             end
595%             check_cell=sum(check_cell,1)==numel(DimCell);%logical array=1 for variables belonging to the current cell
596%             VarIndex=find(check_cell);% list of detected variable indices
597%             if ~(numel(VarIndex)==1 && numel(DimCell)==1)% exclude case of isolated coord_x variable (treated later)
598%                 if ~(numel(VarIndex)==1 && numel(DimCell)>1)% a variable is associated to coordinate
599%                     CellInfo{icell}.CoordIndex=ivar_coord_x(icell);
600%                     % size of coordinate var
601%                     if check_var
602%                         CellInfo{icell}.CoordSize=numel(Data.(Data.ListVarName{ivar_coord_x(icell)}));
603%                     else
604%                         for idim=1:numel(DimCell)
605%                             check_index= strcmp(DimCell{idim},Data.ListDimName);
606%                             CellInfo{icell}.CoordSize(idim)=Data.DimValue(check_index);
607%                         end
608%                         CellInfo{icell}.CoordSize=prod(CellInfo{icell}.CoordSize);
609%                     end
610%                     %             ind_scalar=find(strcmp('scalar',Role(VarIndex)));
611%                     %             ind_vector_x=find(strcmp('vector_x',Role(VarIndex)));
612%                     %             ind_vector_y=find(strcmp('vector_y',Role(VarIndex)));
613%                     ind_y=find(strcmp('coord_y',Role(VarIndex)));
614%                     if numel([ind_scalar ind_vector_x ind_vector_y])==0
615%                         %             if numel(VarIndex)==2||isempty(ind_y)% no variable, except possibly y
616%                         NbDim(icell)=1;
617%                     else
618%                         CellInfo{icell}.CoordType='scattered';
619%                         ind_z=find(strcmp('coord_z',Role(VarIndex)));
620%                         if numel(VarIndex)==3||isempty(ind_z)% no z variable, except possibly as a fct z(x,y)
621%                             CellInfo{icell}.CoordIndex=[VarIndex(ind_y) CellInfo{icell}.CoordIndex];
622%                             NbDim(icell)=2;
623%                         else
624%                             CellInfo{icell}.CoordIndex=[VarIndex(ind_z) CellInfo{icell}.CoordIndex];
625%                             NbDim(icell)=3;
626%                         end
627%                     end
628%                 end
629%                 CellInfo{icell}.VarIndex=VarIndex;
630%                 check_select=check_select|check_cell;
631%             end
632%         end
633%         
634%         %% look for tps coordinates
635%         ivar_remain=find(~check_select);% indices of remaining variables (not already selected)
636%         check_coord_tps= strcmp('coord_tps',Role(~check_select));
637%         ivar_tps=ivar_remain(check_coord_tps);% variable indices corresponding to tps coordinates
638%         
639%         % loop on the tps coordinate sets
640%         for icell_tps=1:numel(ivar_tps)
641%             check_cell=zeros(1,nbvar);% =1 for the variables selected in the current cell
642%             check_cell(ivar_tps(icell_tps))=1;% mark the coordinate variable as selected
643%             DimCell=Data.VarDimName{ivar_tps(icell_tps)};% dimension names for the current tps coordinate variable
644%             icell=numel(CellInfo)+icell_tps; % new field cell index
645%             CellInfo{icell}.CoordIndex=ivar_tps(icell_tps);% index of the  tps coordinate variable
646%             if numel(DimCell)==3
647%                 VarDimName=Data.VarDimName(~check_select);
648%                 for ivardim=1:numel(VarDimName)
649%                     if strcmp(VarDimName{ivardim},DimCell{3})
650%                         CellInfo{icell}.NbCentres_tps= ivar_remain(ivardim);% nbre of sites for each tps subdomain
651%                         check_cell(ivar_remain(ivardim))=1;% mark the variable as selected
652%                     elseif strcmp(VarDimName{ivardim}{1},DimCell{2}) && numel(VarDimName{ivardim})>=3 && strcmp(VarDimName{ivardim}{3},DimCell{3})
653%                         CellInfo{icell}.SubRange_tps=ivar_remain(ivardim);% subrange definiton for tps
654%                         check_cell(ivar_remain(ivardim))=1;% mark the variable as selected
655%                     elseif strcmp(VarDimName{ivardim}{1},DimCell{1}) && strcmp(VarDimName{ivardim}{2},DimCell{3})% variable
656%                         check_cell(ivar_remain(ivardim))=1;% mark the variable as selected
657%                     end
658%                 end
659%             end
660%             CellInfo{icell}.CoordType='tps';
661%             CellInfo{icell}.VarIndex=find(check_cell);
662%             if check_var
663%                 NbDim(icell)=size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),2);
664%                 CellInfo{icell}.CoordSize=size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),1)*size(Data.(Data.ListVarName{CellInfo{icell}.CoordIndex}),3);
665%             else
666%                 check_index_1= strcmp(DimCell{1},Data.ListDimName);
667%                 check_index_2= strcmp(DimCell{2},Data.ListDimName);
668%                 NbDim(icell)=Data.DimValue(check_index_2);
669%                 if numel(DimCell)>=3
670%                     check_index_3= strcmp(DimCell{3},Data.ListDimName);
671%                     CellInfo{icell}.CoordSize=Data.DimValue(check_index_1)*Data.DimValue(check_index_3);
672%                 end
673%             end
674%             check_select=check_select|check_cell;
675%         end
676%         
677%     
678%         
679%         % determine dimension sizes
680%         CoordSize=zeros(size(ListCoordIndex));
681%         for ilist=1:numel(ListCoordIndex)
682%             if iscell(ListDimName{ilist})
683%                 ListDimName(ilist)=ListDimName{ilist};%transform cell to string
684%             end
685%             if check_var% if the list of dimensions has been directly defined, no variable data available
686%                 CoordSize(ilist)=numel(Data.(ListCoordName{ilist}));% number of elements in the variable corresponding to the dimension #ilist
687%             else
688%                 check_index= strcmp(ListDimName{ilist},Data.ListDimName);% find the  index in the list of dimensions
689%                 CoordSize(ilist)=Data.DimValue(check_index);% find the  corresponding dimension value
690%             end
691%             if CoordSize(ilist)==2% case of uniform grid coordinate defined by lower and upper bounds only
692%                 ListDimName{ilist}=ListCoordName{ilist};% replace the dimension name by the coordinate variable name
693%             end
694%         end
695%     end
696% end
697%
698%
699%
700% %% suppress empty cells or cells with a single coordinate variable
701% check_remove=false(size(CellInfo));
702% for icell=1:numel(check_remove)
703%     if isempty(CellInfo{icell})||(numel(CellInfo{icell}.VarIndex)==1 && numel(check_coord)>=icell && check_coord(icell))
704%         check_remove(icell)=1;
705%     end
706% end
707% CellInfo(check_remove)=[];
708% NbDim(check_remove)=[];
709%
710%
Note: See TracBrowser for help on using the repository browser.