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

Last change on this file since 1168 was 1168, checked in by sommeria, 12 months ago

bugs repaired in civ TEST mode

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