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

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

a few bugs corrected

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