source: trunk/src/find_field_indices.m @ 405

Last change on this file since 405 was 404, checked in by sommeria, 13 years ago

various bugs corrected. nc2struct_toolbox suppressed (correspond to obsolete versions of Matlab)

File size: 9.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  [CellVarIndex,NbDim,VarType,errormsg]=find_field_indices(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% VarType: 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% errormsg: error message
22%   
23% INPUT:
24% Data: structure representing fields, output of check_field_structure
25%            .ListVarName: cell array listing the names (cahr strings) of the variables
26%            .VarDimName: cell array of cells containing the set of dimension names for each variable of .ListVarName
27%            .VarAttribute: cell array of structures containing the variable attributes:
28%                     .VarAttribute{ilist}.key=value, where ilist is the variable
29%                     index, key is the name of the attribute, value its value (char string or number)
30%
31% HELP:
32% to get the dimensions of arrays common to the field #icell
33%         VarIndex=CellVarIndex{icell}; % list of variable indices
34%         DimIndex=Data.VarDimIndex{VarIndex(1)} % list of dimensions for each variable in the cell #icell
35%
36%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
37%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
38%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
39%     This file is part of the toolbox UVMAT.
40%
41%     UVMAT is free software; you can redistribute it and/or modify
42%     it under the terms of the GNU General Public License as published by
43%     the Free Software Foundation; either version 2 of the License, or
44%     (at your option) any later version.
45%
46%     UVMAT is distributed in the hope that it will be useful,
47%     but WITHOUT ANY WARRANTY; without even the implied warranty of
48%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
49%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
50%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
51
52function [CellVarIndex,NbDim,VarType,errormsg]=find_field_indices(Data)
53CellVarIndex={};
54NbDim=[];
55VarType=[];
56errormsg=[];
57nbvar=numel(Data.ListVarName);%number of field variables
58icell=0;
59ivardim=0;
60VarDimIndex=[];
61VarDimName={};
62if ~isfield(Data,'VarDimName')
63    errormsg='missing .VarDimName';
64    return
65end
66
67%% loop on the list of variables, group them by common dimensions
68for ivar=1:nbvar
69    DimCell=Data.VarDimName{ivar}; %dimensions associated with the variable #ivar
70    if ischar(DimCell)
71        DimCell={DimCell};
72        Data.VarDimName{ivar}={Data.VarDimName{ivar}};%transform char chain into cell
73    end
74    testnewcell=1;
75    for icell_prev=1:numel(CellVarIndex)%detect whether the dimensions of ivar fit with an existing cell
76        PrevVarIndex=CellVarIndex{icell_prev};%list of variable indices in cell # icell_prev
77        PrevDimName=Data.VarDimName{PrevVarIndex(1)};%list of corresponding variable names
78        if isequal(PrevDimName,DimCell)
79            CellVarIndex{icell_prev}=[CellVarIndex{icell_prev} ivar];% add variable index #ivar to the cell #icell_prev
80            testnewcell=0; %existing cell detected
81            break
82        end
83    end
84    if testnewcell
85        icell=icell+1;
86        CellVarIndex{icell}=ivar;%put the current variabl index in the new cell
87    end
88   
89    %look for dimension variables
90    if numel(DimCell)==1% if the variable has a single dimension
91        Role='';
92        if isfield(Data,'VarAttribute') && length(Data.VarAttribute)>=ivar && isfield(Data.VarAttribute{ivar},'Role')
93            Role=Data.VarAttribute{ivar}.Role;
94        end
95        if strcmp(DimCell{1},Data.ListVarName{ivar}) || strcmp(Role,'dimvar')
96            ivardim=ivardim+1;
97            VarDimIndex(ivardim)=ivar;%index of the variable
98            VarDimName{ivardim}=DimCell{1};%name of the dimension
99        end
100    end
101end
102
103%% find the spatial dimensions and vector components
104ListRole={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','warnflag','errorflag',...
105    'ancillary','image','color','discrete','scalar','coord_tps'};
106NbDim=zeros(size(CellVarIndex));%default
107for icell=1:length(CellVarIndex)
108    for ilist=1:numel(ListRole)
109        eval(['ivar_' ListRole{ilist} '=[];'])
110    end
111    VarIndex=CellVarIndex{icell};%set of variable indices with the same dim
112    DimCell=Data.VarDimName{VarIndex(1)};% list of dimensions for each variable in the cell #icell
113    if isfield(Data,'VarAttribute');
114        VarAttribute=Data.VarAttribute;
115    else
116        VarAttribute={};
117    end
118    test_2D=0;
119    for ivar=VarIndex
120        if length(VarAttribute)>=ivar
121            if isfield(VarAttribute{ivar},'Role')
122                role=VarAttribute{ivar}.Role;
123                switch role
124                    case ListRole
125                        eval(['ivar_' role '=[ivar_' role ' ivar];'])
126                    otherwise
127                       ivar_scalar=[ivar_scalar ivar];%variables are consiered as 'scalar' by default (in the absence of attribute 'Role')
128                end
129            else
130              ivar_scalar=[ivar_scalar ivar];% variable considered as scalar in the absence of Role attribute 
131            end
132            if isfield(VarAttribute{ivar},'Coord_2')
133                test_2D=1; %obsolete convention
134            end
135        else
136            ivar_scalar=[ivar_scalar ivar];%variables are consiered as 'scalar' by default (in the absence of attribute 'Role')
137        end
138    end
139    for ilist=1:numel(ListRole)
140        eval(['VarType{icell}.' ListRole{ilist} '=ivar_' ListRole{ilist} ';'])
141    end   
142    if numel(ivar_coord_x)>1 || numel(ivar_coord_y)>1 || numel(ivar_coord_z)>1
143        errormsg='multiply defined coordinates  in the same cell';
144        return
145    end
146    if numel(ivar_errorflag)>1
147        errormsg='multiply defined error flag in the same cell';
148        return
149    end
150    if numel(ivar_warnflag)>1
151        errormsg='multiply defined warning flag in the same cell';
152        return
153    end
154    test_coord=0;
155    if numel(VarIndex)>1     
156        if ~isempty(ivar_coord_z)
157            NbDim(icell)=3;
158            test_coord=1;
159        elseif ~isempty(ivar_coord_y)
160            NbDim(icell)=2;
161            test_coord=1;
162        elseif ~isempty(ivar_coord_x)
163            NbDim(icell)=1;
164            test_coord=1;
165        end
166    end
167    % look at coordinates variables 
168    coord=zeros(1,numel(DimCell));%default
169%     if NbDim(icell)==0 && ~isempty(VarDimName)% no unstructured coordinate found
170    if  ~test_coord && ~isempty(VarDimName)
171        for idim=1:numel(DimCell)   %loop on the dimensions of the variables in cell #icell
172            for ivardim=1:numel(VarDimName)
173                if strcmp(VarDimName{ivardim},DimCell{idim})
174                    coord(idim)=VarDimIndex(ivardim);
175                    break
176                end
177            end
178        end
179        NbDim(icell)=numel(find(coord)); 
180    end 
181    VarType{icell}.coord=coord;
182    if NbDim(icell)==0 && test_2D %look at attributes Coord_1, coord_2 (obsolete convention)
183        NbDim(icell)=2;
184    end
185    %look for tps data
186    if ~isempty(VarType{icell}.coord_tps)
187        VarType{icell}.var_tps=[];
188        tps_dimnames=Data.VarDimName{VarType{icell}.coord_tps};
189        if length(tps_dimnames)==3
190            for ilist=1:length(Data.VarDimName)
191                if strcmp(tps_dimnames{1},Data.VarDimName{ilist}{1}) && strcmp(tps_dimnames{3},Data.VarDimName{ilist}{2})% identify the variables corresponding to the tps site coordinates coord_tps
192                    VarType{icell}.var_tps=[VarType{icell}.var_tps ilist];
193                elseif length(Data.VarDimName{ilist})==1 && strcmp(tps_dimnames{3},Data.VarDimName{ilist}{1})% identify the variable corresponding to nbsites
194                    VarType{icell}.nbsites_tps= ilist;
195                elseif length(Data.VarDimName{ilist})==3 && strcmp(tps_dimnames{2},Data.VarDimName{ilist}{1})&& strcmp(tps_dimnames{3},Data.VarDimName{ilist}{3})% identify the variable subrange
196                    VarType{icell}.subrange_tps= ilist;
197                end
198            end
199        end
200        NbDim(icell)=2;
201    end 
202end
Note: See TracBrowser for help on using the repository browser.