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

Last change on this file since 1175 was 1175, checked in by sommeria, 3 days ago

add loglin histogram in uvmat and image rescale in civ_series

File size: 45.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%   detect_false: put a flag to false vectors after detection by various criteria
5%   filter_tps: make interpolation-smoothing
6%------------------------------------------------------------------------
7% function [Data,errormsg,result_conv]= civ_series(Param)
8%
9%OUTPUT
10% Data=structure containing the PIV results and information on the processing parameters
11% errormsg=error message char string, decd ..fault=''
12% resul_conv: image inter-correlation function for the last grid point (used for tests)
13%
14%INPUT:
15% Param: Matlab structure of input  parameters
16%     Param contains info of the GUI series using the fct read_GUI.
17%     Param.Action.RUN = 0 (to set the status of the GUI series) or =1 to RUN the computation
18%     Param.InputTable: sets the input file(s)
19%           if absent, the fct looks for input data in Param.ActionInput     (test mode)
20%     Param.OutputSubDir: sets the folder name of output file(s,
21%           if absent no file is produced, result in the output structure Data (test mode)
22%     Param.ActionInput: substructure with the parameters provided by the GUI civ_input
23%                      .Civ1: parameters for civ1cc
24%                      .Fix1: parameters for detect_false1
25%                      .Patch1:
26%                      .Civ2: for civ2
27%                      .Fix2:
28%                      .Patch2:
29
30%=======================================================================
31% Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
32%   http://www.legi.grenoble-inp.fr
33%   Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr
34%
35%     This file is part of the toolbox UVMAT.
36%
37%     UVMAT is free software; you can redistribute it and/or modify
38%     it under the terms of the GNU General Public License as published
39%     by the Free Software Foundation; either version 2 of the license,
40%     or (at your option) any later version.
41%
42%     UVMAT is distributed in the hope that it will be useful,
43%     but WITHOUT ANY WARRANTY; without even the implied warranty of
44%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
45%     GNU General Public License (see LICENSE.txt) for more details.
46%=======================================================================
47
48function [Data,errormsg,result_conv]= civ_series(Param)
49errormsg='';
50
51%% set the input elements needed on the GUI series when the action is selected in the menu ActionName or InputTable refreshed
52if isstruct(Param) && isequal(Param.Action.RUN,0)% function activated from the GUI series but not RUN
53    if 0==1 %never satisfied but trigger compilation with the appropriate transform functions ('eval' inactive for compilation)
54        ima_rescale
55    end
56    path_series=fileparts(which('series'));
57    addpath(fullfile(path_series,'series'))
58    Data=civ_input(Param);% introduce the civ parameters using the GUI civ_input
59    % TODO: change from guide to App: modify the input procedure, adapt read_GUI function
60    %App=civ_input_App
61    %Data=civ_input_App(Param);% introduce the civ parameters using the GUI civ_input
62    % if isempty(App)
63    %     Data=Param;% if  civ_input has been cancelled, keep previous parameters
64    % end
65    Data.Program=mfilename;%gives the name of the current function
66    Data.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default)
67    Data.WholeIndexRange='off';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default)
68    Data.NbSlice='off'; %nbre of slices ('off' by default)
69    Data.VelType='off';% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
70    Data.FieldName='on';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
71    Data.FieldTransform = 'on';%can use a transform function
72    Data.ProjObject='off';%can use projection object(option 'off'/'on',
73    Data.Mask='off';%can use mask option   (option 'off'/'on', 'off' by default)
74    Data.OutputDirExt='.civ';%set the output dir extension
75    Data.OutputSubDirMode='last'; %select the last subDir in the input table as root of the output subdir name (option 'all'/'first'/'last', 'all' by default)
76    Data.OutputFileMode='NbInput_i';% one output file expected per value of i index (used for waitbar)
77    Data.CheckOverwriteVisible='on'; % manage the overwrite of existing files (default=1)
78    if isfield(Data,'ActionInput') && isfield(Data.ActionInput,'PairIndices') && isequal(Data.ActionInput.PairIndices.ListPairMode,'pair j1-j2')
79        Data.IndexRange_j='off';%no j index display in series
80    else
81        Data.IndexRange_j='on';% j index display in series if relevant
82    end
83    return
84end
85
86%% read input parameters from an xml file if input is a file name (batch mode)
87checkrun=1;
88if ischar(Param)
89    Param=xml2struct(Param);% read Param as input file (batch case)
90    checkrun=0;
91end
92
93%% test input
94if ~isfield(Param,'ActionInput')
95    disp_uvmat('ERROR','no parameter set for PIV',checkrun)
96    return
97end
98%iview_A=0;%default values
99NbField=1;
100RUNHandle=[];
101% CheckInputFile=isfield(Param,'InputTable');%= 1 in test use for TestCiv (no nc file involved)
102% CheckOutputFile=isfield(Param,'OutputSubDir');%= 1 in test use for TestPatch (no nc file produced)
103
104%% input files and indexing
105hseries=findobj(allchild(0),'Tag','series');
106RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
107WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series
108MaxIndex_i=Param.IndexRange.MaxIndex_i;
109MinIndex_i=Param.IndexRange.MinIndex_i;
110MaxIndex_j=ones(size(MaxIndex_i));MinIndex_j=ones(size(MinIndex_i));
111if isfield(Param.IndexRange,'MaxIndex_j')&& isfield(Param.IndexRange,'MinIndex_j')
112    MaxIndex_j=Param.IndexRange.MaxIndex_j;
113    MinIndex_j=Param.IndexRange.MinIndex_j;
114end
115if isfield(Param,'InputTable')
116    [filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
117    iview_B=0;% series index (iview) for the second image series (only non zero for option 'shift' comparing two image series )
118    if Param.ActionInput.CheckCiv1
119        iview_A=1;% usual PIV, the image series is on the first line of the table
120    else % Civ1 has been already stored in a netcdf file input
121        iview_A=2;% the second line is used for the input images
122    end
123    RootPath_A=Param.InputTable{iview_A,1};
124    RootFile_A=Param.InputTable{iview_A,3};
125    SubDir_A=Param.InputTable{iview_A,2};
126    NomType_A=Param.InputTable{iview_A,4};
127    FileExt_A=Param.InputTable{iview_A,5};
128    if iview_B==0
129        iview_B=iview_A;% the second image series is the same as the first
130    end
131    RootPath_B=Param.InputTable{iview_B,1};
132    RootFile_B=Param.InputTable{iview_B,3};
133    SubDir_B=Param.InputTable{iview_B,2};
134    NomType_B=Param.InputTable{iview_B,4};
135    FileExt_B=Param.InputTable{iview_B,5};
136    PairCiv2='';
137   
138    switch Param.ActionInput.ListCompareMode
139        case 'PIV'
140            PairCiv1=Param.ActionInput.PairIndices.ListPairCiv1;
141            if isfield(Param.ActionInput.PairIndices,'ListPairCiv2')
142                PairCiv2=Param.ActionInput.PairIndices.ListPairCiv2;%string which determines the civ2 pair
143            end
144            if iview_A==1% if Civ1 is performed
145                [i1_series_Civ1,i2_series_Civ1,j1_series_Civ1,j2_series_Civ1,check_bounds,NomTypeNc]=...
146                    find_pair_indices(PairCiv1,i1_series{1},j1_series{1},MinIndex_i,MaxIndex_i,MinIndex_j,MaxIndex_j);
147                if ~isempty(PairCiv2)
148                    [i1_series_Civ2,i2_series_Civ2,j1_series_Civ2,j2_series_Civ2,check_bounds_Civ2]=...
149                        find_pair_indices(PairCiv2,i1_series{1},j1_series{1},MinIndex_i(1),MaxIndex_i(1),MinIndex_j(1),MaxIndex_j(1));
150                    check_bounds=check_bounds | check_bounds_Civ2;
151                end
152            else% we start from an existing Civ1 file
153                i1_series_Civ1=i1_series{1};
154                i2_series_Civ1=i2_series{1};
155                j1_series_Civ1=j1_series{1};
156                j2_series_Civ1=j2_series{1};
157                NomTypeNc=Param.InputTable{1,4};
158                if ~isempty(PairCiv2)
159                    [i1_series_Civ2,i2_series_Civ2,j1_series_Civ2,j2_series_Civ2,check_bounds,NomTypeNc]=...
160                        find_pair_indices(PairCiv2,i1_series{2},j1_series{2},MinIndex_i(2),MaxIndex_i(2),MinIndex_j(2),MaxIndex_j(2));
161                end
162            end
163        case 'displacement'
164            if isfield(Param.ActionInput,'OriginIndex')
165                i1_series_Civ1=Param.ActionInput.OriginIndex*ones(size(i1_series{1}));
166            else
167                i1_series_Civ1=ones(size(i1_series{1}));
168            end
169            i1_series_Civ2=i1_series_Civ1;
170            i2_series_Civ1=i1_series{1};
171            i2_series_Civ2=i1_series{1};
172            j1_series_Civ1=[];% no j index variation for the ref image
173            j1_series_Civ2=[];
174            if isempty(j1_series{1})
175                j2_series_Civ1=ones(size(i1_series_Civ1));
176            else
177                j2_series_Civ1=j1_series{1};% if j index exist
178            end
179            j2_series_Civ2=j2_series_Civ1;
180            NomTypeNc='_1';
181    end
182    %determine frame indices for input with movie or other multiframe input file
183    if isempty(j1_series_Civ1)% simple movie with index i
184        FrameIndex_A_Civ1=i1_series_Civ1;
185        FrameIndex_B_Civ1=i2_series_Civ1;
186        j1_series_Civ1=ones(size(i1_series_Civ1));
187        if strcmp(Param.ActionInput.ListCompareMode,'PIV')
188            j2_series_Civ1=ones(size(i1_series_Civ1));
189        end
190    else % movie for each burst or volume (index j)
191        FrameIndex_A_Civ1=j1_series_Civ1;
192        FrameIndex_B_Civ1=j2_series_Civ1;
193    end
194    if isempty(PairCiv2)
195        FrameIndex_A_Civ2=FrameIndex_A_Civ1;
196        FrameIndex_B_Civ2=FrameIndex_B_Civ1;
197    else
198        if isempty(j1_series_Civ2)
199            FrameIndex_A_Civ2=i1_series_Civ2;
200            FrameIndex_B_Civ2=i2_series_Civ2;
201            j1_series_Civ2=ones(size(i1_series_Civ2));
202            if strcmp(Param.ActionInput.ListCompareMode,'PIV')
203                j2_series_Civ2=ones(size(i1_series_Civ2));
204            end
205        else
206            FrameIndex_A_Civ2=j1_series_Civ2;
207            FrameIndex_B_Civ2=j2_series_Civ2;
208        end
209    end
210    if isempty(i1_series_Civ1)||(~isempty(PairCiv2) && isempty(i1_series_Civ2))
211        disp_uvmat('ERROR','no image pair for civ in the input file index range',checkrun)
212        return
213    end
214end
215
216%% check the first image pair
217if Param.ActionInput.CheckCiv1% Civ1 is performed
218    NbField=numel(i1_series_Civ1);
219elseif Param.ActionInput.CheckCiv2 % Civ2 is performed without Civ1
220    NbField=numel(i1_series_Civ2);
221else
222    NbField=numel(i1_series_Civ1);% no image used (only detect_false or patch) TO CHECK
223end
224
225%% prepare output Data
226OutputDir=[Param.OutputSubDir Param.OutputDirExt];
227ListGlobalAttribute={'Conventions','Program','CivStage'};
228Data.Conventions='uvmat/civdata';% states the conventions used for the description of field variables and attributes
229Data.Program='civ_series';
230if isfield(Param,'UvmatRevision')
231    Data.Program=[Data.Program ', uvmat r' Param.UvmatRevision];
232end
233Data.CivStage=0;%default
234
235%% get timing from the ImaDoc file or input video
236% if iview_A~=0
237XmlFileName=find_imadoc(RootPath_A,SubDir_A);
238Time=[];
239if ~isempty(XmlFileName)
240    XmlData=imadoc2struct(XmlFileName);%read the time from XmlFileName
241    if isfield(XmlData,'Time')
242        Time=XmlData.Time;
243    end
244    if isfield(XmlData,'Camera')
245        if isfield(XmlData.Camera,'NbSlice')&& ~isempty(XmlData.Camera.NbSlice)
246            NbSlice_calib{iview}=XmlData.Camera.NbSlice;% Nbre of slices for Zindex in phys transform
247            if ~isequal(NbSlice_calib{iview},NbSlice_calib{1})
248                msgbox_uvmat('WARNING','inconsistent number of Z indices for the two field series');
249            end
250        end
251        if isfield(XmlData.Camera,'TimeUnit')&& ~isempty(XmlData.Camera.TimeUnit)
252            TimeUnit=XmlData.Camera.TimeUnit;
253        end
254    end
255end
256
257%% introduce input image transform
258if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName)
259        addpath(Param.FieldTransform.TransformPath)
260    transform_fct=str2func(Param.FieldTransform.TransformName);
261    rmpath(Param.FieldTransform.TransformPath)
262end
263
264
265%%%%% MAIN LOOP %%%%%%
266maskoldname='';% initiate the mask name
267FileType_A='';
268FileType_B='';
269CheckOverwrite=1;%default
270if isfield(Param,'CheckOverwrite')
271    CheckOverwrite=Param.CheckOverwrite;
272end
273for ifield=1:NbField
274    tstart=tic;
275    time_civ1=0;
276    time_patch1=0;
277    time_civ2=0;
278    time_patch2=0;
279    if ~isempty(RUNHandle)% update the waitbar in interactive mode with GUI series  (checkrun=1)
280        update_waitbar(WaitbarHandle,ifield/NbField)
281        if  checkrun && ~strcmp(get(RUNHandle,'BusyAction'),'queue')
282            disp('program stopped by user')
283            break
284        end
285    end
286    OutputPath=fullfile(Param.OutputPath,Param.Experiment,Param.Device);
287
288    if strcmp(Param.ActionInput.ListCompareMode,'PIV')
289        ncfile=fullfile_uvmat(OutputPath,OutputDir,RootFile_A,'.nc',NomTypeNc,i1_series_Civ1(ifield),i2_series_Civ1(ifield),...
290            j1_series_Civ1(ifield),j2_series_Civ1(ifield));
291    else
292        ncfile=fullfile_uvmat(OutputPath,OutputDir,RootFile_A,'.nc',NomTypeNc,i2_series_Civ1(ifield),[],...
293            j1_series_Civ1(ifield),j2_series_Civ1(ifield));
294    end
295    ncfile_out=ncfile;% by default
296   
297    if isfield (Param.ActionInput,'Civ2')
298        i1_civ2=i1_series_Civ2(ifield);
299        i2_civ2=i1_civ2;
300        if ~isempty(i2_series_Civ2)
301            i2_civ2=i2_series_Civ2(ifield);
302        end
303        j1_civ2=1;
304        if ~isempty(j1_series_Civ2)
305            j1_civ2=j1_series_Civ2(ifield);
306        end
307        j2_civ2=i1_civ2;
308        if ~isempty(j2_series_Civ2)
309            j2_civ2=j2_series_Civ2(ifield);
310        end
311        if strcmp(Param.ActionInput.ListCompareMode,'PIV')
312            ncfile_out=fullfile_uvmat(OutputPath,OutputDir,RootFile_A,'.nc',NomTypeNc,i1_civ2,i2_civ2,j1_civ2,j2_civ2);
313        else % displacement
314            ncfile_out=fullfile_uvmat(OutputPath,OutputDir,RootFile_A,'.nc',NomTypeNc,i2_civ2,[],j2_civ2);
315        end
316    end
317    if ~CheckOverwrite && exist(ncfile_out,'file')
318        disp(['existing output file ' ncfile_out ' already exists, skip to next field'])
319        continue% skip iteration if the mode overwrite is desactivated and the result file already exists
320    end
321    %     end
322    ImageName_A='';ImageName_B='';%default
323    VideoObject_A=[];VideoObject_B=[];
324   
325    %% Civ1
326    % if Civ1 computation is requested
327    if Param.ActionInput.CheckCiv1
328        disp('civ1 started')
329        par_civ1=Param.ActionInput.Civ1;% parameters for civ1
330        %if CheckInputFile % read input images (except in mode Test where it is introduced directly in Param.ActionInput.Civ1.ImageNameA and B)
331        try
332            if strcmp(Param.ActionInput.ListCompareMode,'displacement')
333                ImageName_A=Param.ActionInput.RefFile;
334            else
335                ImageName_A=fullfile_uvmat(RootPath_A,SubDir_A,RootFile_A,FileExt_A,NomType_A,i1_series_Civ1(ifield),[],j1_series_Civ1(ifield));
336            end
337            if strcmp(FileExt_A,'.nc')% case of input images in format netcdf
338                FieldName_A=Param.InputFields.FieldName;
339                [DataIn,~,~,errormsg]=nc2struct(ImageName_A,{FieldName_A});
340                par_civ1.ImageA=DataIn.(FieldName_A);
341            else % usual image formats for image A
342                if isempty(FileType_A)% open the image object if not already done in case of movie input
343                    [FileInfo_A,VideoObject_A]=get_file_info(ImageName_A);
344                    FileType_A=FileInfo_A.FileType;
345                    if isempty(Time) && ~isempty(find(strcmp(FileType_A,{'mmreader','video','cine_phantom','telopsIR'}), 1))% case of video input
346                        Time=zeros(FileInfo_A.NumberOfFrames+1,2);                 
347                        Time(:,2)=(0:1/FileInfo_A.FrameRate:(FileInfo_A.NumberOfFrames)/FileInfo_A.FrameRate)';
348                           if ~isempty(j1_series_Civ1) && j1_series_Civ1~=1
349                               Time=Time';
350                           end
351                    end
352                    if ~isempty(FileType_A) && isempty(Time)% Time = index i +0.001 index j by default
353                        MaxIndex_i=max(i2_series_Civ1);
354                        MaxIndex_j=max(j2_series_Civ1);
355                        Time=(1:MaxIndex_i)'*ones(1,MaxIndex_j);
356                        Time=Time+0.001*ones(MaxIndex_i,1)*(1:MaxIndex_j);
357                        Time=[zeros(1,MaxIndex_j);Time];% insert a first line of zeros
358                        Time=[zeros(MaxIndex_i+1,1) Time];% insert a first column of zeros
359                    end
360                end
361                if isempty(regexp(ImageName_A,'(^http://)|(^https://)', 'once')) && ~exist(ImageName_A,'file')
362                    disp([ImageName_A ' missing'])
363                    continue
364                end
365                tsart_input=tic;
366                [par_civ1.ImageA,VideoObject_A] = read_image(ImageName_A,FileType_A,VideoObject_A,FrameIndex_A_Civ1(ifield));
367                time_input=toc(tsart_input);
368            end
369            ImageName_B=fullfile_uvmat(RootPath_B,SubDir_B,RootFile_B,FileExt_B,NomType_B,i2_series_Civ1(ifield),[],j2_series_Civ1(ifield));
370            if isempty(FileType_B)% determine the image type for the first field
371                [FileInfo_B,VideoObject_B]=get_file_info(ImageName_B);
372                FileType_B=FileInfo_B.FileType;
373            end
374            if isempty(regexp(ImageName_B,'(^http://)|(^https://)', 'once')) && ~exist(ImageName_B,'file')
375                disp([ImageName_B ' missing'])
376                continue
377            end
378            [par_civ1.ImageB,VideoObject_B] = read_image(ImageName_B,FileType_B,VideoObject_B,FrameIndex_B_Civ1(ifield));
379           
380        catch ME % display errors in reading input images
381            if ~isempty(ME.message)
382                disp_uvmat('ERROR', ['error reading input image: ' ME.message],checkrun)
383                continue
384            end
385        end
386        %% user defined image transform
387        if ~isempty(transform_fct)
388               par_civ1 =transform_fct(par_civ1,Param);
389        end
390       
391        % par_civ1.ImageWidth=size(par_civ1.ImageA,2);
392        % par_civ1.ImageHeight=size(par_civ1.ImageA,1);
393        list_param=(fieldnames(Param.ActionInput.Civ1))';
394        list_param(strcmp('TestCiv1',list_param))=[];% remove the parameter TestCiv1 from the list
395        Civ1_param=regexprep(list_param,'^.+','Civ1_$0');% insert 'Civ1_' before  each string in list_param
396        Civ1_param=[{'Civ1_ImageA','Civ1_ImageB','Civ1_Time','Civ1_Dt'} Civ1_param]; %insert the names of the two input images
397        %indicate the values of all the global attributes in the output data
398        Data.Civ1_ImageA=ImageName_A;
399        Data.Civ1_ImageB=ImageName_B;
400        i1=i1_series_Civ1(ifield);
401        i2=i1;
402        if ~isempty(i2_series_Civ1)
403            i2=i2_series_Civ1(ifield);
404        end
405        j1=1;
406        if ~isempty(j1_series_Civ1)
407            j1=j1_series_Civ1(ifield);
408        end
409        j2=j1;
410        if ~isempty(j2_series_Civ1)
411            j2=j2_series_Civ1(ifield);
412        end
413        if strcmp(Param.ActionInput.ListCompareMode,'displacement')
414            Data.Civ1_Time=Time(i2+1,j2+1);% the Time is the Time of the second image
415            Data.Civ1_Dt=1;% Time interval is 1, to yield displacement instead of velocity=displacement/Dt at reading
416        else
417            Data.Civ1_Time=(Time(i2+1,j2+1)+Time(i1+1,j1+1))/2;% the Time is the Time at the middle of the image pair
418            Data.Civ1_Dt=Time(i2+1,j2+1)-Time(i1+1,j1+1);
419        end
420        for ilist=1:length(list_param)
421            Data.(Civ1_param{4+ilist})=Param.ActionInput.Civ1.(list_param{ilist});
422        end
423        Data.ListGlobalAttribute=[ListGlobalAttribute Civ1_param];
424        Data.CivStage=1;
425       
426        % set the list of variables
427        Data.ListVarName={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_C','Civ1_FF'};%  cell array containing the names of the fields to record
428        Data.VarDimName={'nb_vec_1','nb_vec_1','nb_vec_1','nb_vec_1','nb_vec_1','nb_vec_1'};
429        Data.VarAttribute{1}.Role='coord_x';
430        Data.VarAttribute{2}.Role='coord_y';
431        Data.VarAttribute{3}.Role='vector_x';
432        Data.VarAttribute{4}.Role='vector_y';
433        Data.VarAttribute{5}.Role='ancillary';
434        Data.VarAttribute{6}.Role='errorflag';
435       
436        % case of mask
437        if par_civ1.CheckMask&&~isempty(par_civ1.Mask)
438            [RootPath_mask,SubDir_mask,RootFile_mask,~,~,~,~,Ext_mask]=fileparts_uvmat(Param.ActionInput.Civ1.Mask);
439            j1=1;
440            if ~isempty(j1_series_Civ1)
441                j1=j1_series_Civ1(ifield);
442            end
443            if ~isempty(i2_series_Civ1)% case of volume,masks act on different j levels
444                maskname=fullfile_uvmat(RootPath_mask,SubDir_mask,RootFile_mask,Ext_mask,'_1',j1);
445            elseif isfield(par_civ1,'NbSlice')
446                i1_mask=mod(i1-1,par_civ1.NbSlice)+1;
447                maskname=fullfile_uvmat(RootPath_mask,SubDir_mask,RootFile_mask,Ext_mask,'_1',i1_mask);
448                if strcmp(Param.ActionInput.PairIndices.ListPairMode,'series(Di)')% case of volume, mask index refers to j index
449                    par_civ1.NbSlice_j=par_civ1.NbSlice;
450                end
451            else
452                maskname=Param.ActionInput.Civ1.Mask;
453            end
454            if strcmp(maskoldname,maskname)% mask exist, not already read in civ1
455                par_civ1.Mask=mask; %use mask already opened
456            else
457                if ~isempty(regexp(maskname,'(^http://)|(^https://)', 'once'))|| exist(maskname,'file')
458                    try
459                        par_civ1.Mask=imread(maskname);%update the mask, an store it for future use
460                    catch ME
461                        if ~isempty(ME.message)
462                            errormsg=['error reading input image: ' ME.message];
463                            disp_uvmat('ERROR',errormsg,checkrun)
464                            return
465                        end
466                    end
467                else
468                    par_civ1.Mask=[];
469                end
470                mask=par_civ1.Mask;
471                maskoldname=maskname;
472            end
473        end
474       
475        % case of input grid
476        if par_civ1.CheckGrid &&~isempty(par_civ1.Grid)
477            GridData=nc2struct(Param.ActionInput.Civ1.Grid);
478            par_civ1.Grid=GridData.Grid;
479            par_civ1.CorrBoxSize=GridData.CorrBox;
480        end
481       
482        % caluclate velocity data
483        [Data.Civ1_X,Data.Civ1_Y,Data.Civ1_U,Data.Civ1_V,Data.Civ1_C,Data.Civ1_FF, result_conv, errormsg] = civ (par_civ1);
484        if ~isempty(errormsg)
485            disp_uvmat('ERROR',errormsg,checkrun)
486            return
487        end
488    end
489   
490   
491    %% Fix1
492    if isfield (Param.ActionInput,'Fix1')
493        disp('detect_false1 started')
494        if ~isfield (Param.ActionInput,'Civ1')% if we use existing Civ1, remove previous data beyond Civ1
495            Fix1_attr=find(strcmp('Fix1',Data.ListGlobalAttribute));
496            Data.ListGlobalAttribute(Fix1_attr)=[];
497            for ilist=1:numel(Fix1_attr)
498                Data=rmfield(Data,Data.ListGlobalAttribute{Fix1_attr(ilist)});
499            end
500        end
501        list_param=fieldnames(Param.ActionInput.Fix1)';
502        Fix1_param=regexprep(list_param,'^.+','Fix1_$0');% insert 'Fix1_' before  each string in ListFixParam
503        %indicate the values of all the global attributes in the output data
504        for ilist=1:length(list_param)
505            Data.(Fix1_param{ilist})=Param.ActionInput.Fix1.(list_param{ilist});
506        end
507        Data.ListGlobalAttribute=[Data.ListGlobalAttribute Fix1_param];
508        Data.Civ1_FF=uint8(detect_false(Param.ActionInput.Fix1,Data.Civ1_C,Data.Civ1_U,Data.Civ1_V,Data.Civ1_FF));
509        Data.CivStage=2;
510    end
511    %% Patch1
512    if Param.ActionInput.CheckPatch1 && isfield (Param.ActionInput,'Patch1')
513        disp('patch1 started')
514        tstart_patch1=tic;
515       
516        % record the processing parameters of Patch1 as global attributes in the result nc file
517        list_param=fieldnames(Param.ActionInput.Patch1)';
518        list_param(strcmp('TestPatch1',list_param))=[];% remove 'TestPatch1' from the list of parameters
519        Patch1_param=regexprep(list_param,'^.+','Patch1_$0');% insert 'Patch1_' before  each parameter name
520        for ilist=1:length(list_param)
521            Data.(Patch1_param{ilist})=Param.ActionInput.Patch1.(list_param{ilist});
522        end
523        Data.CivStage=3;% record the new state of processing
524        Data.ListGlobalAttribute=[Data.ListGlobalAttribute Patch1_param];
525       
526        % list the variables to record
527        nbvar=length(Data.ListVarName);
528        Data.ListVarName=[Data.ListVarName {'Civ1_U_smooth','Civ1_V_smooth','Civ1_SubRange','Civ1_NbCentres','Civ1_Coord_tps','Civ1_U_tps','Civ1_V_tps'}];
529        Data.VarDimName=[Data.VarDimName {'nb_vec_1','nb_vec_1',{'nb_coord','nb_bounds','nb_subdomain_1'},'nb_subdomain_1',...
530            {'nb_tps_1','nb_coord','nb_subdomain_1'},{'nb_tps_1','nb_subdomain_1'},{'nb_tps_1','nb_subdomain_1'}}];
531        Data.VarAttribute{nbvar+1}.Role='vector_x';
532        Data.VarAttribute{nbvar+2}.Role='vector_y';
533        Data.VarAttribute{nbvar+5}.Role='coord_tps';
534        Data.VarAttribute{nbvar+6}.Role='vector_x';
535        Data.VarAttribute{nbvar+7}.Role='vector_y';
536        Data.Civ1_U_smooth=Data.Civ1_U; % zeros(size(Data.Civ1_X));
537        Data.Civ1_V_smooth=Data.Civ1_V; %zeros(size(Data.Civ1_X));
538        if isfield(Data,'Civ1_FF')
539            ind_good=find(Data.Civ1_FF==0);
540        else
541            ind_good=1:numel(Data.Civ1_X);
542        end
543        if isempty(ind_good)
544            disp_uvmat('ERROR','all vectors of civ1 are bad, check input parameters' ,checkrun)
545            return
546        end
547       
548        % perform Patch calculation using the UVMAT fct 'filter_tps'
549        [Data.Civ1_SubRange,Data.Civ1_NbCentres,Data.Civ1_Coord_tps,Data.Civ1_U_tps,Data.Civ1_V_tps,~,Ures, Vres,~,FFres]=...
550            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);
551        Data.Civ1_U_smooth(ind_good)=Ures;% take the interpolated (smoothed) velocity values for good vectors, keep civ1 data for the other
552        Data.Civ1_V_smooth(ind_good)=Vres;
553        Data.Civ1_FF(ind_good)=uint8(4*FFres);%set FF to value =4 for vectors eliminated by filter_tps
554        time_patch1=toc(tstart_patch1);
555        disp('patch1 performed')
556    end
557   
558    %% Civ2
559    if isfield (Param.ActionInput,'Civ2')
560        disp('civ2 started')
561        tstart_civ2=tic;
562        par_civ2=Param.ActionInput.Civ2;
563        %         if CheckInputFile % read input images (except in mode Test where it is introduced directly in Param.ActionInput.Civ1.ImageNameA and B)
564        par_civ2.ImageA=[];
565        par_civ2.ImageB=[];
566        if strcmp(Param.ActionInput.ListCompareMode,'displacement')
567            ImageName_A_Civ2=Param.ActionInput.RefFile;
568        else
569            ImageName_A_Civ2=fullfile_uvmat(RootPath_A,SubDir_A,RootFile_A,FileExt_A,NomType_A,i1_civ2,[],j1_civ2);
570        end
571        if strcmp(ImageName_A_Civ2,ImageName_A) && isequal(FrameIndex_A_Civ1(ifield),FrameIndex_A_Civ2(ifield))
572            par_civ2.ImageA=par_civ1.ImageA;
573        else
574            [par_civ2.ImageA,VideoObject_A] = read_image(ImageName_A_Civ2,FileType_A,VideoObject_A,FrameIndex_A_Civ2(ifield));
575        end
576        ImageName_B_Civ2=fullfile_uvmat(RootPath_B,SubDir_B,RootFile_B,FileExt_B,NomType_B,i2_civ2,[],j2_civ2);
577        if strcmp(ImageName_B_Civ2,ImageName_B) && isequal(FrameIndex_B_Civ1(ifield),FrameIndex_B_Civ2)
578            par_civ2.ImageB=par_civ1.ImageB;
579        else
580            [par_civ2.ImageB,VideoObject_B] = read_image(ImageName_B_Civ2,FileType_B,VideoObject_B,FrameIndex_B_Civ2(ifield));
581        end
582        %  [FileInfo_A,VideoObject_A]=get_file_info(ImageName_A_Civ2);
583        npy_ima=size(par_civ2.ImageA,1);
584        npx_ima=size(par_civ2.ImageA,2);
585        if par_civ2.CheckGrid &&~isempty(par_civ2.Grid) % case of input grid
586            GridData=nc2struct(Param.ActionInput.Civ2.Grid);
587            par_civ2.Grid=GridData.Grid;
588            par_civ2.CorrBoxSize=GridData.CorrBox;
589        else% automatic grid
590            nbinterv_x=floor((npx_ima-1)/par_civ2.Dx);
591            gridlength_x=nbinterv_x*par_civ2.Dx;
592            minix=ceil((npx_ima-gridlength_x)/2);
593            nbinterv_y=floor((npy_ima-1)/par_civ2.Dy);
594            gridlength_y=nbinterv_y*par_civ2.Dy;
595            miniy=ceil((npy_ima-gridlength_y)/2);
596            [GridX,GridY]=meshgrid(minix:par_civ2.Dx:npx_ima-1,miniy:par_civ2.Dy:npy_ima-1);
597            par_civ2.Grid=zeros(numel(GridX),2);
598            par_civ2.Grid(:,1)=reshape(GridX,[],1);
599            par_civ2.Grid(:,2)=reshape(GridY,[],1);% increases with array index
600        end
601       
602                %% user defined image transform
603        if ~isempty(transform_fct)
604               par_civ2 =transform_fct(par_civ2,Param);
605        end
606       
607       
608        % get the guess from patch1 or patch2 (case 'CheckCiv3')
609        if iview_A==2 && isfield (par_civ2,'CheckCiv3') && strcmp(par_civ2.CheckCiv3,'iterate(civ3)') %get the guess from  patch2% Civ1 data read in a netcdf file
610            [DataIn,~,~,errormsg]=nc2struct(filecell{1,ifield});
611            if ~isempty(errormsg)
612                disp(errormsg)
613                return
614            end
615            SubRange= DataIn.Civ2_SubRange;
616            NbCentres=DataIn.Civ2_NbCentres;
617            Coord_tps=DataIn.Civ2_Coord_tps;
618            U_tps=DataIn.Civ2_U_tps;
619            V_tps=DataIn.Civ2_V_tps;
620            %CivStage=DataIn.CivStage;%store the current CivStage
621            Civ1_Dt=DataIn.Civ2_Dt;
622            Data=[];%reinitialise the result structure Data
623            Data.ListGlobalAttribute={'Conventions','Program','CivStage'};
624            Data.Conventions='uvmat/civdata';% states the conventions used for the description of field variables and attributes
625            Data.Program='civ_series';
626           % Data.CivStage=CivStage+1;%update the current civStage after reinitialisation of Data
627            Data.ListVarName={};
628            Data.VarDimName={};
629        else % get the guess from patch1
630            SubRange= Data.Civ1_SubRange;
631            NbCentres=Data.Civ1_NbCentres;
632            Coord_tps=Data.Civ1_Coord_tps;
633            U_tps=Data.Civ1_U_tps;
634            V_tps=Data.Civ1_V_tps;
635            Civ1_Dt=Data.Civ1_Dt;
636%             Data.CivStage=4;
637        end
638         Data.CivStage=4;
639        %             SubRange= par_civ2.Civ1_SubRange;
640        %             NbCentres=par_civ2.Civ1_NbCentres;
641        %             Coord_tps=par_civ2.Civ1_Coord_tps;
642        %             U_tps=par_civ2.Civ1_U_tps;
643        %             V_tps=par_civ2.Civ1_V_tps;
644        %             Civ1_Dt=par_civ2.Civ1_Dt;
645        %             Civ2_Dt=par_civ2.Civ1_Dt;
646        %             Data.ListVarName={};
647        %             Data.VarDimName={};
648        %         end
649        Shiftx=zeros(size(par_civ2.Grid,1),1);% initialise the shift expected from civ1 data
650        Shifty=zeros(size(par_civ2.Grid,1),1);
651        nbval=zeros(size(par_civ2.Grid,1),1);% nbre of interpolated values at each grid point (from the different patch subdomains)
652        if par_civ2.CheckDeformation
653            DUDX=zeros(size(par_civ2.Grid,1),1);
654            DUDY=zeros(size(par_civ2.Grid,1),1);
655            DVDX=zeros(size(par_civ2.Grid,1),1);
656            DVDY=zeros(size(par_civ2.Grid,1),1);
657        end
658        NbSubDomain=size(SubRange,3);
659        for isub=1:NbSubDomain% for each sub-domain of Patch1
660            nbvec_sub=NbCentres(isub);% nbre of Civ vectors in the subdomain
661            ind_sel=find(par_civ2.Grid(:,1)>=SubRange(1,1,isub) & par_civ2.Grid(:,1)<=SubRange(1,2,isub) &...
662                par_civ2.Grid(:,2)>=SubRange(2,1,isub) & par_civ2.Grid(:,2)<=SubRange(2,2,isub));% grid points in the subdomain
663            if ~isempty(ind_sel)
664                epoints = par_civ2.Grid(ind_sel,:);% coordinates of interpolation sites (measurement grids)
665                ctrs=Coord_tps(1:nbvec_sub,:,isub) ;%(=initial points) ctrs
666                EM = tps_eval(epoints,ctrs);% thin plate spline (tps) coefficient
667                CentreX=(SubRange(1,1,isub)+SubRange(1,2,isub))/2; %x posiion of the subdomain center
668                CentreY=(SubRange(2,1,isub)+SubRange(2,2,isub))/2; %y posiion of the subdomain center
669                xwidth=(SubRange(1,2,isub)-SubRange(1,1,isub))/pi;
670                ywidth=(SubRange(2,2,isub)-SubRange(2,1,isub))/pi;
671                x_dist=(epoints(:,1)-CentreX)/xwidth;
672                y_dist=(epoints(:,2)-CentreY)/ywidth;
673                weight=cos(x_dist).*cos(y_dist);%weighting fct =1 at the rectangle center and 0 at edge
674                nbval(ind_sel)=nbval(ind_sel)+weight;% records the number of values for each interpolation point (in case of subdomain overlap)
675                Shiftx(ind_sel)=Shiftx(ind_sel)+weight.*(EM*U_tps(1:nbvec_sub+3,isub));%velocity shift estimated by tps from civ1
676                Shifty(ind_sel)=Shifty(ind_sel)+weight.*(EM*V_tps(1:nbvec_sub+3,isub));
677                if par_civ2.CheckDeformation
678                    [EMDX,EMDY] = tps_eval_dxy(epoints,ctrs);%2D matrix of distances between extrapolation points epoints and spline centres (=site points) ctrs
679                    DUDX(ind_sel)=DUDX(ind_sel)+weight.*(EMDX*U_tps(1:nbvec_sub+3,isub));
680                    DUDY(ind_sel)=DUDY(ind_sel)+weight.*(EMDY*U_tps(1:nbvec_sub+3,isub));
681                    DVDX(ind_sel)=DVDX(ind_sel)+weight.*(EMDX*V_tps(1:nbvec_sub+3,isub));
682                    DVDY(ind_sel)=DVDY(ind_sel)+weight.*(EMDY*V_tps(1:nbvec_sub+3,isub));
683                end
684            end
685        end
686        Shiftx(nbval>0)=Shiftx(nbval>0)./nbval(nbval>0);
687        Shifty(nbval>0)=Shifty(nbval>0)./nbval(nbval>0);
688       
689        % introduce mask
690        if par_civ2.CheckMask && ~isempty(par_civ2.Mask)
691            [RootPath_mask,SubDir_mask,RootFile_mask,~,~,~,~,Ext_mask]=fileparts_uvmat(Param.ActionInput.Civ2.Mask);
692            if ~isempty(i2_series_Civ2) % we do PIV among indices i,  at given indices j (volume scan), mask depends on position j
693                j1=1;
694                if ~isempty(j1_series_Civ2)
695                    j1=j1_series_Civ1(ifield);
696                end
697                maskname=fullfile_uvmat(RootPath_mask,SubDir_mask,RootFile_mask,Ext_mask,'_1',j1);
698            elseif isfield(par_civ2,'NbSlice')
699                i1=i1_series_Civ2(ifield);
700                i1_mask=mod(i1-1,par_civ2.NbSlice)+1;
701                maskname=fullfile_uvmat(RootPath_mask,SubDir_mask,RootFile_mask,Ext_mask,'_1',i1_mask);
702                if strcmp(Param.ActionInput.PairIndices.ListPairMode,'series(Di)')% case of volume, mask index refers to j index
703                    par_civ2.NbSlice_j=par_civ2.NbSlice;
704                end
705            else
706                maskname=Param.ActionInput.Civ2.Mask;
707            end
708            if strcmp(maskoldname,maskname)% mask exist, not already read in civ1
709                par_civ2.Mask=mask; %use mask already opened
710            else
711                if exist(maskname,'file')
712                    try
713                        par_civ2.Mask=imread(maskname);%update the mask, an store it for future use
714                    catch ME
715                        if ~isempty(ME.message)
716                            errormsg=['error reading input image: ' ME.message];
717                            disp_uvmat('ERROR',errormsg,checkrun)
718                            return
719                        end
720                    end
721                else
722                    par_civ2.Mask=[];
723                end
724                mask=par_civ2.Mask;
725                maskoldname=maskname;
726            end
727        end
728       
729       
730        if strcmp(Param.ActionInput.ListCompareMode,'displacement')
731            Civ1_Dt=1;
732            Civ2_Dt=1;
733        else
734            Civ2_Dt=Time(i2_civ2+1,j2_civ2+1)-Time(i1_civ2+1,j1_civ2+1);
735        end
736        par_civ2.SearchBoxShift=zeros(size(par_civ2.Grid));
737        par_civ2.SearchBoxShift(:,1)=(Civ2_Dt/Civ1_Dt)*Shiftx;%rescale the shift in case of Dt different for Civ1 and Civ2
738        par_civ2.SearchBoxShift(:,2)=(Civ2_Dt/Civ1_Dt)*Shifty;
739       
740        if par_civ2.CheckDeformation
741            par_civ2.DUDX(nbval>0)=DUDX(nbval>0)./nbval(nbval>0);
742            par_civ2.DUDY(nbval>0)=DUDY(nbval>0)./nbval(nbval>0);
743            par_civ2.DVDX(nbval>0)=DVDX(nbval>0)./nbval(nbval>0);
744            par_civ2.DVDY(nbval>0)=DVDY(nbval>0)./nbval(nbval>0);
745        end
746       
747        % calculate velocity data (y and v in image indices, reverse to y component)
748       
749        [Data.Civ2_X,Data.Civ2_Y,Data.Civ2_U,Data.Civ2_V,Data.Civ2_C,Data.Civ2_FF,~, errormsg] = civ (par_civ2);
750       
751        list_param=(fieldnames(Param.ActionInput.Civ2))';
752        list_param(strcmp('TestCiv2',list_param))=[];% remove the parameter TestCiv2 from the list
753        Civ2_param=regexprep(list_param,'^.+','Civ2_$0');% insert 'Civ2_' before  each string in list_param
754        Civ2_param=[{'Civ2_ImageA','Civ2_ImageB','Civ2_Time','Civ2_Dt'} Civ2_param]; %insert the names of the two input images
755        %indicate the values of all the global attributes in the output data
756        if exist('ImageName_A','var')
757            Data.Civ2_ImageA=ImageName_A;
758            Data.Civ2_ImageB=ImageName_B;
759            if strcmp(Param.ActionInput.ListCompareMode,'displacement')
760                Data.Civ2_Time=Time(i2_civ2+1,j2_civ2+1);% the Time is the Time of the secodn image
761                Data.Civ2_Dt=1;% Time interval is 1, to yield displacement instead of velocity=displacement/Dt at reading
762            else
763                Data.Civ2_Time=(Time(i2_civ2+1,j2_civ2+1)+Time(i1_civ2+1,j1_civ2+1))/2;
764                Data.Civ2_Dt=Civ2_Dt;
765            end
766        end
767        for ilist=1:length(list_param)
768            Data.(Civ2_param{4+ilist})=Param.ActionInput.Civ2.(list_param{ilist});
769        end
770        Data.ListGlobalAttribute=[Data.ListGlobalAttribute Civ2_param];
771       
772        nbvar=numel(Data.ListVarName);
773        % define the Civ2 variable (if Civ2 data are not replaced from previous calculation)
774        if isempty(find(strcmp('Civ2_X',Data.ListVarName),1))
775            Data.ListVarName=[Data.ListVarName {'Civ2_X','Civ2_Y','Civ2_U','Civ2_V','Civ2_C','Civ2_FF'}];%  cell array containing the names of the fields to record
776            Data.VarDimName=[Data.VarDimName {'nb_vec_2','nb_vec_2','nb_vec_2','nb_vec_2','nb_vec_2','nb_vec_2'}];
777            Data.VarAttribute{nbvar+1}.Role='coord_x';
778            Data.VarAttribute{nbvar+2}.Role='coord_y';
779            Data.VarAttribute{nbvar+3}.Role='vector_x';
780            Data.VarAttribute{nbvar+4}.Role='vector_y';
781            Data.VarAttribute{nbvar+5}.Role='ancillary';
782            Data.VarAttribute{nbvar+6}.Role='errorflag';
783        end
784        disp('civ2 performed')
785        time_civ2=toc(tstart_civ2);
786    elseif ~isfield(Data,'ListVarName') % we start there, using existing Civ2 data
787        if exist('ncfile','var')
788            CivFile=ncfile;
789            [Data,~,~,errormsg]=nc2struct(CivFile);%read civ1 and detect_false1 data in the existing netcdf file
790            if ~isempty(errormsg)
791                disp_uvmat('ERROR',errormsg,checkrun)
792                return
793            end
794        end
795    end
796   
797    %% Fix2
798    if Param.ActionInput.CheckFix2 && isfield (Param.ActionInput,'Fix2')% if Fix2 computation is requested
799        disp('detect_false2 started')
800        list_param=fieldnames(Param.ActionInput.Fix2)';
801        Fix2_param=regexprep(list_param,'^.+','Fix2_$0');% insert 'Fix1_' before  each string in ListFixParam
802        %indicate the values of all the global attributes in the output data
803        for ilist=1:length(list_param)
804            Data.(Fix2_param{ilist})=Param.ActionInput.Fix2.(list_param{ilist});
805        end
806        Data.ListGlobalAttribute=[Data.ListGlobalAttribute Fix2_param];
807        Data.Civ2_FF=double(detect_false(Param.ActionInput.Fix2,Data.Civ2_C,Data.Civ2_U,Data.Civ2_V,Data.Civ2_FF));
808        Data.CivStage=Data.CivStage+1;
809    end
810   
811    %% Patch2
812    if Param.ActionInput.CheckPatch2 && isfield (Param.ActionInput,'Patch2')% if Patch2 computation is requested
813       
814        disp('patch2 started')
815        tstart_patch2=tic;
816        list_param=fieldnames(Param.ActionInput.Patch2)';
817        list_param(strcmp('TestPatch2',list_param))=[];% remove the parameter TestCiv1 from the list
818        Patch2_param=regexprep(list_param,'^.+','Patch2_$0');% insert 'Fix1_' before  each string in ListFixParam
819        %indicate the values of all the global attributes in the output data
820        for ilist=1:length(list_param)
821            Data.(Patch2_param{ilist})=Param.ActionInput.Patch2.(list_param{ilist});
822        end
823        Data.ListGlobalAttribute=[Data.ListGlobalAttribute Patch2_param];
824       
825        nbvar=length(Data.ListVarName);
826        Data.ListVarName=[Data.ListVarName {'Civ2_U_smooth','Civ2_V_smooth','Civ2_SubRange','Civ2_NbCentres','Civ2_Coord_tps','Civ2_U_tps','Civ2_V_tps'}];
827        Data.VarDimName=[Data.VarDimName {'nb_vec_2','nb_vec_2',{'nb_coord','nb_bounds','nb_subdomain_2'},{'nb_subdomain_2'},...
828            {'nb_tps_2','nb_coord','nb_subdomain_2'},{'nb_tps_2','nb_subdomain_2'},{'nb_tps_2','nb_subdomain_2'}}];
829       
830        Data.VarAttribute{nbvar+1}.Role='vector_x';
831        Data.VarAttribute{nbvar+2}.Role='vector_y';
832        Data.VarAttribute{nbvar+5}.Role='coord_tps';
833        Data.VarAttribute{nbvar+6}.Role='vector_x';
834        Data.VarAttribute{nbvar+7}.Role='vector_y';
835        Data.Civ2_U_smooth=Data.Civ2_U;
836        Data.Civ2_V_smooth=Data.Civ2_V;
837        if isfield(Data,'Civ2_FF')
838            ind_good=find(Data.Civ2_FF==0);
839        else
840            ind_good=1:numel(Data.Civ2_X);
841        end
842        if isempty(ind_good)
843            disp_uvmat('ERROR','all vectors of civ2 are bad, check input parameters' ,checkrun)
844            return
845        end
846       
847        [Data.Civ2_SubRange,Data.Civ2_NbCentres,Data.Civ2_Coord_tps,Data.Civ2_U_tps,Data.Civ2_V_tps,tild,Ures,Vres,tild,FFres]=...
848            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);
849        Data.Civ2_U_smooth(ind_good)=Ures;
850        Data.Civ2_V_smooth(ind_good)=Vres;
851        Data.Civ2_FF(ind_good)=uint8(4*FFres);
852        Data.CivStage=Data.CivStage+1;
853        time_patch2=toc(tstart_patch2);
854        disp('patch2 performed')
855    end
856   
857    %% write result in a netcdf file
858    errormsg=struct2nc(ncfile_out,Data);
859    if isempty(errormsg)
860        disp([ncfile_out ' written'])
861    else
862        disp(errormsg)
863    end
864    time_total=toc(tstart);
865    disp(['ellapsed time ' num2str(time_total/60,2) ' minutes'])
866    disp(['time civ1 ' num2str(time_civ1,2) ' s'])
867    disp(['time patch1 ' num2str(time_patch1,2) ' s'])
868    disp(['time civ2 ' num2str(time_civ2,2) ' s'])
869    disp(['time patch2 ' num2str(time_patch2,2) ' s'])
870    if exist('time_input','var')
871        disp(['time image reading ' num2str(time_input,2) ' s'])
872        disp(['time other ' num2str((time_total-time_input-time_civ1-time_patch1-time_civ2-time_patch2),2) ' s'])
873    end
874end
875
876
877
878%------------------------------------------------------------------------
879% --- determine the list of index pairs of processing file
880function [i1_series,i2_series,j1_series,j2_series,check_bounds,NomTypeNc]=...
881    find_pair_indices(str_civ,i_series,j_series,MinIndex_i,MaxIndex_i,MinIndex_j,MaxIndex_j)
882%------------------------------------------------------------------------
883i1_series=i_series;% set of first image indexes
884i2_series=i_series;
885j1_series=j_series;%ones(size(i_series));% set of first image numbers
886j2_series=j_series;%ones(size(i_series));
887r=regexp(str_civ,'^\D(?<ind>[i|j])=( -| )(?<num1>\d+)\|(?<num2>\d+)','names');
888if ~isempty(r)
889    mode=['D' r.ind];
890    ind1=str2num(r.num1);
891    ind2=str2num(r.num2);
892else
893    mode='j1-j2';
894    r=regexp(str_civ,'^j= (?<num1>[a-z])-(?<num2>[a-z])','names');
895    if ~isempty(r)
896        NomTypeNc='_1ab';
897    else
898        r=regexp(str_civ,'^j= (?<num1>[A-Z])-(?<num2>[A-Z])','names');
899        if ~isempty(r)
900            NomTypeNc='_1AB';
901        else
902            r=regexp(str_civ,'^j= (?<num1>\d+)-(?<num2>\d+)','names');
903            if ~isempty(r)
904                NomTypeNc='_1_1-2';
905            end
906        end
907    end
908    if isempty(r)
909        display('wrong pair mode input option')
910    else
911        ind1=stra2num(r.num1);
912        ind2=stra2num(r.num2);
913    end
914end
915switch mode
916    case 'Di'
917        i1_series=i_series-ind1;% set of first image numbers
918        i2_series=i_series+ind2;
919        check_bounds=i1_series<MinIndex_i | i2_series>MaxIndex_i;
920        if isempty(j_series)
921            NomTypeNc='_1-2';
922        else
923            j1_series=j_series;
924            j2_series=j_series;
925            NomTypeNc='_1-2_1';
926        end
927    case 'Dj'
928        j1_series=j_series-ind1;
929        j2_series=j_series+ind2;
930        check_bounds=j1_series<MinIndex_j | j2_series>MaxIndex_j;
931        NomTypeNc='_1_1-2';
932    otherwise %bursts
933        i1_series=i_series(1,:);% do not sweep the j index
934        i2_series=i_series(1,:);
935        j1_series=ind1*ones(1,size(i_series,2));% j index is fixed by pair choice
936        j2_series=ind2*ones(1,size(i_series,2));
937        check_bounds=zeros(size(i1_series));% no limitations due to min-max indices
938end
939
940%------------------------------------------------------------------------
941% --- set the flag for false vectors
942function FF=detect_false(Param,C,U,V,FFIn)
943FF=FFIn;%default, good vectors
944% FF=1, for correlation max at edge, not set in this function
945% FF=2, for too small correlation
946% FF=3, for velocity outside bounds
947% FF=4 for exclusion by difference with the smoothed field, set by call to function filter_tps
948
949if isfield (Param,'MinCorr')
950     FF(C<Param.MinCorr & FFIn==0)=2;
951end
952if (isfield(Param,'MinVel')&&~isempty(Param.MinVel))||(isfield (Param,'MaxVel')&&~isempty(Param.MaxVel))
953    Umod= U.*U+V.*V;
954    if isfield (Param,'MinVel')&&~isempty(Param.MinVel)
955        U2Min=Param.MinVel*Param.MinVel;
956        FF(Umod<U2Min & FFIn==0)=3;
957    end
958    if isfield (Param,'MaxVel')&&~isempty(Param.MaxVel)
959         U2Max=Param.MaxVel*Param.MaxVel;
960        FF(Umod>U2Max & FFIn==0)=3;
961    end
962end
963
964
965
Note: See TracBrowser for help on using the repository browser.