source: trunk/src/sub_field.m @ 2

Last change on this file since 2 was 2, checked in by gostiaux, 15 years ago
  • Initial import
  • Add .m files in src
  • Add .fig files in src
  • Add .xml config files
File size: 14.3 KB
Line 
1%'sub_field': combines two input fields
2%-----------------------------------------------------------------------
3% function SubData=sub_field(Field,Field_1)
4%
5% OUPUT:
6% SubData: structure representing the resulting field
7%
8% INPUT:
9% UvData: main structure UvData associated to the uvmat GUI as 'UserData'
10% Field: cell of Matlab structures representing the input fields
11%
12%    -- TODO: need to be rationalized --
13
14function [SubData,errormsg]=sub_field(Field,Field_1)
15test_attr=0;
16if isfield(Field,'ListGlobalAttribute')
17    SubData.ListGlobalAttribute=Field.ListGlobalAttribute;
18    for ilist=1:numel(Field.ListGlobalAttribute)
19        AttrName=Field.ListGlobalAttribute{ilist};
20        eval(['SubData.' AttrName '=Field.' AttrName ';'])
21    end
22    test_attr=1;
23end
24if isfield(Field_1,'ListGlobalAttribute')
25    for ilist=1:numel(Field_1.ListGlobalAttribute)
26        test_1=1;
27        AttrName=Field_1.ListGlobalAttribute{ilist};
28        if test_attr
29            for i_prev=1:numel(Field.ListGlobalAttribute)
30                if isequal(Field.ListGlobalAttribute{i_prev},AttrName)
31                    test_1=0; %attribute already written
32                    eval(['Val=Field.' AttrName ';'])                 
33                    eval(['Val_1=Field_1.' AttrName ';'])
34                    if isequal(Val,Val_1)           
35                        break% data already written
36                    else
37                        eval(['SubData.' AttrName '_1=Field_1.' AttrName ';'])
38                    end
39                end
40            end
41        end
42        if test_1
43            eval(['SubData.' AttrName '=Field_1.' AttrName ';'])
44        end
45    end
46end
47SubData.ListVarName=Field.ListVarName;
48SubData.VarDimName=Field.VarDimName;
49if isfield(Field,'VarAttribute')
50    SubData.VarAttribute=Field.VarAttribute;
51end
52%reproduce Field by default
53for ivar=1:numel(Field.ListVarName)
54   VarName=Field.ListVarName{ivar};
55   eval(['SubData.' VarName '=Field.' VarName ';'])
56end
57
58%fields     
59[CellVarIndex,NbDim,VarTypeCell,errormsg]=find_field_indices(Field);
60if ~isempty(errormsg)
61    errormsg=['invalid  first input to sub_field:' errormsg];
62    return
63end
64[CellVarIndex_1,NbDim_1,VarTypeCell_1,errormsg]=find_field_indices(Field_1);
65if ~isempty(errormsg)
66    errormsg=['invalid second input to sub_field:' errormsg];
67    return
68end
69iselect=find(NbDim==2);
70if ~isequal(numel(iselect),1)
71    errormsg='invalid  first input to sub_field: it must  contain a single 2D field cell';
72    return
73end
74iselect_1=find(NbDim_1==2);
75if ~isequal(numel(iselect_1),1)
76    errormsg='invalid  second input to sub_field: it must  contain a single 2D field cell';
77    return
78end
79% VarIndex=CellVarIndex{iselect};
80% VarIndex_1=CellVarIndex_1{iselect_1};
81VarType=VarTypeCell{iselect};
82VarType_1=VarTypeCell_1{iselect_1};
83testX=~isempty(VarType.coord_x)&& ~isempty(VarType.coord_y);%unstructured coordiantes
84testX_1=~isempty(VarType_1.coord_x)&& ~isempty(VarType_1.coord_y);%unstructured coordiantes
85testU=~isempty(VarType.vector_x)&& ~isempty(VarType.vector_y);%vector field
86testU_1=~isempty(VarType_1.vector_x)&& ~isempty(VarType_1.vector_y);%vector field
87% testfalse=~isempty(VarType.errorflag);
88testfalse_1=~isempty(VarType_1.errorflag);
89ivar_C=[VarType.scalar VarType.image VarType.color VarType.ancillary]; %defines index (indices) for the scalar or ancillary fields
90if numel(ivar_C)>1
91    errormsg='too many scalar fields in the first input of sub_field.m';
92    return
93end
94ivar_C_1=[VarType_1.scalar VarType_1.image VarType_1.color VarType_1.ancillary]; %defines index (indices) for the scalar or ancillary fields
95if numel(ivar_C_1)>1
96    errormsg='too many scalar fields in the second input of sub_field.m';
97    return
98end
99
100%substract two vector fields or two scalars
101if (testU && testU_1) || (~testU && ~testU_1)
102   %check coincidence in positions
103   %unstructured coordinates
104   if testX
105       XName=Field.ListVarName{VarType.coord_x};
106       YName=Field.ListVarName{VarType.coord_y};
107       eval(['vec_X=Field.' XName ';'])
108       eval(['vec_Y=Field.' YName ';'])
109       nbpoints=numel(vec_X);
110       vec_X=reshape(vec_X,1,nbpoints);
111       vec_Y=reshape(vec_Y,1,nbpoints);
112       if testX_1 %unstructured coordinates for the second field
113            X_1_Name=Field_1.ListVarName{VarType_1.coord_x};
114            Y_1_Name=Field_1.ListVarName{VarType_1.coord_y};
115            eval(['vec_X_1=Field_1.' X_1_Name ';'])
116            eval(['vec_Y_1=Field_1.' Y_1_Name ';'])
117            nbpoints_1=numel(vec_X_1);
118       else   %structured coordinates for the second field
119           y_1_Name=Field_1.ListVarName{VarType_1.coord(1)};
120           x_1_Name=Field_1.ListVarName{VarType_1.coord(2)};
121           eval(['y_1=Field_1.' y_1_Name ';'])
122           eval(['x_1=Field_1.' x_1_Name ';']) 
123           npxy(1)=numel(y_1);
124           npxy(2)=numel(x_1);
125           nbpoints_1=npxy(1)*npxy(2);
126           [vec_X_1,vec_Y_1]=meshgrid(x_1,y_1);
127       end
128       vec_X_1=reshape(vec_X_1,1,nbpoints_1);
129       vec_Y_1=reshape(vec_Y_1,1,nbpoints_1);
130       if testfalse_1
131           FFName_1=Field_1.ListVarName{VarType_1.errorflag};         
132           eval(['vec_FF_1=Field_1.' FFName_1 ';'])
133           vec_FF_1=reshape(vec_FF_1,1,nbpoints_1);
134           indsel=find(~vec_FF_1);
135           vec_X_1=vec_X_1(indsel);
136           vec_Y_1=vec_Y_1(indsel);
137       end
138       if testU % vector fields
139            U_1_Name=Field_1.ListVarName{VarType_1.vector_x};
140            V_1_Name=Field_1.ListVarName{VarType_1.vector_y};
141            eval(['vec_U_1=Field_1.' U_1_Name ';'])
142            eval(['vec_V_1=Field_1.' V_1_Name ';'])
143            vec_U_1=reshape(vec_U_1,1,nbpoints_1);
144            vec_V_1=reshape(vec_V_1,1,nbpoints_1);
145            if testfalse_1
146                vec_U_1=vec_U_1(indsel);
147                vec_V_1=vec_V_1(indsel);
148            end           
149       else
150           A_1_Name=Field_1.ListVarName{ivar_C_1};
151           eval(['vec_A_1=Field_1.' A_1_Name ';'])
152           vec_A_1=reshape(vec_A_1,1,nbpoints_1);
153           if testfalse_1
154                vec_A_1=vec_A_1(indsel);
155           end
156       end
157       if ~isequal(vec_X_1,vec_X) && ~isequal(vec_Y_1,vec_Y) % if the unstructured positions are not the same
158           if testU
159               vec_U_1=griddata_uvmat(vec_X_1,vec_Y_1,vec_U_1,vec_X,vec_Y);  %interpolate vectors in the second field
160               vec_V_1=griddata_uvmat(vec_X_1,vec_Y_1,vec_V_1,vec_X,vec_Y);  %interpolate vectors in the second field   
161           else
162               vec_A_1=griddata_uvmat(vec_X_1,vec_Y_1,vec_A_1,vec_X,vec_Y);  %interpolate vectors in the second field
163           end
164       end
165       if testU
166           UName=Field.ListVarName{VarType.vector_x};
167           VName=Field.ListVarName{VarType.vector_y}; 
168           eval(['vec_U=Field.' UName ';'])
169           eval(['vec_V=Field.' VName ';'])       
170           vec_U=reshape(vec_U,1,numel(vec_U));
171           vec_V=reshape(vec_V,1,numel(vec_V));
172           eval(['SubData.' UName '=vec_U-vec_U_1;'])
173           eval(['SubData.' VName '=vec_V-vec_V_1;'])
174       else
175           AName=Field.ListVarName{ivar_C};
176           eval(['SubData.' AName '=Field.' AName '-vec_A_1;'])
177       end
178   else  %structured coordiantes
179       XName=Field.ListVarName{VarType.coord(2)};
180       YName=Field.ListVarName{VarType.coord(1)};
181       eval(['x=Field.' XName ';'])
182       eval(['y=Field.' YName ';'])
183       if testX_1 %unstructured coordinates for the second field
184           errormsg='the second input scalar is not on a regular grid: comparison option not implemented';
185           return
186       else
187           XName_1=Field.ListVarName{VarType_1.coord(2)};
188           YName_1=Field.ListVarName{VarType_1.coord(1)};
189           eval(['x_1=Field_1.' XName_1 ';'])
190           eval(['y_1=Field_1.' YName_1 ';'])
191       end
192       if testU % vector fields
193           UName=Field.ListVarName{VarType.vector_x};
194           VName=Field.ListVarName{VarType.vector_y};
195           U_1_Name=Field_1.ListVarName{VarType_1.vector_x};
196           V_1_Name=Field_1.ListVarName{VarType_1.vector_y};
197           eval(['U_1=Field_1.' U_1_Name ';'])
198           eval(['V_1=Field_1.' V_1_Name ';'])
199           if ~isequal(x_1,x)||~isequal(y_1,y)
200                [X_1,Y_1]=meshgrid(x_1,y_1);
201                U_1 =interp2(X_1,Y_1,U_1,x,y');
202                V_1 =interp2(X_1,Y_1,V_1,x,y');
203           end
204           val(['SubData.' UName '=Field.' UName '-U_1;'])
205           val(['SubData.' VName '=Field.' VName '-V_1;'])
206       else
207           AName=Field.ListVarName{ivar_C};
208           A_1_Name=Field_1.ListVarName{ivar_C_1};
209           eval(['A_1=double(Field_1.' A_1_Name ');'])
210           if ~isequal(x_1,x)||~isequal(y_1,y)
211                [X_1,Y_1]=meshgrid(x_1,y_1);
212                A_1 =interp2(X_1,Y_1,A_1,x,y');
213           end
214           eval(['SubData.' AName '=double(Field.' AName ')-A_1;'])
215       end
216   end
217end
218
219% merge a vector field and a scalar as second input
220if testU && ~testU_1
221    AName_1=Field_1.ListVarName{ivar_C_1};
222    if isfield(Field_1,'VarAttribute') && numel(Field_1.VarAttribute)>=ivar_C_1
223        AAttr=Field_1.VarAttribute{ivar_C_1} ;
224    else
225        AAttr=[];
226    end
227    if testX_1 %unstructured coordinate
228       XName_1=Field_1.ListVarName{VarType_1.coord_x};
229       YName_1=Field_1.ListVarName{VarType_1.coord_y};
230       SubData.ListVarName=[SubData.ListVarName {XName_1} {YName_1}];
231       DimCell=Field_1.VarDimName([VarType_1.coord_x VarType_1.coord_y ]);
232       if isfield(Field_1,'VarAttribute')
233           if numel(Field_1.VarAttribute)>=VarType_1.coord_x
234                XAttr=Field_1.VarAttribute{VarType_1.coord_x} ;
235           else
236                XAttr=[];
237           end
238           if numel(Field_1.VarAttribute)>=VarType_1.coord_y
239               YAttr=Field_1.VarAttribute{VarType_1.coord_y} ;
240           else
241               YAttr=[];
242           end
243           SubData.VarAttribute=[SubData.VarAttribute {XAttr} {YAttr}];
244       end
245    else
246       XName_1=Field_1.ListVarName{VarType_1.coord(2)};
247       YName_1=Field_1.ListVarName{VarType_1.coord(1)};
248%        DimCell=[{YName_1} {XName_1}];
249       if isfield(Field_1,'VarAttribute')
250           if numel(Field_1.VarAttribute)>=VarType_1.coord(2)
251                XAttr=Field_1.VarAttribute{VarType_1.coord(2)} ;
252           else
253                XAttr=[];
254           end
255           if numel(Field_1.VarAttribute)>=VarType_1.coord(1)
256               YAttr=Field_1.VarAttribute{VarType_1.coord(1)} ;
257           else
258               YAttr=[];
259           end
260           SubData.VarAttribute=[SubData.VarAttribute {YAttr} {XAttr}];
261       end
262    end 
263    %look for previously used variable names
264    XName_1_1=XName_1;%default
265    YName_1_1=YName_1;%default
266    AName_1_1=AName_1;%default
267    for iprev=1:numel(SubData.ListVarName)
268        switch SubData.ListVarName{iprev}
269            case XName_1
270                XName_1_1=[XName_1 '_1'];
271            case YName_1
272                YName_1_1=[YName_1 '_1'];
273            case AName_1
274                AName_1_1=[AName_1 '_1'];
275        end
276    end     
277    if ~testX_1
278          DimCell=[{XName_1_1} {YName_1_1}];
279    end
280    SubData.ListVarName=[SubData.ListVarName {XName_1_1} {YName_1_1} {AName_1_1}];
281    DimCell=[DimCell Field_1.VarDimName(ivar_C_1)]; %(TODO: check for dimension names)
282    SubData.VarDimName=[SubData.VarDimName DimCell];
283    if isfield(Field_1,'VarAttribute')
284        SubData.VarAttribute=[SubData.VarAttribute {AAttr}];
285    end
286    eval(['SubData.' XName_1_1 '=Field_1.' XName_1 ';'])
287    eval(['SubData.' YName_1_1 '=Field_1.' YName_1 ';'])
288    eval(['SubData.' AName_1_1 '=Field_1.' AName_1 ';'])
289end
290
291%merge a scalar as the first input and a vector field as second input
292if ~testU && testU_1
293    UName_1=Field_1.ListVarName{VarType_1.vector_x};
294    VName_1=Field_1.ListVarName{VarType_1.vector_y};
295    UAttr=Field_1.VarAttribute{VarType_1.vector_x};
296    VAttr=Field_1.VarAttribute{VarType_1.vector_y};
297    if testX_1 %unstructured coordinate for the second field
298       XName_1=Field_1.ListVarName{VarType_1.coord_x};
299       YName_1=Field_1.ListVarName{VarType_1.coord_y};
300       
301       XAttr=Field_1.VarAttribute{VarType_1.coord_x};
302       YAttr=Field_1.VarAttribute{VarType_1.coord_y};
303%        SubData.ListVarName=[SubData.ListVarName {XName_1} {YName_1}];
304       DimCell=Field_1.VarDimName([VarType_1.coord_x VarType_1.coord_y ]);
305    else
306       XName_1=Field_1.ListVarName{VarType_1.coord(2)};
307       YName_1=Field_1.ListVarName{VarType_1.coord(1)};
308       if numel(Field_1.VarAttribute)>=VarType_1.coord(2)
309           XAttr=Field_1.VarAttribute{VarType_1.coord(2)};
310       else
311           XAttr=[];
312       end
313       if numel(Field_1.VarAttribute)>=VarType_1.coord(1)
314           YAttr=Field_1.VarAttribute{VarType_1.coord(1)};
315       else
316           YAttr=[];
317       end     
318    end 
319    %check for the existence of the same  variable name
320    XName_1_1=XName_1; %default
321    YName_1_1=YName_1; %default
322    UName_1_1=UName_1; %default
323    VName_1_1=VName_1; %default
324    for iprev=1:numel(SubData.ListVarName)
325        switch SubData.ListVarName{iprev}
326            case XName_1
327                XName_1_1=[XName_1 '_1'];
328            case YName_1
329                YName_1_1=[YName_1 '_1'];
330            case UName_1
331                UName_1_1=[UName_1 '_1'];
332            case VName_1
333                VName_1_1=[VName_1 '_1'];
334        end
335    end     
336    if ~testX_1
337          DimCell=[{XName_1_1} {YName_1_1}];
338    end
339    SubData.ListVarName=[SubData.ListVarName {XName_1_1} {YName_1_1} {UName_1_1} {VName_1_1}];
340    DimCell=[DimCell Field_1.VarDimName([VarType_1.vector_x VarType_1.vector_y ])];
341    SubData.VarDimName=[SubData.VarDimName DimCell];
342    if ~(isfield(SubData,'VarAttribute') && numel(SubData.VarAttribute)==numel(SubData.ListVarName))
343        for ivar=numel(SubData.VarAttribute)+1:numel(SubData.ListVarName)-4
344             SubData.VarAttribute{ivar}=[];
345        end
346    end
347    SubData.VarAttribute=[SubData.VarAttribute {XAttr} {YAttr} {UAttr} {VAttr}];
348    eval(['SubData.' XName_1_1 '=Field_1.' XName_1 ';'])
349    eval(['SubData.' YName_1_1 '=Field_1.' YName_1 ';'])
350    eval(['SubData.' UName_1_1 '=Field_1.' UName_1 ';'])
351    eval(['SubData.' VName_1_1 '=Field_1.' VName_1 ';']) 
352end
353
354 
Note: See TracBrowser for help on using the repository browser.