source: trunk/src/sub_field.m @ 866

Last change on this file since 866 was 866, checked in by sommeria, 9 years ago

stereo_civ_updated

File size: 10.8 KB
RevLine 
[179]1%'sub_field': combines two input fields
2%
3% the two fields are subtstracted when of the same nature (scalar or
4% vector), if the coordinates do not coincide, the second field is
5% interpolated on the cooridintes of the first one
6%
7% when scalar and vectors are combined, the fields are just merged in a single matlab structure for common visualisation
[8]8%-----------------------------------------------------------------------
[515]9% function SubData=sub_field(Field,XmlData,Field_1)
[8]10%
[811]11% OUTPUT:
[8]12% SubData: structure representing the resulting field
13%
14% INPUT:
[179]15% Field: matlab structure representing the first field
16% Field_1:matlab structure representing the second field
[8]17
[809]18%=======================================================================
19% Copyright 2008-2014, LEGI UMR 5519 / CNRS UJF G-INP, Grenoble, France
20%   http://www.legi.grenoble-inp.fr
21%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
22%
23%     This file is part of the toolbox UVMAT.
24%
25%     UVMAT is free software; you can redistribute it and/or modify
26%     it under the terms of the GNU General Public License as published
27%     by the Free Software Foundation; either version 2 of the license,
28%     or (at your option) any later version.
29%
30%     UVMAT is distributed in the hope that it will be useful,
31%     but WITHOUT ANY WARRANTY; without even the implied warranty of
32%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33%     GNU General Public License (see LICENSE.txt) for more details.
34%=======================================================================
35
[515]36function SubData=sub_field(Field,XmlData,Field_1)
[405]37
[515]38SubData=[];
39if strcmp(Field,'*')
40    return
41end
42if nargin<3
43    SubData=Field;
44    return
45end
[674]46
[405]47%% global attributes
[515]48SubData.ListGlobalAttribute={};%default
49%transfer global attributes of Field
[8]50if isfield(Field,'ListGlobalAttribute')
51    SubData.ListGlobalAttribute=Field.ListGlobalAttribute;
52    for ilist=1:numel(Field.ListGlobalAttribute)
53        AttrName=Field.ListGlobalAttribute{ilist};
[405]54        SubData.(AttrName)=Field.(AttrName);
[8]55    end
56end
[515]57%transfer global attributes of Field_1
[8]58if isfield(Field_1,'ListGlobalAttribute')
59    for ilist=1:numel(Field_1.ListGlobalAttribute)
60        AttrName=Field_1.ListGlobalAttribute{ilist};
[515]61        AttrNameNew=AttrName;
62        while ~isempty(find(strcmp(AttrNameNew,SubData.ListGlobalAttribute)))&&~isequal(Field_1.(AttrNameNew),Field.(AttrNameNew))
63            AttrNameNew=[AttrNameNew '_1'];
[8]64        end
[515]65        if ~isfield(Field,AttrName) || ~isequal(Field_1.(AttrName),Field.(AttrName))
[548]66            SubData.ListGlobalAttribute=[SubData.ListGlobalAttribute {AttrNameNew}];
67            SubData.(AttrNameNew)=Field_1.(AttrName);
[8]68        end
69    end
70end
[405]71
72%% variables
[515]73%reproduce variables of the first field and list its dimensions
[8]74SubData.ListVarName=Field.ListVarName;
75SubData.VarDimName=Field.VarDimName;
76if isfield(Field,'VarAttribute')
77    SubData.VarAttribute=Field.VarAttribute;
78end
[515]79ListDimName={};
80for ilist=1:numel(Field.ListVarName)
81    VarName=Field.ListVarName{ilist};
82    SubData.(VarName)=Field.(VarName);
83    SubData.VarAttribute{ilist}.CheckSub=0;
84    DimCell=Field.VarDimName{ilist};
85    if ischar(DimCell)
86        DimCell={DimCell};
87    end
88    for idim=1:numel(DimCell)
89        if isempty(find(strcmp(DimCell{idim},ListDimName)))
90            ListDimName=[ListDimName DimCell(idim)];
91        end
92    end
[8]93end
94
[515]95%% field request
[581]96ProjModeRequest=cell(size(Field.ListVarName));
[515]97for ilist=1:numel(Field.VarAttribute)
[581]98    if isfield(Field.VarAttribute{ilist},'ProjModeRequest')
99        ProjModeRequest{ilist}=Field.VarAttribute{ilist}.ProjModeRequest;
[402]100    end
101end
[581]102ProjModeRequest_1=cell(size(Field_1.ListVarName));
[515]103for ilist=1:numel(Field_1.VarAttribute)
[581]104    if isfield(Field_1.VarAttribute{ilist},'ProjModeRequest')
105        ProjModeRequest_1{ilist}=Field_1.VarAttribute{ilist}.ProjModeRequest;
[402]106    end
107end
[8]108
[515]109%% rename the dimensions of the second field if identical to those of the first
110for ilist=1:numel(Field_1.VarDimName)
111    DimCell=Field_1.VarDimName{ilist};
112    if ischar(DimCell)
113        DimCell={DimCell};
114    end
115    for idim=1:numel(DimCell)
116        ind_dim=find(strcmp(DimCell{idim},ListDimName));
117        if ~isempty(ind_dim)
118            if ischar(Field_1.VarDimName{ilist})
119                Field_1.VarDimName{ilist}=Field_1.VarDimName(ilist);
120            end
121            Field_1.VarDimName{ilist}{idim}=[ListDimName{ind_dim} '_1'];
122        end
123    end
[8]124end
125
[580]126%% look for coordinates common to Field in Field_1
[866]127ind_remove=false(size(Field_1.ListVarName));
[674]128% loop on the variables of the second field Field_1
[515]129for ilist=1:numel(Field_1.ListVarName)
[674]130    % case of variable with a single dimension
[866]131    if ~isempty(Field_1.VarAttribute{ilist}) && ~isempty(regexp(Field_1.VarAttribute{ilist}.Role,'^coord'))% if variable with Role coord... is found.
[864]132        OldDimName=Field_1.VarDimName{ilist};
133        if ischar(OldDimName), OldDimName={OldDimName}; end% transform char string to cell if relevant
134        if numel(OldDimName)==1
135            OldDim=Field_1.(Field_1.ListVarName{ilist});% get variable
136            %look for the existence of the variable OldDim in the first field Field
137            for i1=1:numel(Field.ListVarName)
138                if  isequal(Field.(Field.ListVarName{i1}),OldDim) &&...
139                        ((isempty(ProjModeRequest{i1}) && isempty(ProjModeRequest_1{ilist}))  || strcmp(ProjModeRequest{i1},ProjModeRequest_1{ilist}))
140                    ind_remove(ilist)=1;
141                    NewDimName=Field.VarDimName{i1};
142                    if ischar(NewDimName), NewDimName={NewDimName}; end %transform char chain to cell if needed
143                    Field_1.VarDimName=regexprep_r(Field_1.VarDimName,['^' OldDimName{1} '$'],NewDimName{1});% change the var name of Field_1 to the corresponding var name of Field
144                end
[180]145            end
146        end
[8]147    end
148end
[866]149if ~isempty(find(ind_remove, 1))
150Field_1.ListVarName(ind_remove)=[];%removes the redondent coordinate
151Field_1.VarDimName(ind_remove)=[];
152Field_1.VarAttribute(ind_remove)=[];
153end
[8]154
[580]155%% append the other variables of the second field, modifying their name if needed
[866]156ListVarNameSub=Field_1.ListVarName;
157ListVarNameNew=ListVarNameSub;
158check_rename=zeros(size(ListVarNameSub));
159check_remove=zeros(size(ListVarNameSub));
160for ilist=1:numel(ListVarNameSub)
161    ind_prev=find(strcmp(ListVarNameSub{ilist},Field.ListVarName),1);% look for duplicated variable name
[862]162    if ~isempty(ind_prev)% variable name exists in Field
[866]163        if isfield(Field_1.VarAttribute{ilist},'Role')&&...
164            ismember(Field_1.VarAttribute{ilist}.Role,{'coord_x','coord_y','scalar','vector_x','vector_y','errorflag'})
165            ListVarNameNew{ilist}=[ListVarNameSub{ilist} '_1'];%modify the name of the second variable
166            check_rename(ilist)=1;
167        else
168            check_remove(ilist)=1;% variable will be removed
[864]169        end
[8]170    end
[515]171end
[866]172ListVarNameSub=ListVarNameSub(~check_remove); %eliminate removed variables from the list of the second field
173ListVarNameNew=ListVarNameNew(~check_remove); % %list of renaimed varaibles corresponding to ListVarNameSub
174VarDimNameSub=Field_1.VarDimName(~check_remove);
175VarAttributeSub=Field_1.VarAttribute(~check_remove);
176check_rename=check_rename(~check_remove);
[515]177
[866]178% apply the variable renaming and mark the second field variables with the attribute .CheckSub
179for ilist=1:numel(ListVarNameSub)
180     SubData.(ListVarNameNew{ilist})=Field_1.(ListVarNameSub{ilist});% copy the variable content to the new name
181    if check_rename(ilist)   
182          % replace name in field expression FieldName, e.g. 'norm(U,V)'-> 'norm(U_1,V_1)'
183        if  isfield(VarAttributeSub{ilist},'FieldName')
184            for ivar=1:numel(find(check_rename))
185                VarAttributeSub{ilist}.FieldName=regexprep_r(VarAttributeSub{ilist}.FieldName,...
186                    ListVarNameSub{ivar},ListVarNameNew{ivar});
187            end
[862]188        end
189    end
[866]190    VarAttributeSub{ilist}.CheckSub=1;% mark that the field needs to be substracted as an attribute
[862]191end
192
[866]193SubData.ListVarName=[SubData.ListVarName ListVarNameNew];
194SubData.VarDimName=[SubData.VarDimName VarDimNameSub];
195SubData.VarAttribute=[SubData.VarAttribute VarAttributeSub];
196
[515]197%% substrat fields when possible
[580]198[CellInfo,NbDim,errormsg]=find_field_cells(SubData);
[866]199ind_remove=false(size(SubData.ListVarName));
[515]200ivar=[];
201ivar_1=[];
[548]202for icell=1:numel(CellInfo)
203    if ~isempty(CellInfo{icell})
[866]204        % if two scalar are in the same cell
[548]205        if isfield(CellInfo{icell},'VarIndex_scalar') && numel(CellInfo{icell}.VarIndex_scalar)==2 && SubData.VarAttribute{CellInfo{icell}.VarIndex_scalar(2)}.CheckSub;
206            ivar=[ivar CellInfo{icell}.VarIndex_scalar(1)];
207            ivar_1=[ivar_1 CellInfo{icell}.VarIndex_scalar(2)];
[8]208        end
[866]209        % if two vector u components are in the same cell
[548]210        if isfield(CellInfo{icell},'VarIndex_vector_x') && numel(CellInfo{icell}.VarIndex_vector_x)==2 && SubData.VarAttribute{CellInfo{icell}.VarIndex_vector_x(2)}.CheckSub;
211            ivar=[ivar CellInfo{icell}.VarIndex_vector_x(1)];
212            ivar_1=[ivar_1 CellInfo{icell}.VarIndex_vector_x(2)];
[515]213        end
[866]214         % if two vector v components are in the same cell
[548]215        if isfield(CellInfo{icell},'VarIndex_vector_y') && numel(CellInfo{icell}.VarIndex_vector_y)==2 && SubData.VarAttribute{CellInfo{icell}.VarIndex_vector_y(2)}.CheckSub;
216            ivar=[ivar CellInfo{icell}.VarIndex_vector_y(1)];
217            ivar_1=[ivar_1 CellInfo{icell}.VarIndex_vector_y(2)];
[515]218        end
[866]219        % merge the error flags if needed
220        if isfield(CellInfo{icell},'VarIndex_errorflag') && numel(CellInfo{icell}.VarIndex_errorflag)==2 && SubData.VarAttribute{CellInfo{icell}.VarIndex_vector_y(2)}.CheckSub;
221            ivar_flag=CellInfo{icell}.VarIndex_errorflag(1);
222            ivar_flag_1=CellInfo{icell}.VarIndex_errorflag(2);
223            VarName=SubData.ListVarName{ivar_flag};
224            VarName_1=SubData.ListVarName{ivar_flag_1};
225            SubData.(VarName)=SubData.(VarName)~=0 | SubData.(VarName_1)~=0;% combine the error flags of the two fields
226            ind_remove(ivar_flag_1)=1;
227        end
[8]228    end
229end
[866]230% subtract fields if relevant
[515]231for imod=1:numel(ivar)
232        VarName=SubData.ListVarName{ivar(imod)};
233        VarName_1=SubData.ListVarName{ivar_1(imod)};
[548]234        SubData.(VarName)=double(SubData.(VarName))-double(SubData.(VarName_1));
[515]235        ind_remove(ivar_1(imod))=1;
236end
[866]237SubData.ListVarName(ind_remove)=[];
238SubData.VarDimName(ind_remove)=[];
239SubData.VarAttribute(ind_remove)=[];
[520]240
[580]241function OutputCell=regexprep_r(InputCell,search_string,new_string)
[713]242if ischar(InputCell); InputCell={InputCell}; end
[674]243OutputCell=InputCell;%default
[520]244for icell=1:numel(InputCell)
[580]245    OutputCell{icell}=regexprep(InputCell{icell},search_string,new_string);
[520]246end
[515]247       
248   
Note: See TracBrowser for help on using the repository browser.