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

Last change on this file since 1170 was 1170, checked in by sommeria, 3 weeks ago

Library Teleops infrared camera added

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