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

Last change on this file since 1165 was 1165, checked in by sommeria, 2 months ago

various bugs repaired

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