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

Last change on this file since 1177 was 1177, checked in by sommeria, 9 months ago

background image introduced for civ1

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