source: trunk/src/civ_uvmat.m @ 250

Last change on this file since 250 was 248, checked in by sommeria, 13 years ago

various modifications

File size: 21.8 KB
Line 
1% To develop....
2function [Data,errormsg]= civ_uvmat(Param,ncfile)
3errormsg='';
4Data.ListGlobalAttribute={'Conventions','Program','CivStage'};
5Data.Conventions='uvmat/civdata';% states the conventions used for the description of field variables and attributes
6Data.Program='civ_uvmat';
7Data.CivStage=0;%default
8ListVarCiv1={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_C','Civ1_F'};
9ListVarFix1={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_C','Civ1_F','Civ1_FF'};
10mask='';
11maskname='';%default
12
13%% Civ1
14if isfield (Param,'Civ1')
15    par_civ1=Param.Civ1;
16    image1=imread(par_civ1.filename_ima_a);
17    image2=imread(par_civ1.filename_ima_b);
18    stepx=str2num(par_civ1.dx);
19    stepy=str2num(par_civ1.dy);
20    ibx2=ceil(str2num(par_civ1.ibx)/2);
21    iby2=ceil(str2num(par_civ1.iby)/2);
22    isx2=ceil(str2num(par_civ1.isx)/2);
23    isy2=ceil(str2num(par_civ1.isy)/2);
24    shiftx=str2num(par_civ1.shiftx);
25    shifty=str2num(par_civ1.shifty);
26    miniy=max(1+isy2+shifty,1+iby2);
27    minix=max(1+isx2-shiftx,1+ibx2);
28    maxiy=min(size(image1,1)-isy2+shifty,size(image1,1)-iby2);
29    maxix=min(size(image1,2)-isx2-shiftx,size(image1,2)-ibx2);
30    [GridX,GridY]=meshgrid(minix:stepx:maxix,miniy:stepy:maxiy);
31    PointCoord(:,1)=reshape(GridX,[],1);
32    PointCoord(:,2)=reshape(GridY,[],1);
33    if ~isempty(par_civ1.maskname)
34        maskname=par_civ1.maskname;
35        mask=imread(maskname);
36    end
37    % caluclate velocity data (y and v in indices, reverse to y component)
38    [xtable ytable utable vtable ctable F] = pivlab (image1,image2,ibx2,iby2,isx2,isy2,shiftx,-shifty,PointCoord,str2num(par_civ1.rho), mask);
39    list_param=(fieldnames(par_civ1))';
40    list_remove={'pxcmx','pxcmy','npx','npy','gridflag','maskflag','term_a','term_b','T0'};
41    index=zeros(size(list_param));
42    for ilist=1:length(list_remove)
43        index=strcmp(list_remove{ilist},list_param);
44        if ~isempty(find(index,1))
45            list_param(index)=[];
46        end
47    end
48    for ilist=1:length(list_param)
49        Civ1_param{ilist}=['Civ1_' list_param{ilist}];
50        eval(['Data.Civ1_' list_param{ilist} '=Param.Civ1.' list_param{ilist} ';'])
51    end
52    if isfield(Data,'Civ1_gridname') && strcmp(Data.Civ1_gridname(1:6),'noFile')
53        Data.Civ1_gridname='';
54    end
55    if isfield(Data,'Civ1_maskname') && strcmp(Data.Civ1_maskname(1:6),'noFile')
56        Data.Civ1_maskname='';
57    end
58    Data.ListGlobalAttribute=[Data.ListGlobalAttribute Civ1_param {'Civ1_Time','Civ1_Dt'}];
59    Data.Civ1_Time=str2double(par_civ1.T0);
60    Data.Civ1_Dt=str2double(par_civ1.Dt);
61    Data.ListVarName={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_C','Civ1_F'};%  cell array containing the names of the fields to record
62    Data.VarDimName={'nbvec1','nbvec1','nbvec1','nbvec1','nbvec1','nbvec1'};
63    Data.VarAttribute{1}.Role='coord_x';
64    Data.VarAttribute{2}.Role='coord_y';
65    Data.VarAttribute{3}.Role='vector_x';
66    Data.VarAttribute{4}.Role='vector_y';
67    Data.VarAttribute{5}.Role='warnflag';
68    Data.Civ1_X=reshape(xtable,[],1);
69    Data.Civ1_Y=reshape(size(image1,1)-ytable+1,[],1);
70    Data.Civ1_U=reshape(utable,[],1);
71    Data.Civ1_V=reshape(-vtable,[],1);
72    Data.Civ1_C=reshape(ctable,[],1);
73    Data.Civ1_F=reshape(F,[],1);
74    Data.CivStage=1;
75else
76    if isfield(Param,'Fix1')
77        Data=nc2struct(ncfile,ListVarCiv1);%read civ1 data in the existing netcdf file
78    else
79        Data=nc2struct(ncfile,ListVarFix1);%read civ1 and fix1 data in the existing netcdf file
80    end
81    if isfield(Data,'Txt')
82        msgbox_uvmat('ERROR',Data.Txt)
83        return
84    end
85    % read Civx data
86    if isfield(Data,'absolut_time_T0')%read civx file
87        var={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_C','Civ1_F','Civ1_FF';'vec_X','vec_Y','vec_U','vec_V','vec_C','vec_F','vec_FixFlag'};
88        %var=varcivx_generator('velocity','Civ1');%determine the names of constants and variables to read
89        [Data,vardetect,ichoice]=nc2struct(ncfile,var);%read the variables in the netcdf file
90        Data.ListGlobalAttribute=[{'Conventions','Program','CivStage'} Data.ListGlobalAttribute {'Civ1_Time','Civ1_Dt'}];
91        Data.Conventions='uvmat/civdata';% states the conventions used for the description of field variables and attributes
92        Data.Program='civ_uvmat';
93        Data.Civ1_Time=double(Data.absolut_time_T0);
94        Data.Civ1_Dt=double(Data.dt);
95        Data.VarDimName={'nbvec1','nbvec1','nbvec1','nbvec1','nbvec1','nbvec1','nbvec1'};
96        Data.VarAttribute{1}.Role='coord_x';
97        Data.VarAttribute{2}.Role='coord_y';
98        Data.VarAttribute{3}.Role='vector_x';
99        Data.VarAttribute{4}.Role='vector_y';
100        Data.VarAttribute{5}.Role='ancillary';
101        Data.VarAttribute{6}.Role='warnflag';
102        Data.VarAttribute{7}.Role='errorflag';
103        Data.CivStage=1;
104    end
105end
106
107%% Fix1
108if isfield (Param,'Fix1')
109    ListFixParam=fieldnames(Param.Fix1);
110    for ilist=1:length(ListFixParam)
111        ParamName=ListFixParam{ilist};
112        ListName=['Fix1_' ParamName];
113        eval(['Data.ListGlobalAttribute=[Data.ListGlobalAttribute ''' ParamName '''];'])
114        eval(['Data.' ListName '=Param.Fix1.' ParamName ';'])
115    end
116%     Data.ListGlobalAttribute=[Data.ListGlobalAttribute {'Fix1_WarnFlags','Fix1_TreshCorr','Fix1_TreshVel','Fix1_UpperBoundTest'}];
117%     Data.Fix1_WarnFlags=Param.Fix1.WarnFlags;
118%     Data.Fix1_ThreshCorr=Param.Fix1.ThreshCorr;
119%     Data.Fix1_ThreshVel=Param.Fix1.ThreshVel;
120%     Data.Fix1_UpperBoundTest=Param.Fix1.UpperBoundTest;
121    Data.ListVarName=[Data.ListVarName {'Civ1_FF'}];
122    Data.VarDimName=[Data.VarDimName {'nbvec1'}];
123    nbvar=length(Data.ListVarName);
124    Data.VarAttribute{nbvar}.Role='errorflag';   
125    Data.Civ1_FF=fix_uvmat(Param.Fix1,Data.Civ1_F,Data.Civ1_C,Data.Civ1_U,Data.Civ1_V);
126    Data.CivStage=2;                               
127end   
128%% Patch1
129if isfield (Param,'Patch1')
130    Data.ListGlobalAttribute=[Data.ListGlobalAttribute {'Patch1_Rho','Patch1_Threshold','Patch1_SubDomain'}];
131    Data.Patch1_Rho=str2double(Param.Patch1.Rho);
132    Data.Patch1_Threshold=str2double(Param.Patch1.Threshold);
133    Data.Patch1_SubDomain=str2double(Param.Patch1.SubDomain);
134    Data.ListVarName=[Data.ListVarName {'Civ1_U_Diff','Civ1_V_Diff','Civ1_X_SubRange','Civ1_Y_SubRange','Civ1_NbSites','Civ1_X_tps','Civ1_Y_tps','Civ1_U_tps','Civ1_V_tps'}];
135    Data.VarDimName=[Data.VarDimName {'NbVec1','NbVec1',{'NbSubDomain1','Two'},{'NbSubDomain1','Two'},'NbSubDomain1',...
136             {'NbVec1Sub','NbSubDomain1'},{'NbVec1Sub','NbSubDomain1'},{'Nbtps1','NbSubDomain1'},{'Nbtps1','NbSubDomain1'}}];
137    nbvar=length(Data.ListVarName);
138    Data.VarAttribute{nbvar-1}.Role='vector_x';
139    Data.VarAttribute{nbvar}.Role='vector_y';
140    Data.Civ1_U_Diff=zeros(size(Data.Civ1_X));
141    Data.Civ1_V_Diff=zeros(size(Data.Civ1_X));
142    if isfield(Data,'Civ1_FF')
143        ind_good=find(Data.Civ1_FF==0);
144    else
145        ind_good=1:numel(Data.Civ1_X);
146    end
147    [Data.Civ1_X_SubRange,Data.Civ1_Y_SubRange,Data.Civ1_NbSites,FFres,Ures, Vres,Data.Civ1_X_tps,Data.Civ1_Y_tps,Data.Civ1_U_tps,Data.Civ1_V_tps]=...
148                            patch_uvmat(Data.Civ1_X(ind_good)',Data.Civ1_Y(ind_good)',Data.Civ1_U(ind_good)',Data.Civ1_V(ind_good)',Data.Patch1_Rho,Data.Patch1_Threshold,Data.Patch1_SubDomain);
149      Data.Civ1_U_Diff(ind_good)=Data.Civ1_U(ind_good)-Ures;
150      Data.Civ1_V_Diff(ind_good)=Data.Civ1_V(ind_good)-Vres;
151      Data.Civ1_FF(ind_good)=FFres;
152      Data.CivStage=3;                             
153end   
154
155%% Civ2
156if isfield (Param,'Civ2')
157    par_civ2=Param.Civ2
158    image1=imread(par_civ2.filename_ima_a);
159    image2=imread(par_civ2.filename_ima_b);
160    stepx=str2num(par_civ2.dx);
161    stepy=str2num(par_civ2.dy);
162    ibx2=ceil(str2num(par_civ2.ibx)/2);
163    iby2=ceil(str2num(par_civ2.iby)/2);
164    isx2=4;
165    isy2=4;
166%     shiftx=str2num(par_civ1.shiftx);
167%     shifty=str2num(par_civ1.shifty);
168% TO GET shift from par_civ2.filename_nc1
169    miniy=max(1+isy2+shifty,1+iby2);
170    minix=max(1+isx2-shiftx,1+ibx2);
171    maxiy=min(size(image1,1)-isy2+shifty,size(image1,1)-iby2);
172    maxix=min(size(image1,2)-isx2-shiftx,size(image1,2)-ibx2);
173    [GridX,GridY]=meshgrid(minix:stepx:maxix,miniy:stepy:maxiy);
174    PointCoord(:,1)=reshape(GridX,[],1);
175    PointCoord(:,2)=reshape(GridY,[],1);
176    if ~isempty(par_civ2.maskname)&& ~strcmp(maskname,par_civ2.maskname)% mask exist, not already read in civ1
177        mask=imread(par_civ2.maskname);
178    end
179    % caluclate velocity data (y and v in indices, reverse to y component)
180    [xtable ytable utable vtable ctable F] = pivlab (image1,image2,ibx2,iby2,isx2,isy2,shiftx,-shifty,PointCoord,str2num(par_civ1.rho),mask);
181    list_param=(fieldnames(par_civ1))';
182    list_remove={'pxcmx','pxcmy','npx','npy','gridflag','maskflag','term_a','term_b','T0'};
183    index=zeros(size(list_param));
184    for ilist=1:length(list_remove)
185        index=strcmp(list_remove{ilist},list_param);
186        if ~isempty(find(index,1))
187            list_param(index)=[];
188        end
189    end
190    for ilist=1:length(list_param)
191        Civ1_param{ilist}=['Civ1_' list_param{ilist}];
192        eval(['Data.Civ1_' list_param{ilist} '=Param.Civ1.' list_param{ilist} ';'])
193    end
194    if isfield(Data,'Civ1_gridname') && strcmp(Data.Civ1_gridname(1:6),'noFile')
195        Data.Civ1_gridname='';
196    end
197    if isfield(Data,'Civ1_maskname') && strcmp(Data.Civ1_maskname(1:6),'noFile')
198        Data.Civ1_maskname='';
199    end
200    Data.ListGlobalAttribute=[Data.ListGlobalAttribute Civ1_param {'Civ1_Time','Civ1_Dt'}];
201    Data.Civ1_Time=str2double(par_civ1.T0);
202    Data.Civ1_Dt=str2double(par_civ1.Dt);
203    Data.ListVarName={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_C','Civ1_F'};%  cell array containing the names of the fields to record
204    Data.VarDimName={'nbvec1','nbvec1','nbvec1','nbvec1','nbvec1','nbvec1'};
205    Data.VarAttribute{1}.Role='coord_x';
206    Data.VarAttribute{2}.Role='coord_y';
207    Data.VarAttribute{3}.Role='vector_x';
208    Data.VarAttribute{4}.Role='vector_y';
209    Data.VarAttribute{5}.Role='warnflag';
210    Data.Civ1_X=reshape(xtable,[],1);
211    Data.Civ1_Y=reshape(size(image1,1)-ytable+1,[],1);
212    Data.Civ1_U=reshape(utable,[],1);
213    Data.Civ1_V=reshape(-vtable,[],1);
214    Data.Civ1_C=reshape(ctable,[],1);
215    Data.Civ1_F=reshape(F,[],1);
216    Data.CivStage=Data.CivStage+1;
217end
218%% write result
219% 'TESTcalc'
220% [DataOut,errormsg]=calc_field('velocity',Data)
221if exist('ncfile','var')
222errormsg=struct2nc(ncfile,Data);
223end
224
225
226%'RUN_FIX': function for fixing velocity fields:
227%-----------------------------------------------
228% RUN_FIX(filename,field,flagindex,thresh_vecC,thresh_vel,iter,flag_mask,maskname,fileref,fieldref)
229%
230%filename: name of the netcdf file (used as input and output)
231%field: structure specifying the names of the fields to fix (depending on civ1 or civ2)
232    %.vel_type='civ1' or 'civ2';
233    %.nb=name of the dimension common to the field to fix ('nb_vectors' for civ1);
234    %.fixflag=name of fix flag variable ('vec_FixFlag' for civ1)
235%flagindex: flag specifying which values of vec_f are removed:
236        % if flagindex(1)=1: vec_f=-2 vectors are removed
237        % if flagindex(2)=1: vec_f=3 vectors are removed
238        % if flagindex(3)=1: vec_f=2 vectors are removed (if iter=1) or vec_f=4 vectors are removed (if iter=2)
239%iter=1 for civ1 fields and iter=2 for civ2 fields
240%thresh_vecC: threshold in the image correlation vec_C
241%flag_mask: =1 mask used to remove vectors (0 else)
242%maskname: name of the mask image file for fix
243%thresh_vel: threshold on velocity, or on the difference with the reference file fileref if exists
244%inf_sup=1: remove values smaller than threshold thresh_vel, =2, larger than threshold
245%fileref: .nc file name for a reference velocity (='': refrence 0 used)
246%fieldref: 'civ1','filter1'...feld used in fileref
247
248function FF=fix_uvmat(Param,F,C,U,V)
249%error=[]; %default
250FF=zeros(size(F));%default
251
252%criterium on warn flags
253if isfield (Param,'WarnFlags')
254    for iflag=1:numel(Param.WarnFlags)
255        FF=(FF==1| F==Param.WarnFlags(iflag));
256    end
257end
258
259%criterium on correlation values
260if isfield (Param,'LowerBoundCorr')
261    FF=FF==1 | C<Param.LowerBoundCorr;
262end
263
264if isfield (Param,'LowerBoundVel')&& ~isequal(Param.LowerBoundVel,0)
265    thresh=Param.LowerBoundVel*Param.LowerBoundVel;
266    FF=FF==1 | (U.*U+V.*V)<thresh;
267end
268if isfield (Param,'UpperBoundVel')&& ~isequal(Param.UpperBoundVel,0)
269    thresh=Param.UpperBoundVel*Param.UpperBoundVel;
270    FF=FF==1 | (U.*U+V.*V)>thresh;
271end
272FF=double(FF);
273%
274% % criterium on velocity values
275% delta_u=Field.U;%default without ref file
276% delta_v=Field.V;
277% if exist('fileref','var') && ~isempty(fileref)
278%     if ~exist(fileref,'file')
279%         error='reference file not found in RUN_FIX.m';
280%         display(error);
281%         return
282%     end
283%     FieldRef=read_civxdata(fileref,[],fieldref);   
284%     if isfield(FieldRef,'FF')
285%         index_true=find(FieldRef.FF==0);
286%         FieldRef.X=FieldRef.X(index_true);
287%         FieldRef.Y=FieldRef.Y(index_true);
288%         FieldRef.U=FieldRef.U(index_true);
289%         FieldRef.V=FieldRef.V(index_true);
290%     end
291%     if ~isfield(FieldRef,'X') || ~isfield(FieldRef,'Y') || ~isfield(FieldRef,'U') || ~isfield(FieldRef,'V')
292%         error='reference file is not a velocity field in RUN_FIX.m '; %bad input file
293%         return
294%     end
295%     if length(FieldRef.X)<=1
296%         errordlg('reference field with one vector or less in RUN_FIX.m')
297%         return
298%     end
299%     vec_U_ref=griddata_uvmat(FieldRef.X,FieldRef.Y,FieldRef.U,Field.X,Field.Y);  %interpolate vectors in the ref field
300%     vec_V_ref=griddata_uvmat(FieldRef.X,FieldRef.Y,FieldRef.V,Field.X,Field.Y);  %interpolate vectors in the ref field to the positions  of the main field     
301%     delta_u=Field.U-vec_U_ref;%take the difference with the interpolated ref field
302%     delta_v=Field.V-vec_V_ref;
303% end
304% thresh_vel_x=thresh_vel;
305% thresh_vel_y=thresh_vel;
306% if isequal(inf_sup,1)
307%     flag5=abs(delta_u)<thresh_vel_x & abs(delta_v)<thresh_vel_y &(flag1~=1)&(flag2~=1)&(flag3~=1)&(flag4~=1);
308% elseif isequal(inf_sup,2)
309%     flag5=(abs(delta_u)>thresh_vel_x | abs(delta_v)>thresh_vel_y) &(flag1~=1)&(flag2~=1)&(flag3~=1)&(flag4~=1);
310% end
311%
312%             % flag7 introduce a grey mask, matrix M
313% if isequal (flag_mask,1)
314%    M=imread(maskname);
315%    nxy=size(M);
316%    M=reshape(M,1,nxy(1)*nxy(2));
317%    rangx0=[0.5 nxy(2)-0.5];
318%    rangy0=[0.5 nxy(1)-0.5];
319%    vec_x1=Field.X-Field.U/2;%beginning points
320%    vec_x2=Field.X+Field.U/2;%end points of vectors
321%    vec_y1=Field.Y-Field.V/2;%beginning points
322%    vec_y2=Field.Y+Field.V/2;%end points of vectors
323%    indx=1+round((nxy(2)-1)*(vec_x1-rangx0(1))/(rangx0(2)-rangx0(1)));% image index x at abcissa vec_x
324%    indy=1+round((nxy(1)-1)*(vec_y1-rangy0(1))/(rangy0(2)-rangy0(1)));% image index y at ordinate vec_y   
325%    test_in=~(indx < 1 |indy < 1 | indx > nxy(2) |indy > nxy(1)); %=0 out of the mask image, 1 inside
326%    indx=indx.*test_in+(1-test_in); %replace indx by 1 out of the mask range
327%    indy=indy.*test_in+(1-test_in); %replace indy by 1 out of the mask range
328%    ICOMB=((indx-1)*nxy(1)+(nxy(1)+1-indy));%determine the indices in the image reshaped in a Matlab vector
329%    Mvalues=M(ICOMB);
330%    flag7b=((20 < Mvalues) & (Mvalues < 200))| ~test_in';
331%    indx=1+round((nxy(2)-1)*(vec_x2-rangx0(1))/(rangx0(2)-rangx0(1)));% image index x at abcissa Field.X
332%    indy=1+round((nxy(1)-1)*(vec_y2-rangy0(1))/(rangy0(2)-rangy0(1)));% image index y at ordinate vec_y
333%    test_in=~(indx < 1 |indy < 1 | indx > nxy(2) |indy > nxy(1)); %=0 out of the mask image, 1 inside
334%    indx=indx.*test_in+(1-test_in); %replace indx by 1 out of the mask range
335%    indy=indy.*test_in+(1-test_in); %replace indy by 1 out of the mask range
336%    ICOMB=((indx-1)*nxy(1)+(nxy(1)+1-indy));%determine the indices in the image reshaped in a Matlab vector
337%    Mvalues=M(ICOMB);
338%    flag7e=((Mvalues > 20) & (Mvalues < 200))| ~test_in';
339%    flag7=(flag7b|flag7e)';
340% else
341%    flag7=0;
342% end   
343% flagmagenta=flag1|flag2|flag3|flag4|flag5|flag7;
344% fixflag_unit=Field.FF-10*floor(Field.FF/10); %unity term of fix_flag
345
346
347
348%------------------------------------------------------------------------
349% patch function
350function [SubRangx,SubRangy,nbpoints,FF,U_smooth,V_smooth,X_tps,Y_tps,U_tps,V_tps] =patch_uvmat(X,Y,U,V,Rho,Threshold,SubDomain)
351%subdomain decomposition
352warning off
353U=reshape(U,[],1);
354V=reshape(V,[],1);
355X=reshape(X,[],1);
356Y=reshape(Y,[],1);
357nbvec=numel(X);
358NbSubDomain=ceil(nbvec/SubDomain);
359MinX=min(X);
360MinY=min(Y);
361MaxX=max(X);
362MaxY=max(Y);
363RangX=MaxX-MinX;
364RangY=MaxY-MinY;
365AspectRatio=RangY/RangX;
366NbSubDomainX=max(floor(sqrt(NbSubDomain/AspectRatio)),1);
367NbSubDomainY=max(floor(sqrt(NbSubDomain*AspectRatio)),1);
368NbSubDomain=NbSubDomainX*NbSubDomainY;
369SizX=RangX/NbSubDomainX;%width of subdomains
370SizY=RangY/NbSubDomainY;%height of subdomains
371CentreX=linspace(MinX+SizX/2,MaxX-SizX/2,NbSubDomainX);
372CentreY=linspace(MinY+SizY/2,MaxY-SizY/2,NbSubDomainY);
373[CentreX,CentreY]=meshgrid(CentreX,CentreY);
374CentreY=reshape(CentreY,1,[]);
375CentreX=reshape(CentreX,1,[]);
376rho=SizX*SizY*Rho/1000000;%optimum rho increase as the area of the subdomain (division by 10^6 to reach good values with the default GUI input)
377U_tps_sub=zeros(length(X),NbSubDomain);%default spline
378V_tps_sub=zeros(length(X),NbSubDomain);%default spline
379U_smooth=zeros(length(X),1);
380V_smooth=zeros(length(X),1);
381
382nb_select=zeros(length(X),1);
383FF=zeros(length(X),1);
384test_empty=zeros(1,NbSubDomain);
385for isub=1:NbSubDomain
386    SubRangx(isub,:)=[CentreX(isub)-SizX/2 CentreX(isub)+SizX/2];
387    SubRangy(isub,:)=[CentreY(isub)-SizY/2 CentreY(isub)+SizY/2];
388    ind_sel_previous=[];
389    ind_sel=0;
390    while numel(ind_sel)>numel(ind_sel_previous) %increase the subdomain during four iterations at most
391        ind_sel_previous=ind_sel;
392        ind_sel=find(X>SubRangx(isub,1) & X<SubRangx(isub,2) & Y>SubRangy(isub,1) & Y<SubRangy(isub,2));
393        % if no vector in the subdomain, skip the subdomain
394        if isempty(ind_sel)
395            test_empty(isub)=1;   
396            U_tps(1,isub)=0;%define U_tps and V_tps by default
397            V_tps(1,isub)=0;
398            break
399            % if too few selected vectors, increase the subrange for next iteration
400        elseif numel(ind_sel)<SubDomain/4 && ~isequal( ind_sel,ind_sel_previous);
401            SubRangx(isub,1)=SubRangx(isub,1)-SizX/4;
402            SubRangx(isub,2)=SubRangx(isub,2)+SizX/4;
403            SubRangy(isub,1)=SubRangy(isub,1)-SizY/4;
404            SubRangy(isub,2)=SubRangy(isub,2)+SizY/4;
405        else
406            [U_smooth_sub,U_tps_sub]=tps_coeff(X(ind_sel),Y(ind_sel),U(ind_sel),rho);
407            [V_smooth_sub,V_tps_sub]=tps_coeff(X(ind_sel),Y(ind_sel),V(ind_sel),rho);
408            UDiff=U_smooth_sub-U(ind_sel);
409            VDiff=V_smooth_sub-V(ind_sel);
410            NormDiff=UDiff.*UDiff+VDiff.*VDiff;
411            FF(ind_sel)=20*(NormDiff>Threshold);%put FF value to 20 to identify the criterium of elimmination
412            ind_ind_sel=find(FF(ind_sel)==0); % select the indices of ind_sel corresponding to the remaining vectors
413            % no value exceeds threshold, the result is recorded
414            if isequal(numel(ind_ind_sel),numel(ind_sel))
415                U_smooth(ind_sel)=U_smooth(ind_sel)+U_smooth_sub;
416                V_smooth(ind_sel)=V_smooth(ind_sel)+V_smooth_sub;
417                nbpoints(isub)=numel(ind_sel);
418                X_tps(1:nbpoints(isub),isub)=X(ind_sel);
419                Y_tps(1:nbpoints(isub),isub)=Y(ind_sel);
420                U_tps(1:nbpoints(isub)+3,isub)=U_tps_sub;
421                V_tps(1:nbpoints(isub)+3,isub)=V_tps_sub;         
422                nb_select(ind_sel)=nb_select(ind_sel)+1;
423                 display('good')
424                break
425                % too few selected vectors, increase the subrange for next iteration
426            elseif numel(ind_ind_sel)<SubDomain/4 && ~isequal( ind_sel,ind_sel_previous);
427                SubRangx(isub,1)=SubRangx(isub,1)-SizX/4;
428                SubRangx(isub,2)=SubRangx(isub,2)+SizX/4;
429                SubRangy(isub,1)=SubRangy(isub,1)-SizY/4;
430                SubRangy(isub,2)=SubRangy(isub,2)+SizY/4;
431%                 display('fewsmooth')
432                % interpolation-smoothing is done again with the selected vectors
433            else
434                [U_smooth_sub,U_tps_sub]=tps_coeff(X(ind_sel(ind_ind_sel)),Y(ind_sel(ind_ind_sel)),U(ind_sel(ind_ind_sel)),rho);
435                [V_smooth_sub,V_tps_sub]=tps_coeff(X(ind_sel(ind_ind_sel)),Y(ind_sel(ind_ind_sel)),V(ind_sel(ind_ind_sel)),rho);
436                U_smooth(ind_sel(ind_ind_sel))=U_smooth(ind_sel(ind_ind_sel))+U_smooth_sub;
437                V_smooth(ind_sel(ind_ind_sel))=V_smooth(ind_sel(ind_ind_sel))+V_smooth_sub;
438                nbpoints(isub)=numel(ind_ind_sel);
439                X_tps(1:nbpoints(isub),isub)=X(ind_sel(ind_ind_sel));
440                Y_tps(1:nbpoints(isub),isub)=Y(ind_sel(ind_ind_sel));
441                U_tps(1:nbpoints(isub)+3,isub)=U_tps_sub;
442                V_tps(1:nbpoints(isub)+3,isub)=V_tps_sub;
443                nb_select(ind_sel(ind_ind_sel))=nb_select(ind_sel(ind_ind_sel))+1;
444                display('good2')
445                break
446            end
447        end
448    end
449end
450ind_empty=find(test_empty);
451%remove empty subdomains
452if ~isempty(ind_empty)
453    SubRangx(ind_empty,:)=[];
454    SubRangy(ind_empty,:)=[];
455    X_tps(:,ind_empty)=[];
456    Y_tps(:,ind_empty)=[];
457    U_tps(:,ind_empty)=[];
458    V_tps(:,ind_empty)=[];
459end
460nb_select(nb_select==0)=1;%ones(size(find(nb_select==0)));
461U_smooth=U_smooth./nb_select;
462V_smooth=V_smooth./nb_select;
463
464
465
466
467
468
469% U_patch = EM * spline_coeff;
470% U_patch=reshape(U_patch,npy,npx);
471% PM = [ones(size(dsites,1),1) dsites];
472% EM = [IM_sites PM];
473% U(test_false)=[];
474% U_nodes=EM * spline_coeff;
475
476%exact = testfunctions(epoints);
477%maxerr = norm(Pf-exact,inf);
478% PlotSurf(xe,ye,Pf,neval,exact,maxerr,[160,20]);
479% PlotError2D(xe,ye,Pf,exact,maxerr,neval,[160,20]);
480
481
482
483  % DM = DistanceMatrix(dsites,ctrs)
484% Forms the distance matrix of two sets of points in R^s,
485% i.e., DM(i,j) = || datasite_i - center_j ||_2.
486% Input
487%   dsites: Mxs matrix representing a set of M data sites in R^s
488%              (i.e., each row contains one s-dimensional point)
489%   ctrs:   Nxs matrix representing a set of N centers in R^s
490%              (one center per row)
491% Output
492
493
494
495
Note: See TracBrowser for help on using the repository browser.