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

Last change on this file since 1179 was 1178, checked in by sommeria, 4 weeks ago

virtual frame indexing introduced

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