source: trunk/src/series/civ_input.m @ 1010

Last change on this file since 1010 was 1010, checked in by sommeria, 7 years ago

multimask introduced

File size: 99.4 KB
Line 
1%'civ_input': function associated with the GUI 'civ_input.fig' to set the input parameters for civ_series
2%------------------------------------------------------------------------
3% function ParamOut = civ_input(Param)
4%
5% OUPUT:
6% ParamOut: Matlab structure containing the parameters set by the GUI civ_input
7%
8% INPUT:
9% Param: Matlab structure containing the input parameters set by the GUI
10
11%=======================================================================
12% Copyright 2008-2017, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
13%   http://www.legi.grenoble-inp.fr
14%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
15%
16%     This file is part of the toolbox UVMAT.
17%
18%     UVMAT is free software; you can redistribute it and/or modify
19%     it under the terms of the GNU General Public License as published
20%     by the Free Software Foundation; either version 2 of the license,
21%     or (at your option) any later version.
22%
23%     UVMAT is distributed in the hope that it will be useful,
24%     but WITHOUT ANY WARRANTY; without even the implied warranty of
25%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26%     GNU General Public License (see LICENSE.txt) for more details.
27%=======================================================================
28
29function varargout = civ_input(varargin)
30
31gui_Singleton = 1;
32gui_State = struct('gui_Name',       mfilename, ...
33    'gui_Singleton',  gui_Singleton, ...
34    'gui_OpeningFcn', @civ_input_OpeningFcn, ...
35    'gui_OutputFcn',  @civ_input_OutputFcn, ...
36    'gui_LayoutFcn',  [] , ...
37    'gui_Callback',   []);
38
39if nargin && ischar(varargin{1}) && ~isempty(regexp(varargin{1},'_Callback$','once'))
40        gui_State.gui_Callback = str2func(varargin{1});
41end
42
43if nargout
44    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
45else
46    gui_mainfcn(gui_State, varargin{:});
47end
48
49% End initialization code - DO NOT EDIT
50
51%------------------------------------------------------------------------
52% --- Executes just before civ_input is made visible.
53function civ_input_OpeningFcn(hObject, eventdata, handles, Param)
54%------------------------------------------------------------------------
55% This function has no output args, see OutputFcn.
56
57%% General settings
58handles.output = Param;
59guidata(hObject, handles); % Update handles structure
60set(hObject,'WindowButtonDownFcn',{'mouse_down'}) % allows mouse action with right button (zoom for uicontrol display)
61set(hObject,'WindowKeyPressFcn',{@keyboard_callback,handles})%set keyboard action function
62%set(hObject,'KeyPressFcn',{@KeyPressFcn,handles})%set keyboard action function
63set(handles.ref_i,'KeyPressFcn',{@ref_i_KeyPressFcn,handles})%set keyboard action function
64set(handles.ref_j,'KeyPressFcn',{@ref_i_KeyPressFcn,handles})%set keyboard action function
65%set(hObject,'WindowKeyPressFcn',{'keyboard_callback',handles})%set keyboard action function
66hseries=findobj(allchild(0),'Tag','series');% find the parent GUI 'series'
67hhseries=guidata(hseries); %handles of the elements in 'series'
68SeriesData=get(hseries,'UserData');% info stored in the GUI series
69
70%% set visibility options depending on the calling function (Param.Action.ActionName):
71if strcmp(Param.Action.ActionName,'civ_series')||strcmp(Param.Action.ActionName,'stereo_civ')
72     set(handles.num_MaxDiff,'Visible','on')
73    set(handles.num_MaxVel,'Visible','on')
74    set(handles.title_MaxVel,'Visible','on')
75    set(handles.title_MaxDiff,'Visible','on')
76    set(handles.num_Nx,'Visible','off')
77    set(handles.num_Ny,'Visible','off')
78    set(handles.title_Nx,'Visible','off')
79    set(handles.title_Ny,'Visible','off')
80    set(handles.num_CorrSmooth,'Style','popupmenu')
81    set(handles.num_CorrSmooth,'Value',1)
82    set(handles.num_CorrSmooth,'String',{'1';'2'})
83    set(handles.CheckThreshold,'Visible','on')
84    set(handles.CheckDeformation,'Value',0)% desactivate (work in progress)
85end
86switch Param.Action.ActionName
87    case 'stereo_civ'
88        set(handles.ListCompareMode,'Visible','off')
89        set(handles.PairIndices,'Visible','off')
90    case 'civ_series'
91        set(handles.ListCompareMode,'Visible','on')
92        set(handles.PairIndices,'Visible','on')
93end
94
95%% input file info
96NomTypeInput=Param.InputTable{1,4};
97FileType='image';%fdefault
98FileInfo=[];
99if isfield(SeriesData,'FileType')&&isfield(SeriesData,'FileInfo')...
100        &&numel(SeriesData.FileType)>=1&&numel(SeriesData.FileInfo)>=1
101    FileType=SeriesData.FileType{1};%type of the first input file series
102    FileInfo=SeriesData.FileInfo{1};% info on the first input file series
103else
104    set(hhseries.REFRESH,'BackgroundColor',[1 0 1])% indicate that the file input in series needs to be refreshed
105end
106
107%% case of netcdf file as input, read the processing stage and look for corresponding images
108ind_opening=0;%default
109NomTypeNc='';
110NomTypeImaA=NomTypeInput;
111iview_image=1;%line # for the input images
112switch FileType
113    case {'image','image_DaVis','multimage','video','mmreader','cine_phantom','netcdf'}
114%         NomTypeImaA=NomTypeInput;
115%         iview_image=1;%line # for the input images
116    case 'civdata'
117        if ~strcmp(Param.Action.ActionName,'civ_series')
118            msgbox_uvmat('ERROR','bad input data file: open an image or a nc file from civ_series')
119            return
120        end
121        NomTypeNc=NomTypeInput;
122        ind_opening=FileInfo.CivStage;
123        if isempty(regexp(NomTypeInput,'[ab|AB|-]', 'once'))
124            set(handles.ListCompareMode,'Value',2) %mode displacement advised if the nomencalture does not involve index pairs
125        else
126            set(handles.ListCompareMode,'Value',1)
127        end
128        [Data,tild,tild,errormsg]=nc2struct(FileInfo.FileName,[]);
129        if ~isempty(errormsg)
130            msgbox_uvmat('ERROR',['error in netcdf input file: ' errormsg])
131            return
132        end
133        if isfield(Data,'.Civ1_ImageA')
134        [PathCiv1_ImageA,Civ1_ImageA,FileExtA]=fileparts(Data.Civ1_ImageA);%look for the source image A
135        [PathCiv1_ImageB,Civ1_ImageB,FileExtA]=fileparts(Data.Civ1_ImageB);%look for the source image B
136        end
137        if isfield(Data,'Civ2_ImageA')
138            [PathCiv2_ImageA,Civ2_ImageA,FileExtA]=fileparts(Data.Civ2_ImageA);
139            [PathCiv2_ImageB,Civ2_ImageB,FileExtA]=fileparts(Data.Civ2_ImageB);
140        end
141        if size(Param.InputTable,1)==1
142             if isfield(Data,'.Civ1_ImageA')
143            series('display_file_name',hhseries,Data.Civ1_ImageA,'append');%append the image series to the input list
144                    [RootPath,SubDir,RootFile,i1,i2,j1,j2,FileExt,NomTypeImaA]=fileparts_uvmat(Data.Civ1_ImageA);
145        [RootPath,SubDir,RootFile,i1,i2,j1,j2,FileExt,NomTypeImaB]=fileparts_uvmat(Data.Civ1_ImageB);
146             else
147                 series('display_file_name',hhseries,Data.Civ2_ImageA,'append');%append the image series to the input list
148                         [RootPath,SubDir,RootFile,i1,i2,j1,j2,FileExt,NomTypeImaA]=fileparts_uvmat(Data.Civ2_ImageA);
149        [RootPath,SubDir,RootFile,i1,i2,j1,j2,FileExt,NomTypeImaB]=fileparts_uvmat(Data.Civ2_ImageB);
150             end
151        end
152
153        iview_image=2;%line # for the input images
154    case 'civxdata'% case of  civx data,
155        msgbox_uvmat('ERROR','old civX convention, use the GUI civ')
156        return
157    otherwise
158        msgbox_uvmat('ERROR','civ_series needs images, scalar fields in netcdf format, or civ data as input')
159        return
160end
161
162%% reinitialise menus
163set(handles.ListPairMode,'Value',1)
164set(handles.ListPairMode,'String',{''})
165set(handles.ListPairCiv1,'Value',1)
166set(handles.ListPairCiv1,'String',{''})
167set(handles.ListPairCiv2,'Value',1)
168set(handles.ListPairCiv2,'String',{''})
169       
170%% prepare the GUI with input parameters
171%
172set(handles.ref_i,'String',num2str(Param.IndexRange.first_i))
173if isfield(Param.IndexRange,'first_j')
174    set(handles.ref_j,'String',num2str(Param.IndexRange.first_j))
175end
176set(handles.ConfigSource,'String','\default')
177
178%%  set the menus of image pairs and default selection for civ_input   %%%%%%%%%%%%%%%%%%%
179
180%% display the min and max indices for the whole file series
181if isempty(Param.IndexRange.MaxIndex_i)|| isempty(Param.IndexRange.MinIndex_i)
182    msgbox_uvmat('ERROR','REFRESH the input files in the GUI series')
183     return
184end
185MaxIndex_i=Param.IndexRange.MaxIndex_i(iview_image);
186MinIndex_i=Param.IndexRange.MinIndex_i(iview_image);
187MaxIndex_j=1;%default
188MinIndex_j=1;
189if isfield(Param.IndexRange,'MaxIndex_j')&&isfield(Param.IndexRange,'MinIndex_j')...
190        && numel(Param.IndexRange.MaxIndex_j')>=iview_image &&numel(Param.IndexRange.MinIndex_j')>=iview_image
191    MaxIndex_j=Param.IndexRange.MaxIndex_j(iview_image);
192    MinIndex_j=Param.IndexRange.MinIndex_j(iview_image);
193end
194%update the bounds if possible
195if isfield(SeriesData,'i1_series')&&numel(SeriesData.i1_series)>=iview_image
196    if size(SeriesData.i1_series{iview_image},2)==2 && min(min(SeriesData.i1_series{iview_image}(:,1,:)))==0
197        MinIndex_j=1;% index j set to 1 by default
198        MaxIndex_j=1;
199        MinIndex_i=find(SeriesData.i1_series{iview_image}(1,2,:), 1 )-1;% min ref index i detected in the series (corresponding to the first non-zero value of i1_series, except for zero index)
200        MaxIndex_i=find(SeriesData.i1_series{iview_image}(1,2,:),1,'last' )-1;%max ref index i detected in the series (corresponding to the last non-zero value of i1_series)
201    else
202        ref_i=squeeze(max(SeriesData.i1_series{iview_image}(1,:,:),[],2));% select ref_j index for each ref_i
203        ref_j=squeeze(max(SeriesData.j1_series{iview_image}(1,:,:),[],3));% select ref_i index for each ref_j
204        MinIndex_i=min(find(ref_i))-1;
205        MaxIndex_i=max(find(ref_i))-1;
206        MaxIndex_j=max(find(ref_j))-1;
207        MinIndex_j=min(find(ref_j))-1;
208    end
209end
210
211
212%%  transfer the time from the GUI series, or use file index by default
213time=[];
214TimeUnit='frame'; %default
215CoordUnit='';%default
216pxcm_search=1;
217if isfield(SeriesData,'Time') &&numel(SeriesData.Time')>=1 && ~isempty(SeriesData.Time{1})
218    time=SeriesData.Time{1};
219end
220if isfield(Param.IndexRange,'TimeUnit')&&~isempty(Param.IndexRange.TimeUnit)
221    TimeUnit=Param.IndexRange.TimeUnit;
222end
223% if isfield(SeriesData,'TimeSource')
224%     set(handles.TimeSource,'String',SeriesData.TimeSource)
225% end 
226if isfield(SeriesData,'GeometryCalib')
227    tsai=SeriesData.GeometryCalib;
228    if isfield(tsai,'fx_fy')
229        pxcm_search=max(tsai.fx_fy(1),tsai.fx_fy(2));%pixels:cm estimated for the search range
230    end
231    if isfield(tsai,'CoordUnit')
232        CoordUnit=tsai.CoordUnit;
233    end
234end
235
236%% timing display
237%show the reference image edit box if relevant (not needed for movies or in the absence of time information
238if numel(time)>=2 % if there are at least two time values to define dt
239    if size(time,1)<MaxIndex_i;
240        msgbox_uvmat('WARNING','maximum i index restricted by the timing of the xml file');
241    elseif size(time,2)<MaxIndex_j
242        msgbox_uvmat('WARNING','maximum j index restricted by the timing of the xml file');
243    end
244    MaxIndex_i=min(size(time,1),MaxIndex_i);%possibly adjust the max index according to time data
245    MaxIndex_j=min(size(time,2),MaxIndex_j);
246    set(handles.TimeSource,'String',Param.IndexRange.TimeSource);
247else
248    set(handles.TimeSource,'String',''); %xml file not used for timing
249    TimeUnit='frame';
250    time=ones(MaxIndex_j-MinIndex_j+1,1)*(MinIndex_i:MaxIndex_i);
251    time=time+0.001*(MinIndex_j:MaxIndex_j)'*ones(1,MaxIndex_i-MinIndex_i+1);
252end
253CivInputData.Time=time;
254CivInputData.NomTypeIma=NomTypeImaA;
255set(handles.civ_input,'UserData',CivInputData)
256set(handles.dt_unit,'String',['dt in m' TimeUnit]);%display dt in unit 10-3 of the time (e.g ms)
257set(handles.TimeUnit,'String',TimeUnit);
258%set(handles.CoordUnit,'String',CoordUnit)
259set(handles.SearchRange,'UserData', pxcm_search);
260
261
262%% set the civ_input options, depending on the input file content if a nc file has been opened
263ListOptions={'CheckCiv1', 'CheckFix1' 'CheckPatch1', 'CheckCiv2', 'CheckFix2', 'CheckPatch2'};
264checkbox=zeros(size(ListOptions));%default
265checkrefresh=0;
266if ind_opening==0  %case of image opening, start with Civ1
267    for index=1:numel(ListOptions)
268        checkbox(index)=get(handles.(ListOptions{index}),'Value');
269    end
270    index_max=find(checkbox, 1, 'last' );
271    if isempty(index_max),index_max=1;end
272    for index=1:index_max
273        set(handles.(ListOptions{index}),'Value',1)% select all operations starting from CIV1
274    end
275else  %case of netcdf file opening, start with the stage read in the file if the input file is being refreshed
276    if isequal(get(hhseries.REFRESH,'BackgroundColor'),[1 1 0]) &&...
277            ~(isfield(Param,'ActionInput') && isfield(Param.ActionInput,'ConfigSource'))
278            for index = 1:min(ind_opening,5)
279                set(handles.(ListOptions{index}),'value',0)
280                fill_civ_input(Data,handles); %fill civ_input with the parameters retrieved from an input Civ file
281            end
282            set(handles.ConfigSource,'String',FileInfo.FileName);
283            set(handles.(ListOptions{min(ind_opening+1,6)}),'value',1)
284            for index = ind_opening+2:6
285                set(handles.(ListOptions{index}),'value',0)
286            end
287            checkrefresh=1;
288    end
289    if ind_opening>=3
290        set(handles.CheckCiv3,'Visible','on')% make visible the switch 'iterate/repet' for Civ2.
291    else
292        set(handles.CheckCiv3,'Visible','off')
293    end
294end
295
296%% introduce the stored Civ parameters  if available (from previous input or ImportConfig in series)
297if ~checkrefresh && isfield(Param,'ActionInput')&& strcmp(Param.ActionInput.Program,Param.Action.ActionName)% the program fits with the stored data
298    fill_GUI(Param.ActionInput,hObject);%fill the GUI with the parameters retrieved from the input Param
299    hcheckgrid=findobj(handles.civ_input,'Tag','CheckGrid');
300    for ilist=1:numel(hcheckgrid)
301        if get(hcheckgrid(ilist),'Value')% if a grid is used, do not show Dx and Dy for an automatic grid
302            hparent=get(hcheckgrid(ilist),'parent');%handles of the parent panel
303            hchildren=get(hparent,'children');
304            handle_dx=findobj(hchildren,'tag','num_Dx');
305            handle_dy=findobj(hchildren,'tag','num_Dy');
306            handle_title_dx=findobj(hchildren,'tag','title_Dx');
307            handle_title_dy=findobj(hchildren,'tag','title_Dy');
308            set(handle_dx,'Visible','off');
309            set(handle_dy,'Visible','off');
310            set(handle_title_dy,'Visible','off');
311            set(handle_title_dx,'Visible','off');
312        end
313    end
314    if isfield(Param.ActionInput,'Civ2')
315       CheckDeformation_Callback(hObject, eventdata, handles)
316    end
317end
318
319%% set the menu and default choice of civ pairs
320if ~isfield(Param.IndexRange,'first_j')||isequal(MaxIndex_j,MinIndex_j)% no possibility of j pairs
321    set(handles.ListPairMode,'Value',1)
322    set(handles.ListPairMode,'String',{'series(Di)'})
323elseif  MaxIndex_i==1 && MaxIndex_j>1% simple series in j
324    set(handles.ListPairMode,'String',{'pair j1-j2';'series(Dj)'})
325    if  MaxIndex_j <= 10
326        set(handles.ListPairMode,'Value',1)% advice 'pair j1-j2' except in MaxIndex_j is large
327    end
328else
329    set(handles.ListPairMode,'String',{'pair j1-j2';'series(Dj)';'series(Di)'})%multiple choice
330    if strcmp(NomTypeNc,'_1-2_1')
331        set(handles.ListPairMode,'Value',3)% advise 'series(Di)'
332    elseif  MaxIndex_j <= 10
333        set(handles.ListPairMode,'Value',1)% advice 'pair j1-j2' except in MaxIndex_j is large
334    end
335end
336
337%% indicate the min and max indices i and j on the GUI
338set(handles.MinIndex_i,'String',num2str(MinIndex_i))
339set(handles.MaxIndex_i,'String',num2str(MaxIndex_i))
340set(handles.MinIndex_j,'String',num2str(MinIndex_j))
341set(handles.MaxIndex_j,'String',num2str(MaxIndex_j))
342
343%% set the reference indices from the input file indices
344if ~(isfield(Param,'ActionInput') && isfield(Param.ActionInput,'ConfigSource'))
345update_CivOptions(handles,ind_opening)% fill the menu of possible pairs
346end
347
348%% list the possible index pairs, depending on the option set in ListPairMode
349ListPairMode_Callback([], [], handles)
350%ListPairCiv1_Callback(hObject, eventdata, handles)
351
352%% set the GUI to modal: wait for OK to close
353set(handles.civ_input,'WindowStyle','modal')% Make the GUI modal
354drawnow
355uiwait(handles.civ_input);% wait for OK action to end the function
356
357
358%------------------------------------------------------------------------
359% --- Outputs from this function are returned to the command line.
360function varargout = civ_input_OutputFcn(hObject, eventdata, handles)
361%------------------------------------------------------------------------
362% Get default command line output from handles structure
363varargout{1}=[];% default output when civ_input is canceled (no 'OK')
364if ~isempty(handles)
365    varargout{1} = handles.output;
366    delete(handles.civ_input)
367end
368
369% --- Executes when user attempts to close get_field.
370function civ_input_CloseRequestFcn(hObject, eventdata, handles)
371if isequal(get(handles.get_field, 'waitstatus'), 'waiting')
372    % The GUI is still in UIWAIT, us UIRESUME
373    uiresume(handles.civ_input);
374else
375    % The GUI is no longer waiting, just close it
376    delete(handles.civ_input);
377end
378
379%------------------------------------------------------------------------
380% --- Executes on button press in SetDefaultParam.
381%------------------------------------------------------------------------
382function SetDefaultParam_Callback(hObject, eventdata, handles)
383
384Param.ConfigSource='\default';
385
386%% Civ1 parameters
387%Param.CheckCiv1=1;
388Param.Civ1.CorrBoxSize=[25 25];
389Param.Civ1.SearchBoxSize=[55 55];
390Param.Civ1.SearchBoxShift=[0 0];
391Param.Civ1.CorrSmooth=1;
392Param.Civ1.Dx=20;
393Param.Civ1.Dy=20;
394Param.Civ1.CheckGrid=0;
395Param.Civ1.CheckMask=0;
396Param.Civ1.Mask='';
397Param.Civ1.CheckThreshold=0;
398Param.Civ1.TestCiv1=0;
399
400%% Fix1 parameters
401%Param.CheckFix1=1;
402Param.Fix1.CheckFmin2=1;
403Param.Fix1.CheckF3=1;
404Param.Fix1.MinCorr=0.2000;
405
406%% Patch1 parameters
407%Param.CheckPatch1=1;
408Param.Patch1.FieldSmooth=10;
409Param.Patch1.MaxDiff=1.5000;
410Param.Patch1.SubDomainSize=1000;
411Param.Patch1.TestPatch1=0;
412
413%% Civ2 parameters
414%Param.CheckCiv2=1;
415Param.Civ2.CorrBoxSize=[21 21];
416Param.Civ2.SearchBoxSize=[27 27];
417Param.Civ2.CorrSmooth=1;
418Param.Civ2.Dx=10;
419Param.Civ2.Dy=10;
420Param.Civ2.CheckGrid=0;
421Param.Civ2.CheckMask=0;
422Param.Civ2.Mask='';
423Param.Civ2.CheckThreshold=0;
424Param.Civ2.TestCiv2=0;
425
426%% Fix2 parameters
427%Param.CheckFix2=1;
428Param.Fix2.CheckFmin2=1;
429Param.Fix2.CheckF4=1;
430Param.Fix2.CheckF3=1;
431Param.Fix2.MinCorr=0.2000;
432
433%% Patch2 parameters
434%Param.CheckPatch2=1;
435Param.Patch2.FieldSmooth=2;
436Param.Patch2.MaxDiff=1.5000;
437Param.Patch2.SubDomainSize=1000;
438Param.Patch2.TestPatch2=0;
439
440fill_GUI(Param,handles.civ_input)% fill the elements of the GUI series with the input parameters
441update_CivOptions(handles,0)
442
443% -----------------------------------------------------------------------
444% -----------------------------------------------------------------------
445% --- Open the help html file
446function MenuHelp_Callback(hObject, eventdata, handles)
447% -----------------------------------------------------------------------
448web('http://servforge.legi.grenoble-inp.fr/projects/soft-uvmat/wiki/UvmatHelp#Civ')
449
450%------------------------------------------------------------------------
451% --- Executes on carriage return on the subdir checkciv1 edit window
452function Civ1_ImageB_Callback(hObject, eventdata, handles)
453%------------------------------------------------------------------------
454SubDir=get(handles.Civ1_ImageB,'String');
455menu_str=get(handles.ListSubdirCiv1,'String');% read the list of subdirectories for update
456ichoice=find(strcmp(SubDir,menu_str),1);
457if isempty(ichoice)
458    ilist=numel(menu_str); %select 'new...' in the menu
459else
460    ilist=ichoice;
461end
462set(handles.ListSubdirCiv1,'Value',ilist)% select the selected subdir in the menu
463if get(handles.CheckCiv1,'Value')% if Civ1 is performed
464    set(handles.Civ2_ImageA,'String',SubDir);% set by default civ2 directory the same as civ1
465%     set(handles.ListSubdirCiv2,'Value',ilist)
466else % if Civ1 data already exist
467    errormsg=find_netcpair_civ(handles,1); %update the list of available index pairs in the new directory
468    if ~isempty(errormsg)
469    msgbox_uvmat('ERROR',errormsg)
470    end
471end
472
473%------------------------------------------------------------------------
474% --- Executes on carriage return on the SubDir checkciv1 edit window
475function Civ2_ImageA_Callback(hObject, eventdata, handles)
476%------------------------------------------------------------------------
477SubDir=get(handles.Civ1_ImageB,'String');
478menu_str=get(handles.ListSubdirCiv2,'String');% read the list of subdirectories for update
479ichoice=find(strcmp(SubDir,menu_str),1);
480if isempty(ichoice)
481    ilist=numel(menu_str); %select 'new...' in the menu
482else
483    ilist=ichoice;
484end
485set(handles.ListSubdirCiv2,'Value',ilist)% select the selected subdir in the menu
486%update the list of available pairs from netcdf files in the new directory
487if ~get(handles.CheckCiv2,'Value') && ~get(handles.CheckCiv1,'Value') && ~get(handles.CheckFix1,'Value') && ~get(handles.CheckPatch1,'Value')
488    errormsg=find_netcpair_civ(handles,2);
489        if ~isempty(errormsg)
490    msgbox_uvmat('ERROR',errormsg)
491    end
492end
493
494%------------------------------------------------------------------------
495% --- Executes on button press in CheckCiv1.
496function CheckCiv1_Callback(hObject, eventdata, handles)
497%------------------------------------------------------------------------
498update_CivOptions(handles,0)
499
500%------------------------------------------------------------------------
501% --- Executes on button press in CheckFix1.
502function CheckFix1_Callback(hObject, eventdata, handles)
503%------------------------------------------------------------------------
504update_CivOptions(handles,0)
505
506%------------------------------------------------------------------------
507% --- Executes on button press in CheckPatch1.
508function CheckPatch1_Callback(hObject, eventdata, handles)
509%------------------------------------------------------------------------
510update_CivOptions(handles,0)
511
512%------------------------------------------------------------------------
513% --- Executes on button press in CheckCiv2.
514function CheckCiv2_Callback(hObject, eventdata, handles)
515%------------------------------------------------------------------------
516update_CivOptions(handles,0)
517
518%------------------------------------------------------------------------
519% --- Executes on button press in CheckFix2.
520function CheckFix2_Callback(hObject, eventdata, handles)
521%------------------------------------------------------------------------
522update_CivOptions(handles,0)
523
524%------------------------------------------------------------------------
525% --- Executes on button press in CheckPatch2.
526function CheckPatch2_Callback(hObject, eventdata, handles)
527%------------------------------------------------------------------------
528update_CivOptions(handles,0)
529
530%------------------------------------------------------------------------
531% --- activated by any checkbox controling the selection of Civ1,Fix1,Patch1,Civ2,Fix2,Patch2
532function update_CivOptions(handles,opening)
533%------------------------------------------------------------------------
534if opening>0
535    set(handles.CheckCiv2,'UserData',opening)% store the info on the current status of the civ processing
536end
537checkbox=zeros(1,6);
538checkbox(1)=get(handles.CheckCiv1,'Value');
539checkbox(2)=get(handles.CheckFix1,'Value');
540checkbox(3)=get(handles.CheckPatch1,'Value');
541checkbox(4)=get(handles.CheckCiv2,'Value');
542checkbox(5)=get(handles.CheckFix2,'Value');
543checkbox(6)=get(handles.CheckPatch2,'Value');
544if opening==0
545    errormsg=find_netcpair_civ(handles,1); % select the available netcdf files
546    if ~isempty(errormsg)
547        msgbox_uvmat('ERROR',errormsg)
548    end
549end
550if max(checkbox(4:6))>0% case of civ2 pair choice needed
551    set(handles.TitlePairCiv2,'Visible','on')
552    set(handles.ListPairCiv2,'Visible','on')
553    if ~opening
554        errormsg=find_netcpair_civ(handles,2); % select the available netcdf files
555        if ~isempty(errormsg)
556            msgbox_uvmat('ERROR',errormsg)
557        end
558    end
559else
560    set(handles.ListPairCiv2,'Visible','off')
561end
562hseries=findobj(allchild(0),'Tag','series');% find the parent GUI 'series'
563hhseries=guidata(hseries); %handles of the elements in 'series'
564InputTable=get(hhseries.InputTable,'Data');
565if size(InputTable,1)>=1 && strcmp(InputTable{1,5},'.nc') && max(checkbox(1:3))==0 %&& get(handles.CheckCiv2,'UserData')==6,% no operation asked before Civ2 and input file ready for civ3
566    set(handles.CheckCiv3,'Visible','on')
567else
568    set(handles.CheckCiv3,'Visible','off')
569end
570
571%% set the visibility of the different panels
572options={'Civ1','Fix1','Patch1','Civ2','Fix2','Patch2'};
573for ilist=1:length(options)
574    if checkbox(ilist)
575        set(handles.(options{ilist}),'Visible','on')
576    else
577        set(handles.(options{ilist}),'Visible','off')
578    end
579end
580
581%------------------------------------------------------------------------
582% --- Executes on button press in OK: processing on local computer
583function OK_Callback(hObject, eventdata, handles)
584%------------------------------------------------------------------------
585
586ActionInput=read_GUI(handles.civ_input);% read the infos on the GUI civ_input
587
588%% correct input inconsistencies
589if isfield(ActionInput,'Civ1')
590    checkeven=(mod(ActionInput.Civ1.CorrBoxSize,2)==0);
591    ActionInput.Civ1.CorrBoxSize(checkeven)=ActionInput.Civ1.CorrBoxSize(checkeven)+1;% set correlation box sizes to odd values
592    ActionInput.Civ1.SearchBoxSize=max(ActionInput.Civ1.SearchBoxSize,ActionInput.Civ1.CorrBoxSize+8);% insure that the search box size is large enough
593    checkeven=(mod(ActionInput.Civ1.SearchBoxSize,2)==0);
594    ActionInput.Civ1.SearchBoxSize(checkeven)=ActionInput.Civ1.SearchBoxSize(checkeven)+1;% set search box sizes to odd values
595end
596if isfield(ActionInput,'Civ2')
597    checkeven=(mod(ActionInput.Civ2.CorrBoxSize,2)==0);
598    ActionInput.Civ2.CorrBoxSize(checkeven)=ActionInput.Civ2.CorrBoxSize(checkeven)+1;% set correlation box sizes to odd values
599    ActionInput.Civ2.SearchBoxSize=max(ActionInput.Civ2.SearchBoxSize,ActionInput.Civ2.CorrBoxSize+4);
600    checkeven=(mod(ActionInput.Civ2.SearchBoxSize,2)==0);
601    ActionInput.Civ2.SearchBoxSize(checkeven)=ActionInput.Civ2.SearchBoxSize(checkeven)+1;% set search box sizes to odd values
602end
603
604%% correct mask or grid name for Windows system (replace '\' by '/')
605if isfield(ActionInput,'Civ1')
606    if isfield(ActionInput.Civ1,'Mask')
607        ActionInput.Civ1.Mask=regexprep(ActionInput.Civ1.Mask,'\','/');
608    end
609    if isfield(ActionInput.Civ1,'Grid')
610        ActionInput.Civ1.Grid=regexprep(ActionInput.Civ1.Grid,'\','/');
611    end
612end
613if isfield(ActionInput,'Civ2')
614    if isfield(ActionInput.Civ2,'Mask')
615        ActionInput.Civ2.Mask=regexprep(ActionInput.Civ2.Mask,'\','/');
616    end
617    if isfield(ActionInput.Civ2,'Grid')
618        ActionInput.Civ2.Grid=regexprep(ActionInput.Civ2.Grid,'\','/');
619    end
620end
621
622%% exit the GUI and close it
623handles.output.ActionInput=ActionInput;
624guidata(hObject, handles);% Update handles structure
625uiresume(handles.civ_input);
626
627
628%------------------------------------------------------------------------
629% --- Executes on button press in ListCompareMode.
630function ListCompareMode_Callback(hObject, eventdata, handles)
631%------------------------------------------------------------------------
632ListCompareMode=get(handles.ListCompareMode,'String');
633option=ListCompareMode{get(handles.ListCompareMode,'Value')};
634hseries=findobj(allchild(0),'Tag','series');
635SeriesData=get(hseries,'UserData');
636check_nc=strcmp(SeriesData.FileType{1},'.nc');
637ImageType=SeriesData.FileType(2:end);
638if check_nc
639    ImageType=SeriesData.FileType(2:end);
640else
641    ImageType=SeriesData.FileType;
642end
643hhseries=guidata(hseries);
644InputTable=get(hhseries.InputTable,'Data');
645OriginIndex='off';
646PairIndices='off';
647DoubleInputSeries='off';
648switch option
649    case 'PIV'
650        PairIndices='on';% needs to define index pairs for PIV
651       
652    case 'PIV volume'
653        PairIndices='on';% needs to define index pairs for PIV
654        set(handles.ListPairMode,'Value',1)
655        set(handles.ListPairMode,'String',{'series(Di)'})
656        ListPairMode_Callback(hObject, eventdata, handles)
657    case 'displacement'
658        OriginIndex='on';%define a frame origin for displacement
659%     case 'shift'
660%         if numel(ImageType)==1
661%             fileinput=uigetfile_uvmat('pick a second file series for synchronous shift',InputTable{check_nc+1});
662%             if ~isempty(fileinput)
663%                 series( 'display_file_name',hhseries,fileinput,'append')
664%             end
665%             
666%             
667%         end
668end
669set(handles.num_OriginIndex,'Visible',OriginIndex)
670set(handles.OriginIndex_title,'Visible',OriginIndex)
671set(handles.PairIndices,'Visible',PairIndices)
672ListPairMode_Callback(hObject, eventdata, handles)
673       
674
675
676%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
677% Callbacks in the uipanel Pair Indices
678%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
679%------------------------------------------------------------------------
680% --- Executes on button press in ListPairMode.
681function ListPairMode_Callback(hObject, eventdata, handles)
682%------------------------------------------------------------------------
683compare_list=get(handles.ListCompareMode,'String');
684val=get(handles.ListCompareMode,'Value');
685compare=compare_list{val};
686if strcmp(compare,'displacement')||strcmp(compare,'shift')
687    mode='displacement';
688else
689    mode_list=get(handles.ListPairMode,'String');
690    if ischar(mode_list)
691        mode_list={mode_list};
692    end
693    mode_value=get(handles.ListPairMode,'Value');
694    mode=mode_list{mode_value};
695end
696% displ_num=[];%default
697ref_i=str2double(get(handles.ref_i,'String'));
698% last_i=str2num(get(handles.last_i,'String'));
699CivInputData=get(handles.civ_input,'UserData');
700TimeUnit=get(handles.TimeUnit,'String');
701checkframe=strcmp(TimeUnit,'frame');
702time=CivInputData.Time;
703siztime=size(CivInputData.Time);
704nbfield=siztime(1)-1;
705nbfield2=siztime(2)-1;
706%indchosen=1;  %%first pair selected by default
707%displ_num used to define the indices of the civ_input pairs
708% in mode 'pair j1-j2', j1 and j2 are the file indices, else the indices
709% are relative to the reference indices ref_i and ref_j respectively.
710if isequal(mode,'pair j1-j2')
711    dt=1;
712    displ='';
713    index=0;
714    numlist_a=[];
715    numlist_B=[];
716    %get all the time intervals in bursts
717    displ_dt=1;%default
718    nbfield2=min(nbfield2,10);%limitate the number of pairs to 10x10
719    for numod_a=1:nbfield2-1 %nbfield2 always >=2 for 'pair j1-j2' mode
720        for numod_b=(numod_a+1):nbfield2
721            index=index+1;
722            numlist_a(index)=numod_a;
723            numlist_b(index)=numod_b;
724            if size(time,2)>1 && ~checkframe && size(CivInputData.Time,1)>ref_i && size(CivInputData.Time,2)>numod_b
725                dt(numod_a,numod_b)=CivInputData.Time(ref_i+1,numod_b+1)-CivInputData.Time(ref_i+1,numod_a+1);%first time interval dt
726                displ_dt(index)=dt(numod_a,numod_b);
727            else
728                displ_dt(index)=1;
729            end
730        end
731    end
732    [dtsort,indsort]=sort(displ_dt);
733    if ~isempty(numlist_a)
734        displ_num(1,:)=numlist_a(indsort);
735        displ_num(2,:)=numlist_b(indsort);
736    end
737    displ_num(3,:)=0;
738    displ_num(4,:)=0;
739    enable_j(handles, 'off')
740elseif isequal(mode,'series(Dj)') %| isequal(mode,'st_series(Dj)')
741    index=1:200;
742    displ_num(1,index)=-floor(index/2);
743    displ_num(2,index)=ceil(index/2);
744    displ_num(3:4,index)=zeros(2,200);
745    enable_j(handles, 'on')
746elseif isequal(mode,'series(Di)') %| isequal(mode,'st_series(Di)')
747%     index=1:200;
748%     displ_num(1:2,index)=zeros(2,200);
749%     displ_num(3,index)=-floor(index/2);
750%     displ_num(4,index)=ceil(index/2);
751    enable_i(handles, 'on')
752    if nbfield2 > 1
753        enable_j(handles, 'on')
754    else
755        enable_j(handles, 'off')
756    end
757elseif isequal(mode,'displacement')%the pairs have the same indices
758    displ_num(1,1)=0;
759    displ_num(2,1)=0;
760    displ_num(3,1)=0;
761    displ_num(4,1)=0;
762    if nbfield > 1 || nbfield==0
763        enable_i(handles, 'on')
764    else
765        enable_j(handles, 'off')
766    end
767    if nbfield2 > 1
768        enable_j(handles, 'on')
769    else
770        enable_j(handles, 'off')
771    end
772end
773%set(handles.ListPairCiv1,'UserData',displ_num);
774errormsg=find_netcpair_civ( handles,1);
775    if ~isempty(errormsg)
776    msgbox_uvmat('ERROR',errormsg)
777    end
778% find_netcpair_civ2(handles)
779
780function enable_i(handles, state)
781set(handles.itext,'Visible',state)
782% set(handles.MinIndex_i,'Visible',state)
783% set(handles.last_i,'Visible',state)
784% set(handles.incr_i,'Visible',state)
785set(handles.MaxIndex_i,'Visible',state)
786set(handles.ref_i,'Visible',state)
787
788function enable_j(handles, state)
789set(handles.jtext,'Visible',state)
790% set(handles.MinIndex_j,'Visible',state)
791% set(handles.last_j,'Visible',state)
792% set(handles.incr_j,'Visible',state)
793set(handles.MinIndex_j,'Visible',state)
794set(handles.MaxIndex_j,'Visible',state)
795set(handles.ref_j,'Visible',state)
796%hseries=findobj(allchild(0),'Tag','series');
797%hhseries=guidata(hseries);
798%series('enable_j',hhseries,state); %file input with xml reading  in uvmat, show the image in phys coordinates
799
800
801
802%------------------------------------------------------------------------
803% --- Executes on selection change in ListPairCiv1.
804function ListPairCiv1_Callback(hObject, eventdata, handles)
805%------------------------------------------------------------------------
806%reproduce by default the chosen pair in the checkciv2 menu
807list_pair=get(handles.ListPairCiv1,'String');%get the menu of image pairs
808PairString=list_pair{get(handles.ListPairCiv1,'Value')};
809
810[ind1,ind2]=...
811    find_pair_indices(PairString);
812hseries=findobj(allchild(0),'Tag','series');
813hhseries=guidata(hseries);
814set(hhseries.num_first_j,'String',num2str(ind1));
815set(hhseries.num_last_j,'String',num2str(ind2));
816set(hhseries.num_incr_j,'String',num2str(ind2-ind1));
817set(handles.ListPairCiv2,'Value',get(handles.ListPairCiv1,'Value'))%civ2 selection the same as civ& by default
818
819
820%------------------------------------------------------------------------
821% --- Executes on selection change in ListPairCiv2.
822function ListPairCiv2_Callback(hObject, eventdata, handles)
823%------------------------------------------------------------------------
824
825
826%------------------------------------------------------------------------
827function ref_i_Callback(hObject, eventdata, handles)
828%------------------------------------------------------------------------
829mode_list=get(handles.ListPairMode,'String');
830mode_value=get(handles.ListPairMode,'Value');
831mode=mode_list{mode_value};
832errormsg=find_netcpair_civ(handles,1);% update the menu of pairs depending on the available netcdf files
833if isequal(mode,'series(Di)') || ...% we do patch2 only
834        (get(handles.CheckCiv2,'Value')==0 && get(handles.CheckCiv1,'Value')==0 && get(handles.CheckFix1,'Value')==0 && get(handles.CheckPatch1,'Value')==0)
835    errormsg=find_netcpair_civ( handles,2);
836end
837if isempty(errormsg)
838    set(handles.ref_i,'BackgroundColor',[1 1 1])
839    set(handles.ref_j,'BackgroundColor',[1 1 1])
840else
841    msgbox_uvmat('ERROR',errormsg)
842end
843
844function ref_i_KeyPressFcn(hObject, eventdata, handles)
845set(hObject,'BackgroundColor',[1 0 1])
846       
847% %------------------------------------------------------------------------
848% function ref_j_Callback(hObject, eventdata, handles)
849% %------------------------------------------------------------------------
850% mode_list=get(handles.ListPairMode,'String');
851% mode_value=get(handles.ListPairMode,'Value');
852% mode=mode_list{mode_value};
853% errormsg='';
854% if isequal(get(handles.CheckCiv1,'Value'),0)|| isequal(mode,'series(Dj)')
855%     errormsg=find_netcpair_civ(handles,1);% update the menu of pairs depending on the available netcdf files
856% end
857% if isequal(mode,'series(Dj)') || ...
858%         (get(handles.CheckCiv2,'Value')==0 && get(handles.CheckCiv1,'Value')==0 && get(handles.CheckFix1,'Value')==0 && get(handles.CheckPatch1,'Value')==0)
859%     errormsg=find_netcpair_civ(handles,2);
860% end
861% if ~isempty(errormsg)
862%     msgbox_uvmat('ERROR',errormsg)
863% end
864%
865% function ref_j_KeyPressFcn(hObject, eventdata, handles)
866% set(handles.ref_j,'BackgroundColor',[1 0 1])
867%------------------------------------------------------------------------
868% determine the menu for checkciv1 pairs depending on existing netcdf file at the middle of
869% the field series set by MinIndex_i, incr, last_i
870% index=1: look for pairs for civ1
871% index=2: look for pairs for civ2
872function errormsg=find_netcpair_civ(handles,index)
873%------------------------------------------------------------------------
874set(gcf,'Pointer','watch')% set the mouse pointer to 'watch' (clock)
875
876%% initialisation
877errormsg='';
878CivInputData=get(handles.civ_input,'UserData');
879%browse=get(handles.RootPath,'UserData');
880compare_list=get(handles.ListCompareMode,'String');
881val=get(handles.ListCompareMode,'Value');
882compare=compare_list{val};
883if ~strcmp(compare,'displacement')%||strcmp(compare,'shift')
884 
885    mode_list=get(handles.ListPairMode,'String');
886    mode_value=get(handles.ListPairMode,'Value');
887    if isempty(mode_list)
888        return
889    end
890    mode=mode_list{mode_value};
891end
892nom_type_ima=CivInputData.NomTypeIma;
893menu_pair=get(handles.ListPairCiv1,'String');%previous menu of ListPairCiv1
894PairCiv1Init=menu_pair{get(handles.ListPairCiv1,'Value')};%previous choice of pair
895menu_pair=get(handles.ListPairCiv2,'String');%previous menu of ListPairCiv1
896PairCiv2Init=menu_pair{get(handles.ListPairCiv2,'Value')};%previous choice of pair
897
898%% determine nom_type_nc, nomenclature type of the .nc files:
899%[nom_type_nc]=nomtype2pair(nom_type_ima,mode);
900
901%% reads .nc subdirectoy and image numbers from the interface
902%SubDirImages=get(handles.Civ1_ImageA,'String');
903%TODO: determine
904%subdir_civ1=[SubDirImages get(handles.Civ1_ImageB,'String')];%subdirectory subdir_civ1 for the netcdf data
905%subdir_civ2=[SubDirImages get(handles.Civ2_ImageA,'String')];%subdirectory subdir_civ2 for the netcdf data
906ref_i=str2double(get(handles.ref_i,'String'));
907ref_j=[];
908if isequal(mode,'pair j1-j2')%|isequal(mode,'st_pair j1-j2')
909    ref_j=0;
910elseif strcmp(get(handles.ref_j,'Visible'),'on')
911    ref_j=str2double(get(handles.ref_j,'String'));
912end
913if isempty(ref_j)
914    ref_j=1;
915end
916CivInputData=get(handles.civ_input,'UserData');
917TimeUnit=get(handles.TimeUnit,'String');
918Time=CivInputData.Time;
919checkframe=strcmp(TimeUnit,'frame');
920
921%% case with no Civ1 operation, netcdf files need to exist for reading
922displ_pair={''};
923nbpair=200;%default
924select=ones(size(1:nbpair));%flag for displayed pairs =1 for display
925%nbpair=200; %default
926
927%% determine the menu display in .ListPairCiv1
928switch mode
929    case 'series(Di)'
930        for ipair=1:nbpair
931            if select(ipair)
932                displ_pair{ipair}=['Di= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2))];
933                displ_pair_dt{ipair}=displ_pair{ipair};
934                if ~checkframe
935                    if size(Time,1)>=ref_i+1+ceil(ipair/2) && size(Time,2)>=ref_j+1&& ref_i-floor(ipair/2)>=0 && ref_j>=0
936                        dt=Time(ref_i+1+ceil(ipair/2),ref_j+1)-Time(ref_i+1-floor(ipair/2),ref_j+1);%Time interval dtref_j+1
937                        displ_pair_dt{ipair}=[displ_pair_dt{ipair} ' :dt= ' num2str(dt*1000)];
938                    end
939                else
940                    dt=ipair/1000;
941                    displ_pair_dt{ipair}=[displ_pair_dt{ipair} ' :dt= ' num2str(ipair)];
942                end
943            else
944                displ_pair{ipair}='...';
945                displ_pair_dt{ipair}='...'; %pair not displayed in the menu
946            end
947        end
948    case 'series(Dj)'
949        for ipair=1:nbpair
950            if select(ipair)
951                displ_pair{ipair}=['Dj= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2))];
952                displ_pair_dt{ipair}=displ_pair{ipair};
953                if ~checkframe
954                    if size(Time,2)>=ref_j+1+ceil(ipair/2) && size(Time,1)>=ref_i+1 && ref_j-floor(ipair/2)>=0 && ref_i>=0
955                        dt=Time(ref_i+1,ref_j+1+ceil(ipair/2))-Time(ref_i+1,ref_j+1-floor(ipair/2));%Time interval dtref_j+1
956                        displ_pair_dt{ipair}=[displ_pair_dt{ipair} ' :dt= ' num2str(dt*1000)];
957                    end
958                else
959                    dt=ipair/1000;
960                    displ_pair_dt{ipair}=[displ_pair_dt{ipair} ' :dt= ' num2str(dt*1000)];
961                end
962            else
963                displ_pair{ipair}='...'; %pair not displayed in the menu
964                displ_pair_dt{ipair}='...'; %pair not displayed in the menu
965            end
966        end
967    case 'pair j1-j2'%case of pairs
968%         MinIndex_j=CivInputData.MinIndex_j;
969%         MaxIndex_j=min(CivInputData.MaxIndex_j,10);%limitate the number of pairs to 10x10
970        MinIndex_j=str2num(get(handles.MinIndex_j,'String'));
971        MaxIndex_j=str2num(get(handles.MaxIndex_j,'String'));
972        index_pair=0;
973        %get all the Time intervals in bursts
974        displ_pair_dt='';
975        for numod_a=MinIndex_j:MaxIndex_j-1 %nbfield2 always >=2 for 'pair j1-j2' mode
976            for numod_b=(numod_a+1):MaxIndex_j
977                index_pair=index_pair+1;
978                displ_pair{index_pair}=['j= ' num2stra(numod_a,nom_type_ima) '-' num2stra(numod_b,nom_type_ima)];
979                displ_pair_dt{index_pair}=displ_pair{index_pair};
980                dt(index_pair)=numod_b-numod_a;%default dt
981                if size(Time,1)>ref_i && size(Time,2)>numod_b  % && ~checkframe
982                    dt(index_pair)=Time(ref_i+1,numod_b+1)-Time(ref_i+1,numod_a+1);% Time interval dt
983                    displ_pair_dt{index_pair}=[displ_pair_dt{index_pair} ' :dt= ' num2str(dt(index_pair)*1000)];
984                end
985            end
986           
987        end
988        if index_pair ~=0
989        [tild,indsort]=sort(dt);
990        displ_pair=displ_pair(indsort);
991        displ_pair_dt=displ_pair_dt(indsort);
992        end
993    case 'displacement'
994        displ_pair={'Di=Dj=0'};
995        displ_pair_dt={'Di=Dj=0'};
996end
997if index==1
998    set(handles.ListPairCiv1,'String',displ_pair_dt');
999end
1000
1001%% determine the default selection in the pair menu for Civ1
1002%ichoice=find(select,1);% index of first selected pair
1003%if (isempty(ichoice) || ichoice < 1); ichoice=1; end;
1004end_pair=regexp(PairCiv1Init,' :dt=');
1005if ~isempty(end_pair)
1006    PairCiv1Init=PairCiv1Init(1:end_pair-1);
1007end
1008ichoice=find(strcmp(PairCiv1Init,displ_pair'),1);
1009if ~isempty(ichoice)
1010    set(handles.ListPairCiv1,'Value',ichoice);% first valid pair proposed by default in the menu
1011else
1012   set(handles.ListPairCiv1,'Value',1)
1013end
1014% if initial>nbpair || (numel(select)>=initial && ~isequal(select(initial),1))
1015%     set(handles.ListPairCiv1,'Value',ichoice);% first valid pair proposed by default in the menu
1016% end
1017
1018%% determine the default selection in the pair menu for Civ2
1019if strcmp(get(handles.ListPairCiv2,'Visible'),'on')
1020    end_pair=regexp(PairCiv2Init,' :dt=');
1021    if ~isempty(end_pair)
1022        PairCiv2Init=PairCiv2Init(1:end_pair-1);
1023    end
1024    ichoice=find(strcmp(PairCiv2Init,displ_pair'),1);
1025    if ~isempty(ichoice)
1026        set(handles.ListPairCiv2,'Value',ichoice);% first valid pair proposed by default in the menu
1027    else
1028        set(handles.ListPairCiv2,'Value',1)
1029    end
1030else
1031    set(handles.ListPairCiv2,'Value',get(handles.ListPairCiv1,'Value'))% initiate the choice of Civ2 as a reproduction of if civ1
1032end
1033set(handles.ListPairCiv2,'String',displ_pair_dt');
1034set(gcf,'Pointer','arrow')% Indicate that the process is finished
1035
1036
1037%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1038% Callbacks in the uipanel Reference Indices
1039%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1040%------------------------------------------------------------------------
1041function MinIndex_i_Callback(hObject, eventdata, handles)
1042%------------------------------------------------------------------------
1043first_i=str2double(get(handles.MinIndex_i,'String'));
1044set(handles.ref_i,'String', num2str(first_i))% reference index for pair dt = first index
1045ref_i_Callback(hObject, eventdata, handles)%refresh dispaly of dt for pairs (in case of non constant dt)
1046
1047% %------------------------------------------------------------------------
1048% function MinIndex_j_Callback(hObject, eventdata, handles)
1049% %------------------------------------------------------------------------
1050% first_j=str2num(get(handles.MinIndex_j,'String'));
1051% set(handles.ref_j,'String', num2str(first_j))% reference index for pair dt = first index
1052% ref_j_Callback(hObject, eventdata, handles)%refresh dispaly of dt for pairs (in case of non constant dt)
1053
1054%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1055% Callbacks in the uipanel Civ1
1056%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1057%------------------------------------------------------------------------
1058% --- Executes on button press in SearchRange: determine the search range num_SearchBoxSize_1,num_SearchBoxSize_2
1059function SearchRange_Callback(hObject, eventdata, handles)
1060%------------------------------------------------------------------------
1061%determine pair numbers
1062if strcmp(get(handles.num_UMin,'Visible'),'off')
1063    set(handles.u_title,'Visible','on')
1064    set(handles.v_title,'Visible','on')
1065    set(handles.num_UMin,'Visible','on')
1066    set(handles.num_UMax,'Visible','on')
1067    set(handles.num_VMin,'Visible','on')
1068    set(handles.num_VMax,'Visible','on')
1069    %set(handles.CoordUnit,'Visible','on')
1070    %set(handles.TimeUnit,'Visible','on')
1071    %set(handles.slash_title,'Visible','on')
1072    set(handles.min_title,'Visible','on')
1073    set(handles.max_title,'Visible','on')
1074    set(handles.unit_title,'Visible','on')
1075else
1076    get_search_range(hObject, eventdata, handles)
1077end
1078
1079%------------------------------------------------------------------------
1080% ---  determine the search range num_SearchBoxSize_1,num_SearchBoxSize_2 and shift
1081function get_search_range(hObject, eventdata, handles)
1082%------------------------------------------------------------------------
1083param_civ1=read_GUI(handles.Civ1);
1084umin=param_civ1.UMin;
1085umax=param_civ1.UMax;
1086vmin=param_civ1.VMin;
1087vmax=param_civ1.VMax;
1088%switch min_title and max_title in case of error
1089if umax<=umin
1090    umin_old=umin;
1091    umin=umax;
1092    umax=umin_old;
1093    set(handles.num_UMin,'String', num2str(umin))
1094    set(handles.num_UMax,'String', num2str(umax))
1095end
1096if vmax<=vmin
1097    vmin_old=vmin;
1098    vmin=vmax;
1099    vmax=vmin_old;
1100    set(handles.num_VMin,'String', num2str(vmin))
1101    set(handles.num_VMax,'String', num2str(vmax))
1102end   
1103if ~(isempty(umin)||isempty(umax)||isempty(vmin)||isempty(vmax))
1104%     list_pair=get(handles.ListPairCiv1,'String');%get the menu of image pairs
1105%     index=get(handles.ListPairCiv1,'Value');
1106%     pair_string=list_pair{index};
1107%     time=get(handles.TimeSource,'UserData'); %get the set of times
1108%     pxcm=get(handles.SearchRange,'UserData');
1109%     mode_list=get(handles.ListPairMode,'String');
1110%     mode_value=get(handles.ListPairMode,'Value');
1111%     mode=mode_list{mode_value};     
1112%     if isequal (mode, 'series(Di)' )
1113%         ref_i=str2double(get(handles.ref_i,'String'));
1114%         num1=ref_i-floor(index/2);%  first image numbers
1115%         num2=ref_i+ceil(index/2);
1116%         num_a=1;
1117%         num_b=1;
1118%     elseif isequal (mode, 'series(Dj)')
1119%         num1=1;
1120%         num2=1;
1121%         ref_j=str2double(get(handles.ref_j,'String'));
1122%         num_a=ref_j-floor(index/2);%  first image numbers
1123%         num_b=ref_j+ceil(index/2);
1124%     elseif isequal(mode,'pair j1-j2') %case of bursts (png_old or png_2D)     
1125%         ref_i=str2double(get(handles.ref_i,'String'));
1126%         num1=ref_i;
1127%         num2=ref_i;
1128%                 r=regexp(pair_string,'(?<mode>(Di=)|(Dj=)) -*(?<num1>\d+)\|(?<num2>\d+)','names');
1129%         if isempty(r)
1130%             r=regexp(pair_string,'(?<num1>\d+)(?<mode>-)(?<num2>\d+)','names');
1131%         end 
1132%         num_a=str2num(r.num1);
1133%         num_b=str2num(r.num2);
1134%     end
1135%     dt=time(num2+1,num_b+1)-time(num1+1,num_a+1);
1136%     ibx=str2double(get(handles.num_CorrBoxSize_1,'String'));
1137%     iby=str2double(get(handles.num_CorrBoxSize_2,'String'));
1138%     umin=dt*pxcm*umin;
1139%     umax=dt*pxcm*umax;
1140%     vmin=dt*pxcm*vmin;
1141%     vmax=dt*pxcm*vmax;
1142    shiftx=round((umin+umax)/2);
1143    shifty=round((vmin+vmax)/2);
1144    isx=(umax+2-shiftx)*2+param_civ1.CorrBoxSize(1);
1145    isx=2*ceil(isx/2)+1;
1146    isy=(vmax+2-shifty)*2+param_civ1.CorrBoxSize(2);
1147    isy=2*ceil(isy/2)+1;
1148    set(handles.num_SearchBoxShift_1,'String',num2str(shiftx));
1149    set(handles.num_SearchBoxShift_2,'String',num2str(shifty));
1150    set(handles.num_SearchBoxSize_1,'String',num2str(isx));
1151    set(handles.num_SearchBoxSize_2,'String',num2str(isy));
1152end
1153
1154%------------------------------------------------------------------------
1155% --- Executes on selection in menu CorrSmooth.
1156function num_CorrSmooth_Callback(hObject, eventdata, handles)
1157set(handles.ConfigSource,'String','NEW')
1158set(handles.OK,'BackgroundColor',[1 0 1])
1159%------------------------------------------------------------------------
1160
1161% --- Executes on button press in CheckDeformation.
1162function CheckDeformation_Callback(hObject, eventdata, handles)
1163set(handles.ConfigSource,'String','NEW')
1164set(handles.OK,'BackgroundColor',[1 0 1])
1165handles_CoorSmooth=findobj(get(handles.Civ2,'children'),'Tag','num_CorrSmooth');
1166if get(handles.CheckDeformation,'Value')   
1167    set(handles_CoorSmooth,'Visible','off')
1168else
1169    set(handles_CoorSmooth,'Visible','on')
1170end
1171
1172%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1173% Callbacks in the uipanel Fix1
1174%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1175%------------------------------------------------------------------------
1176% % --- Executes on button press in CheckMask.
1177% function get_mask_fix1_Callback(hObject, eventdata, handles)
1178% %------------------------------------------------------------------------
1179% maskval=get(handles.CheckMask,'Value');
1180% if isequal(maskval,0)
1181%     set(handles.Mask,'String','')
1182% else
1183%     mask_displ='no mask'; %default
1184%     filebase=get(handles.RootPath,'String');
1185%     [nbslice, flag_mask]=get_mask(filebase,handles);
1186%     if isequal(flag_mask,1)
1187%         mask_displ=[num2str(nbslice) 'mask'];
1188%     elseif get(handles.ListCompareMode,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
1189%         filebase_a=get(handles.RootFile_1,'String');
1190%         [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
1191%         if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice)
1192%             mask_displ='no mask';
1193%         end
1194%     end
1195%     if isequal(mask_displ,'no mask')
1196%         [FileName, PathName, filterindex] = uigetfile( ...
1197%             {'*.png', ' (*.png)';
1198%             '*.png',  '.png files '; ...
1199%             '*.*', 'All Files (*.*)'}, ...
1200%             'Pick a mask file *.png',filebase);
1201%         mask_displ=fullfile(PathName,FileName);
1202%         if ~exist(mask_displ,'file')
1203%             mask_displ='no mask';
1204%         end
1205%     end
1206%     if isequal(mask_displ,'no mask')
1207%         set(handles.CheckMask,'Value',0)
1208%         set(handles.CheckMask,'Value',0)
1209%         set(handles.CheckMask,'Value',0)
1210%     else
1211%         %set(handles.CheckMask,'Value',1)
1212%         set(handles.CheckMask,'Value',1)
1213%     end
1214%     set(handles.Mask,'String',mask_displ)
1215%     set(handles.Mask,'String',mask_displ)
1216%     set(handles.Mask,'String',mask_displ)
1217% end
1218
1219%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1220% Callbacks in the uipanel Civ2
1221%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1222%------------------------------------------------------------------------
1223% --- Executes on button press in CheckMask: select box for mask option
1224function get_mask_civ2_Callback(hObject, eventdata, handles)
1225%------------------------------------------------------------------------
1226maskval=get(handles.CheckMask,'Value');
1227if isequal(maskval,0)
1228    set(handles.Mask,'String','')
1229else
1230    mask_displ='no mask'; %default
1231    filebase=get(handles.RootPath,'String');
1232    [nbslice, flag_mask]=get_mask(filebase,handles);
1233    if isequal(flag_mask,1)
1234        mask_displ=[num2str(nbslice) 'mask'];
1235    elseif get(handles.ListCompareMode,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
1236        filebase_a=get(handles.RootFile_1,'String');
1237        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
1238        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice)
1239            mask_displ='no mask';
1240        end
1241    end
1242    if isequal(mask_displ,'no mask')
1243        [FileName, PathName, filterindex] = uigetfile( ...
1244            {'*.png', ' (*.png)';
1245            '*.png',  '.png files '; ...
1246            '*.*', 'All Files (*.*)'}, ...
1247            'Pick a mask file *.png',filebase);
1248        mask_displ=fullfile(PathName,FileName);
1249        if ~exist(mask_displ,'file')
1250            mask_displ='no mask';
1251        end
1252    end
1253    if isequal(mask_displ,'no mask')
1254        set(handles.CheckMask,'Value',0)
1255        set(handles.CheckMask,'Value',0)
1256    else
1257        set(handles.CheckMask,'Value',1)
1258    end
1259    set(handles.Mask,'String',mask_displ)
1260end
1261
1262% %------------------------------------------------------------------------
1263% % --- Executes on button press in CheckMask.
1264% function get_mask_fix2_Callback(hObject, eventdata, handles)
1265% %------------------------------------------------------------------------
1266% maskval=get(handles.CheckMask,'Value');
1267% if isequal(maskval,0)
1268%     set(handles.Mask,'String','')
1269% else
1270%     mask_displ='no mask'; %default
1271%     filebase=get(handles.RootPath,'String');
1272%     [nbslice, flag_mask]=get_mask(filebase,handles);
1273%     if isequal(flag_mask,1)
1274%         mask_displ=[num2str(nbslice) 'mask'];
1275%     elseif get(handles.ListCompareMode,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
1276%         filebase_a=get(handles.RootFile_1,'String');
1277%         [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
1278%         if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice)
1279%             mask_displ='no mask';
1280%         end
1281%     end
1282%     if isequal(mask_displ,'no mask')
1283%         [FileName, PathName, filterindex] = uigetfile( ...
1284%             {'*.png', ' (*.png)';
1285%             '*.png',  '.png files '; ...
1286%             '*.*', 'All Files (*.*)'}, ...
1287%             'Pick a mask file *.png',filebase);
1288%         mask_displ=fullfile(PathName,FileName);
1289%         if ~exist(mask_displ,'file')
1290%             mask_displ='no mask';
1291%         end
1292%     end
1293%     if isequal(mask_displ,'no mask')
1294%         set(handles.CheckMask,'Value',0)
1295%     end
1296%     set(handles.Mask,'String',mask_displ)
1297% end
1298
1299%------------------------------------------------------------------------
1300% --- function called to look for mask files
1301function [nbslice, flag_mask]=get_mask(filebase,handles)
1302%------------------------------------------------------------------------
1303%detect mask files, images with appropriate file base
1304%[filebase '_' xx 'mask'], xx=nbslice
1305%flag_mask=1 indicates detection
1306
1307flag_mask=0;%default
1308nbslice=1;
1309
1310% subdir=get(handles.Civ1_ImageB,'String');
1311[Path,Name]=fileparts(filebase);
1312if ~isdir(Path)
1313    msgbox_uvmat('ERROR','no path for input files')
1314    return
1315end
1316% currentdir=pwd;
1317% cd(Path);%move in the dir of the root name filebase
1318maskfiles=dir(fullfile(Path,[Name '_*mask_*.png']));%look for mask files
1319% cd(currentdir);%come back to the current working directory
1320if ~isempty(maskfiles)
1321    %     msgbox_uvmat('ERROR','no mask available, to create it use Tools/Make mask in the upper menu bar of uvmat')
1322    % else
1323    flag_mask=1;
1324    maskname=maskfiles(1).name;% take the first mask file in the list
1325    [Path2,Name,ext]=fileparts(maskname);
1326    Namedouble=double(Name);
1327    val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
1328    ind_mask=findstr('mask',Name);
1329    i=ind_mask-1;
1330    while val(i)==0 && i>0
1331        i=i-1;
1332    end
1333    nbslice=str2double(Name(i+1:ind_mask-1));
1334    if ~isnan(nbslice) && Name(i)=='_'
1335        flag_mask=1;
1336    else
1337        msgbox_uvmat('ERROR',['bad mask file ' Name ext ' found in ' Path2])
1338        return
1339        nbslice=1;
1340    end
1341end
1342
1343%------------------------------------------------------------------------
1344% --- function called to look for grid files
1345function [nbslice, flag_grid]=get_grid(filebase,handles)
1346%------------------------------------------------------------------------
1347flag_grid=0;%default
1348nbslice=1;
1349[Path,Name]=fileparts(filebase);
1350currentdir=pwd;
1351cd(Path);%move in the dir of the root name filebase
1352gridfiles=dir([Name '_*grid_*.grid']);%look for grid files
1353cd(currentdir);%come back to the current working directory
1354if ~isempty(gridfiles)
1355    flag_grid=1;
1356    gridname=gridfiles(1).name;% take the first grid file in the list
1357    [Path2,Name,ext]=fileparts(gridname);
1358    Namedouble=double(Name);
1359    val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
1360    ind_grid=findstr('grid',Name);
1361    i=ind_grid-1;
1362    while val(i)==0 && i>0
1363        i=i-1;
1364    end
1365    nbslice=str2double(Name(i+1:ind_grid-1));
1366    if ~isnan(nbslice) && Name(i)=='_'
1367        flag_grid=1;
1368    else
1369        msgbox_uvmat('ERROR',['bad grid file ' Name ext ' found in ' Path2])
1370        return
1371        nbslice=1;
1372    end
1373end
1374
1375%------------------------------------------------------------------------
1376% --- transform numbers to letters
1377function str=num2stra(num,nom_type)
1378%------------------------------------------------------------------------
1379if isempty(nom_type)
1380    str='';
1381elseif strcmp(nom_type(end),'a')
1382    str=char(96+num);
1383elseif strcmp(nom_type(end),'A')
1384    str=char(96+num);
1385elseif isempty(nom_type(2:end))%a single index
1386    str='';
1387else
1388    str=num2str(num);
1389end
1390
1391% %------------------------------------------------------------------------
1392% % --- Executes on button press in ListSubdirCiv1.
1393% function ListSubdirCiv1_Callback(hObject, eventdata, handles)
1394% %------------------------------------------------------------------------
1395% list_subdir_civ1=get(handles.ListSubdirCiv1,'String');
1396% val=get(handles.ListSubdirCiv1,'Value');
1397% SubDir=list_subdir_civ1{val};
1398% if strcmp(SubDir,'new...')
1399%     if get(handles.CheckCiv1,'Value')
1400%         SubDir='CIV_INPUT'; %default subdirectory
1401%     else
1402%         msgbox_uvmat('ERROR','select CheckCiv1 to perform a new civ_input operation')
1403%         return
1404%     end   
1405% end
1406% set(handles.Civ1_ImageB,'String',SubDir);
1407% errormsg=find_netcpair_civ(handles,1);
1408% if ~isempty(errormsg)
1409%     msgbox_uvmat('ERROR',errormsg)
1410% end
1411%     
1412%------------------------------------------------------------------------
1413% % --- Executes on button press in ListSubdirCiv2.
1414% function ListSubdirCiv2_Callback(hObject, eventdata, handles)
1415% %------------------------------------------------------------------------
1416% list_subdir_civ2=get(handles.ListSubdirCiv2,'String');
1417% val=get(handles.ListSubdirCiv2,'Value');
1418% SubDir=list_subdir_civ2{val};
1419% if strcmp(SubDir,'new...')
1420%     if get(handles.CheckCiv2,'Value')
1421%         SubDir='CIV_INPUT'; %default subdirectory
1422%     else
1423%         msgbox_uvmat('ERROR','select CheckCiv2 to perform a new civ_input operation')
1424%         return
1425%     end
1426% end
1427% set(handles.Civ2_ImageA,'String',SubDir);
1428
1429%------------------------------------------------------------------------
1430% --- Executes on button press in CheckGrid.
1431function CheckGrid_Callback(hObject, eventdata, handles)
1432%------------------------------------------------------------------------
1433value=get(hObject,'Value');
1434hparent=get(hObject,'parent');%handles of the parent panel
1435hchildren=get(hparent,'children');
1436handle_txtbox=findobj(hchildren,'tag','Grid');% look for the grid name box in the same panel
1437handle_dx=findobj(hchildren,'tag','num_Dx');
1438handle_dy=findobj(hchildren,'tag','num_Dy');
1439handle_title_dx=findobj(hchildren,'tag','title_Dx');
1440handle_title_dy=findobj(hchildren,'tag','title_Dy');
1441testgrid=0;
1442filegrid='';
1443if value
1444        hseries=findobj(allchild(0),'Tag','series');
1445    hhseries=guidata(hseries);
1446    InputTable=get(hhseries.InputTable,'Data');
1447     ind_A=1;% line index of the (first) image series
1448    if strcmp(InputTable{1,5},'.nc');
1449        ind_A=2;
1450    end
1451    filebase=InputTable{ind_A,1};
1452    [nbslice, flag_grid]=get_grid(filebase,handles);% look for a grid with appropriate name
1453    if isequal(flag_grid,1)
1454        filegrid=[num2str(nbslice) 'grid'];
1455        testgrid=1;
1456    else % browse for a grid
1457        filegrid=get(hObject,'UserData');%look for previous grid name stored as UserData
1458        if exist(filegrid,'file')
1459            filebase=filegrid;
1460        end
1461       filegrid = uigetfile_uvmat('pick a grid file .grid:',filebase,'.grid');
1462        set(hObject,'UserData',filegrid);%store for future use
1463        if ~isempty(filegrid)
1464            testgrid=1;
1465        end
1466        set(hObject,'UserData',filegrid);%store for future use
1467    end
1468end
1469if testgrid
1470    set(handle_dx,'Visible','off');
1471    set(handle_dy,'Visible','off');
1472    set(handle_title_dy,'Visible','off');
1473    set(handle_title_dx,'Visible','off');
1474    set(handle_txtbox,'Visible','on')
1475    set(handle_txtbox,'String',filegrid)
1476else
1477    set(hObject,'Value',0);
1478    set(handle_dx,'Visible','on');
1479    set(handle_dy,'Visible','on');
1480    set(handle_title_dy,'Visible','on');
1481    set(handle_title_dx,'Visible','on');
1482    set(handle_txtbox,'Visible','off')
1483end
1484
1485%% if hObject is on the checkciv1 frame, duplicate action for checkciv2 frame
1486PanelName=get(hparent,'tag');
1487if strcmp(PanelName,'Civ1')
1488    hchildren=get(handles.Civ2,'children');
1489    handle_checkbox=findobj(hchildren,'tag','CheckGrid');
1490    handle_txtbox=findobj(hchildren,'tag','Grid');
1491    handle_dx=findobj(hchildren,'tag','num_Dx');
1492    handle_dy=findobj(hchildren,'tag','num_Dy');
1493    handle_title_dx=findobj(hchildren,'tag','title_Dx');
1494    handle_title_dy=findobj(hchildren,'tag','title_Dy');
1495    set(handle_checkbox,'UserData',filegrid);%store for future use
1496    if testgrid
1497        set(handle_checkbox,'Value',1);
1498        set(handle_dx,'Visible','off');
1499        set(handle_dy,'Visible','off');
1500        set(handle_title_dx,'Visible','off');
1501        set(handle_title_dy,'Visible','off');
1502        set(handle_txtbox,'Visible','on')
1503        set(handle_txtbox,'String',filegrid)
1504    end
1505end
1506set(handles.ConfigSource,'String','NEW')
1507set(handles.OK,'BackgroundColor',[1 0 1])
1508
1509%------------------------------------------------------------------------
1510% --- Executes on button press in CheckMask: common to all panels (civ1, Civ2..)
1511function CheckMask_Callback(hObject, eventdata, handles)
1512%------------------------------------------------------------------------
1513value=get(hObject,'Value');
1514hparent=get(hObject,'parent');
1515parent_tag=get(hparent,'Tag');
1516hchildren=get(hparent,'children');
1517handle_txtbox=findobj(hchildren,'tag','Mask');% look for the mask name box in the same panel
1518handle_NbSlice=findobj(hchildren,'tag','num_NbSlice');% look for the mask name box in the same panel
1519testmask=0;
1520if value
1521    hseries=findobj(allchild(0),'Tag','series');
1522    hhseries=guidata(hseries);
1523    InputTable=get(hhseries.InputTable,'Data');
1524    ind_A=1;% line index of the (first) image series
1525    if strcmp(InputTable{1,5},'.nc');
1526        ind_A=2;
1527    end
1528%     [nbslice, flag_mask]=get_mask(InputTable{ind_A,1},handles);% look for a mask with appropriate name
1529%     if isequal(flag_mask,1)
1530%         filemask=[num2str(nbslice) 'mask'];
1531%         testmask=1;
1532%     else % browse for a mask
1533        filemask= uigetfile_uvmat('pick a mask image file:',InputTable{ind_A,1},'image');
1534        [FilePath,FileName,Ext]=fileparts(filemask);   
1535        [RootPath,SubDir,RootFile,i1_series,i2,j1,j2,NomType]=find_file_series(FilePath,[FileName,Ext]);
1536        if strcmp(NomType,'_1')
1537           NbSlice=i1_series(1,2,end);
1538           set(handle_NbSlice,'String',num2str(NbSlice))
1539        end
1540        set(hObject,'UserData',filemask);%store for future use
1541        if ~isempty(filemask)
1542            testmask=1;
1543         end
1544%     end
1545end
1546if testmask
1547    set(handles.Mask,'Visible','on')
1548    set(handles.Mask,'String',filemask)
1549    set(handles.CheckMask,'Value',1)
1550    if strcmp(NomType,'_1')
1551        set(handles.num_NbSlice,'Visible','on')
1552    end
1553else
1554    set(hObject,'Value',0);
1555    set(handle_txtbox,'Visible','off')
1556    set(handles.num_NbSlice,'Visible','off')
1557end
1558set(handles.ConfigSource,'String','NEW')
1559set(handles.ConfigSource,'BackgroundColor',[1 0 1])
1560
1561% %------------------------------------------------------------------------
1562% % --- Executes on button press in get_gridpatch1.
1563% function get_gridpatch1_Callback(hObject, eventdata, handles)
1564% %------------------------------------------------------------------------
1565% filebase=get(handles.RootPath,'String');
1566% [FileName, PathName, filterindex] = uigetfile( ...
1567%     {'*.grid', ' (*.grid)';
1568%     '*.grid',  '.grid files '; ...
1569%     '*.*', 'All Files (*.*)'}, ...
1570%     'Pick a file',filebase);
1571% filegrid=fullfile(PathName,FileName);
1572% set(handles.grid_patch1,'string',filegrid);
1573% set(hObject,'BackgroundColor',[1 0 1])
1574
1575%------------------------------------------------------------------------
1576% --- STEREO Interp
1577function cmd=RUN_STINTERP(stinterpBin,filename_A_nc,filename_B_nc,filename_nc,nx_patch,ny_patch,rho_patch,subdomain_patch,thresh_value,xmlA,xmlB)
1578%------------------------------------------------------------------------
1579namelog=[filename_nc(1:end-3) '_stinterp.log'];
1580cmd=[stinterpBin ' -f1 ' filename_A_nc  ' -f2 ' filename_B_nc ' -f  ' filename_nc ...
1581    ' -m ' nx_patch  ' -n ' ny_patch ' -ro ' rho_patch ' -nopt ' subdomain_patch ' -c1 ' xmlA ' -c2 ' xmlB '  -xy  x -Nfy 1024 > ' namelog ' 2>&1']; % redirect standard output to the log file
1582
1583% %------------------------------------------------------------------------
1584% %--read images and convert them to the uint16 format used for PIV
1585% function A=read_image(filename,type_ima,num,movieobject)
1586% %------------------------------------------------------------------------
1587% %num is the view number needed for an avi movie
1588% switch type_ima
1589%     case 'movie'
1590%         A=read(movieobject,num);
1591%     case 'avi'
1592%         mov=aviread(filename,num);
1593%         A=frame2im(mov(1));
1594%     case 'multimage'
1595%         A=imread(filename,num);
1596%     case 'image'
1597%         A=imread(filename);
1598% end
1599% siz=size(A);
1600% if length(siz)==3;%color images
1601%     A=sum(double(A),3);
1602%     A=uint16(A);
1603% end
1604
1605
1606%------------------------------------------------------------------------
1607% --- Executes on button press in get_ref_fix1.
1608function get_ref_fix1_Callback(hObject, eventdata, handles)
1609%------------------------------------------------------------------------
1610filebase=get(handles.RootPath,'String');
1611[FileName, PathName, filterindex] = uigetfile( ...
1612    {'*.nc', ' (*.nc)';
1613    '*.nc',  'netcdf files '; ...
1614    '*.*', 'All Files (*.*)'}, ...
1615    'Pick a file',filebase);
1616
1617fileinput=[PathName FileName];
1618sizf=size(fileinput);
1619if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
1620%[Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
1621[Path,ref.subdir,File,ref.num1,ref.num2,ref.num_a,ref.num_b,ref.ext,ref.nom_type]=fileparts_uvmat(fileinput);
1622ref.filebase=fullfile(Path,File);
1623% ref.num_a=stra2num(str_a);
1624% ref.num_b=stra2num(str_b);
1625% ref.num1=str2double(field_count);
1626% ref.num2=str2double(str2);
1627browse=[];%initialisation
1628if ~isequal(ref.ext,'.nc')
1629    msgbox_uvmat('ERROR','the reference file must be in netcdf format (*.nc)')
1630    return
1631end
1632set(handles.ref_fix1,'String',[fullfile(ref.subdir,File) '....nc']);
1633set(handles.ref_fix1,'UserData',ref)
1634menu_field{1}='civ1';
1635Data=nc2struct(fileinput,[]);
1636if isfield(Data,'patch') && isequal(Data.patch,1)
1637    menu_field{2}='filter1';
1638end
1639if isfield(Data,'civ2') && isequal(Data.civ2,1)
1640    menu_field{3}='civ2';
1641end
1642if isfield(Data,'patch2') && isequal(Data.patch2,1)
1643    menu_field{4}='filter2';
1644end
1645set(handles.field_ref1,'String',menu_field);
1646set(handles.field_ref1,'Value',length(menu_field));
1647set(handles.num_MinVel,'Value',2);
1648set(handles.num_MinVel,'String','1');%default threshold
1649set(handles.ref_fix1,'Enable','on')
1650
1651%------------------------------------------------------------------------
1652% --- Executes on button press in get_ref_fix2.
1653function get_ref_fix2_Callback(hObject, eventdata, handles)
1654%------------------------------------------------------------------------
1655if isequal(get(handles.get_ref_fix2,'Value'),1)
1656    filebase=get(handles.RootPath,'String');
1657    [FileName, PathName, filterindex] = uigetfile( ...
1658        {'*.nc', ' (*.nc)';
1659        '*.nc',  'netcdf files '; ...
1660        '*.*', 'All Files (*.*)'}, ...
1661        'Pick a file',filebase);
1662    fileinput=[PathName FileName];
1663    sizf=size(fileinput);
1664    if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
1665    %[Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
1666    [Path,ref.subdir,File,ref.num1,ref.num2,ref.num_a,ref.num_b,ref.ext,ref.nom_type]=fileparts_uvmat(fileinput);
1667    ref.filebase=fullfile(Path,File);
1668    %     ref.num_a=stra2num(str_a);
1669    %     ref.num_b=stra2num(str_b);
1670    %     ref.num1=str2num(field_count);
1671    %     ref.num2=str2num(str2);
1672    browse=[];%initialisation
1673    if ~isequal(ref.ext,'.nc')
1674        msgbox_uvmat('ERROR','the reference file must be in netcdf format (*.nc)')
1675        return
1676    end
1677    set(handles.ref_fix2,'String',[fullfile(ref.subdir,File) '....nc']);
1678    set(handles.ref_fix2,'UserData',ref)
1679    menu_field{1}='civ1';
1680    Data=nc2struct(fileinput,[]);
1681    if isfield(Data,'patch') && isequal(Data.patch,1)
1682        menu_field{2}='filter1';
1683    end
1684    if isfield(Data,'civ2') && isequal(Data.civ2,1)
1685        menu_field{3}='civ2';
1686    end
1687    if isfield(Data,'patch2') && isequal(Data.patch2,1)
1688        menu_field{4}='filter2';
1689    end
1690    set(handles.field_ref2,'String',menu_field);
1691    set(handles.field_ref2,'Value',length(menu_field));
1692    set(handles.num_MinVel,'Value',2);
1693    set(handles.num_MinVel,'String','1');%default threshold
1694    set(handles.ref_fix2,'Enable','on')
1695    set(handles.ref_fix2,'Visible','on')
1696    set(handles.field_ref2,'Visible','on')
1697else
1698    set(handles.ref_fix2,'Visible','off')
1699    set(handles.field_ref2,'Visible','off')
1700end
1701
1702%------------------------------------------------------------------------
1703function ref_fix1_Callback(hObject, eventdata, handles)
1704%------------------------------------------------------------------------
1705set(handles.num_MinVel,'Value',1);
1706set(handles.field_ref1,'Value',1)
1707set(handles.field_ref1,'String',{' '})
1708set(handles.ref_fix1,'UserData',[]);
1709set(handles.ref_fix1,'String','');
1710set(handles.thresh_vel1,'String','0');
1711
1712%------------------------------------------------------------------------
1713function ref_fix2_Callback(hObject, eventdata, handles)
1714%------------------------------------------------------------------------
1715set(handles.num_MinVel,'Value',1);
1716set(handles.field_ref2,'Value',1)
1717set(handles.field_ref2,'String',{' '})
1718set(handles.ref_fix2,'UserData',[]);
1719set(handles.ref_fix2,'String','');
1720set(handles.num_MinVel,'String','0');
1721
1722%------------------------------------------------------------------------
1723% --- TO ABANDON Executes on button press in test_stereo1.
1724function CheckStereo_Callback(hObject, eventdata, handles)
1725%------------------------------------------------------------------------
1726hparent=get(hObject,'parent');
1727parent_tag=get(hparent,'Tag');
1728hchildren=get(hparent,'children');
1729handle_txtbox=findobj(hchildren,'tag','txt_Mask');
1730if isequal(get(hObject,'Value'),0)
1731    set(handles.num_SubDomainSize,'Visible','on')
1732    set(handles.num_FieldSmooth,'Visible','on')
1733else
1734    set(handles.num_SubDomainSize,'Visible','off')
1735    set(handles.num_FieldSmooth,'Visible','off')
1736end
1737
1738% %------------------------------------------------------------------------
1739% % --- Executes on button press in CheckStereo.
1740% function StereoCheck_Callback(hObject, eventdata, handles)
1741% %------------------------------------------------------------------------
1742% if isequal(get(handles.CheckStereo,'Value'),0)
1743%     set(handles.num_subdomainsize,'Visible','on')
1744%     set(handles.num_FieldSmooth,'Visible','on')
1745% else
1746
1747%     set(handles.num_subdomainsize,'Visible','off')
1748%     set(handles.num_FieldSmooth,'Visible','off')
1749% end
1750
1751
1752
1753%------------------------------------------------------------------------
1754%----function introduced for the correlation window figure, activated by deleting this window
1755function closeview_field(gcbo,eventdata)
1756%------------------------------------------------------------------------
1757hview_field=findobj(allchild(0),'tag','view_field');% look for view_field
1758if ~isempty(hview_field)
1759    delete(hview_field)
1760end
1761
1762%------------------------------------------------------------------------
1763% --- Executes on button press in CheckThreshold.
1764function CheckThreshold_Callback(hObject, eventdata, handles)
1765%------------------------------------------------------------------------
1766huipanel=get(hObject,'parent');
1767obj(1)=findobj(huipanel,'Tag','num_MinIma');
1768obj(2)=findobj(huipanel,'Tag','num_MaxIma');
1769obj(3)=findobj(huipanel,'Tag','title_Threshold');
1770if get(hObject,'Value')
1771    set(obj,'Visible','on')
1772else
1773    set(obj,'Visible','off')
1774end
1775set(handles.ConfigSource,'String','NEW')
1776set(handles.OK,'BackgroundColor',[1 0 1])
1777%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1778%%%%%%%%%%%%%%   TEST functions
1779%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1780%------------------------------------------------------------------------
1781% --- Executes on button press in TestCiv1: prepare the image correlation function
1782%     activated by mouse motion
1783function TestCiv1_Callback(hObject, eventdata, handles)
1784%------------------------------------------------------------------------
1785drawnow
1786if get(handles.TestCiv1,'Value')
1787    set(handles.TestCiv1,'BackgroundColor',[1 1 0])% paint TestCiv1 button to yellow to confirm civ launch 
1788    set(handles.CheckFix1,'value',0)% desactivate next step
1789    set(handles.CheckPatch1,'value',0)% desactivate next step
1790    set(handles.CheckCiv2,'value',0)% desactivate next step
1791    set(handles.CheckFix2,'value',0)% desactivate next step
1792    set(handles.CheckPatch2,'value',0)% desactivate next step
1793    update_CivOptions(handles,0)
1794      hseries=findobj(allchild(0),'Tag','series');
1795     Param=read_GUI(hseries);
1796     Param.Action.RUN=1;
1797     Param.ActionInput=read_GUI(handles.civ_input);
1798     if isfield(Param.ActionInput,'Fix1')
1799         Param.ActionInput=rmfield(Param.ActionInput,'Fix1');
1800     end
1801     if isfield(Param.ActionInput,'Patch1')
1802         Param.ActionInput=rmfield(Param.ActionInput,'Patch1');
1803     end
1804     if isfield(Param.ActionInput,'Civ2')%remove options that may be selected beyond Patch1
1805         Param.ActionInput=rmfield(Param.ActionInput,'Civ2');
1806     end
1807     if isfield(Param.ActionInput,'Fix2')
1808         Param.ActionInput=rmfield(Param.ActionInput,'Fix2');
1809     end
1810     if isfield(Param.ActionInput,'Patch2')
1811         Param.ActionInput=rmfield(Param.ActionInput,'Patch2');
1812     end
1813     if isfield(Param,'OutputSubDir')
1814     Param=rmfield(Param,'OutputSubDir'); %remove output file option from civ_series
1815     end
1816     Param.ActionInput.Civ1.CorrSmooth=0;% launch Civ1 with no data point (to get the image names for A and B)
1817     Param.IndexRange.first_i=str2num(get(handles.ref_i,'String'));
1818     Param.IndexRange.last_i=str2num(get(handles.ref_i,'String'));
1819     if strcmp(get(handles.ref_j,'Visible'),'on')
1820         Param.IndexRange.first_j=str2num(get(handles.ref_j,'String'));
1821         Param.IndexRange.last_j=Param.IndexRange.first_j;
1822     else
1823         Param.IndexRange.first_j=1;
1824         Param.IndexRange.last_j=1;
1825     end
1826     [Data,errormsg]=civ_series(Param);% get the civ1+fix1 results
1827     if ~isempty(errormsg), return, end % rmq: error msg displayed in civ_series
1828     
1829 %% create image data ImageData for display
1830     ImageData.ListVarName={'ny','nx','A'};
1831     ImageData.VarDimName= {'ny','nx',{'ny','nx'}};
1832     ImageData.A=imread(Data.Civ1_ImageA); % read the first image
1833     if ndims(ImageData.A)==3 %case of color image
1834         ImageData.VarDimName= {'ny','nx',{'ny','nx','rgb'}};
1835     end
1836     ImageData.ny=[size(ImageData.A,1) 1];
1837     ImageData.nx=[1 size(ImageData.A,2)];
1838     ImageData.CoordUnit='pixel';% used to set equal scaling for x and y in image dispa=ly
1839
1840     %% create the figure view_field for image visualization
1841     hview_field=view_field(ImageData); %view the image in the GUI view_field
1842     set(0,'CurrentFigure',hview_field)
1843     hhview_field=guihandles(hview_field);
1844     set(hview_field,'CurrentAxes',hhview_field.PlotAxes)
1845     ViewData=get(hview_field,'UserData');
1846     ViewData.CivHandle=handles.civ_input;% indicate the handle of the civ GUI in view_field
1847     ViewData.PlotAxes.X=Data.Civ1_X';
1848     ViewData.PlotAxes.Y=Data.Civ1_Y';
1849     ViewData.PlotAxes.B=imread(Data.Civ1_ImageB);%store the second image in the UserData of the GUI view_field
1850     set(hview_field,'UserData',ViewData)% store the info in the UserData of image view_field
1851   
1852    %% look for a current figure for image correlation display
1853    corrfig=findobj(allchild(0),'tag','corrfig');
1854    if isempty(corrfig)
1855        corrfig=figure;
1856        set(corrfig,'tag','corrfig')
1857        set(corrfig,'name','image correlation')
1858        set(corrfig,'DeleteFcn',{@closeview_field})%
1859        set(handles.TestCiv1,'BackgroundColor',[1 0 0])
1860    else
1861        set(handles.TestCiv1,'BackgroundColor',[1 0 0])% paint button to red
1862        corrfig=findobj(allchild(0),'tag','corrfig');% look for a current figure for image correlation display
1863        if ~isempty(corrfig)
1864            delete(corrfig)
1865        end
1866        hview_field=findobj(allchild(0),'tag','view_field');% look for view_field
1867        if ~isempty(hview_field)
1868            delete(hview_field)
1869        end
1870    end
1871else
1872    hview_field=findobj(allchild(0),'Tag','view_field'); %view the image in the GUI view_field
1873    if ~isempty(hview_field)
1874        delete(hview_field)
1875    end     
1876end
1877
1878% --------------------------------------------------------------------
1879% --- Executes on button press in TestPatch1.
1880% --------------------------------------------------------------------
1881function TestPatch1_Callback(hObject, eventdata, handles)
1882
1883if get(handles.TestPatch1,'Value')% if TestPatch1 is activated
1884     set(handles.TestPatch1,'BackgroundColor',[1 1 0])%paint TestPatch1 button in yellow to indicate activation
1885     set(handles.Civ1,'BackgroundColor',[1 1 0])% indicate civ1 calculation is performed
1886     hseries=findobj(allchild(0),'Tag','series');
1887     Param=read_GUI(hseries);
1888     Param.Action.RUN=1;
1889     Param.ActionInput=read_GUI(handles.civ_input);
1890     if isfield(Param.ActionInput,'Civ2')%remove options that may be selected beyond Patch1
1891         Param.ActionInput=rmfield(Param.ActionInput,'Civ2');
1892     end
1893     if isfield(Param.ActionInput,'Fix2')
1894         Param.ActionInput=rmfield(Param.ActionInput,'Fix2');
1895     end
1896     if isfield(Param.ActionInput,'Patch2')
1897         Param.ActionInput=rmfield(Param.ActionInput,'Patch2');
1898     end
1899     if isfield(Param,'OutputSubDir')
1900         Param=rmfield(Param,'OutputSubDir'); %remove output file option from civ_series
1901     end
1902     ParamPatch1=Param.ActionInput.Patch1; %store the patch1 parameters
1903     Param.ActionInput=rmfield(Param.ActionInput,'Patch1');% does not execute Patch
1904     Param.IndexRange.first_i=str2num(get(handles.ref_i,'String'));
1905     Param.IndexRange.last_i=Param.IndexRange.first_i;
1906     if strcmp(get(handles.ref_j,'Visible'),'on')
1907         Param.IndexRange.first_j=str2num(get(handles.ref_j,'String'));
1908         Param.IndexRange.last_j=Param.IndexRange.first_j;
1909     else
1910         Param.IndexRange.first_j=1;
1911         Param.IndexRange.last_j=1;
1912     end
1913     [Data,errormsg]=civ_series(Param);% get the civ1+fix1 results
1914     bckcolor=get(handles.civ_input,'Color');
1915     set(handles.Civ1,'BackgroundColor',bckcolor)% indicate civ1 calculation is finished
1916     
1917     %% prepare Param for iterative Patch processing without input file reading
1918     Param.Civ1_X=Data.Civ1_X;
1919     Param.Civ1_Y=Data.Civ1_Y;
1920     Param.Civ1_U=Data.Civ1_U;
1921     Param.Civ1_V=Data.Civ1_V;
1922     Param.Civ1_FF=Data.Civ1_FF;
1923     Param=rmfield(Param,'InputTable');%desactivate input file reading
1924    if isfield(Param.ActionInput,'Civ1')
1925        Param.ActionInput=rmfield(Param.ActionInput,'Civ1');%desactivate civ1: remove civ1 input param if relevant
1926    end
1927    if isfield(Param.ActionInput,'Fix1')
1928        Param.ActionInput=rmfield(Param.ActionInput,'Fix1');%desactivate fix1:remove fix1 input param if relevant
1929    end
1930    SmoothingParam=(ParamPatch1.FieldSmooth/10)*2.^(1:7);%scan the smoothing param from 1/10 to 12.8 current value
1931    NbGood=numel(find(Data.Civ1_FF==0));
1932    NbExclude=zeros(1,7);% initialize the set of smoothing parameters
1933    DiffVel=zeros(1,7);% initialize the rms difference between patch and civ
1934    Param.ActionInput.Patch1=ParamPatch1;% retrieve Patch1 parameters
1935    for irho=1:7
1936        Param.ActionInput.Patch1.FieldSmooth=SmoothingParam(irho);
1937        [Data,errormsg]= civ_series(Param);%apply the processing fct
1938        if ~isempty(errormsg)
1939            msgbox_uvmat('ERROR',errormsg)
1940            return
1941        end
1942        ind_good=find(Data.Civ1_FF==0);
1943        Civ1_U_Diff=Data.Civ1_U(ind_good)-Data.Civ1_U_smooth(ind_good);
1944        Civ1_V_Diff=Data.Civ1_V(ind_good)-Data.Civ1_V_smooth(ind_good);
1945        DiffVel(irho)=sqrt(mean(Civ1_U_Diff.*Civ1_U_Diff+Civ1_V_Diff.*Civ1_V_Diff));
1946        NbExclude(irho)=(NbGood-numel(ind_good))/NbGood;
1947    end
1948    figure(1)
1949    semilogx(SmoothingParam,DiffVel,'b',SmoothingParam,NbExclude,'r',SmoothingParam,0.2*ones(size(SmoothingParam)),'m')
1950    grid on
1951    legend('rms velocity diff. Patch1-Civ1 (pixels)','proportion of excluded vectors (between 0 to 1)','recommended diff Patch1-Civ1')
1952    xlabel('smoothing parameter')
1953    ylabel('smoothing effect')
1954    set(handles.TestPatch1,'BackgroundColor',[0 1 0])
1955else
1956    corrfig=findobj(allchild(0),'tag','corrfig');% look for a current figure for image correlation display
1957    if ~isempty(corrfig)
1958        delete(corrfig)
1959    end
1960    hview_field=findobj(allchild(0),'tag','view_field');% look for view_field
1961    if ~isempty(hview_field)
1962        delete(hview_field)
1963    end
1964end
1965
1966%------------------------------------------------------------------------
1967% --- Executes on button press in TestCiv2.
1968%------------------------------------------------------------------------
1969function TestCiv2_Callback(hObject, eventdata, handles)
1970drawnow
1971if get(handles.TestCiv2,'Value')
1972    set(handles.TestCiv2,'BackgroundColor',[1 1 0])% paint TestCiv1 button to yellow to confirm civ launch
1973    set(handles.CheckFix2,'value',0)% desactivate next step
1974    set(handles.CheckPatch2,'value',0)% desactivate next step
1975    update_CivOptions(handles,0)
1976      hseries=findobj(allchild(0),'Tag','series');
1977     Param=read_GUI(hseries);
1978     Param.Action.RUN=1;
1979     Param.ActionInput=read_GUI(handles.civ_input);
1980     if isfield(Param,'OutputSubDir')
1981     Param=rmfield(Param,'OutputSubDir'); %remove output file option from civ_series
1982     end
1983     Param.ActionInput.Civ2.CorrSmooth=0;% launch Civ2 with no data point (to get the image names for A and B)
1984     set(handles.Civ1,'BackgroundColor',[1 1 0])
1985     set(handles.Fix1,'BackgroundColor',[1 1 0])
1986     set(handles.Patch1,'BackgroundColor',[1 1 0])
1987     Param.IndexRange.first_i=str2num(get(handles.ref_i,'String'));
1988     Param.IndexRange.last_i=Param.IndexRange.first_i;
1989     if strcmp(get(handles.ref_j,'Visible'),'on')
1990         Param.IndexRange.first_j=str2num(get(handles.ref_j,'String'));
1991         Param.IndexRange.last_j=Param.IndexRange.first_j;
1992     else
1993         Param.IndexRange.first_j=1;
1994         Param.IndexRange.last_j=1;
1995     end
1996     [Data,errormsg]=civ_series(Param);% get the civ1+fix1 results
1997     
1998     %% create image data ImageData for display
1999     ImageData.ListVarName={'ny','nx','A'};
2000     ImageData.VarDimName= {'ny','nx',{'ny','nx'}};
2001     ImageData.A=imread(Data.Civ2_ImageA); % read the first image
2002     if ndims(ImageData.A)==3 %case of color image
2003         ImageData.VarDimName= {'ny','nx',{'ny','nx','rgb'}};
2004     end
2005     ImageData.ny=[size(ImageData.A,1) 1];
2006     ImageData.nx=[1 size(ImageData.A,2)];
2007     ImageData.CoordUnit='pixel';% used to set equal scaling for x and y in image dispa=ly
2008
2009     %% create the figure view_field for image visualization
2010    hview_field=view_field(ImageData); %view the image in the GUI view_field
2011    set(0,'CurrentFigure',hview_field)
2012    % plot the boundaries of the subdomains used for patch
2013    for isub=1:size(Data.Civ1_SubRange,3);
2014        pos_x=Data.Civ1_SubRange(1,1,isub);
2015        pos_y=Data.Civ1_SubRange(2,1,isub);
2016        width=Data.Civ1_SubRange(1,2,isub)-Data.Civ1_SubRange(1,1,isub);
2017        height=Data.Civ1_SubRange(2,2,isub)-Data.Civ1_SubRange(2,1,isub);
2018        rectangle('Position',[pos_x pos_y width height],'EdgeColor',[0 0 1])
2019    end
2020    hhview_field=guihandles(hview_field);%
2021    set(hview_field,'CurrentAxes',hhview_field.PlotAxes)
2022    ViewData=get(hview_field,'UserData'); % get the currently plotted field (the image A)
2023    % store info in the UserData of view-field
2024    ViewData.CivHandle=handles.civ_input;% indicate the handle of the civ GUI in view_field
2025    ViewData.PlotAxes.B=imread(Data.Civ2_ImageB);%store the second image in the UserData of the GUI view_field
2026    ViewData.PlotAxes.X=Data.Civ2_X';
2027    ViewData.PlotAxes.Y=Data.Civ2_Y';
2028    ViewData.PlotAxes.ShiftX=Data.Civ2_U';% shift at each point (from patch1) estimated by the preliminary run of civ2
2029    ViewData.PlotAxes.ShiftY=Data.Civ2_V';
2030    ViewData.PlotAxes.Civ1_SubRange=Data.Civ1_SubRange;
2031    ViewData.PlotAxes.Civ1_NbCentres=Data.Civ1_NbCentres;
2032    ViewData.PlotAxes.Civ1_Coord_tps=Data.Civ1_Coord_tps;
2033    ViewData.PlotAxes.Civ1_U_tps=Data.Civ1_U_tps;
2034    ViewData.PlotAxes.Civ1_V_tps=Data.Civ1_V_tps;
2035    ViewData.PlotAxes.Civ1_Dt=Data.Civ1_Dt;
2036    ViewData.PlotAxes.Civ2_Dt=Data.Civ2_Dt;
2037    set(hview_field,'UserData',ViewData)% store the info in the UserData of image view_field
2038    bckcolor=get(handles.civ_input,'Color');
2039    set(handles.Civ1,'BackgroundColor',bckcolor)% indicate civ1 calmculation is finished
2040    set(handles.Fix1,'BackgroundColor',bckcolor)
2041    set(handles.Patch1,'BackgroundColor',bckcolor)
2042    drawnow
2043   
2044    %% look for a current figure for image correlation display
2045    corrfig=findobj(allchild(0),'tag','corrfig');
2046    if isempty(corrfig)
2047        corrfig=figure;
2048        set(corrfig,'tag','corrfig')
2049        set(corrfig,'name','image correlation')
2050        set(corrfig,'DeleteFcn',{@closeview_field})%
2051        set(handles.TestCiv1,'BackgroundColor',[1 0 0])
2052    else
2053        set(handles.TestCiv1,'BackgroundColor',[1 0 0])% paint button to red
2054        corrfig=findobj(allchild(0),'tag','corrfig');% look for a current figure for image correlation display
2055        if ~isempty(corrfig)
2056            delete(corrfig)
2057        end
2058        hview_field=findobj(allchild(0),'tag','view_field');% look for view_field
2059        if ~isempty(hview_field)
2060            delete(hview_field)
2061        end
2062    end   
2063else
2064    hview_field=findobj(allchild(0),'Tag','view_field'); %view the image in the GUI view_field
2065    if ~isempty(hview_field)
2066        delete(hview_field)
2067    end     
2068end
2069
2070
2071% --- Executes on button press in TestPatch2.
2072function TestPatch2_Callback(hObject, eventdata, handles)
2073if get(handles.TestPatch2,'Value')% if TestPatch2 is activated
2074     set(handles.TestPatch2,'BackgroundColor',[1 1 0])%paint TestPatch2 button in yellow to indicate activation
2075     set(handles.Civ1,'BackgroundColor',[1 1 0])% indicate civ1 calculation is activated
2076     set(handles.Fix1,'BackgroundColor',[1 1 0])% indicate fix1 calculation is activated
2077     set(handles.Patch1,'BackgroundColor',[1 1 0])% indicate Patch1 calculation is activated
2078     set(handles.Civ2,'BackgroundColor',[1 1 0])% indicate civ2 calculation is activated
2079     set(handles.Fix2,'BackgroundColor',[1 1 0])% indicate fix2 calculation is activated
2080     hseries=findobj(allchild(0),'Tag','series');
2081     Param=read_GUI(hseries);
2082     Param.Action.RUN=1;
2083     Param.ActionInput=read_GUI(handles.civ_input);
2084     if isfield(Param,'OutputSubDir')
2085     Param=rmfield(Param,'OutputSubDir'); %remove output file option from civ_series
2086     end
2087     ParamPatch2=Param.ActionInput.Patch2; %store the patch1 parameters
2088     Param.ActionInput=rmfield(Param.ActionInput,'Patch2');% does not execute Patch
2089     Param.IndexRange.first_i=str2num(get(handles.ref_i,'String'));
2090     Param.IndexRange.last_i=Param.IndexRange.first_i;
2091     if strcmp(get(handles.ref_j,'Visible'),'on')
2092         Param.IndexRange.first_j=str2num(get(handles.ref_j,'String'));
2093         Param.IndexRange.last_j=Param.IndexRange.first_j;
2094     else
2095         Param.IndexRange.first_j=1;
2096         Param.IndexRange.last_j=1;
2097     end
2098     [Data,errormsg]=civ_series(Param);% get the civ1+fix1 results
2099     bckcolor=get(handles.civ_input,'Color');
2100     set(handles.Civ1,'BackgroundColor',bckcolor)% indicate civ1 calculation is finished
2101     set(handles.Fix1,'BackgroundColor',bckcolor)% indicate fix1 calculation is finished
2102     set(handles.Patch1,'BackgroundColor',bckcolor)% indicate Patch1 calculation is finished
2103     set(handles.Civ2,'BackgroundColor',bckcolor)% indicate civ2 calculation is finished
2104     set(handles.Fix2,'BackgroundColor',bckcolor)% indicate fix2 calculation is finished
2105     
2106     %% prepare Param for iterative Patch processing without input file reading
2107     Param.Civ2_X=Data.Civ2_X;
2108     Param.Civ2_Y=Data.Civ2_Y;
2109     Param.Civ2_U=Data.Civ2_U;
2110     Param.Civ2_V=Data.Civ2_V;
2111     Param.Civ2_FF=Data.Civ2_FF;
2112     Param=rmfield(Param,'InputTable');%desactivate input file reading
2113    if isfield(Param.ActionInput,'Civ1')
2114        Param.ActionInput=rmfield(Param.ActionInput,'Civ1');%desactivate civ1: remove civ1 input param if relevant
2115    end
2116    if isfield(Param.ActionInput,'Fix1')
2117        Param.ActionInput=rmfield(Param.ActionInput,'Fix1');%desactivate fix1:remove fix1 input param if relevant
2118    end
2119    if isfield(Param.ActionInput,'Patch1')
2120        Param.ActionInput=rmfield(Param.ActionInput,'Patch1');%desactivate fix1:remove fix1 input param if relevant
2121    end
2122    if isfield(Param.ActionInput,'Civ2')
2123        Param.ActionInput=rmfield(Param.ActionInput,'Civ2');%desactivate civ2: remove civ2 input param if relevant
2124    end
2125    if isfield(Param.ActionInput,'Fix2')
2126        Param.ActionInput=rmfield(Param.ActionInput,'Fix2');%desactivate fix1:remove fix1 input param if relevant
2127    end
2128    SmoothingParam=(ParamPatch2.FieldSmooth/10)*2.^(1:7);%scan the smoothing param from 1/10 to 12.8 current value
2129    NbGood=numel(find(Data.Civ2_FF==0));
2130    NbExclude=zeros(1,7);% initialize the set of smoothing parameters
2131    DiffVel=zeros(1,7);% initialize the rms difference between patch and civ
2132    Param.ActionInput.Patch2=ParamPatch2;% retrieve Patch2 parameters
2133    for irho=1:7
2134        Param.ActionInput.Patch2.FieldSmooth=SmoothingParam(irho);
2135        [Data,errormsg]= civ_series(Param);%apply the processing fct
2136        if ~isempty(errormsg)
2137            msgbox_uvmat('ERROR',errormsg)
2138            return
2139        end
2140        ind_good=find(Data.Civ2_FF==0);
2141        Civ2_U_Diff=Data.Civ2_U(ind_good)-Data.Civ2_U_smooth(ind_good);
2142        Civ2_V_Diff=Data.Civ2_V(ind_good)-Data.Civ2_V_smooth(ind_good);
2143        DiffVel(irho)=sqrt(mean(Civ2_U_Diff.*Civ2_U_Diff+Civ2_V_Diff.*Civ2_V_Diff));
2144        NbExclude(irho)=(NbGood-numel(ind_good))/NbGood;
2145    end
2146    figure(1)
2147    semilogx(SmoothingParam,DiffVel,'b',SmoothingParam,NbExclude,'r',SmoothingParam,0.1*ones(size(SmoothingParam)),'m')
2148    grid on
2149    legend('rms velocity diff. Patch2-Civ2 (pixels)','proportion of excluded vectors (between 0 to 1)','recommended value diff. Patch2-Civ2')
2150    xlabel('smoothing parameter')
2151    ylabel('smoothing effect')
2152    set(handles.TestPatch2,'BackgroundColor',[0 1 0])
2153else
2154    corrfig=findobj(allchild(0),'tag','corrfig');% look for a current figure for image correlation display
2155    if ~isempty(corrfig)
2156        delete(corrfig)
2157    end
2158    hview_field=findobj(allchild(0),'tag','view_field');% look for view_field
2159    if ~isempty(hview_field)
2160        delete(hview_field)
2161    end
2162end
2163   
2164
2165%'nomtype2pair': creates nomencalture for index pairs knowing the image nomenclature
2166%---------------------------------------------------------------------
2167function NomTypeNc=nomtype2pair(NomTypeIma,mode)
2168%---------------------------------------------------------------------           
2169% OUTPUT:
2170% NomTypeNc
2171%---------------------------------------------------------------------
2172% INPUT:
2173% 'NomTypeIma': string defining the kind of nomenclature used for images
2174
2175NomTypeNc=NomTypeIma;%default
2176switch mode
2177    case 'pair j1-j2'     
2178    if ~isempty(regexp(NomTypeIma,'a$'))
2179        NomTypeNc=[NomTypeIma 'b'];
2180    elseif ~isempty(regexp(NomTypeIma,'A$'))
2181        NomTypeNc=[NomTypeIma 'B'];
2182    else
2183        r=regexp(NomTypeIma,'(?<num1>\d+)_(?<num2>\d+)$','names');
2184        if ~isempty(r)
2185            NomTypeNc='_1_1-2';
2186        end
2187    end
2188    case 'series(Dj)' 
2189%         r=regexp(NomTypeIma,'(?<num1>\d+)_(?<num2>\d+)$','names');
2190%         if ~isempty(r)
2191            NomTypeNc='_1_1-2';
2192%         end
2193   case 'series(Di)'
2194        r=regexp(NomTypeIma,'(?<num1>\d+)_(?<num2>\d+)$','names');
2195        if ~isempty(r)
2196            NomTypeNc='_1-2_1';
2197        else
2198            NomTypeNc='_1-2';
2199        end
2200end
2201
2202
2203%------------------------------------------------------------------------
2204% --- determine the list of index pairs of processing file
2205function [ind1,ind2,mode]=...
2206    find_pair_indices(str_civ,i_series,j_series,MinIndex_i,MaxIndex_i,MinIndex_j,MaxIndex_j)
2207%------------------------------------------------------------------------
2208ind1='';
2209ind2='';
2210r=regexp(str_civ,'^\D(?<ind>[i|j])=( -| )(?<num1>\d+)\|(?<num2>\d+)','names');
2211if ~isempty(r)
2212    mode=['D' r.ind];
2213    ind1=stra2num(r.num1);
2214    ind2=stra2num(r.num2);
2215else
2216    mode='burst';
2217    r=regexp(str_civ,'^j= (?<num1>[a-z])-(?<num2>[a-z])','names');
2218    if ~isempty(r)
2219        NomTypeNc='_1ab';
2220    else
2221        r=regexp(str_civ,'^j= (?<num1>[A-Z])-(?<num2>[A-Z])','names');
2222        if ~isempty(r)
2223            NomTypeNc='_1AB';
2224        else
2225            r=regexp(str_civ,'^j= (?<num1>\d+)-(?<num2>\d+)','names');
2226            if ~isempty(r)
2227                NomTypeNc='_1_1-2';
2228            end           
2229        end
2230    end
2231    if isempty(r)
2232        display('wrong pair mode input option')
2233    else
2234    ind1=stra2num(r.num1);
2235    ind2=stra2num(r.num2);
2236    end
2237end
2238
2239%------------------------------------------------------------------------
2240% --- fill civ_input with the parameters retrieved from an input Civ file
2241%------------------------------------------------------------------------
2242function fill_civ_input(Data,handles)
2243
2244%% Civ param
2245% lists of parameters to enter
2246ListParamNum={'CorrBoxSize','SearchBoxSize','SearchBoxShift','Dx','Dy','Dz','MinIma','MaxIma'};% list of numerical values (to transform in strings)
2247ListParamValue={'CorrSmooth','CheckGrid','CheckMask','CheckThreshold'};
2248ListParamString={'Grid','Mask'};
2249% CorrSmooth ??
2250option={'Civ1','Civ2'};
2251for ichoice=1:2
2252    if isfield(Data,[option{ichoice} '_CorrBoxSize'])
2253        fill_panel(Data,handles,option{ichoice},ListParamNum,ListParamValue,ListParamString)
2254    end
2255end
2256
2257%% Fix param
2258option={'Fix1','Fix2'};
2259for ichoice=1:2
2260    if isfield(Data,[option{ichoice} '_CheckFmin2'])
2261        ListParamNum={'MinVel','MaxVel','MinCorr'};% list of numerical values (to transform in strings)
2262        ListParamValue={'CheckFmin2','CheckF3','CheckF4'};
2263        ListParamString={'ref_fix_1'};
2264        fill_panel(Data,handles,option{ichoice},ListParamNum,ListParamValue,ListParamString)
2265    end
2266end
2267
2268%% Patch param
2269option={'Patch1','Patch2'};
2270for ichoice=1:2
2271    if isfield(Data,[option{ichoice} '_FieldSmooth'])
2272        ListParamNum={'FieldSmooth','MaxDiff','SubDomainSize'};% list of numerical values (to transform in strings)
2273        ListParamValue={};
2274        ListParamString={};
2275        fill_panel(Data,handles,option{ichoice},ListParamNum,ListParamValue,ListParamString)
2276    end
2277end
2278%------------------------------------------------------------------------
2279% --- fill a panel of civ_input with the parameters retrieved from an input Civ file
2280%------------------------------------------------------------------------
2281function fill_panel(Data,handles,panel,ListParamNum,ListParamValue,ListParamString)
2282children=get(handles.(panel),'children');%handles of the children of the input GUI with handle 'GUI_handle'
2283handles_panel=[];
2284for ichild=1:numel(children)
2285    if ~isempty(get(children(ichild),'tag'))
2286        handles_panel.(get(children(ichild),'tag'))=children(ichild);
2287    end
2288end
2289for ilist=1:numel(ListParamNum)
2290    ParamName=ListParamNum{ilist};
2291    CivParamName=[panel '_' ParamName];
2292    if isfield(Data,CivParamName)
2293        for icoord=1:numel(Data.(CivParamName))
2294            if numel(Data.(CivParamName))>1
2295                Tag=['num_' ParamName '_' num2str(icoord)];
2296            else
2297                Tag=['num_' ParamName];
2298            end
2299            if isfield(handles_panel,Tag)
2300                set(handles_panel.(Tag),'String',num2str(Data.(CivParamName)(icoord)))
2301                set(handles_panel.(Tag),'Visible','on')
2302            end
2303        end
2304    end
2305end
2306for ilist=1:numel(ListParamValue)
2307    ParamName=ListParamValue{ilist};
2308    CivParamName=[panel '_' ParamName];
2309    if strcmp(ParamName,'CorrSmooth')
2310        ParamName=['num_' ParamName];
2311    end
2312    if isfield(Data,CivParamName)
2313        if isfield(handles_panel,ParamName)
2314            set(handles_panel.(ParamName),'Value',Data.(CivParamName))
2315        end
2316    end
2317end
2318for ilist=1:numel(ListParamString)
2319    ParamName=ListParamString{ilist};
2320    CivParamName=[panel '_' ParamName];
2321    if isfield(Data,CivParamName)
2322        if isfield(handles_panel,ParamName)
2323            set(handles_panel.(ParamName),'String',Data.(CivParamName))
2324        end
2325    end
2326end
2327
2328%------------------------------------------------------------------------
2329% --- Executes on button press in ImportParam.
2330%------------------------------------------------------------------------
2331function ImportParam_Callback(hObject, eventdata, handles)
2332hseries=findobj(allchild(0),'Tag','series');
2333hhseries=guidata(hseries);
2334InputTable=get(hhseries.InputTable,'Data');% read the input file(s) table in the GUI series
2335oldfile=InputTable{1,1};
2336if isempty(oldfile)
2337    % use a file name stored in prefdir
2338    dir_perso=prefdir;
2339    profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
2340    if exist(profil_perso,'file')
2341        h=load (profil_perso);
2342        if isfield(h,'RootPath') && ischar(h.RootPath)
2343            oldfile=h.RootPath;
2344        end
2345    end
2346end
2347filexml=uigetfile_uvmat('pick a xml parameter file for civ',oldfile,'.xml');% get the xml file containing processing parameters
2348%proceed only if a file has been introduced by the browser
2349if ~isempty(filexml)
2350    Param=xml2struct(filexml);% read the input xml file as a Matlab structure
2351
2352   % Param.Action.RUN=0; %desactivate the input RUN=1
2353    if ~isfield(Param,'InputTable')||~isfield(Param,'IndexRange')
2354        msgbox_uvmat('ERROR','invalid config file: open a file in a folder ''/0_XML''')
2355        return
2356    end
2357    check_input=0;
2358    if isfield(Param,'ActionInput')
2359        if isfield(Param.ActionInput,'Program')&& strcmp(Param.ActionInput.Program,'civ_series')
2360            fill_GUI(Param.ActionInput,handles.civ_input)% fill the elements of the GUI series with the input parameters
2361            set(handles.ConfigSource,'String',filexml)
2362            check_input=1;
2363            update_CivOptions(handles,0)             
2364        end
2365    end
2366    if ~check_input
2367        msgbox_uvmat('ERROR','invalid config file (not for civ_series')
2368        return
2369    end
2370end
2371
2372% --- Executes on selection change in CheckCiv3.
2373function CheckCiv3_Callback(hObject, eventdata, handles)
2374
2375
2376%------------------------------------------------------------------------
2377% --- Executes on key press with selection of a uicontrol
2378%------------------------------------------------------------------------
2379function keyboard_callback(hObject,eventdata,handles)
2380   
2381ListExclude={'CheckCiv1','CheckFix1','CheckPatch1','CheckCiv2','CheckFix2','CheckPatch2','ref_i'};
2382if isempty(find(strcmp(get(gco,'Tag'),ListExclude),1))% if the selected uicontrol is not in the Exclude list
2383    set(handles.ConfigSource,'String','NEW')% indicate that the configuration is new
2384    set(handles.OK,'BackgroundColor',[1 0 1])%
2385    drawnow
2386end
2387
2388
2389
2390function num_NbSlice_Callback(hObject, eventdata, handles)
2391% hObject    handle to num_NbSlice (see GCBO)
2392% eventdata  reserved - to be defined in a future version of MATLAB
2393% handles    structure with handles and user data (see GUIDATA)
2394
2395% Hints: get(hObject,'String') returns contents of num_NbSlice as text
2396%        str2double(get(hObject,'String')) returns contents of num_NbSlice as a double
2397
2398
2399% --- Executes during object creation, after setting all properties.
2400function num_NbSlice_CreateFcn(hObject, eventdata, handles)
2401% hObject    handle to num_NbSlice (see GCBO)
2402% eventdata  reserved - to be defined in a future version of MATLAB
2403% handles    empty - handles not created until after all CreateFcns called
2404
2405% Hint: edit controls usually have a white background on Windows.
2406%       See ISPC and COMPUTER.
2407if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
2408    set(hObject,'BackgroundColor','white');
2409end
Note: See TracBrowser for help on using the repository browser.