source: trunk/src/series/civ_series.m @ 605

Last change on this file since 605 was 605, checked in by sommeria, 8 years ago

bugs corrected to get an advancement bar with status

File size: 42.9 KB
Line 
1%'civ_series': PIV function activated by the general GUI series
2% --- call the sub-functions:
3%   civ: PIV function itself
4%   fix: removes false vectors after detection by various criteria
5%   filter_tps: make interpolation-smoothing
6%------------------------------------------------------------------------
7% function [Data,errormsg,result_conv]= civ_series(Param,ncfile)
8%
9%OUTPUT
10% Data=structure containing the PIV results and information on the processing parameters
11% errormsg=error message char string, default=''
12% resul_conv: image inter-correlation function for the last grid point (used for tests)
13%
14%INPUT:
15% Param: input images and processing parameters
16%     .Civ1: for civ1
17%     .Fix1:
18%     .Patch1:
19%     .Civ2: for civ2
20%     .Fix2:
21%     .Patch2:
22% ncfile: name of a netcdf file to be created for the result (extension .nc)
23%
24%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
25%  Copyright  2011, LEGI / CNRS-UJF-INPG, joel.sommeria@legi.grenoble-inp.fr.
26%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
27%     This is part of the toolbox UVMAT.
28%
29%     UVMAT is free software; you can redistribute it and/or modify
30%     it under the terms of the GNU General Public License as published by
31%     the Free Software Foundation; either version 2 of the License, or
32%     (at your option) any later version.
33%
34%     UVMAT is distributed in the hope that it will be useful,
35%     but WITHOUT ANY WARRANTY; without even the implied warranty of
36%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37%     GNU General Public License (open UVMAT/COPYING.txt) for more details.
38%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
39
40function [Data,errormsg,result_conv]= civ_series(Param,ncfile)
41errormsg='';
42path_series=fileparts(which('series'));
43addpath(fullfile(path_series,'series'))
44%% set the input elements needed on the GUI series when the action is selected in the menu ActionName
45if isstruct(Param) && isequal(Param.Action.RUN,0)
46    Data=civ_input(Param);% introduce the civ parameters using the GUI civ_input
47    Data.Program=mfilename;
48    Data.AllowInputSort='off';...% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default)
49        Data.WholeIndexRange='off';...% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default)
50        Data.NbSlice='off'; ...%nbre of slices ('off' by default)
51        Data.VelType='off';...% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
52        Data.FieldName='off';...% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
53        Data.FieldTransform = 'off';...%can use a transform function
54        Data.ProjObject='off';...%can use projection object(option 'off'/'on',
55        Data.Mask='off';...%can use mask option   (option 'off'/'on', 'off' by default)
56        Data.OutputDirExt='.civ';%set the output dir extension
57       
58    return
59end
60
61%% read input parameters from an xml file if input is a file name (batch mode)
62checkrun=1;
63if ischar(Param)
64    Param=xml2struct(Param);% read Param as input file (batch case)
65    checkrun=0;
66end
67
68%% input files and indexing
69NbField=1;
70if isfield(Param,'InputTable')
71    RootPath=Param.InputTable{1,1};
72    RootFile=Param.InputTable{1,3};
73    SubDir=Param.InputTable{1,2};
74    NomType=Param.InputTable{1,4};
75    FileExt=Param.InputTable{1,5};
76    PairCiv1=Param.ActionInput.PairIndices.ListPairCiv1;
77    PairCiv2='';
78    if isfield(Param.ActionInput.PairIndices,'ListPairCiv2')
79        PairCiv2=Param.ActionInput.PairIndices.ListPairCiv2;
80    end
81    MaxIndex=cell2mat(Param.IndexRange.MaxIndex);
82    MinIndex=cell2mat(Param.IndexRange.MinIndex);
83    [filecell,i_series,tild,j_series]=get_file_series(Param);
84    [i1_series_Civ1,i2_series_Civ1,j1_series_Civ1,j2_series_Civ1,check_bounds,NomTypeNc]=...
85        find_pair_indices(PairCiv1,i_series{1},j_series{1},MinIndex,MaxIndex);
86    if ~isempty(PairCiv2)
87        [i1_series_Civ2,i2_series_Civ2,j1_series_Civ2,j2_series_Civ2,check_bounds_Civ2]=...
88            find_pair_indices(PairCiv2,i_series{1},j_series{1},MinIndex,MaxIndex);
89        check_bounds=check_bounds | check_bounds_Civ2;
90    end
91    i1_series_Civ1=i1_series_Civ1(~check_bounds);
92    i2_series_Civ1=i2_series_Civ1(~check_bounds);
93    j1_series_Civ1=j1_series_Civ1(~check_bounds);
94    j2_series_Civ1=j2_series_Civ1(~check_bounds);
95    if ~isempty(j1_series_Civ1)
96        FrameIndex_A_Civ1=j1_series_Civ1;
97        FrameIndex_B_Civ1=j2_series_Civ1;
98    else
99        FrameIndex_A_Civ1=i1_series_Civ1;
100        FrameIndex_B_Civ1=i2_series_Civ1;
101    end
102    if ~isempty(PairCiv2)
103        i1_series_Civ2=i1_series_Civ2(~check_bounds);
104        i2_series_Civ2=i2_series_Civ2(~check_bounds);
105        j1_series_Civ2=j1_series_Civ2(~check_bounds);
106        j2_series_Civ2=j2_series_Civ2(~check_bounds);
107        if ~isempty(j1_series_Civ2)
108            FrameIndex_A_Civ2=j1_series_Civ2;
109            FrameIndex_B_Civ2=j2_series_Civ2;
110        else
111            FrameIndex_A_Civ2=i1_series_Civ2;
112            FrameIndex_B_Civ2=i2_series_Civ2;
113        end
114    end
115   
116    NbField=numel(i1_series_Civ1);
117    ImageTypeOptions={'image','multimage','mmreader','video'};
118    [FileType_A,FileInfo,MovieObject_A]=get_file_type(filecell{1,1});
119    FileType_B=FileType_A;
120    MovieObject_B=MovieObject_A;
121    if size(filecell,1)>=2 && ~strcmp(filecell{1,1},filecell{2,1})
122        [FileType_B,FileInfo,MovieObject_B]=get_file_type(filecell{2,1});
123        CheckImage_B=~isempty(find(strcmp(FileType,ImageTypeOptions)));% =1 for images
124    end
125end
126
127
128%% Output directory
129OutputDir=[Param.OutputSubDir Param.OutputDirExt];
130
131Data.ListGlobalAttribute={'Conventions','Program','CivStage'};
132Data.Conventions='uvmat/civdata';% states the conventions used for the description of field variables and attributes
133Data.Program='civ_series';
134Data.CivStage=0;%default
135ListVarCiv1={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_C','Civ1_F'}; %variables to read
136ListVarFix1={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_C','Civ1_F','Civ1_FF'};
137mask='';
138maskname='';%default
139check_civx=0;%default
140check_civ1=0;%default
141check_patch1=0;%default
142
143
144
145
146
147%%%%% MAIN LOOP %%%%%%
148
149MovieObject_A=[];
150for ifield=1:NbField
151   
152    %% Civ1
153    if isfield (Param.ActionInput,'Civ1')
154        par_civ1=Param.ActionInput.Civ1;
155        if isfield(par_civ1,'reverse_pair')% A REVOIR
156            if par_civ1.reverse_pair
157                if ischar(par_civ1.ImageB)
158                    temp=par_civ1.ImageA;
159                    par_civ1.ImageA=imread(par_civ1.ImageB);
160                end
161                if ischar(temp)
162                    par_civ1.ImageB=imread(temp);
163                end
164            end
165        else
166            %         if ~isfield(Param.Civ1,'ImageA')
167            ImageName_A=fullfile_uvmat(RootPath,SubDir,RootFile,FileExt,NomType,i1_series_Civ1(ifield),[],j1_series_Civ1(ifield));
168            [par_civ1.ImageA,MovieObject_A] = read_image(ImageName_A,FileType_A,MovieObject_A,FrameIndex_A_Civ1(ifield));
169            %         elseif ischar(Param.Civ1.ImageA)
170            %             Param.Civ1.ImageA=regexprep(Param.Civ1.ImageA,'''','\');
171            %             [par_civ1.ImageA,VideoObject] = read_image(Param.Civ1.ImageA,par_civ1.FileTypeA,MovieObject_A,par_civ1.FrameIndexA);
172            %         end
173            %         if ~isfield(Param.Civ1,'ImageB')
174            ImageName_B=fullfile_uvmat(RootPath,SubDir,RootFile,FileExt,NomType,i2_series_Civ1(ifield),[],j2_series_Civ1(ifield));
175            [par_civ1.ImageB,MovieObject_B] = read_image(ImageName_B,FileType_B,MovieObject_B,FrameIndex_B_Civ1(ifield));
176            %         elseif isfield(Param.Civ1,'ImageB')&& ischar(Param.Civ1.ImageB)
177            %              Param.Civ1.ImageB=regexprep(Param.Civ1.ImageB,'''','\');
178            %              if strcmp(Param.Civ1.ImageA,Param.Civ1.ImageB)% use the same movie object
179            %                  [par_civ1.ImageB,VideoObject] = read_image(Param.Civ1.ImageB,par_civ1.FileTypeB,VideoObject,par_civ1.FrameIndexB);
180            %              else
181            %             [par_civ1.ImageB,VideoObject] = read_image(Param.Civ1.ImageB,par_civ1.FileTypeB,par_civ1.ImageB,par_civ1.FrameIndexB);
182            %              end
183            %         end
184        end
185        ncfile=fullfile_uvmat(RootPath,OutputDir,RootFile,'.nc',NomTypeNc,i1_series_Civ1(ifield),i2_series_Civ1(ifield),...
186            j1_series_Civ1(ifield),j2_series_Civ1(ifield));
187        par_civ1.ImageWidth=FileInfo.Width;
188        par_civ1.ImageHeight=FileInfo.Height;
189        list_param=(fieldnames(Param.ActionInput.Civ1))';
190        Civ1_param=list_param;%default
191       
192        %set the values of all the global attributes in list_param 
193        for ilist=1:length(list_param)
194            Civ1_param{ilist}=['Civ1_' list_param{ilist}];
195            Data.(['Civ1_' list_param{ilist}])=Param.ActionInput.Civ1.(list_param{ilist});
196        end
197        Data.ListGlobalAttribute=[Data.ListGlobalAttribute Civ1_param];
198        Data.CivStage=1;
199       
200        % set the list of variables
201        Data.ListVarName={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_F','Civ1_C'};%  cell array containing the names of the fields to record
202        Data.VarDimName={'nb_vec_1','nb_vec_1','nb_vec_1','nb_vec_1','nb_vec_1','nb_vec_1'};
203        Data.VarAttribute{1}.Role='coord_x';
204        Data.VarAttribute{2}.Role='coord_y';
205        Data.VarAttribute{3}.Role='vector_x';
206        Data.VarAttribute{4}.Role='vector_y';
207        Data.VarAttribute{5}.Role='warnflag';
208       
209        if strcmp(Param.ActionInput.ListCompareMode, 'PIV volume')
210            Data.ListVarName=[Data.ListVarName 'Civ1_Z'];
211            Data.Civ1_X=[];Data.Civ1_Y=[];Data.Civ1_Z=[];
212            Data.Civ1_U=[];Data.Civ1_V=[];Data.Civ1_C=[];Data.Civ1_F=[];
213            for ivol=1:NbSlice
214                % caluclate velocity data (y and v in indices, reverse to y component)
215                [xtable ytable utable vtable ctable F result_conv errormsg] = civ (par_civ1);
216                if ~isempty(errormsg)
217                    return
218                end
219                Data.Civ1_X=[Data.Civ1_X reshape(xtable,[],1)];
220                Data.Civ1_Y=[Data.Civ1_Y reshape(Param.Civ1.ImageHeight-ytable+1,[],1)];
221                Data.Civ1_Z=[Data.Civ1_Z ivol*ones(numel(xtable),1)];% z=image index in image coordinates
222                Data.Civ1_U=[Data.Civ1_U reshape(utable,[],1)];
223                Data.Civ1_V=[Data.Civ1_V reshape(-vtable,[],1)];
224                Data.Civ1_C=[Data.Civ1_C reshape(ctable,[],1)];
225                Data.Civ1_F=[Data.Civ1_C reshape(F,[],1)];
226            end
227        else %usual PIV
228            % caluclate velocity data (y and v in indices, reverse to y component)
229            [xtable ytable utable vtable ctable F result_conv errormsg] = civ (par_civ1);
230            if ~isempty(errormsg)
231                return
232            end
233            Data.Civ1_X=reshape(xtable,[],1);
234            Data.Civ1_Y=reshape(par_civ1.ImageHeight-ytable+1,[],1);
235            Data.Civ1_U=reshape(utable,[],1);
236            Data.Civ1_V=reshape(-vtable,[],1);
237            Data.Civ1_C=reshape(ctable,[],1);
238            Data.Civ1_F=reshape(F,[],1);
239        end
240    else
241        if exist('ncfile','var')
242            CivFile=ncfile;
243        elseif isfield(Param.Patch1,'CivFile')
244            CivFile=Param.Patch1.CivFile;
245        end
246        Data=nc2struct(CivFile,'ListGlobalAttribute','absolut_time_T0'); %look for the constant 'absolut_time_T0' to detect old civx data format
247        if isfield(Data,'Txt')
248            errormsg=Data.Txt;
249            return
250        end
251        if ~isempty(Data.absolut_time_T0')%read civx file
252            check_civx=1;% test for old civx data format
253            [Data,vardetect,ichoice]=nc2struct(CivFile);%read the variables in the netcdf file
254        else
255            Data=nc2struct(CivFile);%read civ1 and fix1 data in the existing netcdf file
256        end
257    end
258   
259    %% Fix1
260    if isfield (Param.ActionInput,'Fix1')
261        ListFixParam=fieldnames(Param.ActionInput.Fix1);
262        for ilist=1:length(ListFixParam)
263            ParamName=ListFixParam{ilist};
264            ListName=['Fix1_' ParamName];
265            eval(['Data.ListGlobalAttribute=[Data.ListGlobalAttribute ''' ParamName '''];'])
266            eval(['Data.' ListName '=Param.ActionInput.Fix1.' ParamName ';'])
267        end
268        if check_civx
269            if ~isfield(Data,'fix')
270                Data.ListGlobalAttribute=[Data.ListGlobalAttribute 'fix'];
271                Data.fix=1;
272                Data.ListVarName=[Data.ListVarName {'vec_FixFlag'}];
273                Data.VarDimName=[Data.VarDimName {'nb_vectors'}];
274            end
275            Data.vec_FixFlag=fix(Param.ActionInput.Fix1,Data.vec_F,Data.vec_C,Data.vec_U,Data.vec_V,Data.vec_X,Data.vec_Y);
276        else
277            Data.ListVarName=[Data.ListVarName {'Civ1_FF'}];
278            Data.VarDimName=[Data.VarDimName {'nb_vec_1'}];
279            nbvar=length(Data.ListVarName);
280            Data.VarAttribute{nbvar}.Role='errorflag';
281            Data.Civ1_FF=fix(Param.ActionInput.Fix1,Data.Civ1_F,Data.Civ1_C,Data.Civ1_U,Data.Civ1_V);
282            Data.CivStage=2;
283        end
284    end
285    %% Patch1
286    if isfield (Param.ActionInput,'Patch1')
287        if check_civx
288            errormsg='Civ Matlab input needed for patch';
289            return
290        end
291       
292        Data.ListGlobalAttribute=[Data.ListGlobalAttribute {'Patch1_Rho','Patch1_Threshold','Patch1_SubDomain'}];
293        Data.Patch1_FieldSmooth=Param.ActionInput.Patch1.FieldSmooth;
294        Data.Patch1_MaxDiff=Param.ActionInput.Patch1.MaxDiff;
295        Data.Patch1_SubDomainSize=Param.ActionInput.Patch1.SubDomainSize;
296        nbvar=length(Data.ListVarName);
297        Data.ListVarName=[Data.ListVarName {'Civ1_U_smooth','Civ1_V_smooth','Civ1_SubRange','Civ1_NbCentres','Civ1_Coord_tps','Civ1_U_tps','Civ1_V_tps'}];
298        Data.VarDimName=[Data.VarDimName {'nb_vec_1','nb_vec_1',{'nb_coord','nb_bounds','nb_subdomain_1'},'nb_subdomain_1',...
299            {'nb_tps_1','nb_coord','nb_subdomain_1'},{'nb_tps_1','nb_subdomain_1'},{'nb_tps_1','nb_subdomain_1'}}];
300        Data.VarAttribute{nbvar+1}.Role='vector_x';
301        Data.VarAttribute{nbvar+2}.Role='vector_y';
302        Data.VarAttribute{nbvar+5}.Role='coord_tps';
303        Data.VarAttribute{nbvar+6}.Role='vector_x';
304        Data.VarAttribute{nbvar+7}.Role='vector_y';
305        Data.Civ1_U_smooth=zeros(size(Data.Civ1_X));
306        Data.Civ1_V_smooth=zeros(size(Data.Civ1_X));
307        if isfield(Data,'Civ1_FF')
308            ind_good=find(Data.Civ1_FF==0);
309        else
310            ind_good=1:numel(Data.Civ1_X);
311        end
312        [Data.Civ1_SubRange,Data.Civ1_NbCentres,Data.Civ1_Coord_tps,Data.Civ1_U_tps,Data.Civ1_V_tps,tild,Ures, Vres,tild,FFres]=...
313            filter_tps([Data.Civ1_X(ind_good) Data.Civ1_Y(ind_good)],Data.Civ1_U(ind_good),Data.Civ1_V(ind_good),[],Data.Patch1_SubDomainSize,Data.Patch1_FieldSmooth,Data.Patch1_MaxDiff);
314        Data.Civ1_U_smooth(ind_good)=Ures;
315        Data.Civ1_V_smooth(ind_good)=Vres;
316        Data.Civ1_FF(ind_good)=FFres;
317        Data.CivStage=3;
318    end
319   
320    %% Civ2
321    if isfield (Param.ActionInput,'Civ2')
322        par_civ2=Param.ActionInput.Civ2;
323        par_civ2.ImageA=[];
324        par_civ2.ImageB=[];
325        %         if ~isfield(Param.Civ1,'ImageA')
326        ImageName_A_Civ2=fullfile_uvmat(RootPath,SubDir,RootFile,FileExt,NomType,i1_series_Civ2(ifield),[],j1_series_Civ2(ifield));
327
328        if strcmp(ImageName_A_Civ2,ImageName_A) && isequal(FrameIndex_A_Civ1(ifield),FrameIndex_A_Civ2)
329            par_civ2.ImageA=par_civ1.ImageA;
330        else
331            [par_civ2.ImageA,MovieObject_A] = read_image(ImageName_A,FileType_A,MovieObject_A,FrameIndex_A_Civ2);
332        end
333        ImageName_B_Civ2=fullfile_uvmat(RootPath,SubDir,RootFile,FileExt,NomType,i2_series_Civ2(ifield),[],j2_series_Civ2(ifield));
334        if strcmp(ImageName_B_Civ2,ImageName_B) && isequal(FrameIndex_B_Civ1(ifield),FrameIndex_B_Civ2)
335            par_civ2.ImageB=par_civ1.ImageB;
336        else
337            [par_civ2.ImageB,MovieObject_B] = read_image(ImageName_B,FileType_B,MovieObject_B,FrameIndex_B_Civ2);
338        end     
339       
340        ncfile=fullfile_uvmat(RootPath,OutputDir,RootFile,'.nc',NomTypeNc,i1_series_Civ2(ifield),i2_series_Civ2(ifield),...
341            j1_series_Civ2(ifield),j2_series_Civ2(ifield));
342        par_civ2.ImageWidth=FileInfo.Width;
343        par_civ2.ImageHeight=FileInfo.Height;
344       
345        if isfield(par_civ2,'Grid')% grid points set as input file
346            if ischar(par_civ2.Grid)%read the grid file if the input is a file name
347                par_civ2.Grid=dlmread(par_civ2.Grid);
348                par_civ2.Grid(1,:)=[];%the first line must be removed (heading in the grid file)
349            end
350        else% automatic grid
351            minix=floor(par_civ2.Dx/2)-0.5;
352            maxix=minix+par_civ2.Dx*floor((par_civ2.ImageWidth-1)/par_civ2.Dx);
353            miniy=floor(par_civ2.Dy/2)-0.5;
354            maxiy=minix+par_civ2.Dy*floor((par_civ2.ImageHeight-1)/par_civ2.Dy);
355            [GridX,GridY]=meshgrid(minix:par_civ2.Dx:maxix,miniy:par_civ2.Dy:maxiy);
356            par_civ2.Grid(:,1)=reshape(GridX,[],1);
357            par_civ2.Grid(:,2)=reshape(GridY,[],1);
358        end
359        Shiftx=zeros(size(par_civ2.Grid,1),1);% shift expected from civ1 data
360        Shifty=zeros(size(par_civ2.Grid,1),1);
361        nbval=zeros(size(par_civ2.Grid,1),1);
362        if par_civ2.CheckDeformation
363            DUDX=zeros(size(par_civ2.Grid,1),1);
364            DUDY=zeros(size(par_civ2.Grid,1),1);
365            DVDX=zeros(size(par_civ2.Grid,1),1);
366            DVDY=zeros(size(par_civ2.Grid,1),1);
367        end
368        NbSubDomain=size(Data.Civ1_SubRange,3);
369        % get the guess from patch1
370        for isub=1:NbSubDomain
371            nbvec_sub=Data.Civ1_NbCentres(isub);
372            ind_sel=find(GridX>=Data.Civ1_SubRange(1,1,isub) & GridX<=Data.Civ1_SubRange(1,2,isub) & GridY>=Data.Civ1_SubRange(2,1,isub) & GridY<=Data.Civ1_SubRange(2,2,isub));
373            epoints = [GridX(ind_sel) GridY(ind_sel)];% coordinates of interpolation sites
374            ctrs=Data.Civ1_Coord_tps(1:nbvec_sub,:,isub) ;%(=initial points) ctrs
375            nbval(ind_sel)=nbval(ind_sel)+1;% records the number of values for eacn interpolation point (in case of subdomain overlap)
376            EM = tps_eval(epoints,ctrs);
377            Shiftx(ind_sel)=Shiftx(ind_sel)+EM*Data.Civ1_U_tps(1:nbvec_sub+3,isub);
378            Shifty(ind_sel)=Shifty(ind_sel)+EM*Data.Civ1_V_tps(1:nbvec_sub+3,isub);
379            if par_civ2.CheckDeformation
380                [EMDX,EMDY] = tps_eval_dxy(epoints,ctrs);%2D matrix of distances between extrapolation points epoints and spline centres (=site points) ctrs
381                DUDX(ind_sel)=DUDX(ind_sel)+EMDX*Data.Civ1_U_tps(1:nbvec_sub+3,isub);
382                DUDY(ind_sel)=DUDY(ind_sel)+EMDY*Data.Civ1_U_tps(1:nbvec_sub+3,isub);
383                DVDX(ind_sel)=DVDX(ind_sel)+EMDX*Data.Civ1_V_tps(1:nbvec_sub+3,isub);
384                DVDY(ind_sel)=DVDY(ind_sel)+EMDY*Data.Civ1_V_tps(1:nbvec_sub+3,isub);
385            end
386        end
387        mask='';
388        if par_civ2.CheckMask&&~isempty(par_civ2.Mask)&& ~strcmp(maskname,par_civ2.Mask)% mask exist, not already read in civ1
389            mask=imread(par_civ2.Mask);
390        end
391        ibx2=ceil(par_civ2.CorrBoxSize(1)/2);
392        iby2=ceil(par_civ2.CorrBoxSize(2)/2);
393        %     isx2=ibx2+4;% search ara +-4 pixels around the guess
394        %     isy2=iby2+4;
395        par_civ2.SearchBoxSize(1)=2*ibx2+9;% search ara +-4 pixels around the guess
396        par_civ2.SearchBoxSize(2)=2*iby2+9;
397        %par_civ2.SearchBoxSize(1)=2*isx2+1;
398        %par_civ2.SearchBoxSize(2)=2*isy2+1;
399        par_civ2.SearchBoxShift=[Shiftx(nbval>=1)./nbval(nbval>=1) Shifty(nbval>=1)./nbval(nbval>=1)];
400        par_civ2.Grid=[GridX(nbval>=1)-par_civ2.SearchBoxShift(:,1)/2 GridY(nbval>=1)-par_civ2.SearchBoxShift(:,2)/2];% grid taken at the extrapolated origin of the displacement vectors
401        if par_civ2.CheckDeformation
402            par_civ2.DUDX=DUDX./nbval;
403            par_civ2.DUDY=DUDY./nbval;
404            par_civ2.DVDX=DVDX./nbval;
405            par_civ2.DVDY=DVDY./nbval;
406        end
407        % caluclate velocity data (y and v in indices, reverse to y component)
408        [xtable ytable utable vtable ctable F] = civ (par_civ2);
409
410        list_param=(fieldnames(Param.ActionInput.Civ2))';
411        list_remove={'pxcmx','pxcmy','npx','npy','gridflag','maskflag','term_a','term_b','T0'};
412        for ilist=1:length(list_remove)
413            index=strcmp(list_remove{ilist},list_param);
414            if ~isempty(find(index,1))
415                list_param(index)=[];
416            end
417        end
418        for ilist=1:length(list_param)
419            Civ2_param{ilist}=['Civ2_' list_param{ilist}];
420            eval(['Data.Civ2_' list_param{ilist} '=Param.ActionInput.Civ2.' list_param{ilist} ';'])
421        end
422        if isfield(Data,'Civ2_gridname') && strcmp(Data.Civ1_gridname(1:6),'noFile')
423            Data.Civ1_gridname='';
424        end
425        if isfield(Data,'Civ2_maskname') && strcmp(Data.Civ1_maskname(1:6),'noFile')
426            Data.Civ2_maskname='';
427        end
428        Data.ListGlobalAttribute=[Data.ListGlobalAttribute Civ2_param {'Civ2_Time','Civ2_Dt'}];
429        nbvar=numel(Data.ListVarName);
430        Data.ListVarName=[Data.ListVarName {'Civ2_X','Civ2_Y','Civ2_U','Civ2_V','Civ2_F','Civ2_C'}];%  cell array containing the names of the fields to record
431        Data.VarDimName=[Data.VarDimName {'nb_vec_2','nb_vec_2','nb_vec_2','nb_vec_2','nb_vec_2','nb_vec_2'}];
432        Data.VarAttribute{nbvar+1}.Role='coord_x';
433        Data.VarAttribute{nbvar+2}.Role='coord_y';
434        Data.VarAttribute{nbvar+3}.Role='vector_x';
435        Data.VarAttribute{nbvar+4}.Role='vector_y';
436        Data.VarAttribute{nbvar+5}.Role='warnflag';
437        Data.Civ2_X=reshape(xtable,[],1);
438        Data.Civ2_Y=reshape(size(par_civ2.ImageA,1)-ytable+1,[],1);
439        Data.Civ2_U=reshape(utable,[],1);
440        Data.Civ2_V=reshape(-vtable,[],1);
441        Data.Civ2_C=reshape(ctable,[],1);
442        Data.Civ2_F=reshape(F,[],1);
443        Data.CivStage=Data.CivStage+1;
444    end
445   
446    %% Fix2
447    if isfield (Param.ActionInput,'Fix2')
448        ListFixParam=fieldnames(Param.ActionInput.Fix2);
449        for ilist=1:length(ListFixParam)
450            ParamName=ListFixParam{ilist};
451            ListName=['Fix2_' ParamName];
452            eval(['Data.ListGlobalAttribute=[Data.ListGlobalAttribute ''' ParamName '''];'])
453            eval(['Data.' ListName '=Param.ActionInput.Fix2.' ParamName ';'])
454        end
455        if check_civx
456            if ~isfield(Data,'fix2')
457                Data.ListGlobalAttribute=[Data.ListGlobalAttribute 'fix2'];
458                Data.fix2=1;
459                Data.ListVarName=[Data.ListVarName {'vec2_FixFlag'}];
460                Data.VarDimName=[Data.VarDimName {'nb_vectors2'}];
461            end
462            Data.vec_FixFlag=fix(Param.Fix2,Data.vec2_F,Data.vec2_C,Data.vec2_U,Data.vec2_V,Data.vec2_X,Data.vec2_Y);
463        else
464            Data.ListVarName=[Data.ListVarName {'Civ2_FF'}];
465            Data.VarDimName=[Data.VarDimName {'nb_vec_2'}];
466            nbvar=length(Data.ListVarName);
467            Data.VarAttribute{nbvar}.Role='errorflag';
468            Data.Civ2_FF=fix(Param.ActionInput.Fix2,Data.Civ2_F,Data.Civ2_C,Data.Civ2_U,Data.Civ2_V);
469            Data.CivStage=Data.CivStage+1;
470        end
471       
472    end
473   
474    %% Patch2
475    if isfield (Param.ActionInput,'Patch2')
476        Data.ListGlobalAttribute=[Data.ListGlobalAttribute {'Patch2_Rho','Patch2_Threshold','Patch2_SubDomain'}];
477        Data.Patch2_FieldSmooth=Param.ActionInput.Patch2.FieldSmooth;
478        Data.Patch2_MaxDiff=Param.ActionInput.Patch2.MaxDiff;
479        Data.Patch2_SubDomainSize=Param.ActionInput.Patch2.SubDomainSize;
480        nbvar=length(Data.ListVarName);
481        Data.ListVarName=[Data.ListVarName {'Civ2_U_smooth','Civ2_V_smooth','Civ2_SubRange','Civ2_NbCentres','Civ2_Coord_tps','Civ2_U_tps','Civ2_V_tps'}];
482        Data.VarDimName=[Data.VarDimName {'nb_vec_2','nb_vec_2',{'nb_coord','nb_bounds','nb_subdomain_2'},{'nb_subdomain_2'},...
483            {'nb_tps_2','nb_coord','nb_subdomain_2'},{'nb_tps_2','nb_subdomain_2'},{'nb_tps_2','nb_subdomain_2'}}];
484       
485        Data.VarAttribute{nbvar+1}.Role='vector_x';
486        Data.VarAttribute{nbvar+2}.Role='vector_y';
487        Data.VarAttribute{nbvar+5}.Role='coord_tps';
488        Data.VarAttribute{nbvar+6}.Role='vector_x';
489        Data.VarAttribute{nbvar+7}.Role='vector_y';
490        Data.Civ2_U_smooth=zeros(size(Data.Civ2_X));
491        Data.Civ2_V_smooth=zeros(size(Data.Civ2_X));
492        if isfield(Data,'Civ2_FF')
493            ind_good=find(Data.Civ2_FF==0);
494        else
495            ind_good=1:numel(Data.Civ2_X);
496        end
497        [Data.Civ2_SubRange,Data.Civ2_NbCentres,Data.Civ2_Coord_tps,Data.Civ2_U_tps,Data.Civ2_V_tps,tild,Ures, Vres,tild,FFres]=...
498            filter_tps([Data.Civ2_X(ind_good) Data.Civ2_Y(ind_good)],Data.Civ2_U(ind_good),Data.Civ2_V(ind_good),[],Data.Patch2_SubDomainSize,Data.Patch2_FieldSmooth,Data.Patch2_MaxDiff);
499        Data.Civ2_U_smooth(ind_good)=Ures;
500        Data.Civ2_V_smooth(ind_good)=Vres;
501        Data.Civ2_FF(ind_good)=FFres;
502        Data.CivStage=Data.CivStage+1;
503    end
504   
505    %% write result in a netcdf file if requested
506    if exist('ncfile','var')
507        errormsg=struct2nc(ncfile,Data);
508        if isempty(errormsg)
509            disp([ncfile ' written'])
510        else
511            disp(errormsg)
512        end
513    end
514end
515
516% 'civ': function piv.m adapted from PIVlab http://pivlab.blogspot.com/
517%--------------------------------------------------------------------------
518% function [xtable ytable utable vtable typevector] = civ (image1,image2,ibx,iby step, subpixfinder, mask, roi)
519%
520% OUTPUT:
521% xtable: set of x coordinates
522% ytable: set of y coordiantes
523% utable: set of u displacements (along x)
524% vtable: set of v displacements (along y)
525% ctable: max image correlation for each vector
526% typevector: set of flags, =1 for good, =0 for NaN vectors
527%
528%INPUT:
529% par_civ: structure of input parameters, with fields:
530%  .CorrBoxSize
531%  .SearchBoxSize
532%  .SearchBoxShift
533%  .ImageHeight
534%  .ImageWidth
535%  .Dx, Dy
536%  .Grid
537%  .Mask
538%  .MinIma
539%  .MaxIma
540%  .image1:first image (matrix)
541% image2: second image (matrix)
542% ibx2,iby2: half size of the correlation box along x and y, in px (size=(2*iby2+1,2*ibx2+1)
543% isx2,isy2: half size of the search box along x and y, in px (size=(2*isy2+1,2*isx2+1)
544% shiftx, shifty: shift of the search box (in pixel index, yshift reversed)
545% step: mesh of the measurement points (in px)
546% subpixfinder=1 or 2 controls the curve fitting of the image correlation
547% mask: =[] for no mask
548% roi: 4 element vector defining a region of interest: x position, y position, width, height, (in image indices), for the whole image, roi=[];
549function [xtable ytable utable vtable ctable F result_conv errormsg] = civ (par_civ)
550%this funtion performs the DCC PIV analysis. Recent window-deformation
551%methods perform better and will maybe be implemented in the future.
552
553%% prepare measurement grid
554if isfield(par_civ,'Grid')% grid points set as input
555    if ischar(par_civ.Grid)%read the drid file if the input is a file name
556        par_civ.Grid=dlmread(par_civ.Grid);
557        par_civ.Grid(1,:)=[];%the first line must be removed (heading in the grid file)
558    end
559else% automatic grid
560    minix=floor(par_civ.Dx/2)-0.5;
561    maxix=minix+par_civ.Dx*floor((par_civ.ImageWidth-1)/par_civ.Dx);
562    miniy=floor(par_civ.Dy/2)-0.5;
563    maxiy=minix+par_civ.Dy*floor((par_civ.ImageHeight-1)/par_civ.Dy);
564    [GridX,GridY]=meshgrid(minix:par_civ.Dx:maxix,miniy:par_civ.Dy:maxiy);
565    par_civ.Grid(:,1)=reshape(GridX,[],1);
566    par_civ.Grid(:,2)=reshape(GridY,[],1);
567end
568nbvec=size(par_civ.Grid,1);
569
570%% prepare correlation and search boxes
571ibx2=ceil(par_civ.CorrBoxSize(1)/2);
572iby2=ceil(par_civ.CorrBoxSize(2)/2);
573isx2=ceil(par_civ.SearchBoxSize(1)/2);
574isy2=ceil(par_civ.SearchBoxSize(2)/2);
575shiftx=round(par_civ.SearchBoxShift(:,1));
576shifty=-round(par_civ.SearchBoxShift(:,2));% sign minus because image j index increases when y decreases
577if numel(shiftx)==1% case of a unique shift for the whole field( civ1)
578    shiftx=shiftx*ones(nbvec,1);
579    shifty=shifty*ones(nbvec,1);
580end
581
582%% Default output
583xtable=par_civ.Grid(:,1);
584ytable=par_civ.Grid(:,2);
585utable=zeros(nbvec,1);
586vtable=zeros(nbvec,1);
587ctable=zeros(nbvec,1);
588F=zeros(nbvec,1);
589result_conv=[];
590errormsg='';
591
592%% prepare mask
593if isfield(par_civ,'Mask') && ~isempty(par_civ.Mask)
594    if strcmp(par_civ.Mask,'all')
595        return    % get the grid only, no civ calculation
596    elseif ischar(par_civ.Mask)
597        par_civ.Mask=imread(par_civ.Mask);
598    end
599end
600check_MinIma=isfield(par_civ,'MinIma');% test for image luminosity threshold
601check_MaxIma=isfield(par_civ,'MaxIma') && ~isempty(par_civ.MaxIma);
602
603% %% prepare images
604% if isfield(par_civ,'reverse_pair')
605%     if par_civ.reverse_pair
606%         if ischar(par_civ.ImageB)
607%             temp=par_civ.ImageA;
608%             par_civ.ImageA=imread(par_civ.ImageB);
609%         end
610%         if ischar(temp)
611%             par_civ.ImageB=imread(temp);
612%         end
613%     end
614% else
615%     if ischar(par_civ.ImageA)
616%         par_civ.ImageA=imread(par_civ.ImageA);
617%     end
618%     if ischar(par_civ.ImageB)
619%         par_civ.ImageB=imread(par_civ.ImageB);
620%     end
621% end
622par_civ.ImageA=sum(double(par_civ.ImageA),3);%sum over rgb component for color images
623par_civ.ImageB=sum(double(par_civ.ImageB),3);
624[npy_ima npx_ima]=size(par_civ.ImageA);
625if ~isequal(size(par_civ.ImageB),[npy_ima npx_ima])
626    errormsg='image pair with unequal size';
627    return
628end
629
630%% Apply mask
631    % Convention for mask IDEAS TO IMPLEMENT ?
632    % mask >200 : velocity calculated
633    %  200 >=mask>150;velocity not calculated, interpolation allowed (bad spots)
634    % 150>=mask >100: velocity not calculated, nor interpolated
635    %  100>=mask> 20: velocity not calculated, impermeable (no flux through mask boundaries)
636    %  20>=mask: velocity=0
637checkmask=0;
638MinA=min(min(par_civ.ImageA));
639MinB=min(min(par_civ.ImageB));
640if isfield(par_civ,'Mask') && ~isempty(par_civ.Mask)
641   checkmask=1;
642   if ~isequal(size(par_civ.Mask),[npy_ima npx_ima])
643        errormsg='mask must be an image with the same size as the images';
644        return
645   end
646  %  check_noflux=(par_civ.Mask<100) ;%TODO: to implement
647    check_undefined=(par_civ.Mask<200 & par_civ.Mask>=20 );
648    par_civ.ImageA(check_undefined)=MinA;% put image A to zero (i.e. the min image value) in the undefined  area
649    par_civ.ImageB(check_undefined)=MinB;% put image B to zero (i.e. the min image value) in the undefined  area
650end
651
652%% compute image correlations: MAINLOOP on velocity vectors
653corrmax=0;
654sum_square=1;% default
655mesh=1;% default
656CheckDecimal=isfield(par_civ,'CheckDecimal')&& par_civ.CheckDecimal==1;
657if CheckDecimal
658    mesh=0.2;%mesh in pixels for subpixel image interpolation
659    CheckDeformation=isfield(par_civ,'CheckDeformation')&& par_civ.CheckDeformation==1;
660end
661% vector=[0 0];%default
662for ivec=1:nbvec
663    iref=round(par_civ.Grid(ivec,1)+0.5);% xindex on the image A for the middle of the correlation box
664    jref=round(par_civ.ImageHeight-par_civ.Grid(ivec,2)+0.5);% yindex on the image B for the middle of the correlation box
665    %if ~(checkmask && par_civ.Mask(jref,iref)<=20) %velocity not set to zero by the black mask
666    %         if jref-iby2<1 || jref+iby2>par_civ.ImageHeight|| iref-ibx2<1 || iref+ibx2>par_civ.ImageWidth||...
667    %               jref+shifty(ivec)-isy2<1||jref+shifty(ivec)+isy2>par_civ.ImageHeight|| iref+shiftx(ivec)-isx2<1 || iref+shiftx(ivec)+isx2>par_civ.ImageWidth  % we are outside the image
668    %             F(ivec)=3;
669    %         else
670    F(ivec)=0;
671    subrange1_x=iref-ibx2:iref+ibx2;% x indices defining the first subimage
672    subrange1_y=jref-iby2:jref+iby2;% y indices defining the first subimage
673    subrange2_x=iref+shiftx(ivec)-isx2:iref+shiftx(ivec)+isx2;%x indices defining the second subimage
674    subrange2_y=jref+shifty(ivec)-isy2:jref+shifty(ivec)+isy2;%y indices defining the second subimage
675    image1_crop=MinA*ones(numel(subrange1_y),numel(subrange1_x));% default value=min of image A
676    image2_crop=MinA*ones(numel(subrange2_y),numel(subrange2_x));% default value=min of image A
677    check1_x=subrange1_x>=1 & subrange1_x<=par_civ.ImageWidth;% check which points in the subimage 1 are contained in the initial image 1
678    check1_y=subrange1_y>=1 & subrange1_y<=par_civ.ImageHeight;
679    check2_x=subrange2_x>=1 & subrange2_x<=par_civ.ImageWidth;% check which points in the subimage 2 are contained in the initial image 2
680    check2_y=subrange2_y>=1 & subrange2_y<=par_civ.ImageHeight;
681   
682    image1_crop(check1_y,check1_x)=par_civ.ImageA(subrange1_y(check1_y),subrange1_x(check1_x));%extract a subimage (correlation box) from image A
683    image2_crop(check2_y,check2_x)=par_civ.ImageB(subrange2_y(check2_y),subrange2_x(check2_x));%extract a larger subimage (search box) from image B
684    image1_mean=mean(mean(image1_crop));
685    image2_mean=mean(mean(image2_crop));
686    %threshold on image minimum
687    if check_MinIma && (image1_mean < par_civ.MinIma || image2_mean < par_civ.MinIma)
688        F(ivec)=3;
689    end
690    %threshold on image maximum
691    if check_MaxIma && (image1_mean > par_civ.MaxIma || image2_mean > par_civ.MaxIma)
692        F(ivec)=3;
693    end
694    %         end
695    if F(ivec)~=3
696        image1_crop=image1_crop-image1_mean;%substract the mean
697        image2_crop=image2_crop-image2_mean;
698        if CheckDecimal
699            xi=(1:mesh:size(image1_crop,2));
700            yi=(1:mesh:size(image1_crop,1))';
701            if CheckDeformation
702                [XI,YI]=meshgrid(xi-ceil(size(image1_crop,2)/2),yi-ceil(size(image1_crop,1)/2));
703                XIant=XI-par_civ.DUDX(ivec)*XI-par_civ.DUDY(ivec)*YI+ceil(size(image1_crop,2)/2);
704                YIant=YI-par_civ.DVDX(ivec)*XI-par_civ.DVDY(ivec)*YI+ceil(size(image1_crop,1)/2);
705                image1_crop=interp2(image1_crop,XIant,YIant);
706            else
707                image1_crop=interp2(image1_crop,xi,yi);
708            end
709            xi=(1:mesh:size(image2_crop,2));
710            yi=(1:mesh:size(image2_crop,1))';
711            image2_crop=interp2(image2_crop,xi,yi);
712        end
713        sum_square=sum(sum(image1_crop.*image1_crop));
714        %reference: Oliver Pust, PIV: Direct Cross-Correlation
715        result_conv= conv2(image2_crop,flipdim(flipdim(image1_crop,2),1),'valid');
716        corrmax= max(max(result_conv));
717        result_conv=(result_conv/corrmax)*255; %normalize, peak=always 255
718        %Find the correlation max, at 255
719        [y,x] = find(result_conv==255,1);
720        if ~isempty(y) && ~isempty(x)
721            try
722                if par_civ.CorrSmooth==1
723                    [vector,F(ivec)] = SUBPIXGAUSS (result_conv,x,y);
724                elseif par_civ.CorrSmooth==2
725                    [vector,F(ivec)] = SUBPIX2DGAUSS (result_conv,x,y);
726                end
727                utable(ivec)=vector(1)*mesh+shiftx(ivec);
728                vtable(ivec)=vector(2)*mesh+shifty(ivec);
729                xtable(ivec)=iref+utable(ivec)/2-0.5;% convec flow (velocity taken at the point middle from imgae 1 and 2)
730                ytable(ivec)=jref+vtable(ivec)/2-0.5;% and position of pixel 1=0.5 (convention for image coordinates=0 at the edge)
731                iref=round(xtable(ivec));% image index for the middle of the vector
732                jref=round(ytable(ivec));
733                if checkmask && par_civ.Mask(jref,iref)<200 && par_civ.Mask(jref,iref)>=100
734                    utable(ivec)=0;
735                    vtable(ivec)=0;
736                    F(ivec)=3;
737                end
738                ctable(ivec)=corrmax/sum_square;% correlation value
739            catch ME
740                F(ivec)=3;
741            end
742        else
743            F(ivec)=3;
744        end
745    end
746end
747result_conv=result_conv*corrmax/(255*sum_square);% keep the last correlation matrix for output
748
749%------------------------------------------------------------------------
750% --- Find the maximum of the correlation function after interpolation
751function [vector,F] = SUBPIXGAUSS (result_conv,x,y)
752%------------------------------------------------------------------------
753vector=[0 0]; %default
754F=0;
755[npy,npx]=size(result_conv);
756
757% if (x <= (size(result_conv,1)-1)) && (y <= (size(result_conv,1)-1)) && (x >= 1) && (y >= 1)
758    %the following 8 lines are copyright (c) 1998, Uri Shavit, Roi Gurka, Alex Liberzon, Technion ï¿œ Israel Institute of Technology
759    %http://urapiv.wordpress.com
760    peaky = y;
761    if y <= npy-1 && y >= 1
762        f0 = log(result_conv(y,x));
763        f1 = real(log(result_conv(y-1,x)));
764        f2 = real(log(result_conv(y+1,x)));
765        peaky = peaky+ (f1-f2)/(2*f1-4*f0+2*f2);
766    else
767        F=-2; % warning flag for vector truncated by the limited search box
768    end
769    peakx=x;
770    if x <= npx-1 && x >= 1
771        f0 = log(result_conv(y,x));
772        f1 = real(log(result_conv(y,x-1)));
773        f2 = real(log(result_conv(y,x+1)));
774        peakx = peakx+ (f1-f2)/(2*f1-4*f0+2*f2);
775    else
776        F=-2; % warning flag for vector truncated by the limited search box
777    end
778    vector=[peakx-floor(npx/2)-1 peaky-floor(npy/2)-1];
779
780%------------------------------------------------------------------------
781% --- Find the maximum of the correlation function after interpolation
782function [vector,F] = SUBPIX2DGAUSS (result_conv,x,y)
783%------------------------------------------------------------------------
784vector=[0 0]; %default
785F=-2;
786peaky=y;
787peakx=x;
788[npy,npx]=size(result_conv);
789if (x <= npx-1) && (y <= npy-1) && (x >= 1) && (y >= 1)
790    F=0;
791    for i=-1:1
792        for j=-1:1
793            %following 15 lines based on
794            %H. Nobach ï¿œ M. Honkanen (2005)
795            %Two-dimensional Gaussian regression for sub-pixel displacement
796            %estimation in particle image velocimetry or particle position
797            %estimation in particle tracking velocimetry
798            %Experiments in Fluids (2005) 38: 511ï¿œ515
799            c10(j+2,i+2)=i*log(result_conv(y+j, x+i));
800            c01(j+2,i+2)=j*log(result_conv(y+j, x+i));
801            c11(j+2,i+2)=i*j*log(result_conv(y+j, x+i));
802            c20(j+2,i+2)=(3*i^2-2)*log(result_conv(y+j, x+i));
803            c02(j+2,i+2)=(3*j^2-2)*log(result_conv(y+j, x+i));
804        end
805    end
806    c10=(1/6)*sum(sum(c10));
807    c01=(1/6)*sum(sum(c01));
808    c11=(1/4)*sum(sum(c11));
809    c20=(1/6)*sum(sum(c20));
810    c02=(1/6)*sum(sum(c02));
811    deltax=(c11*c01-2*c10*c02)/(4*c20*c02-c11^2);
812    deltay=(c11*c10-2*c01*c20)/(4*c20*c02-c11^2);
813    if abs(deltax)<1
814        peakx=x+deltax;
815    end
816    if abs(deltay)<1
817        peaky=y+deltay;
818    end
819end
820vector=[peakx-floor(npx/2)-1 peaky-floor(npy/2)-1];
821
822%'RUN_FIX': function for fixing velocity fields:
823%-----------------------------------------------
824% RUN_FIX(filename,field,flagindex,thresh_vecC,thresh_vel,iter,flag_mask,maskname,fileref,fieldref)
825%
826%filename: name of the netcdf file (used as input and output)
827%field: structure specifying the names of the fields to fix (depending on civ1 or civ2)
828    %.vel_type='civ1' or 'civ2';
829    %.nb=name of the dimension common to the field to fix ('nb_vectors' for civ1);
830    %.fixflag=name of fix flag variable ('vec_FixFlag' for civ1)
831%flagindex: flag specifying which values of vec_f are removed:
832        % if flagindex(1)=1: vec_f=-2 vectors are removed
833        % if flagindex(2)=1: vec_f=3 vectors are removed
834        % if flagindex(3)=1: vec_f=2 vectors are removed (if iter=1) or vec_f=4 vectors are removed (if iter=2)
835%iter=1 for civ1 fields and iter=2 for civ2 fields
836%thresh_vecC: threshold in the image correlation vec_C
837%flag_mask: =1 mask used to remove vectors (0 else)
838%maskname: name of the mask image file for fix
839%thresh_vel: threshold on velocity, or on the difference with the reference file fileref if exists
840%inf_sup=1: remove values smaller than threshold thresh_vel, =2, larger than threshold
841%fileref: .nc file name for a reference velocity (='': refrence 0 used)
842%fieldref: 'civ1','filter1'...feld used in fileref
843
844function FF=fix(Param,F,C,U,V,X,Y)
845FF=zeros(size(F));%default
846
847%criterium on warn flags
848FlagName={'CheckFmin2','CheckF2','CheckF3','CheckF4'};
849FlagVal=[-2 2 3 4];
850for iflag=1:numel(FlagName)
851    if isfield(Param,FlagName{iflag}) && Param.(FlagName{iflag})
852        FF=(FF==1| F==FlagVal(iflag));
853    end
854end
855%criterium on correlation values
856if isfield (Param,'MinCorr')
857    FF=FF==1 | C<Param.MinCorr;
858end
859if (isfield(Param,'MinVel')&&~isempty(Param.MinVel))||(isfield (Param,'MaxVel')&&~isempty(Param.MaxVel))
860    Umod= U.*U+V.*V;
861    if isfield (Param,'MinVel')&&~isempty(Param.MinVel)
862        FF=FF==1 | Umod<(Param.MinVel*Param.MinVel);
863    end
864    if isfield (Param,'MaxVel')&&~isempty(Param.MaxVel)
865        FF=FF==1 | Umod>(Param.MaxVel*Param.MaxVel);
866    end
867end
868
869
870%------------------------------------------------------------------------
871% --- determine the list of index pairs of processing file
872function [i1_series,i2_series,j1_series,j2_series,check_bounds,NomTypeNc]=...
873    find_pair_indices(str_civ,i_series,j_series,MinIndex,MaxIndex)
874%------------------------------------------------------------------------
875i1_series=i_series;% set of first image numbers
876i2_series=i_series;
877j1_series=ones(size(i_series));% set of first image numbers
878j2_series=ones(size(i_series));
879check_bounds=false(size(i_series));
880r=regexp(str_civ,'^\D(?<ind>[i|j])= (?<num1>\d+)\|(?<num2>\d+)','names');
881if ~isempty(r)
882    mode=['D' r.ind];
883    ind1=str2num(r.num1);
884    ind2=str2num(r.num2);
885else
886    mode='burst';
887    r=regexp(str_civ,'^j= (?<num1>[a-z])-(?<num2>[a-z])','names');
888    if ~isempty(r)
889        NomTypeNc='_1ab';
890    else
891        r=regexp(str_civ,'^j= (?<num1>[A-Z])-(?<num2>[A-Z])','names');
892        if ~isempty(r)
893            NomTypeNc='_1AB';
894        else
895            r=regexp(str_civ,'^j= (?<num1>\d+)-(?<num2>\d+)','names');
896            if ~isempty(r)
897                NomTypeNc='_1_1-2';
898            end           
899        end
900    end
901    if isempty(r)
902        display('wrong pair mode input option')
903    else
904    ind1=stra2num(r.num1);
905    ind2=stra2num(r.num2);
906    end
907end
908if strcmp (mode,'Di')
909    i1_series=i_series-ind1;% set of first image numbers
910    i2_series=i_series+ind2;
911    check_bounds=i1_series<MinIndex(1,1) | i2_series>MaxIndex(1,1);
912    if isempty(j_series)
913        NomTypeNc='_1-2';
914    else
915        j1_series=j_series;
916        j2_series=j_series;
917        NomTypeNc='_1-2_1';
918    end
919elseif strcmp (mode,'Dj')
920    j1_series=j_series-ind1;
921    j2_series=j_series+ind2;
922    check_bounds=j1_series<MinIndex(1,2) | j2_series>MaxIndex(1,2);
923    NomTypeNc='_1_1-2';
924else  %bursts
925    j1_series=ind1*ones(size(i_series));
926    j2_series=ind2*ones(size(i_series));
927end
928
929%     if length(indsel)>=1
930%         firstind=indsel(1);
931%         lastind=indsel(end);
932%         set(handles.first_j,'String',num2str(ref_j(firstind)))%update the display of first and last fields
933%         set(handles.last_j,'String',num2str(ref_j(lastind)))
934%         ref_j=ref_j(indsel);
935%         j1_civ1=j1_civ1(indsel);
936%         j2_civ1=j2_civ1(indsel);
937%         j1_civ2=j1_civ2(indsel);
938%         j2_civ2=j2_civ2(indsel);
939%     end
940% elseif isequal(mode,'pair j1-j2') %case of bursts (png_old or png_2D)
941%     displ_num=get(handles.ListPairCiv1,'UserData');
942%     i1_civ1=ref_i;
943%     i2_civ1=ref_i;
944%     j1_civ1=displ_num(1,index_civ1);
945%     j2_civ1=displ_num(2,index_civ1);
946%     i1_civ2=ref_i;
947%     i2_civ2=ref_i;
948%     j1_civ2=displ_num(1,index_civ2);
949%     j2_civ2=displ_num(2,index_civ2);
950% elseif isequal(mode,'displacement')
951%     i1_civ1=ref_i;
952%     i2_civ1=ref_i;
953%     j1_civ1=ref_j;
954%     j2_civ1=ref_j;
955%     i1_civ2=ref_i;
956%     i2_civ2=ref_i;
957%     j1_civ2=ref_j;
958%     j2_civ2=ref_j;
959% end
960
961
962
963
Note: See TracBrowser for help on using the repository browser.