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

Last change on this file since 1176 was 1176, checked in by sommeria, 2 days ago

background image introduced for civ1

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