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 | %-----------------------------------------------------------------------
|
---|
9 | % function SubData=sub_field(Field,XmlData,Field_1)
|
---|
10 | %
|
---|
11 | % OUPUT:
|
---|
12 | % SubData: structure representing the resulting field
|
---|
13 | %
|
---|
14 | % INPUT:
|
---|
15 | % Field: matlab structure representing the first field
|
---|
16 | % Field_1:matlab structure representing the second field
|
---|
17 |
|
---|
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 |
|
---|
36 | function SubData=sub_field(Field,XmlData,Field_1)
|
---|
37 |
|
---|
38 | SubData=[];
|
---|
39 | if strcmp(Field,'*')
|
---|
40 | return
|
---|
41 | end
|
---|
42 | if nargin<3
|
---|
43 | SubData=Field;
|
---|
44 | return
|
---|
45 | end
|
---|
46 |
|
---|
47 | %% global attributes
|
---|
48 | SubData.ListGlobalAttribute={};%default
|
---|
49 | %transfer global attributes of Field
|
---|
50 | if isfield(Field,'ListGlobalAttribute')
|
---|
51 | SubData.ListGlobalAttribute=Field.ListGlobalAttribute;
|
---|
52 | for ilist=1:numel(Field.ListGlobalAttribute)
|
---|
53 | AttrName=Field.ListGlobalAttribute{ilist};
|
---|
54 | SubData.(AttrName)=Field.(AttrName);
|
---|
55 | end
|
---|
56 | end
|
---|
57 | %transfer global attributes of Field_1
|
---|
58 | if isfield(Field_1,'ListGlobalAttribute')
|
---|
59 | for ilist=1:numel(Field_1.ListGlobalAttribute)
|
---|
60 | AttrName=Field_1.ListGlobalAttribute{ilist};
|
---|
61 | AttrNameNew=AttrName;
|
---|
62 | while ~isempty(find(strcmp(AttrNameNew,SubData.ListGlobalAttribute)))&&~isequal(Field_1.(AttrNameNew),Field.(AttrNameNew))
|
---|
63 | AttrNameNew=[AttrNameNew '_1'];
|
---|
64 | end
|
---|
65 | if ~isfield(Field,AttrName) || ~isequal(Field_1.(AttrName),Field.(AttrName))
|
---|
66 | SubData.ListGlobalAttribute=[SubData.ListGlobalAttribute {AttrNameNew}];
|
---|
67 | SubData.(AttrNameNew)=Field_1.(AttrName);
|
---|
68 | end
|
---|
69 | end
|
---|
70 | end
|
---|
71 |
|
---|
72 | %% variables
|
---|
73 | %reproduce variables of the first field and list its dimensions
|
---|
74 | SubData.ListVarName=Field.ListVarName;
|
---|
75 | SubData.VarDimName=Field.VarDimName;
|
---|
76 | if isfield(Field,'VarAttribute')
|
---|
77 | SubData.VarAttribute=Field.VarAttribute;
|
---|
78 | end
|
---|
79 | ListDimName={};
|
---|
80 | for 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
|
---|
93 | end
|
---|
94 |
|
---|
95 | %% field request
|
---|
96 | ProjModeRequest=cell(size(Field.ListVarName));
|
---|
97 | for ilist=1:numel(Field.VarAttribute)
|
---|
98 | if isfield(Field.VarAttribute{ilist},'ProjModeRequest')
|
---|
99 | ProjModeRequest{ilist}=Field.VarAttribute{ilist}.ProjModeRequest;
|
---|
100 | end
|
---|
101 | end
|
---|
102 | ProjModeRequest_1=cell(size(Field_1.ListVarName));
|
---|
103 | for ilist=1:numel(Field_1.VarAttribute)
|
---|
104 | if isfield(Field_1.VarAttribute{ilist},'ProjModeRequest')
|
---|
105 | ProjModeRequest_1{ilist}=Field_1.VarAttribute{ilist}.ProjModeRequest;
|
---|
106 | end
|
---|
107 | end
|
---|
108 |
|
---|
109 | %% rename the dimensions of the second field if identical to those of the first
|
---|
110 | for 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
|
---|
124 | end
|
---|
125 |
|
---|
126 | %% look for coordinates common to Field in Field_1
|
---|
127 | ind_remove=zeros(size(Field_1.ListVarName));
|
---|
128 | % loop on the variables of the second field Field_1
|
---|
129 | for ilist=1:numel(Field_1.ListVarName)
|
---|
130 | % case of variable with a single dimension
|
---|
131 | OldDimName=Field_1.VarDimName{ilist};
|
---|
132 | if ischar(OldDimName), OldDimName={OldDimName}; end% transform char string to cell if relevant
|
---|
133 | if numel(OldDimName)==1
|
---|
134 | OldDim=Field_1.(Field_1.ListVarName{ilist});% get variable
|
---|
135 | %look for the existence of the variable OldDim in the first field Field
|
---|
136 | for i1=1:numel(Field.ListVarName)
|
---|
137 | if isequal(Field.(Field.ListVarName{i1}),OldDim) &&...
|
---|
138 | ((isempty(ProjModeRequest{i1}) && isempty(ProjModeRequest_1{ilist})) || strcmp(ProjModeRequest{i1},ProjModeRequest_1{ilist}))
|
---|
139 | ind_remove(ilist)=1;
|
---|
140 | NewDimName=Field.VarDimName{i1};
|
---|
141 | if ischar(NewDimName), NewDimName={NewDimName}; end %transform char chain to cell if needed
|
---|
142 | 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
|
---|
143 | end
|
---|
144 | end
|
---|
145 | end
|
---|
146 | end
|
---|
147 | Field_1.ListVarName(find(ind_remove))=[];%removes the redondent coordinate
|
---|
148 | Field_1.VarDimName(find(ind_remove))=[];
|
---|
149 | Field_1.VarAttribute(find(ind_remove))=[];
|
---|
150 |
|
---|
151 | %% append the other variables of the second field, modifying their name if needed
|
---|
152 | for ilist=1:numel(Field_1.ListVarName)
|
---|
153 | VarName=Field_1.ListVarName{ilist};
|
---|
154 | ind_prev=find(strcmp(VarName,Field.ListVarName));
|
---|
155 | if isempty(ind_prev)% variable name does not exist in Field
|
---|
156 | VarNameNew=VarName;
|
---|
157 | else % variable name exists in Field
|
---|
158 | VarNameNew=[VarName '_1'];
|
---|
159 | if isfield(Field_1.VarAttribute{ilist},'FieldName')
|
---|
160 | Field_1.VarAttribute{ilist}.FieldName=regexprep_r(Field_1.VarAttribute{ilist}.FieldName,VarName,VarNameNew);
|
---|
161 | end
|
---|
162 | end
|
---|
163 | SubData.ListVarName=[SubData.ListVarName {VarNameNew}];
|
---|
164 | SubData.VarDimName=[SubData.VarDimName Field_1.VarDimName(ilist)];
|
---|
165 | SubData.(VarNameNew)=Field_1.(VarName);
|
---|
166 | SubData.VarAttribute=[SubData.VarAttribute Field_1.VarAttribute(ilist)];
|
---|
167 | SubData.VarAttribute{end}.CheckSub=1;% mark that the field needs to be substracted
|
---|
168 | end
|
---|
169 |
|
---|
170 | %% substrat fields when possible
|
---|
171 | [CellInfo,NbDim,errormsg]=find_field_cells(SubData);
|
---|
172 | ind_remove=zeros(size(SubData.ListVarName));
|
---|
173 | ivar=[];
|
---|
174 | ivar_1=[];
|
---|
175 | for icell=1:numel(CellInfo)
|
---|
176 | if ~isempty(CellInfo{icell})
|
---|
177 | if isfield(CellInfo{icell},'VarIndex_scalar') && numel(CellInfo{icell}.VarIndex_scalar)==2 && SubData.VarAttribute{CellInfo{icell}.VarIndex_scalar(2)}.CheckSub;
|
---|
178 | ivar=[ivar CellInfo{icell}.VarIndex_scalar(1)];
|
---|
179 | ivar_1=[ivar_1 CellInfo{icell}.VarIndex_scalar(2)];
|
---|
180 | end
|
---|
181 | if isfield(CellInfo{icell},'VarIndex_vector_x') && numel(CellInfo{icell}.VarIndex_vector_x)==2 && SubData.VarAttribute{CellInfo{icell}.VarIndex_vector_x(2)}.CheckSub;
|
---|
182 | ivar=[ivar CellInfo{icell}.VarIndex_vector_x(1)];
|
---|
183 | ivar_1=[ivar_1 CellInfo{icell}.VarIndex_vector_x(2)];
|
---|
184 | end
|
---|
185 | if isfield(CellInfo{icell},'VarIndex_vector_y') && numel(CellInfo{icell}.VarIndex_vector_y)==2 && SubData.VarAttribute{CellInfo{icell}.VarIndex_vector_y(2)}.CheckSub;
|
---|
186 | ivar=[ivar CellInfo{icell}.VarIndex_vector_y(1)];
|
---|
187 | ivar_1=[ivar_1 CellInfo{icell}.VarIndex_vector_y(2)];
|
---|
188 | end
|
---|
189 | end
|
---|
190 | end
|
---|
191 | for imod=1:numel(ivar)
|
---|
192 | VarName=SubData.ListVarName{ivar(imod)};
|
---|
193 | VarName_1=SubData.ListVarName{ivar_1(imod)};
|
---|
194 | SubData.(VarName)=double(SubData.(VarName))-double(SubData.(VarName_1));
|
---|
195 | ind_remove(ivar_1(imod))=1;
|
---|
196 | end
|
---|
197 | SubData.ListVarName(find(ind_remove))=[];
|
---|
198 | SubData.VarDimName(find(ind_remove))=[];
|
---|
199 | SubData.VarAttribute(find(ind_remove))=[];
|
---|
200 | 'end'
|
---|
201 |
|
---|
202 | function OutputCell=regexprep_r(InputCell,search_string,new_string)
|
---|
203 | if ischar(InputCell); InputCell={InputCell}; end
|
---|
204 | OutputCell=InputCell;%default
|
---|
205 | for icell=1:numel(InputCell)
|
---|
206 | OutputCell{icell}=regexprep(InputCell{icell},search_string,new_string);
|
---|
207 | end
|
---|
208 |
|
---|
209 |
|
---|