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

Last change on this file since 1201 was 1201, checked in by sommeria, 5 hours ago

various improvements...

File size: 53.6 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
[1201]48function [Data,errormsg]= 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)
[1181]71    Data.FieldTransform = 'off';%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
[597]98
[1195]99inv_scale_factor=100; % scale factor of displacements for uin16 records in netcdf files (dx expressed in pixels)
100%inv_scale_factor=[];% no scale factor, displacements written as single precision real
101
102%% input files and indexing
[1169]103hseries=findobj(allchild(0),'Tag','series');
104RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
[1199]105% WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series
[1169]106MaxIndex_i=Param.IndexRange.MaxIndex_i;
107MinIndex_i=Param.IndexRange.MinIndex_i;
108MaxIndex_j=ones(size(MaxIndex_i));MinIndex_j=ones(size(MinIndex_i));
109if isfield(Param.IndexRange,'MaxIndex_j')&& isfield(Param.IndexRange,'MinIndex_j')
110    MaxIndex_j=Param.IndexRange.MaxIndex_j;
111    MinIndex_j=Param.IndexRange.MinIndex_j;
112end
113if isfield(Param,'InputTable')
114    [filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
115    iview_B=0;% series index (iview) for the second image series (only non zero for option 'shift' comparing two image series )
116    if Param.ActionInput.CheckCiv1
117        iview_A=1;% usual PIV, the image series is on the first line of the table
118    else % Civ1 has been already stored in a netcdf file input
119        iview_A=2;% the second line is used for the input images
[609]120    end
[1169]121    RootPath_A=Param.InputTable{iview_A,1};
122    RootFile_A=Param.InputTable{iview_A,3};
123    SubDir_A=Param.InputTable{iview_A,2};
124    NomType_A=Param.InputTable{iview_A,4};
125    FileExt_A=Param.InputTable{iview_A,5};
126    if iview_B==0
127        iview_B=iview_A;% the second image series is the same as the first
128    end
129    RootPath_B=Param.InputTable{iview_B,1};
130    RootFile_B=Param.InputTable{iview_B,3};
131    SubDir_B=Param.InputTable{iview_B,2};
132    NomType_B=Param.InputTable{iview_B,4};
133    FileExt_B=Param.InputTable{iview_B,5};
134    PairCiv2='';
135   
136    switch Param.ActionInput.ListCompareMode
137        case 'PIV'
138            PairCiv1=Param.ActionInput.PairIndices.ListPairCiv1;
139            if isfield(Param.ActionInput.PairIndices,'ListPairCiv2')
140                PairCiv2=Param.ActionInput.PairIndices.ListPairCiv2;%string which determines the civ2 pair
[758]141            end
[1169]142            if iview_A==1% if Civ1 is performed
143                [i1_series_Civ1,i2_series_Civ1,j1_series_Civ1,j2_series_Civ1,check_bounds,NomTypeNc]=...
144                    find_pair_indices(PairCiv1,i1_series{1},j1_series{1},MinIndex_i,MaxIndex_i,MinIndex_j,MaxIndex_j);
145                if ~isempty(PairCiv2)
146                    [i1_series_Civ2,i2_series_Civ2,j1_series_Civ2,j2_series_Civ2,check_bounds_Civ2]=...
147                        find_pair_indices(PairCiv2,i1_series{1},j1_series{1},MinIndex_i(1),MaxIndex_i(1),MinIndex_j(1),MaxIndex_j(1));
148                    check_bounds=check_bounds | check_bounds_Civ2;
[761]149                end
[1169]150            else% we start from an existing Civ1 file
151                i1_series_Civ1=i1_series{1};
152                i2_series_Civ1=i2_series{1};
153                j1_series_Civ1=j1_series{1};
154                j2_series_Civ1=j2_series{1};
155                NomTypeNc=Param.InputTable{1,4};
156                if ~isempty(PairCiv2)
157                    [i1_series_Civ2,i2_series_Civ2,j1_series_Civ2,j2_series_Civ2,check_bounds,NomTypeNc]=...
158                        find_pair_indices(PairCiv2,i1_series{2},j1_series{2},MinIndex_i(2),MaxIndex_i(2),MinIndex_j(2),MaxIndex_j(2));
[761]159                end
[1169]160            end
161        case 'displacement'
162            if isfield(Param.ActionInput,'OriginIndex')
[855]163                i1_series_Civ1=Param.ActionInput.OriginIndex*ones(size(i1_series{1}));
[1169]164            else
165                i1_series_Civ1=ones(size(i1_series{1}));
[1020]166            end
[1169]167            i1_series_Civ2=i1_series_Civ1;
168            i2_series_Civ1=i1_series{1};
169            i2_series_Civ2=i1_series{1};
170            j1_series_Civ1=[];% no j index variation for the ref image
171            j1_series_Civ2=[];
172            if isempty(j1_series{1})
173                j2_series_Civ1=ones(size(i1_series_Civ1));
[855]174            else
[1169]175                j2_series_Civ1=j1_series{1};% if j index exist
[855]176            end
[1169]177            j2_series_Civ2=j2_series_Civ1;
178            NomTypeNc='_1';
179    end
180    %determine frame indices for input with movie or other multiframe input file
181    if isempty(j1_series_Civ1)% simple movie with index i
182        FrameIndex_A_Civ1=i1_series_Civ1;
183        FrameIndex_B_Civ1=i2_series_Civ1;
184        j1_series_Civ1=ones(size(i1_series_Civ1));
185        if strcmp(Param.ActionInput.ListCompareMode,'PIV')
186            j2_series_Civ1=ones(size(i1_series_Civ1));
[758]187        end
[1195]188        Check_j_Civ1=false;
[1169]189    else % movie for each burst or volume (index j)
190        FrameIndex_A_Civ1=j1_series_Civ1;
191        FrameIndex_B_Civ1=j2_series_Civ1;
[1195]192        Check_j_Civ1=true;
[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
[1195]205            Check_j_Civ2=false;
[819]206        else
[1169]207            FrameIndex_A_Civ2=j1_series_Civ2;
208            FrameIndex_B_Civ2=j2_series_Civ2;
[1195]209            Check_j_Civ2=true;
[781]210        end
[758]211    end
[1169]212    if isempty(i1_series_Civ1)||(~isempty(PairCiv2) && isempty(i1_series_Civ2))
213        disp_uvmat('ERROR','no image pair for civ in the input file index range',checkrun)
214        return
215    end
[758]216end
217
[1169]218%% check the first image pair
219if Param.ActionInput.CheckCiv1% Civ1 is performed
220    NbField=numel(i1_series_Civ1);
221elseif Param.ActionInput.CheckCiv2 % Civ2 is performed without Civ1
222    NbField=numel(i1_series_Civ2);
223else
224    NbField=numel(i1_series_Civ1);% no image used (only detect_false or patch) TO CHECK
225end
226
[855]227%% prepare output Data
[1169]228OutputDir=[Param.OutputSubDir Param.OutputDirExt];
[886]229ListGlobalAttribute={'Conventions','Program','CivStage'};
[1195]230Data.Conventions='uvmat/civdata/compress';% states the conventions used for the description of field variables and attributes
[605]231Data.Program='civ_series';
[1154]232if isfield(Param,'UvmatRevision')
233    Data.Program=[Data.Program ', uvmat r' Param.UvmatRevision];
234end
[605]235Data.CivStage=0;%default
236
[606]237%% get timing from the ImaDoc file or input video
[1169]238% if iview_A~=0
239XmlFileName=find_imadoc(RootPath_A,SubDir_A);
240Time=[];
241if ~isempty(XmlFileName)
242    XmlData=imadoc2struct(XmlFileName);%read the time from XmlFileName
243    if isfield(XmlData,'Time')
244        Time=XmlData.Time;
245    end
246    if isfield(XmlData,'Camera')
247        if isfield(XmlData.Camera,'NbSlice')&& ~isempty(XmlData.Camera.NbSlice)
248            NbSlice_calib{iview}=XmlData.Camera.NbSlice;% Nbre of slices for Zindex in phys transform
249            if ~isequal(NbSlice_calib{iview},NbSlice_calib{1})
250                msgbox_uvmat('WARNING','inconsistent number of Z indices for the two field series');
[609]251            end
252        end
[1169]253        if isfield(XmlData.Camera,'TimeUnit')&& ~isempty(XmlData.Camera.TimeUnit)
254            TimeUnit=XmlData.Camera.TimeUnit;
255        end
[833]256    end
[610]257end
[1180]258
259%% File relabeling documented by the xml file (e.g. PCO)
[1188]260CheckRelabel=isfield(Param.IndexRange,'Relabel' )&& Param.IndexRange.Relabel;%=true for index relabeling (PCO)
[609]261
[598]262%%%%% MAIN LOOP %%%%%%
[1010]263maskoldname='';% initiate the mask name
[1176]264backgroundoldname='';
[997]265FileType_A='';
266FileType_B='';
[904]267CheckOverwrite=1;%default
268if isfield(Param,'CheckOverwrite')
269    CheckOverwrite=Param.CheckOverwrite;
270end
[598]271for ifield=1:NbField
[1131]272    tstart=tic;
273    time_civ1=0;
[1164]274    time_patch1=0;
275    time_civ2=0;
276    time_patch2=0;
[855]277    if ~isempty(RUNHandle)% update the waitbar in interactive mode with GUI series  (checkrun=1)
[905]278        if  checkrun && ~strcmp(get(RUNHandle,'BusyAction'),'queue')
[855]279            disp('program stopped by user')
280            break
281        end
[624]282    end
[1169]283    OutputPath=fullfile(Param.OutputPath,Param.Experiment,Param.Device);
[1178]284    if CheckRelabel
[1195]285        RootFileOut=index2filename(XmlData.FileSeries,1,1,MaxIndex_j);
[1178]286    else
287        RootFileOut=RootFile_A;
288    end
[1169]289    if strcmp(Param.ActionInput.ListCompareMode,'PIV')
[1178]290        ncfile=fullfile_uvmat(OutputPath,OutputDir,RootFileOut,'.nc',NomTypeNc,i1_series_Civ1(ifield),i2_series_Civ1(ifield),...
[1169]291            j1_series_Civ1(ifield),j2_series_Civ1(ifield));
292    else
[1178]293        ncfile=fullfile_uvmat(OutputPath,OutputDir,RootFileOut,'.nc',NomTypeNc,i2_series_Civ1(ifield),[],...
[1169]294            j1_series_Civ1(ifield),j2_series_Civ1(ifield));
295    end
296    ncfile_out=ncfile;% by default
297   
298    if isfield (Param.ActionInput,'Civ2')
299        i1_civ2=i1_series_Civ2(ifield);
300        i2_civ2=i1_civ2;
301        if ~isempty(i2_series_Civ2)
302            i2_civ2=i2_series_Civ2(ifield);
[822]303        end
[1169]304        j1_civ2=1;
305        if ~isempty(j1_series_Civ2)
306            j1_civ2=j1_series_Civ2(ifield);
[904]307        end
[1169]308        j2_civ2=i1_civ2;
309        if ~isempty(j2_series_Civ2)
310            j2_civ2=j2_series_Civ2(ifield);
[912]311        end
[1169]312        if strcmp(Param.ActionInput.ListCompareMode,'PIV')
[1178]313            ncfile_out=fullfile_uvmat(OutputPath,OutputDir,RootFileOut,'.nc',NomTypeNc,i1_civ2,i2_civ2,j1_civ2,j2_civ2);
[1169]314        else % displacement
[1178]315            ncfile_out=fullfile_uvmat(OutputPath,OutputDir,RootFileOut,'.nc',NomTypeNc,i2_civ2,[],j2_civ2);
[1169]316        end
[761]317    end
[1169]318    if ~CheckOverwrite && exist(ncfile_out,'file')
319        disp(['existing output file ' ncfile_out ' already exists, skip to next field'])
320        continue% skip iteration if the mode overwrite is desactivated and the result file already exists
321    end
322    %     end
[1122]323    ImageName_A='';ImageName_B='';%default
324    VideoObject_A=[];VideoObject_B=[];
[1195]325    Civ_FF=[];%default
[1143]326   
[598]327    %% Civ1
[707]328    % if Civ1 computation is requested
[1164]329    if Param.ActionInput.CheckCiv1
[1198]330        tstart_civ1=tic;
[1169]331        disp('civ1 started')
[1020]332        par_civ1=Param.ActionInput.Civ1;% parameters for civ1
[1169]333        %if CheckInputFile % read input images (except in mode Test where it is introduced directly in Param.ActionInput.Civ1.ImageNameA and B)
[1199]334        % try
[1169]335            if strcmp(Param.ActionInput.ListCompareMode,'displacement')
336                ImageName_A=Param.ActionInput.RefFile;
[1187]337                FrameIndex_A=1;
[1178]338            elseif CheckRelabel
[1188]339                [RootFile,FrameIndex_A]=index2filename(XmlData.FileSeries,i1_series_Civ1(ifield),j1_series_Civ1(ifield),MaxIndex_j);
340                ImageName_A=fullfile(RootPath_A,SubDir_A,RootFile);
[1169]341            else
342                ImageName_A=fullfile_uvmat(RootPath_A,SubDir_A,RootFile_A,FileExt_A,NomType_A,i1_series_Civ1(ifield),[],j1_series_Civ1(ifield));
[1178]343                FrameIndex_A=FrameIndex_A_Civ1(ifield);
[1169]344            end
345            if strcmp(FileExt_A,'.nc')% case of input images in format netcdf
346                FieldName_A=Param.InputFields.FieldName;
347                [DataIn,~,~,errormsg]=nc2struct(ImageName_A,{FieldName_A});
348                par_civ1.ImageA=DataIn.(FieldName_A);
349            else % usual image formats for image A
350                if isempty(FileType_A)% open the image object if not already done in case of movie input
351                    [FileInfo_A,VideoObject_A]=get_file_info(ImageName_A);
352                    FileType_A=FileInfo_A.FileType;
[1199]353                    if isempty(Time) && ismember(FileType_A,{'video','cine_phantom','telopsIR'})% case of video inputFrameIndex_A
[1180]354                        Time=zeros(FileInfo_A.NumberOfFrames+1,2);
[1169]355                        Time(:,2)=(0:1/FileInfo_A.FrameRate:(FileInfo_A.NumberOfFrames)/FileInfo_A.FrameRate)';
[1199]356                        if ~isempty(j1_series_Civ1) & j1_series_Civ1~=1
[1180]357                            Time=Time';
358                        end
[997]359                    end
[1169]360                    if ~isempty(FileType_A) && isempty(Time)% Time = index i +0.001 index j by default
361                        MaxIndex_i=max(i2_series_Civ1);
362                        MaxIndex_j=max(j2_series_Civ1);
363                        Time=(1:MaxIndex_i)'*ones(1,MaxIndex_j);
364                        Time=Time+0.001*ones(MaxIndex_i,1)*(1:MaxIndex_j);
365                        Time=[zeros(1,MaxIndex_j);Time];% insert a first line of zeros
366                        Time=[zeros(MaxIndex_i+1,1) Time];% insert a first column of zeros
[997]367                    end
[855]368                end
[1169]369                if isempty(regexp(ImageName_A,'(^http://)|(^https://)', 'once')) && ~exist(ImageName_A,'file')
370                    disp([ImageName_A ' missing'])
[1164]371                    continue
372                end
[1169]373                tsart_input=tic;
[1178]374                [par_civ1.ImageA,VideoObject_A] = read_image(ImageName_A,FileType_A,VideoObject_A,FrameIndex_A);
[1169]375                time_input=toc(tsart_input);
[781]376            end
[1180]377            if CheckRelabel
[1188]378                [RootFile,FrameIndex_B]=index2filename(XmlData.FileSeries,i2_series_Civ1(ifield),j2_series_Civ1(ifield),MaxIndex_j);
[1180]379                ImageName_B=fullfile(RootPath_B,SubDir_B,RootFile);
380            else
381                ImageName_B=fullfile_uvmat(RootPath_B,SubDir_B,RootFile_B,FileExt_B,NomType_B,i2_series_Civ1(ifield),[],j2_series_Civ1(ifield));
382                FrameIndex_B=FrameIndex_B_Civ1(ifield);
383            end
[1169]384            if isempty(FileType_B)% determine the image type for the first field
385                [FileInfo_B,VideoObject_B]=get_file_info(ImageName_B);
386                FileType_B=FileInfo_B.FileType;
[781]387            end
[1169]388            if isempty(regexp(ImageName_B,'(^http://)|(^https://)', 'once')) && ~exist(ImageName_B,'file')
389                disp([ImageName_B ' missing'])
390                continue
[758]391            end
[1178]392            [par_civ1.ImageB,VideoObject_B] = read_image(ImageName_B,FileType_B,VideoObject_B,FrameIndex_B);
[1195]393           
394       
395        % case of background image to subtract
[1176]396        if par_civ1.CheckBackground &&~isempty(par_civ1.Background)
397            [RootPath_background,SubDir_background,RootFile_background,~,~,~,~,Ext_background]=fileparts_uvmat(Param.ActionInput.Civ1.Background);
[1200]398            if strcmp(NomTypeNc,'_1-2_1')% case of volume,backgrounds act on different j levels
[1195]399                backgroundname=fullfile_uvmat(RootPath_background,SubDir_background,RootFile_background,Ext_background,'_1',j1_series_Civ1(ifield));
[1201]400            elseif isfield(par_civ1,'NbSlice')
[1176]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
[1200]414                        par_civ1.Background=uint16(imread(backgroundname));%update the background, an store it for future use
[1176]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
[1200]428            par_civ1.ImageA=uint16(par_civ1.ImageA)-par_civ1.Background;
429            par_civ1.ImageB=uint16(par_civ1.ImageB)-par_civ1.Background;
[1176]430        end
[1195]431       
432        % case of image luminosity rescaling
433        if par_civ1.CheckRescale &&~isempty(par_civ1.Maxtanh)
434            par_civ1.ImageA =par_civ1.Maxtanh*tanh(double(par_civ1.ImageA)/par_civ1.Maxtanh);
435            par_civ1.ImageB=par_civ1.Maxtanh*tanh(double(par_civ1.ImageB)/par_civ1.Maxtanh);
[1175]436        end
[1169]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
[1195]452       
453        j1=j1_series_Civ1(ifield);
454       
[1169]455        j2=j1;
[1195]456        if Check_j_Civ1
[1169]457            j2=j2_series_Civ1(ifield);
458        end
459        if strcmp(Param.ActionInput.ListCompareMode,'displacement')
460            Data.Civ1_Time=Time(i2+1,j2+1);% the Time is the Time of the second image
461            Data.Civ1_Dt=1;% Time interval is 1, to yield displacement instead of velocity=displacement/Dt at reading
[1097]462        else
[1169]463            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
464            Data.Civ1_Dt=Time(i2+1,j2+1)-Time(i1+1,j1+1);
[597]465        end
[1169]466        for ilist=1:length(list_param)
467            Data.(Civ1_param{4+ilist})=Param.ActionInput.Civ1.(list_param{ilist});
468        end
469        Data.ListGlobalAttribute=[ListGlobalAttribute Civ1_param];
470        Data.CivStage=1;
471       
[598]472        % set the list of variables
[1195]473        %         Data.ListVarName={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_C','Civ1_FF'};%  cell array containing the names of the fields to record
474        %         Data.VarDimName={'nb_vec_1','nb_vec_1','nb_vec_1','nb_vec_1','nb_vec_1','nb_vec_1'};
475        %         Data.VarAttribute{1}.Role='coord_x';
476        %         Data.VarAttribute{2}.Role='coord_y';
477        %         Data.VarAttribute{3}.Role='vector_x';
478        %         Data.VarAttribute{4}.Role='vector_y';
479        %         Data.VarAttribute{5}.Role='ancillary';
480        %         Data.VarAttribute{6}.Role='errorflag';
[1164]481       
[1010]482        % case of mask
[856]483        if par_civ1.CheckMask&&~isempty(par_civ1.Mask)
[1163]484            [RootPath_mask,SubDir_mask,RootFile_mask,~,~,~,~,Ext_mask]=fileparts_uvmat(Param.ActionInput.Civ1.Mask);
[1195]485            if Check_j_Civ1
[1163]486                j1=j1_series_Civ1(ifield);
[1195]487            else
488                j1=[];
[1163]489            end
[1183]490            if ~isempty(i2_series_Civ1)&& ~isequal(i1_series_Civ1,i2_series_Civ1)% case of volume,masks act on different j levels
[1163]491                maskname=fullfile_uvmat(RootPath_mask,SubDir_mask,RootFile_mask,Ext_mask,'_1',j1);
[1201]492            elseif isfield(par_civ1,'NbSlice')
[1010]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);
[1164]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
[1165]498            else
499                maskname=Param.ActionInput.Civ1.Mask;
[1010]500            end
[1165]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
[861]513                    end
[1165]514                else
515                    par_civ1.Mask=[];
[1187]516                    disp_uvmat('ERROR',[maskname ' does not exist'],checkrun);
517                    return
[861]518                end
[1165]519                mask=par_civ1.Mask;
520                maskoldname=maskname;
[856]521            end
522        end
[1195]523       
[1163]524        % case of input grid
525        if par_civ1.CheckGrid &&~isempty(par_civ1.Grid)
526            GridData=nc2struct(Param.ActionInput.Civ1.Grid);
527            par_civ1.Grid=GridData.Grid;
528            par_civ1.CorrBoxSize=GridData.CorrBox;
[597]529        end
[1164]530       
531        % caluclate velocity data
[1201]532        if strcmp(Param.RunMode,'cluster')
533            [Civ_X,Civ_Y,Civ_U,Civ_V,Civ_C,Civ_FF,~, errormsg] = civ (par_civ1);% single processor used in cluster
534        else
535            [Civ_X,Civ_Y,Civ_U,Civ_V,Civ_C,Civ_FF,errormsg] = parciv (par_civ1);%use parfor loop
536        end
[1195]537        Civ_X_shifted=Civ_X-0.5+Civ_U/2;% get the exact positions
538        Civ_Y_shifted=Civ_Y-0.5+Civ_V/2;
[1163]539        if ~isempty(errormsg)
540            disp_uvmat('ERROR',errormsg,checkrun)
541            return
542        end
[1198]543        time_civ1=toc(tstart_civ1);
[597]544    end
545   
[1169]546   
[598]547    %% Fix1
548    if isfield (Param.ActionInput,'Fix1')
[1143]549        disp('detect_false1 started')
[822]550        if ~isfield (Param.ActionInput,'Civ1')% if we use existing Civ1, remove previous data beyond Civ1
551            Fix1_attr=find(strcmp('Fix1',Data.ListGlobalAttribute));
552            Data.ListGlobalAttribute(Fix1_attr)=[];
553            for ilist=1:numel(Fix1_attr)
554                Data=rmfield(Data,Data.ListGlobalAttribute{Fix1_attr(ilist)});
555            end
[859]556        end
[852]557        list_param=fieldnames(Param.ActionInput.Fix1)';
558        Fix1_param=regexprep(list_param,'^.+','Fix1_$0');% insert 'Fix1_' before  each string in ListFixParam
[859]559        %indicate the values of all the global attributes in the output data
[852]560        for ilist=1:length(list_param)
561            Data.(Fix1_param{ilist})=Param.ActionInput.Fix1.(list_param{ilist});
[598]562        end
[852]563        Data.ListGlobalAttribute=[Data.ListGlobalAttribute Fix1_param];
[1195]564        % Data.Civ1_FF=uint8(detect_false(Param.ActionInput.Fix1,Data.Civ1_C,Data.Civ1_U,Data.Civ1_V,Data.Civ1_FF));
565        Civ_FF=uint8(detect_false(Param.ActionInput.Fix1,Civ_C,Civ_U,Civ_V,Civ_FF));
[859]566        Data.CivStage=2;
[597]567    end
[598]568    %% Patch1
[1169]569    if Param.ActionInput.CheckPatch1 && isfield (Param.ActionInput,'Patch1')
[896]570        disp('patch1 started')
[1169]571        tstart_patch1=tic;
572       
[855]573        % record the processing parameters of Patch1 as global attributes in the result nc file
[852]574        list_param=fieldnames(Param.ActionInput.Patch1)';
[855]575        list_param(strcmp('TestPatch1',list_param))=[];% remove 'TestPatch1' from the list of parameters
576        Patch1_param=regexprep(list_param,'^.+','Patch1_$0');% insert 'Patch1_' before  each parameter name
[852]577        for ilist=1:length(list_param)
578            Data.(Patch1_param{ilist})=Param.ActionInput.Patch1.(list_param{ilist});
579        end
[855]580        Data.CivStage=3;% record the new state of processing
[852]581        Data.ListGlobalAttribute=[Data.ListGlobalAttribute Patch1_param];
[598]582       
[855]583        % list the variables to record
[1195]584        %         nbvar=length(Data.ListVarName);
585        %         Data.ListVarName=[Data.ListVarName {'Civ1_U_smooth','Civ1_V_smooth','Civ1_SubRange','Civ1_NbCentres','Civ1_Coord_tps','Civ1_U_tps','Civ1_V_tps'}];
586        %         Data.VarDimName=[Data.VarDimName {'nb_vec_1','nb_vec_1',{'nb_coord','nb_bounds','nb_subdomain_1'},'nb_subdomain_1',...
587        %             {'nb_tps_1','nb_coord','nb_subdomain_1'},{'nb_tps_1','nb_subdomain_1'},{'nb_tps_1','nb_subdomain_1'}}];
588        %         Data.VarAttribute{nbvar+1}.Role='vector_x';
589        %         Data.VarAttribute{nbvar+2}.Role='vector_y';
590        %         Data.VarAttribute{nbvar+5}.Role='coord_tps';
591        %         Data.VarAttribute{nbvar+6}.Role='vector_x';
592        %         Data.VarAttribute{nbvar+7}.Role='vector_y';
593        %Data.Civ1_U_smooth=Data.Civ1_U; % zeros(size(Data.Civ1_X));
594        %Data.Civ1_V_smooth=Data.Civ1_V; %zeros(size(Data.Civ1_X));
595        %         if isfield(Data,'Civ1_FF')
596        if isempty(Civ_FF)
597            ind_good=1:numel(Civ_X);
[597]598        else
[1195]599            ind_good=find(Civ_FF==0);
[597]600        end
[1115]601        if isempty(ind_good)
[1169]602            disp_uvmat('ERROR','all vectors of civ1 are bad, check input parameters' ,checkrun)
603            return
[1115]604        end
[1169]605       
[859]606        % perform Patch calculation using the UVMAT fct 'filter_tps'
[1195]607        [SubRange,NbCentres,Coord_tps,U_tps,V_tps,~,Ures, Vres,~,FFres]=...
608            filter_tps([Civ_X_shifted(ind_good),Civ_Y_shifted(ind_good)],Civ_U(ind_good),Civ_V(ind_good),[],Data.Patch1_SubDomainSize,Data.Patch1_FieldSmooth,Data.Patch1_MaxDiff);
609        Civ_U_smooth=Civ_U;% false vectors kept unchanged
610        Civ_V_smooth=Civ_V;
611        Civ_U_smooth(ind_good)=Ures;% take the interpolated (smoothed) velocity values for good vectors, keep civ1 data for the other
612        Civ_V_smooth(ind_good)=Vres;
613        Civ_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
[1188]629            [RootFile,FrameIndex_A_2]=index2filename(XmlData.FileSeries,i1_series_Civ2(ifield),j1_series_Civ2(ifield),MaxIndex_j);
[1180]630            ImageName_A_Civ2=fullfile(RootPath_A,SubDir_A,RootFile);
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;
[1200]637            CheckDuplicate_1to2A=true;
[1169]638        else
[1178]639            [par_civ2.ImageA,VideoObject_A] = read_image(ImageName_A_Civ2,FileType_A,VideoObject_A,FrameIndex_A_2);
[1200]640            CheckDuplicate_1to2A=false;
[1169]641        end
[1178]642        if CheckRelabel
[1188]643            [RootFile,FrameIndex_B_2]=index2filename(XmlData.FileSeries,i2_civ2,j2_civ2,MaxIndex_j);
[1180]644            ImageName_B_Civ2=fullfile(RootPath_B,SubDir_B,RootFile);
645        else
646            ImageName_B_Civ2=fullfile_uvmat(RootPath_B,SubDir_B,RootFile_B,FileExt_B,NomType_B,i2_civ2,[],j2_civ2);
647            FrameIndex_B_2=FrameIndex_B_Civ2(ifield);
[1178]648        end
649        if strcmp(ImageName_B_Civ2,ImageName_B) && isequal(FrameIndex_B_2,FrameIndex_B)
[1169]650            par_civ2.ImageB=par_civ1.ImageB;
[1200]651            CheckDuplicate_1to2B=true;
[1169]652        else
[1178]653            [par_civ2.ImageB,VideoObject_B] = read_image(ImageName_B_Civ2,FileType_B,VideoObject_B,FrameIndex_B_2);
[1200]654             CheckDuplicate_1to2B=false;
[1169]655        end
656        %  [FileInfo_A,VideoObject_A]=get_file_info(ImageName_A_Civ2);
657        npy_ima=size(par_civ2.ImageA,1);
658        npx_ima=size(par_civ2.ImageA,2);
659        if par_civ2.CheckGrid &&~isempty(par_civ2.Grid) % case of input grid
660            GridData=nc2struct(Param.ActionInput.Civ2.Grid);
661            par_civ2.Grid=GridData.Grid;
662            par_civ2.CorrBoxSize=GridData.CorrBox;
663        else% automatic grid
664            nbinterv_x=floor((npx_ima-1)/par_civ2.Dx);
665            gridlength_x=nbinterv_x*par_civ2.Dx;
666            minix=ceil((npx_ima-gridlength_x)/2);
667            nbinterv_y=floor((npy_ima-1)/par_civ2.Dy);
668            gridlength_y=nbinterv_y*par_civ2.Dy;
669            miniy=ceil((npy_ima-gridlength_y)/2);
670            [GridX,GridY]=meshgrid(minix:par_civ2.Dx:npx_ima-1,miniy:par_civ2.Dy:npy_ima-1);
671            par_civ2.Grid=zeros(numel(GridX),2);
672            par_civ2.Grid(:,1)=reshape(GridX,[],1);
673            par_civ2.Grid(:,2)=reshape(GridY,[],1);% increases with array index
674        end
675       
[1200]676        %% case of background image to subtract, if images civ2 different from civ1
677        if (~CheckDuplicate_1to2A || ~CheckDuplicate_1to2B) && par_civ2.CheckBackground &&~isempty(par_civ2.Background)
678            [RootPath_background,SubDir_background,RootFile_background,~,~,~,~,Ext_background]=fileparts_uvmat(Param.ActionInput.Civ2.Background);
679            j1=1;
680            if ~isempty(j1_series_Civ2)
681                j1=j1_series_Civ2(ifield);
682            end
683            if ~isempty(i2_series_Civ2)% case of volume,backgrounds act on different j levels
684                backgroundname=fullfile_uvmat(RootPath_background,SubDir_background,RootFile_background,Ext_background,'_1',j1);
[1201]685            elseif isfield(par_civ2,'NbSlice')
[1200]686                i1_background=mod(i1-1,par_civ2.NbSlice)+1;
687                backgroundname=fullfile_uvmat(RootPath_background,SubDir_background,RootFile_background,Ext_background,'_1',i1_background);
688                if strcmp(Param.ActionInput.PairIndices.ListPairMode,'series(Di)')% case of volume, background index refers to j index
689                    par_civ2.NbSlice_j=par_civ2.NbSlice;
690                end
691            else
692                backgroundname=Param.ActionInput.Civ2.Background;
693            end
694            if strcmp(backgroundoldname,backgroundname)% background exist, not already read in civ2
695                par_civ2.Background=background; %use background already opened
696            else
697                if ~isempty(regexp(backgroundname,'(^http://)|(^https://)', 'once'))|| exist(backgroundname,'file')
698                    try
699                        par_civ2.Background=uint16(imread(backgroundname));%update the background, an store it for future use
700                    catch ME
701                        if ~isempty(ME.message)
702                            errormsg=['error reading input image: ' ME.message];
703                            disp_uvmat('ERROR',errormsg,checkrun)
704                            return
705                        end
706                    end
707                else
708                    par_civ2.Background=[];
709                end
710                background=par_civ2.Background;
711                backgroundoldname=backgroundname;
712            end
713            if ~CheckDuplicate_1to2A
714            par_civ2.ImageA=uint16(par_civ2.ImageA)-par_civ2.Background;
715            end
716            if ~CheckDuplicate_1to2B
717            par_civ2.ImageB=uint16(par_civ2.ImageB)-par_civ2.Background;
718            end
719        end
720   
[1195]721        %% case of image luminosity rescaling
[1200]722        if  par_civ2.CheckRescale && ~isempty(par_civ2.Maxtanh)
723            if ~CheckDuplicate_1to2A %if the image A is different from civ1
724                par_civ2.ImageA =par_civ2.Maxtanh*tanh(double(par_civ2.ImageA)/par_civ2.Maxtanh);
725            end
726            if ~CheckDuplicate_1to2B % if the image B is different from civ1
727                par_civ2.ImageB=par_civ2.Maxtanh*tanh(double(par_civ2.ImageB)/par_civ2.Maxtanh);
728            end
[1175]729        end
730       
[856]731        % get the guess from patch1 or patch2 (case 'CheckCiv3')
[1169]732        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
733            [DataIn,~,~,errormsg]=nc2struct(filecell{1,ifield});
734            if ~isempty(errormsg)
735                disp(errormsg)
736                return
737            end
738            SubRange= DataIn.Civ2_SubRange;
739            NbCentres=DataIn.Civ2_NbCentres;
740            Coord_tps=DataIn.Civ2_Coord_tps;
741            U_tps=DataIn.Civ2_U_tps;
742            V_tps=DataIn.Civ2_V_tps;
743            Civ1_Dt=DataIn.Civ2_Dt;
[1163]744            Data=[];%reinitialise the result structure Data
745            Data.ListGlobalAttribute={'Conventions','Program','CivStage'};
746            Data.Conventions='uvmat/civdata';% states the conventions used for the description of field variables and attributes
747            Data.Program='civ_series';
[859]748            Data.ListVarName={};
749            Data.VarDimName={};
[1163]750        else % get the guess from patch1
[1195]751            %             SubRange= Data.Civ_SubRange;
752            %             NbCentres=Data.Civ_NbCentres;
753            %             Coord_tps=Data.Civ_Coord_tps;
754            %             U_tps=Data.Civ_U_tps;
755            %             V_tps=Data.Civ_V_tps;
[1163]756            Civ1_Dt=Data.Civ1_Dt;
[851]757        end
[1195]758        Data.CivStage=4;
[1169]759        Shiftx=zeros(size(par_civ2.Grid,1),1);% initialise the shift expected from civ1 data
[856]760        Shifty=zeros(size(par_civ2.Grid,1),1);
761        nbval=zeros(size(par_civ2.Grid,1),1);% nbre of interpolated values at each grid point (from the different patch subdomains)
[862]762        if par_civ2.CheckDeformation
763            DUDX=zeros(size(par_civ2.Grid,1),1);
764            DUDY=zeros(size(par_civ2.Grid,1),1);
765            DVDX=zeros(size(par_civ2.Grid,1),1);
766            DVDY=zeros(size(par_civ2.Grid,1),1);
767        end
[856]768        NbSubDomain=size(SubRange,3);
[761]769        for isub=1:NbSubDomain% for each sub-domain of Patch1
[851]770            nbvec_sub=NbCentres(isub);% nbre of Civ vectors in the subdomain
771            ind_sel=find(par_civ2.Grid(:,1)>=SubRange(1,1,isub) & par_civ2.Grid(:,1)<=SubRange(1,2,isub) &...
[862]772                par_civ2.Grid(:,2)>=SubRange(2,1,isub) & par_civ2.Grid(:,2)<=SubRange(2,2,isub));% grid points in the subdomain
[859]773            if ~isempty(ind_sel)
[862]774                epoints = par_civ2.Grid(ind_sel,:);% coordinates of interpolation sites (measurement grids)
[859]775                ctrs=Coord_tps(1:nbvec_sub,:,isub) ;%(=initial points) ctrs
[862]776                EM = tps_eval(epoints,ctrs);% thin plate spline (tps) coefficient
[1163]777                CentreX=(SubRange(1,1,isub)+SubRange(1,2,isub))/2; %x posiion of the subdomain center
778                CentreY=(SubRange(2,1,isub)+SubRange(2,2,isub))/2; %y posiion of the subdomain center
779                xwidth=(SubRange(1,2,isub)-SubRange(1,1,isub))/pi;
780                ywidth=(SubRange(2,2,isub)-SubRange(2,1,isub))/pi;
781                x_dist=(epoints(:,1)-CentreX)/xwidth;
782                y_dist=(epoints(:,2)-CentreY)/ywidth;
783                weight=cos(x_dist).*cos(y_dist);%weighting fct =1 at the rectangle center and 0 at edge
784                nbval(ind_sel)=nbval(ind_sel)+weight;% records the number of values for each interpolation point (in case of subdomain overlap)
785                Shiftx(ind_sel)=Shiftx(ind_sel)+weight.*(EM*U_tps(1:nbvec_sub+3,isub));%velocity shift estimated by tps from civ1
786                Shifty(ind_sel)=Shifty(ind_sel)+weight.*(EM*V_tps(1:nbvec_sub+3,isub));
[859]787                if par_civ2.CheckDeformation
788                    [EMDX,EMDY] = tps_eval_dxy(epoints,ctrs);%2D matrix of distances between extrapolation points epoints and spline centres (=site points) ctrs
[1163]789                    DUDX(ind_sel)=DUDX(ind_sel)+weight.*(EMDX*U_tps(1:nbvec_sub+3,isub));
790                    DUDY(ind_sel)=DUDY(ind_sel)+weight.*(EMDY*U_tps(1:nbvec_sub+3,isub));
791                    DVDX(ind_sel)=DVDX(ind_sel)+weight.*(EMDX*V_tps(1:nbvec_sub+3,isub));
792                    DVDY(ind_sel)=DVDY(ind_sel)+weight.*(EMDY*V_tps(1:nbvec_sub+3,isub));
[859]793                end
[597]794            end
795        end
[1163]796        Shiftx(nbval>0)=Shiftx(nbval>0)./nbval(nbval>0);
797        Shifty(nbval>0)=Shifty(nbval>0)./nbval(nbval>0);
[1169]798       
[1163]799        % introduce mask
800        if par_civ2.CheckMask && ~isempty(par_civ2.Mask)
801            [RootPath_mask,SubDir_mask,RootFile_mask,~,~,~,~,Ext_mask]=fileparts_uvmat(Param.ActionInput.Civ2.Mask);
[1183]802            if ~isempty(i2_series_Civ2) && ~isequal(i1_series_Civ2,i2_series_Civ2) % we do PIV among indices i,  at given indices j (volume scan), mask depends on position j
[1195]803                if Check_j_Civ2
804                    j1=j1_series_Civ2(ifield);
805                else
806                    j1=[];
[1163]807                end
808                maskname=fullfile_uvmat(RootPath_mask,SubDir_mask,RootFile_mask,Ext_mask,'_1',j1);
[1201]809            elseif isfield(par_civ2,'NbSlice')
[1163]810                i1=i1_series_Civ2(ifield);
[1010]811                i1_mask=mod(i1-1,par_civ2.NbSlice)+1;
812                maskname=fullfile_uvmat(RootPath_mask,SubDir_mask,RootFile_mask,Ext_mask,'_1',i1_mask);
[1164]813                if strcmp(Param.ActionInput.PairIndices.ListPairMode,'series(Di)')% case of volume, mask index refers to j index
814                    par_civ2.NbSlice_j=par_civ2.NbSlice;
815                end
[1010]816            else
817                maskname=Param.ActionInput.Civ2.Mask;
818            end
819            if strcmp(maskoldname,maskname)% mask exist, not already read in civ1
[856]820                par_civ2.Mask=mask; %use mask already opened
821            else
[1187]822                if exist(maskname,'file')|| ~isempty(regexp(maskname,'(^http://)|(^https://)', 'once'))
[1163]823                    try
824                        par_civ2.Mask=imread(maskname);%update the mask, 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
[1010]831                    end
[1062]832                else
833                    par_civ2.Mask=[];
[1187]834                    disp_uvmat('ERROR',[maskname ' does not exist'],checkrun);
835                    return
[1062]836                end
[856]837                mask=par_civ2.Mask;
[1010]838                maskoldname=maskname;
[856]839            end
[598]840        end
[1169]841       
[1200]842        %% get civ2 correlation parameters
[1169]843        if strcmp(Param.ActionInput.ListCompareMode,'displacement')
844            Civ1_Dt=1;
845            Civ2_Dt=1;
846        else
847            Civ2_Dt=Time(i2_civ2+1,j2_civ2+1)-Time(i1_civ2+1,j1_civ2+1);
[856]848        end
[1163]849        par_civ2.SearchBoxShift=zeros(size(par_civ2.Grid));
[1169]850        par_civ2.SearchBoxShift(:,1)=(Civ2_Dt/Civ1_Dt)*Shiftx;%rescale the shift in case of Dt different for Civ1 and Civ2
[1163]851        par_civ2.SearchBoxShift(:,2)=(Civ2_Dt/Civ1_Dt)*Shifty;
[1169]852       
[598]853        if par_civ2.CheckDeformation
[1163]854            par_civ2.DUDX(nbval>0)=DUDX(nbval>0)./nbval(nbval>0);
855            par_civ2.DUDY(nbval>0)=DUDY(nbval>0)./nbval(nbval>0);
856            par_civ2.DVDX(nbval>0)=DVDX(nbval>0)./nbval(nbval>0);
857            par_civ2.DVDY(nbval>0)=DVDY(nbval>0)./nbval(nbval>0);
[598]858        end
[1169]859       
[1201]860        % calculate velocity data
861        if strcmp(Param.RunMode,'cluster')
862            [Civ_X,Civ_Y,Civ_U,Civ_V,Civ_C,Civ_FF,~, errormsg] = civ (par_civ2);% single processor used in cluster
863        else
864            [Civ_X,Civ_Y,Civ_U,Civ_V,Civ_C,Civ_FF,errormsg] = parciv (par_civ2);%use parfor loop
865        end
[1195]866        Civ_X_shifted=Civ_X-0.5+Civ_U/2;% get the exact positions
867        Civ_Y_shifted=Civ_Y-0.5+Civ_V/2;
[598]868        list_param=(fieldnames(Param.ActionInput.Civ2))';
[859]869        list_param(strcmp('TestCiv2',list_param))=[];% remove the parameter TestCiv2 from the list
[606]870        Civ2_param=regexprep(list_param,'^.+','Civ2_$0');% insert 'Civ2_' before  each string in list_param
871        Civ2_param=[{'Civ2_ImageA','Civ2_ImageB','Civ2_Time','Civ2_Dt'} Civ2_param]; %insert the names of the two input images
[856]872        %indicate the values of all the global attributes in the output data
873        if exist('ImageName_A','var')
[859]874            Data.Civ2_ImageA=ImageName_A;
875            Data.Civ2_ImageB=ImageName_B;
[997]876            if strcmp(Param.ActionInput.ListCompareMode,'displacement')
[950]877                Data.Civ2_Time=Time(i2_civ2+1,j2_civ2+1);% the Time is the Time of the secodn image
878                Data.Civ2_Dt=1;% Time interval is 1, to yield displacement instead of velocity=displacement/Dt at reading
[997]879            else
880                Data.Civ2_Time=(Time(i2_civ2+1,j2_civ2+1)+Time(i1_civ2+1,j1_civ2+1))/2;
881                Data.Civ2_Dt=Civ2_Dt;
882            end
[856]883        end
[598]884        for ilist=1:length(list_param)
[606]885            Data.(Civ2_param{4+ilist})=Param.ActionInput.Civ2.(list_param{ilist});
[598]886        end
[606]887        Data.ListGlobalAttribute=[Data.ListGlobalAttribute Civ2_param];
[1169]888       
[896]889        disp('civ2 performed')
[1131]890        time_civ2=toc(tstart_civ2);
[1195]891    elseif Param.ActionInput.CheckFix2 && isfield (Param.ActionInput,'Fix2') % we start there, using existing Civ2 data
[860]892        if exist('ncfile','var')
893            CivFile=ncfile;
[1169]894            [Data,~,~,errormsg]=nc2struct(CivFile);%read civ1 and detect_false1 data in the existing netcdf file
[860]895            if ~isempty(errormsg)
896                disp_uvmat('ERROR',errormsg,checkrun)
897                return
[997]898            end
[860]899        end
[598]900    end
[1169]901   
[598]902    %% Fix2
[1195]903    if (Param.ActionInput.CheckFix2 && isfield (Param.ActionInput,'Fix2'))||(Param.ActionInput.CheckPatch2 && isfield (Param.ActionInput,'Patch2'))%% if Fix2 computation is requested
[1143]904        disp('detect_false2 started')
[856]905        list_param=fieldnames(Param.ActionInput.Fix2)';
[852]906        Fix2_param=regexprep(list_param,'^.+','Fix2_$0');% insert 'Fix1_' before  each string in ListFixParam
[859]907        %indicate the values of all the global attributes in the output data
[852]908        for ilist=1:length(list_param)
909            Data.(Fix2_param{ilist})=Param.ActionInput.Fix2.(list_param{ilist});
[598]910        end
[1163]911        Data.ListGlobalAttribute=[Data.ListGlobalAttribute Fix2_param];
[1195]912        Civ_FF=uint8(detect_false(Param.ActionInput.Fix2,Civ_C,Civ_U,Civ_V,Civ_FF));
[1144]913        Data.CivStage=Data.CivStage+1;
[598]914    end
915   
916    %% Patch2
[1169]917    if Param.ActionInput.CheckPatch2 && isfield (Param.ActionInput,'Patch2')% if Patch2 computation is requested
918       
[896]919        disp('patch2 started')
[1131]920        tstart_patch2=tic;
[859]921        list_param=fieldnames(Param.ActionInput.Patch2)';
922        list_param(strcmp('TestPatch2',list_param))=[];% remove the parameter TestCiv1 from the list
[852]923        Patch2_param=regexprep(list_param,'^.+','Patch2_$0');% insert 'Fix1_' before  each string in ListFixParam
[859]924        %indicate the values of all the global attributes in the output data
[852]925        for ilist=1:length(list_param)
926            Data.(Patch2_param{ilist})=Param.ActionInput.Patch2.(list_param{ilist});
927        end
928        Data.ListGlobalAttribute=[Data.ListGlobalAttribute Patch2_param];
929       
[1195]930        if isempty(Civ_FF)
931            ind_good=1:numel(Civ_X);
[597]932        else
[1195]933            ind_good=find(Civ_FF==0);
[597]934        end
[1169]935        if isempty(ind_good)
936            disp_uvmat('ERROR','all vectors of civ2 are bad, check input parameters' ,checkrun)
937            return
938        end
939       
[1195]940        [Civ_SubRange,Civ_NbCentres,Civ_Coord_tps,Civ_U_tps,Civ_V_tps,~,Ures,Vres,~,FFres]=...
941            filter_tps([Civ_X_shifted(ind_good) Civ_Y_shifted(ind_good)],Civ_U(ind_good),Civ_V(ind_good),[],Data.Patch2_SubDomainSize,Data.Patch2_FieldSmooth,Data.Patch2_MaxDiff);
942        Civ_U_smooth=Civ_U;% keep the false vectors unchanged
943        Civ_V_smooth=Civ_V;
944        Civ_U_smooth(ind_good)=Ures;
945        Civ_V_smooth(ind_good)=Vres;
946        Civ_FF(ind_good)=uint8(4*FFres);
[598]947        Data.CivStage=Data.CivStage+1;
[1131]948        time_patch2=toc(tstart_patch2);
[896]949        disp('patch2 performed')
[597]950    end
[598]951   
[1169]952    %% write result in a netcdf file
[1195]953   
954    Data.ListVarName={'X','Y','U','V','C','FF'};%  cell array containing the names of the fields to record
955    Data.VarDimName={'nb_vec','nb_vec','nb_vec','nb_vec','nb_vec','nb_vec'};
956    Data.VarAttribute{1}.Role='coord_x';
957    Data.VarAttribute{2}.Role='coord_y';
958    Data.VarAttribute{3}.Role='vector_x';
959    Data.VarAttribute{3}.scale_factor=1/inv_scale_factor;
960    Data.VarAttribute{4}.Role='vector_y';
961    Data.VarAttribute{4}.scale_factor=1/inv_scale_factor;
962    Data.VarAttribute{5}.Role='ancillary';
[1196]963    Data.VarAttribute{5}.scale_factor=1/100;%scla factor for correlation
[1195]964    Data.VarAttribute{6}.Role='errorflag';
965    Data.X=uint16(Civ_X);
966    Data.Y=uint16(Civ_Y);
967    Data.U=int16(inv_scale_factor*Civ_U);
968    Data.V=int16(inv_scale_factor*Civ_V);
[1196]969    Data.C=uint8(100*Civ_C);
[1195]970    Data.FF=uint8(Civ_FF);
[1196]971    % add smoothed field if ptch is done
[1195]972    if (Param.ActionInput.CheckPatch1 && ~Param.ActionInput.CheckCiv2) ||Param.ActionInput.CheckPatch2
973        nbvar=6;
974        Data.ListVarName=[Data.ListVarName {'U_smooth','V_smooth'}];
975        Data.VarDimName=[Data.VarDimName {'nb_vec','nb_vec'}];
976        Data.VarAttribute{nbvar+1}.Role='vector_x';
977        Data.VarAttribute{nbvar+1}.scale_factor=1/inv_scale_factor;
978        Data.VarAttribute{nbvar+2}.Role='vector_y';
979        Data.VarAttribute{nbvar+2}.scale_factor=1/inv_scale_factor;
980        Data.U_smooth=int16(inv_scale_factor*Civ_U_smooth);
981        Data.V_smooth=int16(inv_scale_factor*Civ_V_smooth);
982    end
[1169]983    errormsg=struct2nc(ncfile_out,Data);
984    if isempty(errormsg)
985        disp([ncfile_out ' written'])
986    else
987        disp(errormsg)
[598]988    end
[1169]989    time_total=toc(tstart);
990    disp(['ellapsed time ' num2str(time_total/60,2) ' minutes'])
991    disp(['time civ1 ' num2str(time_civ1,2) ' s'])
992    disp(['time patch1 ' num2str(time_patch1,2) ' s'])
993    disp(['time civ2 ' num2str(time_civ2,2) ' s'])
994    disp(['time patch2 ' num2str(time_patch2,2) ' s'])
995    if exist('time_input','var')
996        disp(['time image reading ' num2str(time_input,2) ' s'])
[1199]997        disp(['time other ' num2str((time_total-time_civ1-time_patch1-time_civ2-time_patch2),2) ' s'])
[1169]998    end
[597]999end
1000
[1169]1001
1002
[598]1003%------------------------------------------------------------------------
1004% --- determine the list of index pairs of processing file
1005function [i1_series,i2_series,j1_series,j2_series,check_bounds,NomTypeNc]=...
[635]1006    find_pair_indices(str_civ,i_series,j_series,MinIndex_i,MaxIndex_i,MinIndex_j,MaxIndex_j)
[598]1007%------------------------------------------------------------------------
[610]1008i1_series=i_series;% set of first image indexes
[598]1009i2_series=i_series;
[819]1010j1_series=j_series;%ones(size(i_series));% set of first image numbers
1011j2_series=j_series;%ones(size(i_series));
[610]1012r=regexp(str_civ,'^\D(?<ind>[i|j])=( -| )(?<num1>\d+)\|(?<num2>\d+)','names');
[598]1013if ~isempty(r)
1014    mode=['D' r.ind];
1015    ind1=str2num(r.num1);
1016    ind2=str2num(r.num2);
1017else
[668]1018    mode='j1-j2';
[598]1019    r=regexp(str_civ,'^j= (?<num1>[a-z])-(?<num2>[a-z])','names');
1020    if ~isempty(r)
1021        NomTypeNc='_1ab';
1022    else
1023        r=regexp(str_civ,'^j= (?<num1>[A-Z])-(?<num2>[A-Z])','names');
1024        if ~isempty(r)
1025            NomTypeNc='_1AB';
[597]1026        else
[598]1027            r=regexp(str_civ,'^j= (?<num1>\d+)-(?<num2>\d+)','names');
1028            if ~isempty(r)
1029                NomTypeNc='_1_1-2';
[859]1030            end
[597]1031        end
1032    end
[598]1033    if isempty(r)
1034        display('wrong pair mode input option')
1035    else
[859]1036        ind1=stra2num(r.num1);
1037        ind2=stra2num(r.num2);
[598]1038    end
[597]1039end
[819]1040switch mode
1041    case 'Di'
1042        i1_series=i_series-ind1;% set of first image numbers
1043        i2_series=i_series+ind2;
1044        check_bounds=i1_series<MinIndex_i | i2_series>MaxIndex_i;
1045        if isempty(j_series)
1046            NomTypeNc='_1-2';
1047        else
1048            j1_series=j_series;
1049            j2_series=j_series;
1050            NomTypeNc='_1-2_1';
1051        end
1052    case 'Dj'
1053        j1_series=j_series-ind1;
1054        j2_series=j_series+ind2;
1055        check_bounds=j1_series<MinIndex_j | j2_series>MaxIndex_j;
1056        NomTypeNc='_1_1-2';
1057    otherwise %bursts
1058        i1_series=i_series(1,:);% do not sweep the j index
1059        i2_series=i_series(1,:);
1060        j1_series=ind1*ones(1,size(i_series,2));% j index is fixed by pair choice
1061        j2_series=ind2*ones(1,size(i_series,2));
1062        check_bounds=zeros(size(i1_series));% no limitations due to min-max indices
[598]1063end
[597]1064
[1169]1065%------------------------------------------------------------------------
1066% --- set the flag for false vectors
[1163]1067function FF=detect_false(Param,C,U,V,FFIn)
1068FF=FFIn;%default, good vectors
1069% FF=1, for correlation max at edge, not set in this function
1070% FF=2, for too small correlation
1071% FF=3, for velocity outside bounds
1072% FF=4 for exclusion by difference with the smoothed field, set by call to function filter_tps
[598]1073
[1163]1074if isfield (Param,'MinCorr')
1075     FF(C<Param.MinCorr & FFIn==0)=2;
1076end
1077if (isfield(Param,'MinVel')&&~isempty(Param.MinVel))||(isfield (Param,'MaxVel')&&~isempty(Param.MaxVel))
1078    Umod= U.*U+V.*V;
1079    if isfield (Param,'MinVel')&&~isempty(Param.MinVel)
1080        U2Min=Param.MinVel*Param.MinVel;
1081        FF(Umod<U2Min & FFIn==0)=3;
1082    end
1083    if isfield (Param,'MaxVel')&&~isempty(Param.MaxVel)
1084         U2Max=Param.MaxVel*Param.MaxVel;
1085        FF(Umod>U2Max & FFIn==0)=3;
1086    end
1087end
[598]1088
[1195]1089% % --- compress the data using integers instead of (single precision)floating reals
1090% function DataOut=compress_data(DataIn,inv_scale_factor)
1091% DataOut=DataIn; %default
1092% DataOut.Conventions= 'uvmat/civdata/compress';
1093% ListVarName=DataIn.ListVarName;
1094% for ilist=1:numel(ListVarName)
1095%     switch ListVarName{ilist}
1096%         case {'Civ1_C','Civ2_C'}
1097%             DataOut.(ListVarName{ilist})=uint8(inv_scale_factor*DataIn.(ListVarName{ilist}));
1098%             DataOut.VarAttribute{ilist}.scale_factor=1/inv_scale_factor;
1099%         case {'Civ1_U','Civ1_V','Civ2_U','Civ2_V','Civ1_U_smooth','Civ1_V_smooth','Civ2_U_smooth','Civ2_V_smooth'}
1100%             DataOut.(ListVarName{ilist})=int16(inv_scale_factor*DataIn.(ListVarName{ilist}));
1101%             DataOut.VarAttribute{ilist}.scale_factor=1/inv_scale_factor;
1102%         case 'Civ1_X'
1103%             DataOut.Civ1_X=uint16(DataIn.Civ1_X);
1104%         case 'Civ1_Y'
1105%             DataOut.Civ1_Y=uint16(DataIn.Civ1_Y);
1106%        case 'Civ2_X'
1107%             DataOut.Civ2_X=uint16(DataIn.Civ2_X);
1108%         case 'Civ2_Y'
1109%             DataOut.Civ2_Y=uint16(DataIn.Civ2_Y);
1110%     end
1111% end
Note: See TracBrowser for help on using the repository browser.