source: trunk/src/sub_field.m @ 887

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

deformation modified in civ_series and bug corrections

File size: 11.0 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
[876]46if ~isfield(Field_1,'VarAttribute')
47    Field_1.VarAttribute={};
48end
[674]49
[405]50%% global attributes
[515]51SubData.ListGlobalAttribute={};%default
52%transfer global attributes of Field
[8]53if isfield(Field,'ListGlobalAttribute')
54    SubData.ListGlobalAttribute=Field.ListGlobalAttribute;
55    for ilist=1:numel(Field.ListGlobalAttribute)
56        AttrName=Field.ListGlobalAttribute{ilist};
[405]57        SubData.(AttrName)=Field.(AttrName);
[8]58    end
59end
[515]60%transfer global attributes of Field_1
[8]61if isfield(Field_1,'ListGlobalAttribute')
62    for ilist=1:numel(Field_1.ListGlobalAttribute)
63        AttrName=Field_1.ListGlobalAttribute{ilist};
[515]64        AttrNameNew=AttrName;
65        while ~isempty(find(strcmp(AttrNameNew,SubData.ListGlobalAttribute)))&&~isequal(Field_1.(AttrNameNew),Field.(AttrNameNew))
66            AttrNameNew=[AttrNameNew '_1'];
[8]67        end
[515]68        if ~isfield(Field,AttrName) || ~isequal(Field_1.(AttrName),Field.(AttrName))
[548]69            SubData.ListGlobalAttribute=[SubData.ListGlobalAttribute {AttrNameNew}];
70            SubData.(AttrNameNew)=Field_1.(AttrName);
[8]71        end
72    end
73end
[405]74
75%% variables
[515]76%reproduce variables of the first field and list its dimensions
[8]77SubData.ListVarName=Field.ListVarName;
78SubData.VarDimName=Field.VarDimName;
79if isfield(Field,'VarAttribute')
80    SubData.VarAttribute=Field.VarAttribute;
81end
[515]82ListDimName={};
83for ilist=1:numel(Field.ListVarName)
84    VarName=Field.ListVarName{ilist};
85    SubData.(VarName)=Field.(VarName);
86    SubData.VarAttribute{ilist}.CheckSub=0;
87    DimCell=Field.VarDimName{ilist};
88    if ischar(DimCell)
89        DimCell={DimCell};
90    end
91    for idim=1:numel(DimCell)
92        if isempty(find(strcmp(DimCell{idim},ListDimName)))
93            ListDimName=[ListDimName DimCell(idim)];
94        end
95    end
[8]96end
97
[515]98%% field request
[581]99ProjModeRequest=cell(size(Field.ListVarName));
[515]100for ilist=1:numel(Field.VarAttribute)
[581]101    if isfield(Field.VarAttribute{ilist},'ProjModeRequest')
102        ProjModeRequest{ilist}=Field.VarAttribute{ilist}.ProjModeRequest;
[402]103    end
104end
[581]105ProjModeRequest_1=cell(size(Field_1.ListVarName));
[515]106for ilist=1:numel(Field_1.VarAttribute)
[581]107    if isfield(Field_1.VarAttribute{ilist},'ProjModeRequest')
108        ProjModeRequest_1{ilist}=Field_1.VarAttribute{ilist}.ProjModeRequest;
[402]109    end
110end
[8]111
[876]112
[515]113%% rename the dimensions of the second field if identical to those of the first
114for ilist=1:numel(Field_1.VarDimName)
115    DimCell=Field_1.VarDimName{ilist};
116    if ischar(DimCell)
117        DimCell={DimCell};
118    end
119    for idim=1:numel(DimCell)
120        ind_dim=find(strcmp(DimCell{idim},ListDimName));
121        if ~isempty(ind_dim)
122            if ischar(Field_1.VarDimName{ilist})
123                Field_1.VarDimName{ilist}=Field_1.VarDimName(ilist);
124            end
125            Field_1.VarDimName{ilist}{idim}=[ListDimName{ind_dim} '_1'];
126        end
127    end
[8]128end
129
[580]130%% look for coordinates common to Field in Field_1
[866]131ind_remove=false(size(Field_1.ListVarName));
[674]132% loop on the variables of the second field Field_1
[876]133for ilist=1:numel(Field_1.VarAttribute)
[674]134    % case of variable with a single dimension
[866]135    if ~isempty(Field_1.VarAttribute{ilist}) && ~isempty(regexp(Field_1.VarAttribute{ilist}.Role,'^coord'))% if variable with Role coord... is found.
[864]136        OldDimName=Field_1.VarDimName{ilist};
137        if ischar(OldDimName), OldDimName={OldDimName}; end% transform char string to cell if relevant
138        if numel(OldDimName)==1
139            OldDim=Field_1.(Field_1.ListVarName{ilist});% get variable
140            %look for the existence of the variable OldDim in the first field Field
141            for i1=1:numel(Field.ListVarName)
142                if  isequal(Field.(Field.ListVarName{i1}),OldDim) &&...
143                        ((isempty(ProjModeRequest{i1}) && isempty(ProjModeRequest_1{ilist}))  || strcmp(ProjModeRequest{i1},ProjModeRequest_1{ilist}))
144                    ind_remove(ilist)=1;
145                    NewDimName=Field.VarDimName{i1};
146                    if ischar(NewDimName), NewDimName={NewDimName}; end %transform char chain to cell if needed
147                    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
148                end
[180]149            end
150        end
[8]151    end
152end
[866]153if ~isempty(find(ind_remove, 1))
154Field_1.ListVarName(ind_remove)=[];%removes the redondent coordinate
155Field_1.VarDimName(ind_remove)=[];
156Field_1.VarAttribute(ind_remove)=[];
157end
[8]158
[580]159%% append the other variables of the second field, modifying their name if needed
[866]160ListVarNameSub=Field_1.ListVarName;
161ListVarNameNew=ListVarNameSub;
162check_rename=zeros(size(ListVarNameSub));
163check_remove=zeros(size(ListVarNameSub));
164for ilist=1:numel(ListVarNameSub)
165    ind_prev=find(strcmp(ListVarNameSub{ilist},Field.ListVarName),1);% look for duplicated variable name
[862]166    if ~isempty(ind_prev)% variable name exists in Field
[866]167        if isfield(Field_1.VarAttribute{ilist},'Role')&&...
168            ismember(Field_1.VarAttribute{ilist}.Role,{'coord_x','coord_y','scalar','vector_x','vector_y','errorflag'})
169            ListVarNameNew{ilist}=[ListVarNameSub{ilist} '_1'];%modify the name of the second variable
170            check_rename(ilist)=1;
171        else
172            check_remove(ilist)=1;% variable will be removed
[864]173        end
[8]174    end
[515]175end
[866]176ListVarNameSub=ListVarNameSub(~check_remove); %eliminate removed variables from the list of the second field
177ListVarNameNew=ListVarNameNew(~check_remove); % %list of renaimed varaibles corresponding to ListVarNameSub
178VarDimNameSub=Field_1.VarDimName(~check_remove);
[876]179if numel(Field_1.VarAttribute)<max(find(~check_remove))
180    for ilist=numel(Field_1.VarAttribute)+1:max(find(~check_remove))
181        Field_1.VarAttribute{ilist}={};
182    end
183end
[866]184VarAttributeSub=Field_1.VarAttribute(~check_remove);
185check_rename=check_rename(~check_remove);
[515]186
[866]187% apply the variable renaming and mark the second field variables with the attribute .CheckSub
188for ilist=1:numel(ListVarNameSub)
189     SubData.(ListVarNameNew{ilist})=Field_1.(ListVarNameSub{ilist});% copy the variable content to the new name
190    if check_rename(ilist)   
191          % replace name in field expression FieldName, e.g. 'norm(U,V)'-> 'norm(U_1,V_1)'
192        if  isfield(VarAttributeSub{ilist},'FieldName')
193            for ivar=1:numel(find(check_rename))
194                VarAttributeSub{ilist}.FieldName=regexprep_r(VarAttributeSub{ilist}.FieldName,...
195                    ListVarNameSub{ivar},ListVarNameNew{ivar});
196            end
[862]197        end
198    end
[866]199    VarAttributeSub{ilist}.CheckSub=1;% mark that the field needs to be substracted as an attribute
[862]200end
201
[866]202SubData.ListVarName=[SubData.ListVarName ListVarNameNew];
203SubData.VarDimName=[SubData.VarDimName VarDimNameSub];
204SubData.VarAttribute=[SubData.VarAttribute VarAttributeSub];
205
[515]206%% substrat fields when possible
[580]207[CellInfo,NbDim,errormsg]=find_field_cells(SubData);
[866]208ind_remove=false(size(SubData.ListVarName));
[515]209ivar=[];
210ivar_1=[];
[548]211for icell=1:numel(CellInfo)
212    if ~isempty(CellInfo{icell})
[866]213        % if two scalar are in the same cell
[548]214        if isfield(CellInfo{icell},'VarIndex_scalar') && numel(CellInfo{icell}.VarIndex_scalar)==2 && SubData.VarAttribute{CellInfo{icell}.VarIndex_scalar(2)}.CheckSub;
215            ivar=[ivar CellInfo{icell}.VarIndex_scalar(1)];
216            ivar_1=[ivar_1 CellInfo{icell}.VarIndex_scalar(2)];
[8]217        end
[866]218        % if two vector u components are in the same cell
[548]219        if isfield(CellInfo{icell},'VarIndex_vector_x') && numel(CellInfo{icell}.VarIndex_vector_x)==2 && SubData.VarAttribute{CellInfo{icell}.VarIndex_vector_x(2)}.CheckSub;
220            ivar=[ivar CellInfo{icell}.VarIndex_vector_x(1)];
221            ivar_1=[ivar_1 CellInfo{icell}.VarIndex_vector_x(2)];
[515]222        end
[866]223         % if two vector v components are in the same cell
[548]224        if isfield(CellInfo{icell},'VarIndex_vector_y') && numel(CellInfo{icell}.VarIndex_vector_y)==2 && SubData.VarAttribute{CellInfo{icell}.VarIndex_vector_y(2)}.CheckSub;
225            ivar=[ivar CellInfo{icell}.VarIndex_vector_y(1)];
226            ivar_1=[ivar_1 CellInfo{icell}.VarIndex_vector_y(2)];
[515]227        end
[866]228        % merge the error flags if needed
229        if isfield(CellInfo{icell},'VarIndex_errorflag') && numel(CellInfo{icell}.VarIndex_errorflag)==2 && SubData.VarAttribute{CellInfo{icell}.VarIndex_vector_y(2)}.CheckSub;
230            ivar_flag=CellInfo{icell}.VarIndex_errorflag(1);
231            ivar_flag_1=CellInfo{icell}.VarIndex_errorflag(2);
232            VarName=SubData.ListVarName{ivar_flag};
233            VarName_1=SubData.ListVarName{ivar_flag_1};
234            SubData.(VarName)=SubData.(VarName)~=0 | SubData.(VarName_1)~=0;% combine the error flags of the two fields
235            ind_remove(ivar_flag_1)=1;
236        end
[8]237    end
238end
[866]239% subtract fields if relevant
[515]240for imod=1:numel(ivar)
241        VarName=SubData.ListVarName{ivar(imod)};
242        VarName_1=SubData.ListVarName{ivar_1(imod)};
[548]243        SubData.(VarName)=double(SubData.(VarName))-double(SubData.(VarName_1));
[515]244        ind_remove(ivar_1(imod))=1;
245end
[866]246SubData.ListVarName(ind_remove)=[];
247SubData.VarDimName(ind_remove)=[];
248SubData.VarAttribute(ind_remove)=[];
[520]249
[580]250function OutputCell=regexprep_r(InputCell,search_string,new_string)
[713]251if ischar(InputCell); InputCell={InputCell}; end
[674]252OutputCell=InputCell;%default
[520]253for icell=1:numel(InputCell)
[580]254    OutputCell{icell}=regexprep(InputCell{icell},search_string,new_string);
[520]255end
[515]256       
257   
Note: See TracBrowser for help on using the repository browser.