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 | function SubData=sub_field(Field,XmlData,Field_1)
|
---|
19 |
|
---|
20 | SubData=[];
|
---|
21 | if strcmp(Field,'*')
|
---|
22 | return
|
---|
23 | end
|
---|
24 | if nargin<3
|
---|
25 | SubData=Field;
|
---|
26 | return
|
---|
27 | end
|
---|
28 |
|
---|
29 | %% global attributes
|
---|
30 | SubData.ListGlobalAttribute={};%default
|
---|
31 | %transfer global attributes of Field
|
---|
32 | if isfield(Field,'ListGlobalAttribute')
|
---|
33 | SubData.ListGlobalAttribute=Field.ListGlobalAttribute;
|
---|
34 | for ilist=1:numel(Field.ListGlobalAttribute)
|
---|
35 | AttrName=Field.ListGlobalAttribute{ilist};
|
---|
36 | SubData.(AttrName)=Field.(AttrName);
|
---|
37 | end
|
---|
38 | end
|
---|
39 | %transfer global attributes of Field_1
|
---|
40 | if isfield(Field_1,'ListGlobalAttribute')
|
---|
41 | for ilist=1:numel(Field_1.ListGlobalAttribute)
|
---|
42 | AttrName=Field_1.ListGlobalAttribute{ilist};
|
---|
43 | AttrNameNew=AttrName;
|
---|
44 | while ~isempty(find(strcmp(AttrNameNew,SubData.ListGlobalAttribute)))&&~isequal(Field_1.(AttrNameNew),Field.(AttrNameNew))
|
---|
45 | AttrNameNew=[AttrNameNew '_1'];
|
---|
46 | end
|
---|
47 | if ~isfield(Field,AttrName) || ~isequal(Field_1.(AttrName),Field.(AttrName))
|
---|
48 | SubData.ListGlobalAttribute=[SubData.ListGlobalAttribute {AttrNameNew}];
|
---|
49 | SubData.(AttrNameNew)=Field_1.(AttrName);
|
---|
50 | end
|
---|
51 | end
|
---|
52 | end
|
---|
53 |
|
---|
54 | %% variables
|
---|
55 | %reproduce variables of the first field and list its dimensions
|
---|
56 | SubData.ListVarName=Field.ListVarName;
|
---|
57 | SubData.VarDimName=Field.VarDimName;
|
---|
58 | if isfield(Field,'VarAttribute')
|
---|
59 | SubData.VarAttribute=Field.VarAttribute;
|
---|
60 | end
|
---|
61 | ListDimName={};
|
---|
62 | for ilist=1:numel(Field.ListVarName)
|
---|
63 | VarName=Field.ListVarName{ilist};
|
---|
64 | SubData.(VarName)=Field.(VarName);
|
---|
65 | SubData.VarAttribute{ilist}.CheckSub=0;
|
---|
66 | DimCell=Field.VarDimName{ilist};
|
---|
67 | if ischar(DimCell)
|
---|
68 | DimCell={DimCell};
|
---|
69 | end
|
---|
70 | for idim=1:numel(DimCell)
|
---|
71 | if isempty(find(strcmp(DimCell{idim},ListDimName)))
|
---|
72 | ListDimName=[ListDimName DimCell(idim)];
|
---|
73 | end
|
---|
74 | end
|
---|
75 | end
|
---|
76 |
|
---|
77 | %% field request
|
---|
78 | ProjModeRequest=cell(size(Field.ListVarName));
|
---|
79 | for ilist=1:numel(Field.VarAttribute)
|
---|
80 | if isfield(Field.VarAttribute{ilist},'ProjModeRequest')
|
---|
81 | ProjModeRequest{ilist}=Field.VarAttribute{ilist}.ProjModeRequest;
|
---|
82 | end
|
---|
83 | end
|
---|
84 | ProjModeRequest_1=cell(size(Field_1.ListVarName));
|
---|
85 | for ilist=1:numel(Field_1.VarAttribute)
|
---|
86 | if isfield(Field_1.VarAttribute{ilist},'ProjModeRequest')
|
---|
87 | ProjModeRequest_1{ilist}=Field_1.VarAttribute{ilist}.ProjModeRequest;
|
---|
88 | end
|
---|
89 | end
|
---|
90 |
|
---|
91 | %% rename the dimensions of the second field if identical to those of the first
|
---|
92 | for ilist=1:numel(Field_1.VarDimName)
|
---|
93 | DimCell=Field_1.VarDimName{ilist};
|
---|
94 | if ischar(DimCell)
|
---|
95 | DimCell={DimCell};
|
---|
96 | end
|
---|
97 | for idim=1:numel(DimCell)
|
---|
98 | ind_dim=find(strcmp(DimCell{idim},ListDimName));
|
---|
99 | if ~isempty(ind_dim)
|
---|
100 | if ischar(Field_1.VarDimName{ilist})
|
---|
101 | Field_1.VarDimName{ilist}=Field_1.VarDimName(ilist);
|
---|
102 | end
|
---|
103 | Field_1.VarDimName{ilist}{idim}=[ListDimName{ind_dim} '_1'];
|
---|
104 | end
|
---|
105 | end
|
---|
106 | end
|
---|
107 |
|
---|
108 | %% look for coordinates common to Field in Field_1
|
---|
109 | ind_remove=zeros(size(Field_1.ListVarName));
|
---|
110 | % loop on the variables of the second field Field_1
|
---|
111 | for ilist=1:numel(Field_1.ListVarName)
|
---|
112 | % case of variable with a single dimension
|
---|
113 | OldDimName=Field_1.VarDimName{ilist};
|
---|
114 | if ischar(OldDimName), OldDimName={OldDimName}; end% transform char string to cell if relevant
|
---|
115 | if numel(OldDimName)==1
|
---|
116 | OldDim=Field_1.(Field_1.ListVarName{ilist});% get variable
|
---|
117 | %look for the existence of the variable OldDim in the first field Field
|
---|
118 | for i1=1:numel(Field.ListVarName)
|
---|
119 | if isequal(Field.(Field.ListVarName{i1}),OldDim) &&...
|
---|
120 | ((isempty(ProjModeRequest{i1}) && isempty(ProjModeRequest_1{ilist})) || strcmp(ProjModeRequest{i1},ProjModeRequest_1{ilist}))
|
---|
121 | ind_remove(ilist)=1;
|
---|
122 | NewDimName=Field.VarDimName{i1};
|
---|
123 | if ischar(NewDimName), NewDimName={NewDimName}; end %transform char chain to cell if needed
|
---|
124 | 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
|
---|
125 | end
|
---|
126 | end
|
---|
127 | end
|
---|
128 | end
|
---|
129 | Field_1.ListVarName(find(ind_remove))=[];%removes the redondent coordinate
|
---|
130 | Field_1.VarDimName(find(ind_remove))=[];
|
---|
131 | Field_1.VarAttribute(find(ind_remove))=[];
|
---|
132 |
|
---|
133 | %% append the other variables of the second field, modifying their name if needed
|
---|
134 | for ilist=1:numel(Field_1.ListVarName)
|
---|
135 | VarName=Field_1.ListVarName{ilist};
|
---|
136 | ind_prev=find(strcmp(VarName,Field.ListVarName));
|
---|
137 | if isempty(ind_prev)% variable name does not exist in Field
|
---|
138 | VarNameNew=VarName;
|
---|
139 | else % variable name exists in Field
|
---|
140 | VarNameNew=[VarName '_1'];
|
---|
141 | if isfield(Field_1.VarAttribute{ilist},'FieldName')
|
---|
142 | Field_1.VarAttribute{ilist}.FieldName=regexprep_r(Field_1.VarAttribute{ilist}.FieldName,VarName,VarNameNew);
|
---|
143 | end
|
---|
144 | end
|
---|
145 | SubData.ListVarName=[SubData.ListVarName {VarNameNew}];
|
---|
146 | SubData.VarDimName=[SubData.VarDimName Field_1.VarDimName(ilist)];
|
---|
147 | SubData.(VarNameNew)=Field_1.(VarName);
|
---|
148 | SubData.VarAttribute=[SubData.VarAttribute Field_1.VarAttribute(ilist)];
|
---|
149 | SubData.VarAttribute{end}.CheckSub=1;% mark that the field needs to be substracted
|
---|
150 | end
|
---|
151 |
|
---|
152 | %% substrat fields when possible
|
---|
153 | [CellInfo,NbDim,errormsg]=find_field_cells(SubData);
|
---|
154 | ind_remove=zeros(size(SubData.ListVarName));
|
---|
155 | ivar=[];
|
---|
156 | ivar_1=[];
|
---|
157 | for icell=1:numel(CellInfo)
|
---|
158 | if ~isempty(CellInfo{icell})
|
---|
159 | if isfield(CellInfo{icell},'VarIndex_scalar') && numel(CellInfo{icell}.VarIndex_scalar)==2 && SubData.VarAttribute{CellInfo{icell}.VarIndex_scalar(2)}.CheckSub;
|
---|
160 | ivar=[ivar CellInfo{icell}.VarIndex_scalar(1)];
|
---|
161 | ivar_1=[ivar_1 CellInfo{icell}.VarIndex_scalar(2)];
|
---|
162 | end
|
---|
163 | if isfield(CellInfo{icell},'VarIndex_vector_x') && numel(CellInfo{icell}.VarIndex_vector_x)==2 && SubData.VarAttribute{CellInfo{icell}.VarIndex_vector_x(2)}.CheckSub;
|
---|
164 | ivar=[ivar CellInfo{icell}.VarIndex_vector_x(1)];
|
---|
165 | ivar_1=[ivar_1 CellInfo{icell}.VarIndex_vector_x(2)];
|
---|
166 | end
|
---|
167 | if isfield(CellInfo{icell},'VarIndex_vector_y') && numel(CellInfo{icell}.VarIndex_vector_y)==2 && SubData.VarAttribute{CellInfo{icell}.VarIndex_vector_y(2)}.CheckSub;
|
---|
168 | ivar=[ivar CellInfo{icell}.VarIndex_vector_y(1)];
|
---|
169 | ivar_1=[ivar_1 CellInfo{icell}.VarIndex_vector_y(2)];
|
---|
170 | end
|
---|
171 | end
|
---|
172 | end
|
---|
173 | for imod=1:numel(ivar)
|
---|
174 | VarName=SubData.ListVarName{ivar(imod)};
|
---|
175 | VarName_1=SubData.ListVarName{ivar_1(imod)};
|
---|
176 | SubData.(VarName)=double(SubData.(VarName))-double(SubData.(VarName_1));
|
---|
177 | ind_remove(ivar_1(imod))=1;
|
---|
178 | end
|
---|
179 | SubData.ListVarName(find(ind_remove))=[];
|
---|
180 | SubData.VarDimName(find(ind_remove))=[];
|
---|
181 | SubData.VarAttribute(find(ind_remove))=[];
|
---|
182 | 'end'
|
---|
183 |
|
---|
184 | function OutputCell=regexprep_r(InputCell,search_string,new_string)
|
---|
185 | OutputCell=InputCell;%default
|
---|
186 | for icell=1:numel(InputCell)
|
---|
187 | OutputCell{icell}=regexprep(InputCell{icell},search_string,new_string);
|
---|
188 | end
|
---|
189 |
|
---|
190 |
|
---|