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

Last change on this file since 1169 was 1169, checked in by sommeria, 8 weeks ago

civ3 introduced

File size: 45.7 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') && strcmp(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'}), 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                    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        catch ME % display errors in reading input images
380            if ~isempty(ME.message)
381                disp_uvmat('ERROR', ['error reading input image: ' ME.message],checkrun)
382                continue
383            end
384        end
385       
386        % par_civ1.ImageWidth=size(par_civ1.ImageA,2);
387        % par_civ1.ImageHeight=size(par_civ1.ImageA,1);
388        list_param=(fieldnames(Param.ActionInput.Civ1))';
389        list_param(strcmp('TestCiv1',list_param))=[];% remove the parameter TestCiv1 from the list
390        Civ1_param=regexprep(list_param,'^.+','Civ1_$0');% insert 'Civ1_' before  each string in list_param
391        Civ1_param=[{'Civ1_ImageA','Civ1_ImageB','Civ1_Time','Civ1_Dt'} Civ1_param]; %insert the names of the two input images
392        %indicate the values of all the global attributes in the output data
393        Data.Civ1_ImageA=ImageName_A;
394        Data.Civ1_ImageB=ImageName_B;
395        i1=i1_series_Civ1(ifield);
396        i2=i1;
397        if ~isempty(i2_series_Civ1)
398            i2=i2_series_Civ1(ifield);
399        end
400        j1=1;
401        if ~isempty(j1_series_Civ1)
402            j1=j1_series_Civ1(ifield);
403        end
404        j2=j1;
405        if ~isempty(j2_series_Civ1)
406            j2=j2_series_Civ1(ifield);
407        end
408        if strcmp(Param.ActionInput.ListCompareMode,'displacement')
409            Data.Civ1_Time=Time(i2+1,j2+1);% the Time is the Time of the second image
410            Data.Civ1_Dt=1;% Time interval is 1, to yield displacement instead of velocity=displacement/Dt at reading
411        else
412            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
413            Data.Civ1_Dt=Time(i2+1,j2+1)-Time(i1+1,j1+1);
414        end
415        for ilist=1:length(list_param)
416            Data.(Civ1_param{4+ilist})=Param.ActionInput.Civ1.(list_param{ilist});
417        end
418        Data.ListGlobalAttribute=[ListGlobalAttribute Civ1_param];
419        Data.CivStage=1;
420       
421        % set the list of variables
422        Data.ListVarName={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_C','Civ1_FF'};%  cell array containing the names of the fields to record
423        Data.VarDimName={'nb_vec_1','nb_vec_1','nb_vec_1','nb_vec_1','nb_vec_1','nb_vec_1'};
424        Data.VarAttribute{1}.Role='coord_x';
425        Data.VarAttribute{2}.Role='coord_y';
426        Data.VarAttribute{3}.Role='vector_x';
427        Data.VarAttribute{4}.Role='vector_y';
428        Data.VarAttribute{5}.Role='ancillary';
429        Data.VarAttribute{6}.Role='errorflag';
430       
431        % case of mask
432        if par_civ1.CheckMask&&~isempty(par_civ1.Mask)
433            [RootPath_mask,SubDir_mask,RootFile_mask,~,~,~,~,Ext_mask]=fileparts_uvmat(Param.ActionInput.Civ1.Mask);
434            j1=1;
435            if ~isempty(j1_series_Civ1)
436                j1=j1_series_Civ1(ifield);
437            end
438            if ~isempty(i2_series_Civ1)% case of volume,masks act on different j levels
439                maskname=fullfile_uvmat(RootPath_mask,SubDir_mask,RootFile_mask,Ext_mask,'_1',j1);
440            elseif isfield(par_civ1,'NbSlice')
441                i1_mask=mod(i1-1,par_civ1.NbSlice)+1;
442                maskname=fullfile_uvmat(RootPath_mask,SubDir_mask,RootFile_mask,Ext_mask,'_1',i1_mask);
443                if strcmp(Param.ActionInput.PairIndices.ListPairMode,'series(Di)')% case of volume, mask index refers to j index
444                    par_civ1.NbSlice_j=par_civ1.NbSlice;
445                end
446            else
447                maskname=Param.ActionInput.Civ1.Mask;
448            end
449            if strcmp(maskoldname,maskname)% mask exist, not already read in civ1
450                par_civ1.Mask=mask; %use mask already opened
451            else
452                if ~isempty(regexp(maskname,'(^http://)|(^https://)', 'once'))|| exist(maskname,'file')
453                    try
454                        par_civ1.Mask=imread(maskname);%update the mask, an store it for future use
455                    catch ME
456                        if ~isempty(ME.message)
457                            errormsg=['error reading input image: ' ME.message];
458                            disp_uvmat('ERROR',errormsg,checkrun)
459                            return
460                        end
461                    end
462                else
463                    par_civ1.Mask=[];
464                end
465                mask=par_civ1.Mask;
466                maskoldname=maskname;
467            end
468        end
469       
470        % case of input grid
471        if par_civ1.CheckGrid &&~isempty(par_civ1.Grid)
472            GridData=nc2struct(Param.ActionInput.Civ1.Grid);
473            par_civ1.Grid=GridData.Grid;
474            par_civ1.CorrBoxSize=GridData.CorrBox;
475        end
476       
477        % caluclate velocity data
478        [Data.Civ1_X,Data.Civ1_Y,Data.Civ1_U,Data.Civ1_V,Data.Civ1_C,Data.Civ1_FF, result_conv, errormsg] = civ (par_civ1);
479        if ~isempty(errormsg)
480            disp_uvmat('ERROR',errormsg,checkrun)
481            return
482        end
483    end
484   
485   
486    %% Fix1
487    if isfield (Param.ActionInput,'Fix1')
488        disp('detect_false1 started')
489        if ~isfield (Param.ActionInput,'Civ1')% if we use existing Civ1, remove previous data beyond Civ1
490            Fix1_attr=find(strcmp('Fix1',Data.ListGlobalAttribute));
491            Data.ListGlobalAttribute(Fix1_attr)=[];
492            for ilist=1:numel(Fix1_attr)
493                Data=rmfield(Data,Data.ListGlobalAttribute{Fix1_attr(ilist)});
494            end
495        end
496        list_param=fieldnames(Param.ActionInput.Fix1)';
497        Fix1_param=regexprep(list_param,'^.+','Fix1_$0');% insert 'Fix1_' before  each string in ListFixParam
498        %indicate the values of all the global attributes in the output data
499        for ilist=1:length(list_param)
500            Data.(Fix1_param{ilist})=Param.ActionInput.Fix1.(list_param{ilist});
501        end
502        Data.ListGlobalAttribute=[Data.ListGlobalAttribute Fix1_param];
503        Data.Civ1_FF=uint8(detect_false(Param.ActionInput.Fix1,Data.Civ1_C,Data.Civ1_U,Data.Civ1_V,Data.Civ1_FF));
504        Data.CivStage=2;
505    end
506    %% Patch1
507    if Param.ActionInput.CheckPatch1 && isfield (Param.ActionInput,'Patch1')
508        disp('patch1 started')
509        tstart_patch1=tic;
510       
511        % record the processing parameters of Patch1 as global attributes in the result nc file
512        list_param=fieldnames(Param.ActionInput.Patch1)';
513        list_param(strcmp('TestPatch1',list_param))=[];% remove 'TestPatch1' from the list of parameters
514        Patch1_param=regexprep(list_param,'^.+','Patch1_$0');% insert 'Patch1_' before  each parameter name
515        for ilist=1:length(list_param)
516            Data.(Patch1_param{ilist})=Param.ActionInput.Patch1.(list_param{ilist});
517        end
518        Data.CivStage=3;% record the new state of processing
519        Data.ListGlobalAttribute=[Data.ListGlobalAttribute Patch1_param];
520       
521        % list the variables to record
522        nbvar=length(Data.ListVarName);
523        Data.ListVarName=[Data.ListVarName {'Civ1_U_smooth','Civ1_V_smooth','Civ1_SubRange','Civ1_NbCentres','Civ1_Coord_tps','Civ1_U_tps','Civ1_V_tps'}];
524        Data.VarDimName=[Data.VarDimName {'nb_vec_1','nb_vec_1',{'nb_coord','nb_bounds','nb_subdomain_1'},'nb_subdomain_1',...
525            {'nb_tps_1','nb_coord','nb_subdomain_1'},{'nb_tps_1','nb_subdomain_1'},{'nb_tps_1','nb_subdomain_1'}}];
526        Data.VarAttribute{nbvar+1}.Role='vector_x';
527        Data.VarAttribute{nbvar+2}.Role='vector_y';
528        Data.VarAttribute{nbvar+5}.Role='coord_tps';
529        Data.VarAttribute{nbvar+6}.Role='vector_x';
530        Data.VarAttribute{nbvar+7}.Role='vector_y';
531        Data.Civ1_U_smooth=Data.Civ1_U; % zeros(size(Data.Civ1_X));
532        Data.Civ1_V_smooth=Data.Civ1_V; %zeros(size(Data.Civ1_X));
533        if isfield(Data,'Civ1_FF')
534            ind_good=find(Data.Civ1_FF==0);
535        else
536            ind_good=1:numel(Data.Civ1_X);
537        end
538        if isempty(ind_good)
539            disp_uvmat('ERROR','all vectors of civ1 are bad, check input parameters' ,checkrun)
540            return
541        end
542       
543        % perform Patch calculation using the UVMAT fct 'filter_tps'
544        [Data.Civ1_SubRange,Data.Civ1_NbCentres,Data.Civ1_Coord_tps,Data.Civ1_U_tps,Data.Civ1_V_tps,~,Ures, Vres,~,FFres]=...
545            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);
546        Data.Civ1_U_smooth(ind_good)=Ures;% take the interpolated (smoothed) velocity values for good vectors, keep civ1 data for the other
547        Data.Civ1_V_smooth(ind_good)=Vres;
548        Data.Civ1_FF(ind_good)=uint8(4*FFres);%set FF to value =4 for vectors eliminated by filter_tps
549        time_patch1=toc(tstart_patch1);
550        disp('patch1 performed')
551    end
552   
553    %% Civ2
554    if isfield (Param.ActionInput,'Civ2')
555        disp('civ2 started')
556        tstart_civ2=tic;
557        par_civ2=Param.ActionInput.Civ2;
558        %         if CheckInputFile % read input images (except in mode Test where it is introduced directly in Param.ActionInput.Civ1.ImageNameA and B)
559        par_civ2.ImageA=[];
560        par_civ2.ImageB=[];
561        if strcmp(Param.ActionInput.ListCompareMode,'displacement')
562            ImageName_A_Civ2=Param.ActionInput.RefFile;
563        else
564            ImageName_A_Civ2=fullfile_uvmat(RootPath_A,SubDir_A,RootFile_A,FileExt_A,NomType_A,i1_civ2,[],j1_civ2);
565        end
566        if strcmp(ImageName_A_Civ2,ImageName_A) && isequal(FrameIndex_A_Civ1(ifield),FrameIndex_A_Civ2(ifield))
567            par_civ2.ImageA=par_civ1.ImageA;
568        else
569            [par_civ2.ImageA,VideoObject_A] = read_image(ImageName_A_Civ2,FileType_A,VideoObject_A,FrameIndex_A_Civ2(ifield));
570        end
571        ImageName_B_Civ2=fullfile_uvmat(RootPath_B,SubDir_B,RootFile_B,FileExt_B,NomType_B,i2_civ2,[],j2_civ2);
572        if strcmp(ImageName_B_Civ2,ImageName_B) && isequal(FrameIndex_B_Civ1(ifield),FrameIndex_B_Civ2)
573            par_civ2.ImageB=par_civ1.ImageB;
574        else
575            [par_civ2.ImageB,VideoObject_B] = read_image(ImageName_B_Civ2,FileType_B,VideoObject_B,FrameIndex_B_Civ2(ifield));
576        end
577        %  [FileInfo_A,VideoObject_A]=get_file_info(ImageName_A_Civ2);
578        npy_ima=size(par_civ2.ImageA,1);
579        npx_ima=size(par_civ2.ImageA,2);
580        if par_civ2.CheckGrid &&~isempty(par_civ2.Grid) % case of input grid
581            GridData=nc2struct(Param.ActionInput.Civ2.Grid);
582            par_civ2.Grid=GridData.Grid;
583            par_civ2.CorrBoxSize=GridData.CorrBox;
584        else% automatic grid
585            nbinterv_x=floor((npx_ima-1)/par_civ2.Dx);
586            gridlength_x=nbinterv_x*par_civ2.Dx;
587            minix=ceil((npx_ima-gridlength_x)/2);
588            nbinterv_y=floor((npy_ima-1)/par_civ2.Dy);
589            gridlength_y=nbinterv_y*par_civ2.Dy;
590            miniy=ceil((npy_ima-gridlength_y)/2);
591            [GridX,GridY]=meshgrid(minix:par_civ2.Dx:npx_ima-1,miniy:par_civ2.Dy:npy_ima-1);
592            par_civ2.Grid=zeros(numel(GridX),2);
593            par_civ2.Grid(:,1)=reshape(GridX,[],1);
594            par_civ2.Grid(:,2)=reshape(GridY,[],1);% increases with array index
595        end
596       
597        % get the guess from patch1 or patch2 (case 'CheckCiv3')
598        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
599            [DataIn,~,~,errormsg]=nc2struct(filecell{1,ifield});
600            if ~isempty(errormsg)
601                disp(errormsg)
602                return
603            end
604            SubRange= DataIn.Civ2_SubRange;
605            NbCentres=DataIn.Civ2_NbCentres;
606            Coord_tps=DataIn.Civ2_Coord_tps;
607            U_tps=DataIn.Civ2_U_tps;
608            V_tps=DataIn.Civ2_V_tps;
609            %CivStage=DataIn.CivStage;%store the current CivStage
610            Civ1_Dt=DataIn.Civ2_Dt;
611            Data=[];%reinitialise the result structure Data
612            Data.ListGlobalAttribute={'Conventions','Program','CivStage'};
613            Data.Conventions='uvmat/civdata';% states the conventions used for the description of field variables and attributes
614            Data.Program='civ_series';
615           % Data.CivStage=CivStage+1;%update the current civStage after reinitialisation of Data
616            Data.ListVarName={};
617            Data.VarDimName={};
618        else % get the guess from patch1
619            SubRange= Data.Civ1_SubRange;
620            NbCentres=Data.Civ1_NbCentres;
621            Coord_tps=Data.Civ1_Coord_tps;
622            U_tps=Data.Civ1_U_tps;
623            V_tps=Data.Civ1_V_tps;
624            Civ1_Dt=Data.Civ1_Dt;
625%             Data.CivStage=4;
626        end
627         Data.CivStage=4;
628        %             SubRange= par_civ2.Civ1_SubRange;
629        %             NbCentres=par_civ2.Civ1_NbCentres;
630        %             Coord_tps=par_civ2.Civ1_Coord_tps;
631        %             U_tps=par_civ2.Civ1_U_tps;
632        %             V_tps=par_civ2.Civ1_V_tps;
633        %             Civ1_Dt=par_civ2.Civ1_Dt;
634        %             Civ2_Dt=par_civ2.Civ1_Dt;
635        %             Data.ListVarName={};
636        %             Data.VarDimName={};
637        %         end
638        Shiftx=zeros(size(par_civ2.Grid,1),1);% initialise the shift expected from civ1 data
639        Shifty=zeros(size(par_civ2.Grid,1),1);
640        nbval=zeros(size(par_civ2.Grid,1),1);% nbre of interpolated values at each grid point (from the different patch subdomains)
641        if par_civ2.CheckDeformation
642            DUDX=zeros(size(par_civ2.Grid,1),1);
643            DUDY=zeros(size(par_civ2.Grid,1),1);
644            DVDX=zeros(size(par_civ2.Grid,1),1);
645            DVDY=zeros(size(par_civ2.Grid,1),1);
646        end
647        NbSubDomain=size(SubRange,3);
648        for isub=1:NbSubDomain% for each sub-domain of Patch1
649            nbvec_sub=NbCentres(isub);% nbre of Civ vectors in the subdomain
650            ind_sel=find(par_civ2.Grid(:,1)>=SubRange(1,1,isub) & par_civ2.Grid(:,1)<=SubRange(1,2,isub) &...
651                par_civ2.Grid(:,2)>=SubRange(2,1,isub) & par_civ2.Grid(:,2)<=SubRange(2,2,isub));% grid points in the subdomain
652            if ~isempty(ind_sel)
653                epoints = par_civ2.Grid(ind_sel,:);% coordinates of interpolation sites (measurement grids)
654                ctrs=Coord_tps(1:nbvec_sub,:,isub) ;%(=initial points) ctrs
655                EM = tps_eval(epoints,ctrs);% thin plate spline (tps) coefficient
656                CentreX=(SubRange(1,1,isub)+SubRange(1,2,isub))/2; %x posiion of the subdomain center
657                CentreY=(SubRange(2,1,isub)+SubRange(2,2,isub))/2; %y posiion of the subdomain center
658                xwidth=(SubRange(1,2,isub)-SubRange(1,1,isub))/pi;
659                ywidth=(SubRange(2,2,isub)-SubRange(2,1,isub))/pi;
660                x_dist=(epoints(:,1)-CentreX)/xwidth;
661                y_dist=(epoints(:,2)-CentreY)/ywidth;
662                weight=cos(x_dist).*cos(y_dist);%weighting fct =1 at the rectangle center and 0 at edge
663                nbval(ind_sel)=nbval(ind_sel)+weight;% records the number of values for each interpolation point (in case of subdomain overlap)
664                Shiftx(ind_sel)=Shiftx(ind_sel)+weight.*(EM*U_tps(1:nbvec_sub+3,isub));%velocity shift estimated by tps from civ1
665                Shifty(ind_sel)=Shifty(ind_sel)+weight.*(EM*V_tps(1:nbvec_sub+3,isub));
666                if par_civ2.CheckDeformation
667                    [EMDX,EMDY] = tps_eval_dxy(epoints,ctrs);%2D matrix of distances between extrapolation points epoints and spline centres (=site points) ctrs
668                    DUDX(ind_sel)=DUDX(ind_sel)+weight.*(EMDX*U_tps(1:nbvec_sub+3,isub));
669                    DUDY(ind_sel)=DUDY(ind_sel)+weight.*(EMDY*U_tps(1:nbvec_sub+3,isub));
670                    DVDX(ind_sel)=DVDX(ind_sel)+weight.*(EMDX*V_tps(1:nbvec_sub+3,isub));
671                    DVDY(ind_sel)=DVDY(ind_sel)+weight.*(EMDY*V_tps(1:nbvec_sub+3,isub));
672                end
673            end
674        end
675        Shiftx(nbval>0)=Shiftx(nbval>0)./nbval(nbval>0);
676        Shifty(nbval>0)=Shifty(nbval>0)./nbval(nbval>0);
677       
678        % introduce mask
679        if par_civ2.CheckMask && ~isempty(par_civ2.Mask)
680            [RootPath_mask,SubDir_mask,RootFile_mask,~,~,~,~,Ext_mask]=fileparts_uvmat(Param.ActionInput.Civ2.Mask);
681            if ~isempty(i2_series_Civ2) % we do PIV among indices i,  at given indices j (volume scan), mask depends on position j
682                j1=1;
683                if ~isempty(j1_series_Civ2)
684                    j1=j1_series_Civ1(ifield);
685                end
686                maskname=fullfile_uvmat(RootPath_mask,SubDir_mask,RootFile_mask,Ext_mask,'_1',j1);
687            elseif isfield(par_civ2,'NbSlice')
688                i1=i1_series_Civ2(ifield);
689                i1_mask=mod(i1-1,par_civ2.NbSlice)+1;
690                maskname=fullfile_uvmat(RootPath_mask,SubDir_mask,RootFile_mask,Ext_mask,'_1',i1_mask);
691                if strcmp(Param.ActionInput.PairIndices.ListPairMode,'series(Di)')% case of volume, mask index refers to j index
692                    par_civ2.NbSlice_j=par_civ2.NbSlice;
693                end
694            else
695                maskname=Param.ActionInput.Civ2.Mask;
696            end
697            if strcmp(maskoldname,maskname)% mask exist, not already read in civ1
698                par_civ2.Mask=mask; %use mask already opened
699            else
700                if exist(maskname,'file')
701                    try
702                        par_civ2.Mask=imread(maskname);%update the mask, an store it for future use
703                    catch ME
704                        if ~isempty(ME.message)
705                            errormsg=['error reading input image: ' ME.message];
706                            disp_uvmat('ERROR',errormsg,checkrun)
707                            return
708                        end
709                    end
710                else
711                    par_civ2.Mask=[];
712                end
713                mask=par_civ2.Mask;
714                maskoldname=maskname;
715            end
716        end
717       
718       
719        if strcmp(Param.ActionInput.ListCompareMode,'displacement')
720            Civ1_Dt=1;
721            Civ2_Dt=1;
722        else
723            Civ2_Dt=Time(i2_civ2+1,j2_civ2+1)-Time(i1_civ2+1,j1_civ2+1);
724        end
725        par_civ2.SearchBoxShift=zeros(size(par_civ2.Grid));
726        par_civ2.SearchBoxShift(:,1)=(Civ2_Dt/Civ1_Dt)*Shiftx;%rescale the shift in case of Dt different for Civ1 and Civ2
727        par_civ2.SearchBoxShift(:,2)=(Civ2_Dt/Civ1_Dt)*Shifty;
728       
729        if par_civ2.CheckDeformation
730            par_civ2.DUDX(nbval>0)=DUDX(nbval>0)./nbval(nbval>0);
731            par_civ2.DUDY(nbval>0)=DUDY(nbval>0)./nbval(nbval>0);
732            par_civ2.DVDX(nbval>0)=DVDX(nbval>0)./nbval(nbval>0);
733            par_civ2.DVDY(nbval>0)=DVDY(nbval>0)./nbval(nbval>0);
734        end
735       
736        % calculate velocity data (y and v in image indices, reverse to y component)
737       
738        [Data.Civ2_X,Data.Civ2_Y,Data.Civ2_U,Data.Civ2_V,Data.Civ2_C,Data.Civ2_FF,~, errormsg] = civ (par_civ2);
739       
740        list_param=(fieldnames(Param.ActionInput.Civ2))';
741        list_param(strcmp('TestCiv2',list_param))=[];% remove the parameter TestCiv2 from the list
742        Civ2_param=regexprep(list_param,'^.+','Civ2_$0');% insert 'Civ2_' before  each string in list_param
743        Civ2_param=[{'Civ2_ImageA','Civ2_ImageB','Civ2_Time','Civ2_Dt'} Civ2_param]; %insert the names of the two input images
744        %indicate the values of all the global attributes in the output data
745        if exist('ImageName_A','var')
746            Data.Civ2_ImageA=ImageName_A;
747            Data.Civ2_ImageB=ImageName_B;
748            if strcmp(Param.ActionInput.ListCompareMode,'displacement')
749                Data.Civ2_Time=Time(i2_civ2+1,j2_civ2+1);% the Time is the Time of the secodn image
750                Data.Civ2_Dt=1;% Time interval is 1, to yield displacement instead of velocity=displacement/Dt at reading
751            else
752                Data.Civ2_Time=(Time(i2_civ2+1,j2_civ2+1)+Time(i1_civ2+1,j1_civ2+1))/2;
753                Data.Civ2_Dt=Civ2_Dt;
754            end
755        end
756        for ilist=1:length(list_param)
757            Data.(Civ2_param{4+ilist})=Param.ActionInput.Civ2.(list_param{ilist});
758        end
759        Data.ListGlobalAttribute=[Data.ListGlobalAttribute Civ2_param];
760       
761        nbvar=numel(Data.ListVarName);
762        % define the Civ2 variable (if Civ2 data are not replaced from previous calculation)
763        if isempty(find(strcmp('Civ2_X',Data.ListVarName),1))
764            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
765            Data.VarDimName=[Data.VarDimName {'nb_vec_2','nb_vec_2','nb_vec_2','nb_vec_2','nb_vec_2','nb_vec_2'}];
766            Data.VarAttribute{nbvar+1}.Role='coord_x';
767            Data.VarAttribute{nbvar+2}.Role='coord_y';
768            Data.VarAttribute{nbvar+3}.Role='vector_x';
769            Data.VarAttribute{nbvar+4}.Role='vector_y';
770            Data.VarAttribute{nbvar+5}.Role='ancillary';
771            Data.VarAttribute{nbvar+6}.Role='errorflag';
772        end
773        disp('civ2 performed')
774        time_civ2=toc(tstart_civ2);
775    elseif ~isfield(Data,'ListVarName') % we start there, using existing Civ2 data
776        if exist('ncfile','var')
777            CivFile=ncfile;
778            [Data,~,~,errormsg]=nc2struct(CivFile);%read civ1 and detect_false1 data in the existing netcdf file
779            if ~isempty(errormsg)
780                disp_uvmat('ERROR',errormsg,checkrun)
781                return
782            end
783        end
784    end
785   
786    %% Fix2
787    if Param.ActionInput.CheckFix2 && isfield (Param.ActionInput,'Fix2')% if Fix2 computation is requested
788        disp('detect_false2 started')
789        list_param=fieldnames(Param.ActionInput.Fix2)';
790        Fix2_param=regexprep(list_param,'^.+','Fix2_$0');% insert 'Fix1_' before  each string in ListFixParam
791        %indicate the values of all the global attributes in the output data
792        for ilist=1:length(list_param)
793            Data.(Fix2_param{ilist})=Param.ActionInput.Fix2.(list_param{ilist});
794        end
795        Data.ListGlobalAttribute=[Data.ListGlobalAttribute Fix2_param];
796        Data.Civ2_FF=double(detect_false(Param.ActionInput.Fix2,Data.Civ2_C,Data.Civ2_U,Data.Civ2_V,Data.Civ2_FF));
797        Data.CivStage=Data.CivStage+1;
798    end
799   
800    %% Patch2
801    if Param.ActionInput.CheckPatch2 && isfield (Param.ActionInput,'Patch2')% if Patch2 computation is requested
802       
803        disp('patch2 started')
804        tstart_patch2=tic;
805        list_param=fieldnames(Param.ActionInput.Patch2)';
806        list_param(strcmp('TestPatch2',list_param))=[];% remove the parameter TestCiv1 from the list
807        Patch2_param=regexprep(list_param,'^.+','Patch2_$0');% insert 'Fix1_' before  each string in ListFixParam
808        %indicate the values of all the global attributes in the output data
809        for ilist=1:length(list_param)
810            Data.(Patch2_param{ilist})=Param.ActionInput.Patch2.(list_param{ilist});
811        end
812        Data.ListGlobalAttribute=[Data.ListGlobalAttribute Patch2_param];
813       
814        nbvar=length(Data.ListVarName);
815        Data.ListVarName=[Data.ListVarName {'Civ2_U_smooth','Civ2_V_smooth','Civ2_SubRange','Civ2_NbCentres','Civ2_Coord_tps','Civ2_U_tps','Civ2_V_tps'}];
816        Data.VarDimName=[Data.VarDimName {'nb_vec_2','nb_vec_2',{'nb_coord','nb_bounds','nb_subdomain_2'},{'nb_subdomain_2'},...
817            {'nb_tps_2','nb_coord','nb_subdomain_2'},{'nb_tps_2','nb_subdomain_2'},{'nb_tps_2','nb_subdomain_2'}}];
818       
819        Data.VarAttribute{nbvar+1}.Role='vector_x';
820        Data.VarAttribute{nbvar+2}.Role='vector_y';
821        Data.VarAttribute{nbvar+5}.Role='coord_tps';
822        Data.VarAttribute{nbvar+6}.Role='vector_x';
823        Data.VarAttribute{nbvar+7}.Role='vector_y';
824        Data.Civ2_U_smooth=Data.Civ2_U;
825        Data.Civ2_V_smooth=Data.Civ2_V;
826        if isfield(Data,'Civ2_FF')
827            ind_good=find(Data.Civ2_FF==0);
828        else
829            ind_good=1:numel(Data.Civ2_X);
830        end
831        if isempty(ind_good)
832            disp_uvmat('ERROR','all vectors of civ2 are bad, check input parameters' ,checkrun)
833            return
834        end
835       
836        [Data.Civ2_SubRange,Data.Civ2_NbCentres,Data.Civ2_Coord_tps,Data.Civ2_U_tps,Data.Civ2_V_tps,tild,Ures,Vres,tild,FFres]=...
837            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);
838        Data.Civ2_U_smooth(ind_good)=Ures;
839        Data.Civ2_V_smooth(ind_good)=Vres;
840        Data.Civ2_FF(ind_good)=uint8(4*FFres);
841        Data.CivStage=Data.CivStage+1;
842        time_patch2=toc(tstart_patch2);
843        disp('patch2 performed')
844    end
845   
846    %% write result in a netcdf file
847    errormsg=struct2nc(ncfile_out,Data);
848    if isempty(errormsg)
849        disp([ncfile_out ' written'])
850    else
851        disp(errormsg)
852    end
853    time_total=toc(tstart);
854    disp(['ellapsed time ' num2str(time_total/60,2) ' minutes'])
855    disp(['time civ1 ' num2str(time_civ1,2) ' s'])
856    disp(['time patch1 ' num2str(time_patch1,2) ' s'])
857    disp(['time civ2 ' num2str(time_civ2,2) ' s'])
858    disp(['time patch2 ' num2str(time_patch2,2) ' s'])
859    if exist('time_input','var')
860        disp(['time image reading ' num2str(time_input,2) ' s'])
861        disp(['time other ' num2str((time_total-time_input-time_civ1-time_patch1-time_civ2-time_patch2),2) ' s'])
862    end
863end
864
865
866
867%------------------------------------------------------------------------
868% --- determine the list of index pairs of processing file
869function [i1_series,i2_series,j1_series,j2_series,check_bounds,NomTypeNc]=...
870    find_pair_indices(str_civ,i_series,j_series,MinIndex_i,MaxIndex_i,MinIndex_j,MaxIndex_j)
871%------------------------------------------------------------------------
872i1_series=i_series;% set of first image indexes
873i2_series=i_series;
874j1_series=j_series;%ones(size(i_series));% set of first image numbers
875j2_series=j_series;%ones(size(i_series));
876r=regexp(str_civ,'^\D(?<ind>[i|j])=( -| )(?<num1>\d+)\|(?<num2>\d+)','names');
877if ~isempty(r)
878    mode=['D' r.ind];
879    ind1=str2num(r.num1);
880    ind2=str2num(r.num2);
881else
882    mode='j1-j2';
883    r=regexp(str_civ,'^j= (?<num1>[a-z])-(?<num2>[a-z])','names');
884    if ~isempty(r)
885        NomTypeNc='_1ab';
886    else
887        r=regexp(str_civ,'^j= (?<num1>[A-Z])-(?<num2>[A-Z])','names');
888        if ~isempty(r)
889            NomTypeNc='_1AB';
890        else
891            r=regexp(str_civ,'^j= (?<num1>\d+)-(?<num2>\d+)','names');
892            if ~isempty(r)
893                NomTypeNc='_1_1-2';
894            end
895        end
896    end
897    if isempty(r)
898        display('wrong pair mode input option')
899    else
900        ind1=stra2num(r.num1);
901        ind2=stra2num(r.num2);
902    end
903end
904switch mode
905    case 'Di'
906        i1_series=i_series-ind1;% set of first image numbers
907        i2_series=i_series+ind2;
908        check_bounds=i1_series<MinIndex_i | i2_series>MaxIndex_i;
909        if isempty(j_series)
910            NomTypeNc='_1-2';
911        else
912            j1_series=j_series;
913            j2_series=j_series;
914            NomTypeNc='_1-2_1';
915        end
916    case 'Dj'
917        j1_series=j_series-ind1;
918        j2_series=j_series+ind2;
919        check_bounds=j1_series<MinIndex_j | j2_series>MaxIndex_j;
920        NomTypeNc='_1_1-2';
921    otherwise %bursts
922        i1_series=i_series(1,:);% do not sweep the j index
923        i2_series=i_series(1,:);
924        j1_series=ind1*ones(1,size(i_series,2));% j index is fixed by pair choice
925        j2_series=ind2*ones(1,size(i_series,2));
926        check_bounds=zeros(size(i1_series));% no limitations due to min-max indices
927end
928
929%------------------------------------------------------------------------
930% --- set the flag for false vectors
931function FF=detect_false(Param,C,U,V,FFIn)
932FF=FFIn;%default, good vectors
933% FF=1, for correlation max at edge, not set in this function
934% FF=2, for too small correlation
935% FF=3, for velocity outside bounds
936% FF=4 for exclusion by difference with the smoothed field, set by call to function filter_tps
937
938if isfield (Param,'MinCorr')
939     FF(C<Param.MinCorr & FFIn==0)=2;
940end
941if (isfield(Param,'MinVel')&&~isempty(Param.MinVel))||(isfield (Param,'MaxVel')&&~isempty(Param.MaxVel))
942    Umod= U.*U+V.*V;
943    if isfield (Param,'MinVel')&&~isempty(Param.MinVel)
944        U2Min=Param.MinVel*Param.MinVel;
945        FF(Umod<U2Min & FFIn==0)=3;
946    end
947    if isfield (Param,'MaxVel')&&~isempty(Param.MaxVel)
948         U2Max=Param.MaxVel*Param.MaxVel;
949        FF(Umod>U2Max & FFIn==0)=3;
950    end
951end
952
953
954
Note: See TracBrowser for help on using the repository browser.