source: trunk/src/civ.m @ 284

Last change on this file since 284 was 284, checked in by gostiaux, 12 years ago

CIV1 works again (windows)

File size: 221.4 KB
Line 
1%'civ': function associated with the interface 'civ.fig' for PIV, spline interpolation and stereo PIV (patch)
2%------------------------------------------------------------------------
3%  provides an interface for the software CIVx
4% function varargout = civ(varargin)
5% provides an interface for the software CIVx
6%
7%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
8%  Copyright Joel Sommeria, 2011, LEGI / CNRS-UJF-INPG, sommeria@legi.grenoble-inp.fr
9%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
10%     This file is part of the toolbox UVMAT.
11%
12%     UVMAT is free software; you can redistribute it and/or modify
13%     it under the terms of the GNU General Public License as published by
14%     the Free Software Foundation; either version 2 of the License, or
15%     (at your option) any later version.
16%
17%     UVMAT is distributed in the hope that it will be useful,
18%     but WITHOUT ANY WARRANTY; without even the implied warranty of
19%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
21%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
22function varargout = civ(varargin)
23%TODO: search range
24
25% Last Modified by GUIDE v2.5 18-Nov-2011 15:30:17
26% Begin initialization code - DO NOT EDIT
27gui_Singleton = 1;
28gui_State = struct('gui_Name',       mfilename, ...
29    'gui_Singleton',  gui_Singleton, ...
30    'gui_OpeningFcn', @civ_OpeningFcn, ...
31    'gui_OutputFcn',  @civ_OutputFcn, ...
32    'gui_LayoutFcn',  [] , ...
33    'gui_Callback',   []);
34if nargin && ischar(varargin{1})
35    gui_State.gui_Callback = str2func(varargin{1});
36end
37
38if nargout
39    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
40else
41    gui_mainfcn(gui_State, varargin{:});
42end
43% End initialization code - DO NOT EDIT
44
45%------------------------------------------------------------------------
46% --- Executes just before civ is made visible.
47function civ_OpeningFcn(hObject, eventdata, handles, param)
48%------------------------------------------------------------------------
49% This function has no output args, see OutputFcn.
50%filebase: root name
51%nom_type: nomencalture used ('png_old','_i_j'...)
52%list of field numbers to process
53%subdir: subdirectory of the opened netcdf file
54%ind_opening: operation number advised for beginning (1=checkciv1,2=checkfix1,3=checkpatch1,4=checkciv2,5=checkfix2,6=checkpatch2),
55%ind_a_opening ind_b_opening chosen pair from the opened netcdf file
56% Choose default command line output for civ
57handles.output = hObject;
58% Update handles structure
59guidata(hObject, handles);
60set(hObject,'WindowButtonDownFcn',{'mouse_alt_gui',handles}) % allows mouse action with right button (zoom for uicontrol display)
61%default initial parameters
62filebase=''; % root file name ('filebase'.civ)
63ext=[];
64
65%% read names of the .exe file to adjust the interface according to available binaries
66%path_uvmat=which('uvmat');% check the path detected for source file uvmat
67path_civ=fileparts(which('civ')); %path to civ
68addpath (path_civ) ; %add the path to UVMAT, (useful in case of change of working directory after civ has been s opened in the working directory)
69errormsg=[];%default error message
70xmlfile='PARAM.xml';
71if exist(xmlfile,'file')
72    try
73        t=xmltree(xmlfile);
74        sparam=convert(t);
75    catch
76        errormsg={' Unable to read the file PARAM.xml defining the civx binaries:'; lasterr};
77        return
78    end
79else
80    errormsg=[xmlfile ' not found: path to civx binaries undefined'];
81    return
82end
83if ~isempty(errormsg)
84    msgbox_uvmat('ERROR',errormsg);
85end
86
87test_batch=0;%default: ,no batch mode available
88if isfield(sparam,'BatchParam') && isfield(sparam.BatchParam,'BatchMode')
89    test_batch=strcmp(sparam.BatchParam.BatchMode,'sge'); %sge is currently the only implemented batch mod
90end
91if test_batch==0
92    set(handles.BATCH,'Enable','off')% put the BATCH button in grey (unactivated)
93    set(handles.BATCH,'BackgroundColor',[0.831 0.816 0.784])% put the BATCH button in grey (unactivated)
94end
95if isfield(sparam.RunParam,'CivBin')
96    if ~exist(sparam.RunParam.CivBin,'file')
97        sparam.RunParam.CivBin=fullfile(path_civ,sparam.RunParam.CivBin);
98    end
99else
100    sparam.RunParam.CivBin='';
101end
102
103%% load the list of previously browsed files in menu Open
104 dir_perso=prefdir; % path to the directory .matlab for personal data
105 profil_perso=fullfile(dir_perso,'uvmat_perso.mat');% personal data file uvmauvmat_perso.mat' in .matlab
106 if exist(profil_perso,'file')
107      h=load (profil_perso);
108      if isfield(h,'MenuFile')
109          set(handles.MenuFile_1,'Label',h.MenuFile{1})
110          %set(handles.MenuFile_1,'Label',h.MenuFile_1);
111      end
112%       if isfield(h,'MenuFile_2')
113%           set(handles.MenuFile_2,'Label',h.MenuFile_2);
114%       end
115%       if isfield(h,'MenuFile_3')
116%           set(handles.MenuFile_3,'Label',h.MenuFile_3);
117%       end
118%       if isfield(h,'MenuFile_4')
119%           set(handles.MenuFile_4,'Label',h.MenuFile_4);
120%       end
121%       if isfield(h,'MenuFile_5')
122%           set(handles.MenuFile_5,'Label',h.MenuFile_5);
123%       end
124end
125
126%default input parameters:
127num_i1=1; % set of field i numbers
128num_i2=1; % set of field i numbers
129num_j1=1; % set of field j numbers (fields a)
130num_j2=1; % second set of field j numbers (fields b)
131%subdir='A'; % subdir for the netcdf result files
132ind_opening=0; % proposed operation number (1=civ1,2=fix1,3=patch1,4=civ2,5=fix2,6=patch2)
133%load the initial parameters if the interface is started from uvmat
134if exist('param','var')&&isstruct(param)% the interface is opened from uvmat
135    filebase=param.RootName;
136    nom_type_read=param.NomType;
137    num_i1=param.num1;
138    if isnan(num_i1),num_i1=1;end
139    num_i2=param.num2;
140    if isnan(num_i2),num_i2=num_i1;end
141    num_j1=param.num_a;
142    if isnan(num_j1),num_j1=1;end
143    num_j2=param.num_b;
144    if isnan(num_j2),num_j2=num_j1;end
145    subdir=param.SubDir;
146    ind_opening=param.IndOpening;
147    ext=param.ImaExt;
148end
149browse.num_i1=num_i1;
150browse.num_i2=num_i2;
151browse.num_j1=num_j1;
152browse.num_j2=num_j2;
153if ~isempty(ext) && (~isempty(imformats(ext(2:end)))||strcmpi(ext,'.avi'));%if an image file has been opened by uvmat
154    set(handles.ImaExt,'String',ext)
155    browse.ext_ima=ext;
156    if exist('nom_type_read','var')
157        browse.nom_type_ima=nom_type_read; % the image nomenclature is stored
158    end
159elseif isequal(ext,'.nc')
160    if exist('nom_type_read','var')
161        browse.nom_type_nc=nom_type_read;% the netcdf  nomenclature is stored
162    end
163end
164set(handles.RootName,'String',filebase);
165set(handles.ImaDoc,'String',ext)
166
167%initiate advised operations
168enable_civ1(handles,0)
169enable_civ2(handles,0)
170%enable_pair1(handles,'on')
171enable_fix1(handles,0)
172enable_patch1(handles,0)
173enable_fix2(handles,0)
174enable_patch2(handles,0)
175set(handles.CheckCiv1,'Value',0)
176set(handles.CheckFix1,'Value',0)
177set(handles.CheckPatch1,'Value',0)
178set(handles.CheckCiv2,'Value',0)
179set(handles.CheckFix2,'Value',0)
180set(handles.CheckPatch2,'Value',0)
181%set(handles.frame_subdirciv2,'BackgroundColor',[0.831 0.816 0.784])
182if isequal(ind_opening,1)
183    set(handles.CheckCiv1,'Value',1)
184    enable_civ1(handles,1)
185elseif isequal(ind_opening,2)
186    set(handles.CheckFix1,'Value',1)
187    enable_fix1(handles,1)
188elseif isequal(ind_opening,3)
189    set(handles.CheckPatch1,'Value',1)
190    enable_patch1(handles,1)
191elseif isequal(ind_opening,4)
192    set(handles.CheckCiv2,'Value',1)
193    enable_civ2(handles,1)
194elseif isequal(ind_opening,5)
195    set(handles.CheckFix2,'Value',1)
196    enable_fix2(handles,1)
197    set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
198    set(handles.ListPairCiv2,'Enable','On')
199    set(handles.ListPairCiv2,'Enable','On')
200    enable_pair1(handles,'off')
201elseif isequal(ind_opening,6)
202    set(handles.CheckPatch2,'Value',1)
203    enable_patch2(handles,1)
204    set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
205    set(handles.ListPairCiv2,'Enable','On')
206    enable_pair1(handles,'off')
207end
208
209% set the range of fields (1:1 by default) and selected pair
210if isequal(num_i2,num_i1)
211    num_ref_i=num_i1;
212else
213    num_ref_i=floor((num_i1+num_i2)/2);
214    browse.incr_pair(1)=num_i2-num_i1;
215    browse.incr_pair(2)=0;
216end
217if isequal(num_j1,num_j2)
218    if isnan(num_j1)
219        num_ref_j=1;
220    else
221        num_ref_j=num_j1;
222    end
223else
224    num_ref_j=floor((num_j1+num_j2)/2);
225    browse.incr_pair(2)=num_j2-num_j1;
226end
227set(handles.first_i,'String',num2str(num_ref_i));
228set(handles.last_i,'String',num2str(num_ref_i));
229set(handles.first_j,'String',num2str(num_ref_j));
230set(handles.last_j,'String',num2str(num_ref_j));
231set(handles.ref_i,'String',num2str(num_ref_i));
232set(handles.ref_j,'String',num2str(num_ref_j));
233% set(handles.ref_i_civ2,'String',num2str(num_ref_i));
234% set(handles.ref_j_civ2,'String',num2str(num_ref_j));
235set(handles.RootName,'UserData',browse);
236if exist('param','var') && isfield(param,'RootName') && ~isempty(param.RootName)%varargin the interface is opened from uvmat
237    RootName_Callback(hObject, eventdata, handles);
238end
239
240%------------------------------------------------------------------------
241% --- Outputs from this function are returned to the command line.
242function varargout = civ_OutputFcn(hObject, eventdata, handles)
243%------------------------------------------------------------------------
244% varargout  cell array for returning output args (see VARARGOUT);
245% hObject    handle to figure
246% eventdata  reserved - to be defined in a future version of MATLAB
247% handles    structure with handles and user data (see GUIDATA)
248% Get default command line output from handles structure
249varargout{1} = handles.output;
250
251% --------------------------------------------------------------------
252function MenuBrowse_Callback(hObject, eventdata, handles)
253
254%get the input file properties
255filebase=get(handles.RootName,'String');
256oldfile=''; %default
257if isempty(filebase)|| isequal(filebase,'')%loads the previously stored file name and set it as default in the file_input box
258    dir_perso=prefdir;
259    profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
260    if exist(profil_perso,'file')
261        h=load (profil_perso);
262        if isfield(h,'filebase')&& ischar(h.filebase)
263            oldfile=h.filebase;
264        end
265        if isfield(h,'RootPath') && ischar(h.RootPath)
266            oldfile=h.RootPath;
267        end
268    end
269else
270    oldfile=filebase;
271end
272ind_opening=1;%default
273browse.incr_pair=[0 0]; %default
274menu={'*.xml;*.civ;*.png;*.jpg;*.tif;*.avi;*.AVI;*.nc;', ' (*.xml,*.civ,*.png,*.jpg ,.tif, *.avi,*.nc)';
275       '*.xml',  '.xml files '; ...
276        '*.civ',  '.civ files '; ...
277        '*.png','.png image files'; ...
278        '*.jpg',' jpeg image files'; ...
279        '*.tif','.tif image files'; ...
280        '*.avi;*.AVI','.avi movie files'; ...
281        '*.nc','.netcdf files'; ...
282        '*.*',  'All Files (*.*)'};
283[FileName, PathName, filtindex] = uigetfile( menu, 'Pick a file',oldfile);
284fileinput=[PathName FileName];%complete file name
285sizf=size(fileinput);
286if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
287[path,name,ext]=fileparts(fileinput);
288testeditxml=0;
289if isequal(ext,'.xml')
290    testeditxml=1;
291    t_browse=xmltree(fileinput);
292    head_element=get(t_browse,1);
293    if isfield(head_element,'name')&& isequal(head_element.name,'ImaDoc')
294        testeditxml=0;
295    end
296end
297if testeditxml==1 || isequal(ext,'.xls')
298    heditxml=editxml({fileinput});
299    set(heditxml,'Tag','browser')
300    waitfor(heditxml,'Tag','idle')
301    if ~ishandle(heditxml)
302        return
303    end
304    attr=findobj(get(heditxml,'children'),'Tag','CurrentAttributes');
305    set(handles.browse,'UserData',fileinput)% store for future opening with browser
306    fileinput=get(attr,'UserData');
307    if ~exist(fileinput,'file')
308        return
309    end
310end
311[RootPath,RootFile,str1,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fileinput);
312filebase=fullfile(RootPath,RootFile);
313num_i1=str2double(str1);
314if isnan(num_i1),num_i1=1;end
315num_i2=str2double(str2);
316if isnan(num_i2),num_i2=num_i1;end
317num_j1=stra2num(str_a);
318if isnan(num_j1),num_j1=1;end
319num_j2=stra2num(str_b);
320if isnan(num_j2),num_j2=num_j1;end
321if isequal(get(handles.ListCompareMode,'Value'),1)
322    browse=[];%initialisation
323else
324    browse=get(handlesRootName,'UserData');
325end
326browse.num_i1=num_i1;
327browse.num_i2=num_i2;
328browse.num_j1=num_j1;
329browse.num_j2=num_j2;
330if length(ext)>1 && (~isempty(imformats(ext(2:end)))||strcmpi(ext,'.avi'));%if an image file has been opened by uvmat
331    browse.nom_type_ima=nom_type;
332    browse.ext_ima=ext;
333    set(handles.ImaExt,'String',ext)
334end
335set(handles.ImaDoc,'String',ext);
336
337%%%%% read the state of the selected netcdf file to advise default operation
338if isequal(ext,'.nc')
339    browse.nom_type_nc=nom_type;
340    ind_opening=2;% propose 'fix' as the default option
341    Data=nc2struct(fileinput,'ListGlobalAttribute','CivStage','absolut_time_T0','fix','patch','civ2','fix2');
342    if ~isempty(Data.CivStage)%test for civ files
343        ind_opening=Data.CivStage;
344        set(handles.ListPairMode,'Value',3)
345    end
346    if ~isempty(Data.absolut_time_T0)%test for civx files
347        set(handles.ListPairMode,'Value',1)
348        if isfield(Data,'fix') && isequal(Data.fix,1)
349            ind_opening=3;
350        end
351        if isequal(Data.patch,1)
352            ind_opening=4;
353        end
354        if isequal(Data.civ2,1)
355            ind_opening=5;
356        end
357        if  isequal(Data.fix2,1)
358            ind_opening=6;
359        end
360        testciv=1; %TO SUPPRESS WITH NEW VERSION OF CIVX
361    else
362        ind_opening=3; %GUI used only for patch
363        testciv=0;
364    end
365    set(handles.subdir_civ1,'String',subdir);%set the default subdir directories for installing the .nc results
366    set(handles.subdir_civ2,'String',subdir);
367    browse.testciv=testciv;
368    browse.ind_opening=ind_opening;
369end
370set(handles.RootName,'String',filebase);
371set(handles.ImaDoc,'String',ext);
372if ~isempty(num_i1)
373    ref_i=num_i1;
374    if ~isempty(num_i2)
375        ref_i=floor((ref_i+num_i2)/2);% reference image number corresponding to the file
376        browse.incr_pair(1)=num_i2-num_i1;
377        browse.incr_pair(2)=0;
378    end
379    set(handles.first_i,'String',num2str(ref_i));
380    set(handles.last_i,'String',num2str(ref_i));
381    set(handles.ref_i,'String',num2str(ref_i));
382%     set(handles.ref_i_civ2,'String',num2str(ref_i))
383end
384if isempty(num_j1)
385    set(handles.ref_j,'String','1');
386    set(handles.ref_j_civ2,'String','1');
387else
388    ref_j=num_j1;
389    if ~isempty(num_j2)
390        ref_j=floor((num_j1+num_j2)/2);
391        browse.incr_pair(2)=num_j2-num_j1;
392    end
393    set(handles.first_j,'String',num2str(ref_j));
394    set(handles.last_j,'String',num2str(ref_j));
395    set(handles.ref_j,'String',num2str(ref_j));
396%     set(handles.ref_j_civ2,'String',num2str(ref_j));
397end
398
399% set default operation options
400enable_civ1(handles,0)
401enable_civ2(handles,0')
402enable_pair1(handles,'on')
403enable_fix1(handles,0)
404enable_patch1(handles,0)
405enable_fix2(handles,0)
406enable_patch2(handles,0)
407set(handles.CheckCiv1,'Value',0)
408set(handles.CheckFix1,'Value',0)
409set(handles.CheckPatch1,'Value',0)
410set(handles.CheckCiv2,'Value',0)
411set(handles.CheckFix2,'Value',0)
412set(handles.CheckPatch2,'Value',0)
413% set(handles.frame_subdirciv2,'BackgroundColor',[0.831 0.816 0.784])
414if isequal(ind_opening,1)
415    set(handles.CheckCiv1,'Value',1)
416    enable_civ1(handles,'on')
417elseif isequal(ind_opening,2)
418    set(handles.CheckFix1,'Value',1)
419    enable_fix1(handles,'on')
420elseif isequal(ind_opening,3)
421    set(handles.CheckPatch1,'Value',1)
422    enable_patch1(handles)
423elseif isequal(ind_opening,4)
424    set(handles.CheckCiv2,'Value',1)
425    enable_civ2(handles,'on')
426elseif isequal(ind_opening,5)
427    enable_pair1(handles,'off')
428    set(handles.CheckFix2,'Value',1)
429    enable_fix2(handles)
430    set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
431    set(handles.ListPairCiv2,'Enable','On')
432    set(handles.ListPairCiv2,'Enable','On')
433elseif isequal(ind_opening,6)
434    enable_pair1(handles,'off')
435    set(handles.CheckPatch2,'Value',1)
436    enable_patch2(handles)
437    set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
438    set(handles.ListPairCiv2,'Enable','On')
439end
440set(handles.RootName,'UserData',browse);% store information from browser
441RootName_Callback(hObject, eventdata, handles);
442
443
444% -----------------------------------------------------------------------
445% --- Open again the file whose name has been recorded in MenuFile_1
446function MenuFile_1_Callback(hObject, eventdata, handles)
447%------------------------------------------------------------------------
448fileinput=get(handles.MenuFile_1,'Label');
449display_file_name(hObject, eventdata, handles,fileinput)
450
451% -----------------------------------------------------------------------
452% --- Open again the file whose name has been recorded in MenuFile_2
453function MenuFile_2_Callback(hObject, eventdata, handles)
454%------------------------------------------------------------------------
455fileinput=get(handles.MenuFile_2,'Label');
456display_file_name(hObject, eventdata, handles,fileinput)
457
458% -----------------------------------------------------------------------
459% --- Open again the file whose name has been recorded in MenuFile_3
460function MenuFile_3_Callback(hObject, eventdata, handles)
461%------------------------------------------------------------------------
462fileinput=get(handles.MenuFile_3,'Label');
463display_file_name(hObject, eventdata, handles,fileinput)
464
465% -----------------------------------------------------------------------
466% --- Open again the file whose name has been recorded in MenuFile_4
467function MenuFile_4_Callback(hObject, eventdata, handles)
468%------------------------------------------------------------------------
469fileinput=get(handles.MenuFile_4,'Label');
470display_file_name(hObject, eventdata, handles,fileinput)
471
472% -----------------------------------------------------------------------
473% --- Open again the file whose name has been recorded in MenuFile_5
474function MenuFile_5_Callback(hObject, eventdata, handles)
475%------------------------------------------------------------------------
476fileinput=get(handles.MenuFile_5,'Label');
477display_file_name(hObject, eventdata, handles,fileinput)
478
479%------------------------------------------------------------------------
480function ImaDoc_Callback(hObject, eventdata, handles)
481%------------------------------------------------------------------------
482RootName_Callback(hObject, eventdata, handles)
483
484%------------------------------------------------------------------------
485% --- function activated when a new filebase (image series) is introduced
486function RootName_Callback(hObject, eventdata, handles)
487%------------------------------------------------------------------------
488filebase=get(handles.RootName,'String');
489display_file_name(hObject, eventdata, handles,filebase)
490
491%------------------------------------------------------------------------
492function display_file_name(hObject, eventdata, handles,filebase)
493%------------------------------------------------------------------------
494set(handles.ListCompareMode,'Visible','on')
495ext_ima='';%default
496nom_type_ima=[];%default
497field_count=1;%default
498nom_type_nc=[];
499time=[];
500TimeUnit='frame'; %default
501CoordUnit='px';%default
502pxcmx_search=[];%default
503pxcmy_search=[];%default
504
505ext_imadoc=get(handles.ImaDoc,'String');
506browse=get(handles.RootName,'UserData');%default
507if isfield(browse,'nom_type_ima')
508    nom_type_ima=browse.nom_type_ima;% get an image nomenclature type already determined by an input image name
509end
510if isfield(browse,'ext_ima')
511    ext_ima=browse.ext_ima;
512end
513if isfield(browse,'nom_type_nc')
514    nom_type_nc=browse.nom_type_nc;% get an image nomenclature type already determined by an input image name
515end
516if isfield(browse,'num_i1')
517    field_count=browse.num_i1;% get an image index type already determined by an input file
518end
519set(handles.civ,'UserData',[]); %refresh list of previous civ files (for STATUS)
520
521%default first_i and j and increments
522first_i=str2double(get(handles.first_i,'String'));%value possibly set by uvmat_Opening
523if isnan(first_i)|| first_i < 1
524    first_i=1; %default first_i
525end
526last_i=str2double(get(handles.last_i,'String'));
527if isnan(last_i)|| last_i < first_i
528    last_i=first_i;  %default last_i
529end
530first_j=str2double(get(handles.first_j,'String'));
531if isnan(first_j)|| first_j < 1
532    first_j=1; %default first_j
533end
534last_j=str2double(get(handles.last_j,'String'));
535if isnan(last_j)|| last_j < first_j
536    last_j=first_j; %default last_j
537end
538incr_i=str2double(get(handles.incr_i,'String'));
539if isnan(incr_i) || incr_i < 1;
540    set(handles.incr_i,'String','1') %default incr_i
541end
542incr_j=str2double(get(handles.incr_j,'String'));
543if isnan(incr_j) || incr_j < 1;
544    set(handles.incr_j,'String','1') %default incr_j
545end
546dt=[];%default
547testmode=0;%default
548nbfield=[]; %default
549nburst=[];%default
550pxcmx=1;
551pxcmy=1;
552
553%look for an image documentation file
554if ~strcmp(ext_imadoc,'.xml') && ~strcmp(ext_imadoc,'.civ')&& ~strcmpi(ext_imadoc,'.avi')
555    if exist([filebase '.xml'],'file')
556        ext_imadoc='.xml';
557    elseif exist([filebase '.civxml'],'file')
558        ext_imadoc='.civxml';
559    elseif exist([filebase '.civ'],'file')
560        ext_imadoc='.civ';
561    elseif exist([filebase '.avi'],'file')
562        ext_imadoc='.avi';
563    elseif exist([filebase '.AVI'],'file')
564        ext_imadoc='.AVI';
565    end
566    set(handles.ImaDoc,'String',ext_imadoc)
567end
568
569%%%%%%%%   read image documentation file  %%%%%%%%%%%%%%%%%%%%%%%%%%%
570mode=''; %default
571set(handles.ImaDoc,'BackgroundColor',[1 1 0])
572drawnow
573if isequal(ext_imadoc,'.civxml') || isequal(ext_imadoc,'.xml')|| isequal(ext_imadoc,'.civ')
574    set(handles.ref_i,'Visible','On')%use a reference index
575    set(handles.ref_j,'Visible','On')
576elseif isequal(ext_imadoc,'.avi') || isequal(ext_imadoc,'.AVI')
577    set(handles.ref_j,'Visible','Off')
578else
579    set(handles.ref_i,'Visible','Off')
580    set(handles.ref_j,'Visible','Off')
581end
582testima_xml=0;
583if isequal(ext_imadoc,'.civxml')%TO ABANDON
584    [nbfield,nbfield2,time]=read_civxml([filebase '.civxml']);
585    mode='pair j1-j2';
586    if isempty(nom_type_ima)% dtermine types by default if not already selected by browser or uvmat
587        nom_type_ima='_i_j';
588    end
589elseif isequal(ext_imadoc,'.xml')
590    [XmlData,warntext]=imadoc2struct([filebase '.xml']);
591    ext_ima_read=[];
592    nom_type_read=[];
593    if isfield(XmlData,'Heading')&&isfield(XmlData.Heading','ImageName')&&ischar(XmlData.Heading.ImageName)% get image nom type and extension from the xml file
594        [PP,FF,fc,str2,str_a,str_b,ext_ima_read,nom_type_read]=name2display(XmlData.Heading.ImageName);
595        fullname=fullfile(fileparts(filebase),XmlData.Heading.ImageName); %full name (including path) of the first image defined by the xmle file,
596        if ~exist(fullname,'file')
597            msgbox_uvmat('WARNING',['FirstImage ' fullname ' defined in the xml file does not exist'])
598        end
599    end
600    if isfield(XmlData,'Time')
601        time=XmlData.Time;
602        nbfield=size(time,1);
603        nbfield2=size(time,2);
604        %transform .Time to a column vector if it is a line vector thenomenclature uses a single index: correct possible bug in xml
605        if isequal(nbfield,1) && ~isequal(nbfield2,1)% .Time is a line vector
606            if numel(nom_type_read)>=2 && isempty(regexp(nom_type_read(2:end),'\D'))
607                time=time';
608                nbfield=nbfield2;
609                nbfield2=1;
610            end
611        end
612    end
613    if isfield(XmlData,'TimeUnit')
614        TimeUnit=XmlData.TimeUnit;
615    end
616    if isfield(XmlData,'Npx')
617        npx=XmlData.Npx;
618        npy=XmlData.Npy;
619    end
620    pxcmx_search=1;
621    pxcmy_search=1;
622    if isfield(XmlData,'GeometryCalib')
623        tsai=XmlData.GeometryCalib;
624        if isfield(tsai,'f') && isfield(tsai,'Tz') && isfield(tsai,'dpx') && isfield(tsai,'dpy')&& isfield(tsai,'R')
625            rot2D=tsai.R(1:2,[1,2]);
626            pxcmx_search=tsai.f * sqrt(det(rot2D))/(tsai.Tz*tsai.dpx);
627            pxcmy_search=tsai.f * sqrt(det(rot2D))/(tsai.Tz*tsai.dpy);
628        end
629        if isfield(tsai,'CoordUnit')
630            CoordUnit=tsai.CoordUnit;
631        end
632    end
633elseif strcmp(ext_imadoc,'.civ')% case of .civ image documentation file
634    [error,time,TimeUnit,mode,npx,npy]=read_imatext([filebase '.civ']);
635    if error==2, msgbox_uvmat('WARNING',['no file ' filebase '.civ']);
636    elseif error==1, msgbox_uvmat('WARNING','inconsistent number of fields in the .civ file');
637    end
638    nom_type_ima='001a';
639elseif strcmpi(ext_imadoc,'.avi')
640    nom_type_ima='*';
641    ext_ima=ext_imadoc;
642    set(handles.ListPairMode,'Value',1);
643    set(handles.ListPairMode,'String',{'series(Di)'})
644    dt=0.04;%default
645    if exist([filebase ext_imadoc],'file')==2
646        info=aviinfo([filebase ext_imadoc]);%read infos on the avi movie
647        dt=1/info.FramesPerSecond;%time interval between successive frames
648        nbfield=info.NumFrames;%number of frames
649    end
650    time=(dt*(0:nbfield-1))';%list of image times
651end
652if isempty(time)
653    set(handles.ImaDoc,'String',''); %xml file not used for timing
654end
655set(handles.ImaDoc,'BackgroundColor',[1 1 1])
656
657%get the imabe nomenclature type if not defined by the input file nor by the xml file
658dirima=[];%default
659if isempty(nom_type_ima)
660    %look for double image series '_i_j'
661    dirima=dir([filebase '_' num2str(first_i) '_' num2str(first_j) '.*']);
662    if isempty(dirima)
663        % look for images series  with sub marker '_'
664        dirima=dir([filebase '_*' num2str(first_i) '.*']);
665        if isempty(dirima)
666            % look for other images series
667            dirima=dir([filebase '*' num2str(first_i) '.*']);
668            if isempty(dirima)
669                % look for other images series witth letter appendix
670                appendix=char(96+first_j);
671                dirima=dir([filebase '*' num2str(first_i) appendix '.*']);
672            end
673        end
674    end
675end
676for ilist=1:numel(dirima)
677    [pp,ff,fc,str2,str_a,str_b,ext_list,nom_type_list]=name2display(dirima(ilist).name);
678    form=imformats(ext_list(2:end));
679    if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
680        ext_ima=ext_list;
681        nom_type_ima=nom_type_list;
682            break
683    end
684end
685% no image documentation file found: look for a series of existing files,images by priority or .nc files
686if isempty(nom_type_ima)
687    ext_search=ext_imadoc;
688    nom_type_search=nom_type_nc;
689else
690    ext_search=ext_ima;
691    nom_type_search=nom_type_ima;
692end
693if isempty(time) && ~strcmp(nom_type_search,'none') && ~strcmp(nom_type_search,'') && ~strcmp(nom_type_search,'*')
694    subdir=get(handles.subdir_civ1,'String');
695    incr_pair=[0 0];%default
696    if isfield(browse,'incr_pair')
697        incr_pair=browse.incr_pair;
698    end
699    %     nbdetect=0;%test of detected images
700    field_i=browse.num_i2;
701    imagename=name_generator(filebase,field_i,1,ext_search,nom_type_search);
702    imagename_plus='';
703    idetect=1;
704    while idetect %look for the maximum file number in the series
705        imagename_plus=name_generator(filebase,field_i+1,1,ext_search,nom_type_search);
706        idetect=(exist(imagename_plus,'file')==2)&& ~strcmp(imagename,imagename_plus);
707        if idetect
708            field_i=field_i+1;
709        end
710        %SEE CASE OF NETCDF FILES
711        %             nbdetect=nbdetect+(exist(imagename,'file')==2);
712    end
713    nbfield=field_i;% last detected field number
714    field_i=browse.num_i1;%look for the minimum file number in the series
715    imagename_min='';
716    idetect=1;
717    while idetect==1
718        imagename_min=name_generator(filebase,field_i-1,1,ext_search,nom_type_search);
719        idetect=(exist(imagename_min,'file')==2)&& ~strcmp(imagename,imagename_min);
720        if idetect
721            field_i=field_i-1;
722        end
723    end
724    first_i=max(field_i,1);
725    if numel(regexp(nom_type_search,'\D'))>=1%two indices i and j
726        field_i=browse.num_i1;
727        field_j=browse.num_j2;
728        imagename=name_generator(filebase,field_i,field_j,ext_search,nom_type_search);
729        imagename_plus='';
730        jdetect=1;
731        while jdetect==1 %look for the maximum file number in the series
732            imagename_plus=name_generator(filebase,field_i,field_j+1,ext_search,nom_type_search);
733            jdetect=(exist(imagename_plus,'file')==2)&& ~strcmp(imagename,imagename_plus);
734            if jdetect
735                field_j=field_j+1;
736            end
737            %SEE CASE OF NETCDF FILES
738            %             nbdetect=nbdetect+(exist(imagename,'file')==2);
739        end
740        nbfield2=field_j;% last detected field number
741     end
742 
743    %determine the set of times and possible intervals for CIV
744    %   dt=(1/1000)*str2double(get(handles.dt,'String'));
745    time=(0:nbfield-1)';% time=file index -1  by default
746    if numel(regexp(nom_type_search,'\D'))>=1%two indices i and j
747        [x,y]=meshgrid(0:nbfield2-1,0:nbfield-1);
748        time=x+y;
749    end
750end
751
752if exist('time','var')
753    if size(time,1)+size(time,2)>=3 % if there are at least two time values to define dt
754        nbfield=size(time,1);
755        nbfield2=size(time,2);
756        set(handles.ImaDoc,'UserData',time); %store the set of times
757        set(handles.dt_unit,'String',['dt in m' TimeUnit]);
758%         set(handles.dt_unit_civ2,'String',['dt in m' TimeUnit]);
759        set(handles.TimeUnit,'String',TimeUnit);
760        set(handles.nb_field,'String',num2str(nbfield));
761        set(handles.nb_field2,'String',num2str(nbfield2));
762    end
763end
764set(handles.CoordUnit,'String',CoordUnit)
765set(handles.SearchRange,'UserData',[pxcmx_search pxcmy_search]);
766% npxy=[npy npx];
767set(handles.ImaExt,'String',ext_ima)
768set(handles.first_i,'String',num2str(first_i));
769set(handles.last_i,'String',num2str(last_i));%
770set(handles.first_j,'String',num2str(first_j));
771set(handles.last_j,'String',num2str(last_j));%
772browse.nom_type_ima=nom_type_ima;
773set(handles.RootName,'UserData',browse)% store the nomenclature type
774
775%%  set the menus of image pairs and default selection for civ   %%%%%%%%%%%%%%%%%%%
776test_ima_i=numel(nom_type_ima)>1 && isempty(regexp(nom_type_ima(2:end),'\D','once'));%images with single indexing
777if test_ima_i || isequal(nom_type_nc,'_i1-i2')||~(exist('nbfield2','var')&&(nbfield2~=1))
778    set(handles.ListPairMode,'Value',1)
779    set(handles.ListPairMode,'String',{'series(Di)'})   
780elseif (nbfield==1)% simple series in j
781    set(handles.ListPairMode,'Value',1)
782    set(handles.ListPairMode,'String',{'series(Dj)'})
783else
784    set(handles.ListPairMode,'String',{'pair j1-j2';'series(Dj)';'series(Di)'})%multiple choice
785    if nbfield2 <= 10
786        set(handles.ListPairMode,'Value',1)% advice 'pair j1-j2' for small burst
787    end
788end
789
790
791%% desable status and RUN button
792set(handles.RUN, 'Enable','On')
793set(handles.RUN,'BackgroundColor',[1 0 0])
794set(handles.BATCH,'Enable','On')
795set(handles.BATCH,'BackgroundColor',[1 0 0])
796if isfield(handles,'status')
797set(handles.status,'Value',0);%suppress status display
798status_Callback(hObject, eventdata, handles)
799end
800
801%% store the root input filename for future opening
802dir_perso=prefdir;
803profil_perso=fullfile(prefdir,'uvmat_perso.mat');
804RootPath=fileparts(filebase);
805if exist(profil_perso,'file')
806    save (profil_perso,'RootPath','-append'); %store the root name for future opening of uvmat
807else
808    txt=ver('MATLAB');
809    Release=txt.Release;
810    relnumb=str2double(Release(3:4));
811    if relnumb >= 14
812        save (profil_perso,'RootPath','-V6'); %store the root name for future opening of uvmat
813    else
814        save (profil_perso,'RootPath'); %store the root name for future opening of uvmat
815    end
816end
817
818%% update the subdir
819pathdir=fileparts(filebase);%path to the current xml file
820listot=dir(pathdir);
821idir=0;
822listdir={''};%default
823for ilist=1:length(listot)
824    if listot(ilist).isdir
825        name=listot(ilist).name;
826        if ~isequal(name,'.') && ~isequal(name,'..')
827            idir=idir+1;
828            listdir{idir,1}=listot(ilist).name;
829        end
830    end
831end
832set(handles.ListSubdirCiv1,'Value',1)
833set(handles.ListSubDirCiv2,'Value',1)
834set(handles.ListSubdirCiv1,'String',[listdir;'new...'])
835set(handles.ListSubDirCiv2,'String',[listdir;'new...'])
836if isempty(listdir)
837    dirname=listdir{1};
838else
839    dirname='CIV'; %default civ directory name
840end
841set(handles.subdir_civ1,'String',dirname)
842set(handles.subdir_civ2,'String',dirname)
843%check wether the current subdir exists:
844% subdir_civ1=get(handles.subdir_civ1,'String');
845% subdir_civ2=get(handles.subdir_civ2,'String');
846
847ListPairMode_Callback(hObject, eventdata, handles)
848
849%------------------------------------------------------------------------
850% --- Executes on button press in ListPairMode.
851function ListPairMode_Callback(hObject, eventdata, handles)
852%------------------------------------------------------------------------
853browse=get(handles.RootName,'UserData');
854compare_list=get(handles.ListCompareMode,'String');
855val=get(handles.ListCompareMode,'Value');
856compare=compare_list{val};
857if strcmp(compare,'displacement')
858    mode='displacement';
859else
860    mode_list=get(handles.ListPairMode,'String');
861    if ischar(mode_list)
862        mode_list={mode_list};
863    end
864    mode_value=get(handles.ListPairMode,'Value');
865    mode=mode_list{mode_value};
866end
867displ_num=[];%default
868ref_i=str2double(get(handles.ref_i,'String'));
869% last_i=str2num(get(handles.last_i,'String'));
870time=get(handles.ImaDoc,'UserData'); %get the set of times
871siztime=size(time);
872nbfield=siztime(1);
873nbfield2=siztime(2);
874indchosen=1;  %%first pair selected by default
875if isequal(mode,'pair j1-j2')%| isequal(mode,'st_pair j1-j2')
876    dt=1;
877    displ='';
878    index=0;
879    numlist_a=[];
880    numlist_B=[];
881    %get all the time intervals in bursts
882    displ_dt=1;%default
883    nbfield2=min(nbfield2,10);%limitate the number of pairs to 10x10
884    for numod_a=1:nbfield2-1 %nbfield2 always >=2 for 'pair j1-j2' mode
885        for numod_b=(numod_a+1):nbfield2
886            index=index+1;
887            numlist_a(index)=numod_a;
888            numlist_b(index)=numod_b;
889            if ~isempty(time)
890                dt(numod_a,numod_b)=time(ref_i,numod_b)-time(ref_i,numod_a);%first time interval dt
891                displ_dt(index)=dt(numod_a,numod_b);
892            else
893                displ_dt(index)=1;
894            end
895        end
896    end
897    [dtsort,indsort]=sort(displ_dt);
898    if ~isempty(numlist_a)
899        displ_num(1,:)=numlist_a(indsort);
900        displ_num(2,:)=numlist_b(indsort);
901    end
902    displ_num(3,:)=0;
903    displ_num(4,:)=0;
904    set(handles.jtext,'Visible','Off')
905    set(handles.first_j,'Visible','Off')
906    set(handles.last_j,'Visible','Off')
907    set(handles.incr_j,'Visible','Off')
908    set(handles.nb_field2,'Visible','Off')
909    set(handles.ref_j,'Visible','Off')
910elseif isequal(mode,'series(Dj)') %| isequal(mode,'st_series(Dj)')
911    for index=1:min(nbfield2-1,200)
912        displ_num(1,index)=-floor(index/2);
913        displ_num(2,index)=ceil(index/2);
914        displ_num(3,index)=0;
915        displ_num(4,index)=0;
916    end
917    set(handles.jtext,'Visible','On')
918    set(handles.first_j,'Visible','On')
919    set(handles.last_j,'Visible','On')
920    set(handles.incr_j,'Visible','On')
921    set(handles.nb_field2,'Visible','On')
922    set(handles.ref_j,'Visible','On')
923    if nbfield > 1
924        set(handles.itext,'Visible','On')
925        set(handles.first_i,'Visible','On')
926        set(handles.last_i,'Visible','On')
927        set(handles.incr_i,'Visible','On')
928        set(handles.nb_field,'Visible','On')
929        set(handles.ref_i,'Visible','On')
930    else
931        set(handles.itext,'Visible','Off')
932        set(handles.first_i,'Visible','Off')
933        set(handles.last_i,'Visible','Off')
934        set(handles.incr_i,'Visible','Off')
935        set(handles.nb_field,'Visible','Off')
936        set(handles.ref_i,'Visible','Off')
937    end
938elseif isequal(mode,'series(Di)') %| isequal(mode,'st_series(Di)')
939    for index=1:200%min(nbfield-1,200)
940        displ_num(1,index)=0;
941        displ_num(2,index)=0;
942        displ_num(3,index)=-floor(index/2);
943        displ_num(4,index)=ceil(index/2);
944    end
945    set(handles.itext,'Visible','On')
946    set(handles.first_i,'Visible','On')
947    set(handles.last_i,'Visible','On')
948    set(handles.incr_i,'Visible','On')
949    set(handles.nb_field,'Visible','On')
950    set(handles.ref_i,'Visible','On')
951    if nbfield2 > 1
952        set(handles.jtext,'Visible','On')
953        set(handles.first_j,'Visible','On')
954        set(handles.last_j,'Visible','On')
955        set(handles.incr_j,'Visible','On')
956        set(handles.nb_field2,'Visible','On')
957        set(handles.ref_j,'Visible','On')
958    else
959        set(handles.jtext,'Visible','Off')
960        set(handles.first_j,'Visible','Off')
961        set(handles.last_j,'Visible','Off')
962        set(handles.incr_j,'Visible','Off')
963        set(handles.nb_field2,'Visible','Off')
964        set(handles.ref_j,'Visible','Off')
965    end
966elseif isequal(mode,'displacement')%the pairs have the same indices
967    displ_num(1,1)=0;
968    displ_num(2,1)=0;
969    displ_num(3,1)=0;
970    displ_num(4,1)=0;
971    if nbfield > 1
972        set(handles.itext,'Visible','On')
973        set(handles.first_i,'Visible','On')
974        set(handles.last_i,'Visible','On')
975        set(handles.incr_i,'Visible','On')
976        set(handles.nb_field,'Visible','On')
977        set(handles.ref_i,'Visible','On')
978    else
979        set(handles.itext,'Visible','Off')
980        set(handles.first_i,'Visible','Off')
981        set(handles.last_i,'Visible','Off')
982        set(handles.incr_i,'Visible','Off')
983        set(handles.nb_field,'Visible','Off')
984        set(handles.ref_i,'Visible','Off')
985    end
986    if nbfield2 > 1
987        set(handles.jtext,'Visible','On')
988        set(handles.first_j,'Visible','On')
989        set(handles.last_j,'Visible','On')
990        set(handles.incr_j,'Visible','On')
991        set(handles.nb_field2,'Visible','On')
992        set(handles.ref_j,'Visible','On')
993    else
994        set(handles.jtext,'Visible','Off')
995        set(handles.first_j,'Visible','Off')
996        set(handles.last_j,'Visible','Off')
997        set(handles.incr_j,'Visible','Off')
998        set(handles.nb_field2,'Visible','Off')
999        set(handles.ref_j,'Visible','Off')
1000    end
1001end
1002set(handles.ListPairCiv1,'UserData',displ_num);
1003find_netcpair_civ1(hObject, eventdata, handles)
1004find_netcpair_civ2(hObject, eventdata, handles)
1005
1006%------------------------------------------------------------------------
1007% determine the menu for checkciv1 pairs depending on existing netcdf file at the middle of
1008% the field series set by first_i, incr, last_i
1009function find_netcpair_civ1(hObject, eventdata, handles)
1010%------------------------------------------------------------------------
1011set(gcf,'Pointer','watch')
1012%nomenclature types
1013filebase=get(handles.RootName,'String');
1014[filepath,Nme,ext_dir]=fileparts(filebase);
1015browse=get(handles.RootName,'UserData');
1016compare_list=get(handles.ListCompareMode,'String');
1017val=get(handles.ListCompareMode,'Value');
1018compare=compare_list{val};
1019if strcmp(compare,'displacement')
1020    mode='displacement';
1021else
1022    mode_list=get(handles.ListPairMode,'String');
1023    mode_value=get(handles.ListPairMode,'Value');
1024    if isempty(mode_list)
1025        return
1026    end
1027    mode=mode_list{mode_value};
1028end
1029
1030% nomenclature type of the .nc files
1031nom_type_ima=[];%default
1032if isfield(browse,'nom_type_ima')
1033    nom_type_ima=browse.nom_type_ima;
1034end
1035
1036%determine nom_type_nc:
1037nom_type_nc=[];%default
1038if isfield(browse,'nom_type_nc')
1039    nom_type_nc=browse.nom_type_nc;
1040end
1041if isempty(nom_type_nc)
1042    [nom_type_nc]=nomtype2pair(nom_type_ima,isequal(mode,'series(Di)'),isequal(mode,'series(Dj)'));
1043end
1044browse.nom_type_nc=nom_type_nc;
1045set(handles.RootName,'UserData',browse)
1046
1047%reads .nc subdirectoy and image numbers from the interface
1048subdir_civ1=get(handles.subdir_civ1,'String');%subdirectory subdir_civ1 for the netcdf data
1049ref_i=str2double(get(handles.ref_i,'String'));
1050if isequal(mode,'pair j1-j2')%|isequal(mode,'st_pair j1-j2')
1051    ref_j=0;
1052else
1053    ref_j=str2double(get(handles.ref_j,'String'));
1054end
1055time=get(handles.ImaDoc,'UserData');%get the set of times
1056if isempty(time)
1057    time=[0 1];
1058end
1059dt_unit=1000;%default
1060displ_num=get(handles.ListPairCiv1,'UserData');
1061
1062%eliminate the first pairs inconsistent with the position
1063if isempty(displ_num)
1064    nbpair=0;
1065else
1066    nbpair=length(displ_num(1,:));%nbre of displayed pairs
1067    if  isequal(mode,'series(Di)')  %| isequal(mode,'st_series(Di)')
1068        nbpair=min(2*ref_i-1,nbpair);%limit the number of pairs with positive first index
1069    elseif  isequal(mode,'series(Dj)')% | isequal(mode,'st_series(Dj)')
1070        nbpair=min(2*ref_j-1,nbpair);%limit the number of pairs with positive first index
1071    end
1072end
1073nbpair=min(200,nbpair);%limit the number of displayed pairs to 200
1074
1075%look for existing processed pairs involving the field at the middle of the series if checkciv1 will not
1076% be performed, while the result is needed for next steps.
1077displ_pair={''};
1078select=ones(size(1:nbpair));%flag for displayed pairs =1 for display
1079testpair=0;
1080% case with no checkciv1 operation, netcdf files need to exist for reading
1081if get(handles.CheckCiv1,'Value')==0 %
1082    if ~exist(fullfile(filepath,subdir_civ1,ext_dir),'dir')
1083        msgbox_uvmat('ERROR',['no civ1 file available: subdirectory ' subdir_civ1 ' does not exist']);
1084        set(handles.ListPairCiv1,'String',{});
1085        return
1086    end
1087    for ipair=1:nbpair
1088        filename=name_generator(filebase,ref_i+displ_num(3,ipair),ref_j+displ_num(1,ipair),'.nc',nom_type_nc,1,...
1089            ref_i+displ_num(4,ipair),ref_j+displ_num(2,ipair),subdir_civ1);
1090        select(ipair)=exist(filename,'file')==2;% put flag to 0 if the file does not exist
1091    end   
1092    % case of no displayed pair
1093    if isequal(select,zeros(size(1:nbpair)))
1094        if isfield(browse,'incr_pair') && ~isequal(browse.incr_pair,[0 0])
1095            num_i1=ref_i-floor(browse.incr_pair(1)/2);
1096            num_i2=ref_i+ceil(browse.incr_pair(1)/2);
1097            num_j1=ref_j-floor(browse.incr_pair(2)/2);
1098            num_j2=ref_j+ceil(browse.incr_pair(2)/2);
1099            filename=name_generator(filebase,num_i1,num_j1,'.nc',nom_type_nc,1,num_i2,num_j2,subdir_civ1);
1100            select(1)=exist(filename,'file')==2;
1101            testpair=1;
1102        else
1103            if  isequal(mode,'series(Dj)')% | isequal(mode,'st_series(Dj)')
1104                msgbox_uvmat('ERROR',['no civ1 file available for the selected reference index j=' num2str(ref_j) ' and subdirectory ' subdir_civ1]);
1105            else
1106                msgbox_uvmat('ERROR',['no civ1 file available for the selected reference index i=' num2str(ref_i) ' and subdirectory ' subdir_civ1]);
1107            end
1108            set(handles.ListPairCiv1,'String',{''});
1109            %COMPLETER CAS STEREO
1110            return
1111        end
1112    end
1113end
1114if isequal(mode,'series(Di)') %| isequal(mode,'st_series(Di)')
1115    if testpair
1116        displ_pair{1}=['Di= ' num2str(-floor(browse.incr_pair(1)/2)) '|' num2str(ceil(browse.incr_pair(1)/2))];
1117    else
1118        for ipair=1:nbpair
1119            if select(ipair)
1120                if size(time,1)>=ref_i+displ_num(4,ipair) && size(time,2)>=ref_j+displ_num(2,ipair)
1121                    dt=time(ref_i+displ_num(4,ipair),ref_j+displ_num(2,ipair))-time(ref_i+displ_num(3,ipair),ref_j+displ_num(1,ipair));%time interval dt
1122                    displ_pair{ipair}=['Di= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ' :dt= ' num2str(dt*1000)];
1123                else
1124                    displ_pair{ipair}='...'; %pair not displayed in the menu
1125                end
1126            end
1127        end
1128    end
1129elseif isequal(mode,'series(Dj)')%|isequal(mode,'st_series(Dj)')% series on the j index
1130    if testpair
1131        displ_pair{1}=['Dj= ' num2str(-floor(browse.incr_pair(1)/2)) '|' num2str(ceil(browse.incr_pair(1)/2))];
1132    else
1133        for ipair=1:nbpair
1134            if select(ipair)
1135                if size(time,1)>=ref_i+displ_num(4,ipair) && size(time,2)>=ref_j+displ_num(2,ipair)
1136                    dt=time(ref_i+displ_num(4,ipair),ref_j+displ_num(2,ipair))-time(ref_i+displ_num(3,ipair),ref_j+displ_num(1,ipair));%time interval dt
1137                    displ_pair{ipair}=['Dj= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ' :dt= ' num2str(dt*1000)];
1138                end
1139            elseif testpair
1140                displ_pair{1}=['Dj= ' num2str(-floor(browse.incr_pair(2)/2)) '|' num2str(ceil(browse.incr_pair(2)/2))];
1141            else
1142                displ_pair{ipair}='...'; %pair not displayed in the menu
1143            end
1144        end
1145    end
1146elseif isequal(mode,'pair j1-j2')%|isequal(mode,'st_pair j1-j2')%case of pairs
1147    for ipair=1:nbpair
1148        if select(ipair)
1149            dt=time(ref_i+displ_num(4,ipair),displ_num(2,ipair))-time(ref_i+displ_num(3,ipair),displ_num(1,ipair));%time interval dt
1150            displ_pair{ipair}=['j= ' num2stra(displ_num(1,ipair),nom_type_ima) '-' num2stra(displ_num(2,ipair),nom_type_ima) ...
1151                ' :dt= ' num2str(dt*1000)];
1152        else
1153            displ_pair{ipair}='...'; %pair not displayed in the menu
1154        end
1155    end
1156elseif isequal(mode,'displacement')
1157    displ_pair={'Di=Dj=0'};
1158end
1159set(handles.ListPairCiv1,'String',displ_pair');
1160ichoice=find(select,1);
1161if (isempty(ichoice) || ichoice < 1); ichoice=1; end;
1162initial=get(handles.ListPairCiv1,'Value');%initial choice of pair
1163if initial>nbpair
1164    set(handles.ListPairCiv1,'Value',ichoice);% first valid pair proposed by default in the menu
1165end
1166if numel(select)>=initial && ~isequal(select(initial),1)
1167    set(handles.ListPairCiv1,'Value',ichoice);% first valid pair proposed by default in the menu
1168end
1169
1170%set(handles.ListPairCiv2,'String',displ_pair');
1171initial=get(handles.ListPairCiv2,'Value');
1172if initial>length(displ_pair')%|~isequal(select(initial),1)
1173    if ichoice <= length(displ_pair')
1174        set(handles.ListPairCiv2,'Value',ichoice);% same pair proposed by default for civ2
1175    else
1176        set(handles.ListPairCiv2,'Value',1);% same pair proposed by default for civ2
1177    end
1178end
1179set(handles.ListPairCiv2,'String',displ_pair');
1180set(gcf,'Pointer','arrow')
1181
1182%------------------------------------------------------------------------
1183% determine the menu for checkciv2 pairs depending on the existing netcdf file at the
1184%middle of the series set by first_i, incr, last_i
1185function find_netcpair_civ2(hObject, eventdata, handles)
1186%------------------------------------------------------------------------
1187set(gcf,'Pointer','watch')
1188%nomenclature types
1189filebase=get(handles.RootName,'String');
1190[filepath,Nme,ext_dir]=fileparts(filebase);
1191browse=get(handles.RootName,'UserData');
1192compare_list=get(handles.ListCompareMode,'String');
1193val=get(handles.ListCompareMode,'Value');
1194compare=compare_list{val};
1195if strcmp(compare,'displacement')
1196    mode='displacement';
1197else
1198    mode_list=get(handles.ListPairMode,'String');
1199    if isempty(mode_list)
1200        msgbox_uvmat('ERROR','please enter an input image or netcdf file')
1201        return
1202    end
1203    mode_value=get(handles.ListPairMode,'Value');
1204    mode=mode_list{mode_value};
1205end
1206
1207% nomenclature type of the .nc files
1208nom_type_ima='ima_num';%default
1209if isfield(browse,'nom_type_ima')
1210    nom_type_ima=browse.nom_type_ima;
1211end
1212nom_type_nc='_i1-i2';%default
1213if isfield(browse,'nom_type_nc')
1214    nom_type_nc=browse.nom_type_nc;
1215end
1216if isequal(nom_type_ima,'png_old') || isequal(nom_type_ima,'netc_old')|| isequal(nom_type_ima,'raw_SMD')|| isequal(nom_type_nc,'netc_old')
1217    nom_type_nc='netc_old';%nom_type for the netcdf files
1218elseif isequal(nom_type_ima,'none')||isequal(nom_type_nc,'none')
1219    nom_type_nc='none';
1220elseif isequal(nom_type_ima,'avi')||isequal(nom_type_ima,'_i')||isequal(nom_type_ima,'ima_num')||isequal(nom_type_nc,'_i1-i2')
1221    nom_type_nc='_i1-i2';
1222else
1223    if  isequal(mode,'series(Di)')%|isequal(mode,'st_series(Di)')
1224        nom_type_nc='_i1-i2_j'; % PIV in volume
1225    else
1226        nom_type_nc='_i_j1-j2';
1227    end
1228end
1229browse.nom_type_nc=nom_type_nc;
1230set(handles.RootName,'UserData',browse)
1231
1232%reads .nc subdirectory and image numbers from the interface
1233subdir_civ1=get(handles.subdir_civ1,'String');%subdirectory subdir_civ1 for the netcdf data
1234subdir_civ2=get(handles.subdir_civ2,'String');%subdirectory subdir_civ2 for the netcdf data
1235% first_i=str2num(get(handles.first_i,'String'));
1236% last_i=str2num(get(handles.last_i,'String'));
1237% incr=str2num(get(handles.incr_i,'String'));
1238% num1=first_i:incr:last_i;
1239% if isempty(num1)
1240%     set(handles.ListPairCiv2,'Value',1);
1241%     set(handles.ListPairCiv2,'String',{''});
1242%     return
1243% end
1244ref_i=str2double(get(handles.ref_i,'String'));
1245if isequal(mode,'pair j1-j2')%|isequal(mode,'st_pair j1-j2')
1246    ref_j=0;
1247else
1248    ref_j=str2double(get(handles.ref_j,'String'));
1249end
1250time=get(handles.ImaDoc,'UserData'); %get the set of times
1251if isempty(time)
1252    time=[0 1];%default
1253end
1254%dt_unit=str2num(get(handles.dt,'String'));% used when there is no image documentation file
1255%dt_unit=1000;
1256displ_num=get(handles.ListPairCiv1,'UserData');
1257
1258
1259%eliminate the first pairs inconsistent with the position
1260if isempty(displ_num)
1261    nbpair=0;
1262else
1263    nbpair=length(displ_num(1,:));%nbre of displayed pairs
1264    if  isequal(mode,'series(Di)')% | isequal(mode,'st_series(Di)')
1265        nbpair=min(2*ref_i-1,nbpair);%limit the number of pairs with positive first index
1266    elseif  isequal(mode,'series(Dj)')% | isequal(mode,'st_series(Dj)')
1267        nbpair=min(2*ref_j-1,nbpair);%limit the number of pairs with positive first index
1268    end
1269end
1270nbpair=min(200,nbpair);%limit the number of displayed pairs to 200
1271
1272%look for existing processed pairs involving the field at the middle of the series if checkciv1 will not
1273% be performed, while the result is needed for next steps.
1274displ_pair={''}; %default
1275select=ones(size(1:nbpair));%default =1 for numbers of displayed pairs
1276if get(handles.CheckCiv2,'Value')==0 & get(handles.CheckCiv1,'Value')==0 & get(handles.CheckFix1,'Value')==0 & get(handles.CheckPatch1,'Value')==0%&...
1277    if ~exist(fullfile(filepath,subdir_civ2,ext_dir),'dir')
1278        errordlg(['no civ2 file available: subdirectory ' subdir_civ2 ' does not exist'])
1279        set(handles.ListPairCiv2,'Value',1);
1280        set(handles.ListPairCiv2,'String',{''});
1281        return
1282    end
1283    for ipair=1:nbpair
1284        filename=name_generator(filebase,ref_i+displ_num(3,ipair),ref_j+displ_num(1,ipair),'.nc',nom_type_nc,1,...
1285            ref_i+displ_num(4,ipair),ref_j+displ_num(2,ipair),subdir_civ1);
1286        select(ipair)=exist(filename,'file')==2;
1287    end
1288    if  isequal(select,zeros(size(1:nbpair)))
1289        if isfield(browse,'incr_pair')
1290            num_i1=ref_i-floor(browse.incr_pair(1)/2);
1291            num_i2=ref_i+floor((browse.incr_pair(1)+1)/2);
1292            num_j1=ref_j-floor(browse.incr_pair(2)/2);
1293            num_j2=ref_j+floor((browse.incr_pair(2)+1)/2);
1294            filename=name_generator(filebase,num_i1,num_j1,'.nc',nom_type_nc,1,num_i2,num_j2,subdir_civ2);
1295            select(1)=exist(filename,'file')==2;
1296        else
1297            if  isequal(mode,'series(Dj)')% | isequal(mode,'st_series(Dj)')
1298                errordlg(['no civ2 file available for the selected reference index j=' num2str(ref_j) ' and subdirectory ' subdir_civ2])
1299            else
1300                errordlg(['no civ2 file available for the selected reference index i=' num2str(ref_i) ' and subdirectory ' subdir_civ2])
1301            end
1302            set(handles.ListPairCiv2,'Value',1);
1303            set(handles.ListPairCiv2,'String',{''});
1304            return
1305        end
1306    end
1307end
1308if isequal(mode,'series(Di)') % | isequal(mode,'st_series(Di)')
1309    for ipair=1:nbpair
1310        if select(ipair)
1311            if size(time,1)>=ref_i+displ_num(4,ipair) && size(time,2)>=ref_j+displ_num(2,ipair)
1312                dt=time(ref_i+displ_num(4,ipair),ref_j+displ_num(2,ipair))-time(ref_i+displ_num(3,ipair),ref_j+displ_num(1,ipair));%time interval dt
1313                displ_pair{ipair}=['Di= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ' :dt= ' num2str(dt*1000)];
1314            end
1315        else
1316            displ_pair{ipair}='...'; %pair not displayed in the menu
1317        end
1318    end
1319elseif isequal(mode,'series(Dj)') %| isequal(mode,'st_series(Dj)') % series on the j index
1320    for ipair=1:nbpair
1321        if select(ipair)
1322            if size(time,1)>=ref_i+displ_num(4,ipair) && size(time,2)>=ref_j+displ_num(2,ipair)
1323                dt=time(ref_i+displ_num(4,ipair),ref_j+displ_num(2,ipair))-time(ref_i+displ_num(3,ipair),ref_j+displ_num(1,ipair));%time interval dt
1324                displ_pair{ipair}=['Dj= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ' :dt= ' num2str(dt*1000)];
1325            end
1326        else
1327            displ_pair{ipair}='...'; %pair not displayed in the menu
1328        end
1329    end
1330elseif isequal(mode,'pair j1-j2')% | isequal(mode,'st_pair j1-j2') %case of pairs
1331    for ipair=1:nbpair
1332        if select(ipair)
1333            dt=time(ref_i+displ_num(4,ipair),displ_num(2,ipair))-time(ref_i+displ_num(3,ipair),displ_num(1,ipair));%time interval dt
1334            displ_pair{ipair}=['j= ' num2stra(displ_num(1,ipair),nom_type_ima) '-' num2stra(displ_num(2,ipair),nom_type_ima) ...
1335                ' :dt= ' num2str(dt*1000)];
1336        else
1337            displ_pair{ipair}='...'; %pair not displayed in the menu
1338        end
1339    end
1340elseif isequal(mode,'displacement')
1341    displ_pair={'Di=Dj=0'};
1342end
1343val=get(handles.ListPairCiv2,'Value');
1344ichoice=find(select,1);
1345if (isempty(ichoice) || ichoice < 1); ichoice=1; end;
1346if get(handles.CheckCiv2,'Value')==0 && get(handles.CheckCiv1,'Value')==0 && get(handles.CheckFix1,'Value')==0 && get(handles.CheckPatch1,'Value')==0
1347    val=ichoice;% first valid pair proposed by default in the menu
1348end
1349if val>length(displ_pair')
1350    set(handles.ListPairCiv2,'Value',1);% first valid pair proposed by default in the menu
1351else
1352    set(handles.ListPairCiv2,'Value',val);
1353end
1354set(handles.ListPairCiv2,'String',displ_pair');
1355set(gcf,'Pointer','arrow')
1356
1357
1358
1359
1360%------------------------------------------------------------------------
1361% --- Executes on selection change in ListPairCiv1.
1362function ListPairCiv1_Callback(hObject, eventdata, handles)
1363%------------------------------------------------------------------------
1364%reproduce by default the chosen pair in the checkciv2 menu
1365list_pair=get(handles.ListPairCiv1,'String');%get the menu of image pairs
1366index_pair=get(handles.ListPairCiv1,'Value');
1367displ_num=get(handles.ListPairCiv1,'UserData');
1368% num_a=displ_num(1,index_pair);
1369% num_b=displ_num(2,index_pair);
1370list_pair2=get(handles.ListPairCiv2,'String');%get the menu of image pairs
1371if index_pair<=length(list_pair2)
1372    set(handles.ListPairCiv2,'Value',index_pair);
1373end
1374
1375%update first_i and last_i according to the chosen image pairs
1376mode_list=get(handles.ListPairMode,'String');
1377mode_value=get(handles.ListPairMode,'Value');
1378mode=mode_list{mode_value};
1379if isequal(mode,'series(Di)')
1380    first_i=str2double(get(handles.first_i,'String'));
1381    last_i=str2double(get(handles.last_i,'String'));
1382    incr_i=str2double(get(handles.incr_i,'String'));
1383    num1=first_i:incr_i:last_i;
1384    lastfield=str2double(get(handles.nb_field,'String'));
1385    if ~isnan(lastfield)
1386        test_find=(num1-floor(index_pair/2)*ones(size(num1))>0)& ...
1387            (num1+ceil(index_pair/2)*ones(size(num1))<=lastfield);
1388        num1=num1(test_find);
1389    end
1390    set(handles.first_i,'String',num2str(num1(1)));
1391    set(handles.last_i,'String',num2str(num1(end)));
1392elseif isequal(mode,'series(Dj)')
1393    first_j=str2double(get(handles.first_j,'String'));
1394    last_j=str2double(get(handles.last_j,'String'));
1395    incr_j=str2double(get(handles.incr_j,'String'));
1396    num_j=first_j:incr_j:last_j;
1397    lastfield2=str2double(get(handles.nb_field2,'String'));
1398    if ~isnan(lastfield2)
1399        test_find=(num_j-floor(index_pair/2)*ones(size(num_j))>0)& ...
1400            (num_j+ceil(index_pair/2)*ones(size(num_j))<=lastfield2);
1401        num1=num_j(test_find);
1402    end
1403    set(handles.first_j,'String',num2str(num1(1)));
1404    set(handles.last_j,'String',num2str(num1(end)));
1405end
1406
1407%------------------------------------------------------------------------
1408% --- Executes on selection change in ListPairCiv2.
1409function ListPairCiv2_Callback(hObject, eventdata, handles)
1410%------------------------------------------------------------------------
1411index_pair=get(handles.ListPairCiv2,'Value');%get the selected position index in the menu
1412
1413%update first_i and last_i according to the chosen image pairs
1414mode_list=get(handles.ListPairMode,'String');
1415mode_value=get(handles.ListPairMode,'Value');
1416mode=mode_list{mode_value};
1417if isequal(mode,'series(Di)')
1418    first_i=str2double(get(handles.first_i,'String'));
1419    last_i=str2double(get(handles.last_i,'String'));
1420    incr_i=str2double(get(handles.incr_i,'String'));
1421    num1=first_i:incr_i:last_i;
1422    lastfield=str2double(get(handles.nb_field,'String'));
1423    if ~isnan(lastfield)
1424        test_find=(num1-floor(index_pair/2)*ones(size(num1))>0)& ...
1425            (num1+ceil(index_pair/2)*ones(size(num1))<=lastfield);
1426        num1=num1(test_find);
1427    end
1428    set(handles.first_i,'String',num2str(num1(1)));
1429    set(handles.last_i,'String',num2str(num1(end)));
1430elseif isequal(mode,'series(Dj)')
1431    first_j=str2double(get(handles.first_j,'String'));
1432    last_j=str2double(get(handles.last_j,'String'));
1433    incr_j=str2double(get(handles.incr_j,'String'));
1434    num_j=first_j:incr_j:last_j;
1435    lastfield2=str2double(get(handles.nb_field2,'String'));
1436    if ~isnan(lastfield2)
1437        test_find=(num_j-floor(index_pair/2)*ones(size(num_j))>0)& ...
1438            (num_j+ceil(index_pair/2)*ones(size(num_j))<=lastfield2);
1439        num1=num_j(test_find);
1440    end
1441    set(handles.first_j,'String',num2str(num1(1)));
1442    set(handles.last_j,'String',num2str(num1(end)));
1443end
1444
1445%------------------------------------------------------------------------
1446% --- Executes on button press in RUN: processing on local computer
1447function RUN_Callback(hObject, eventdata, handles)
1448%------------------------------------------------------------------------
1449set(handles.RUN, 'Enable','Off')
1450set(handles.RUN,'BackgroundColor',[0.831 0.816 0.784])
1451batch=0;
1452errormsg=launch_jobs(hObject, eventdata, handles,batch);
1453set(handles.RUN, 'Enable','On')
1454set(handles.RUN,'BackgroundColor',[1 0 0])
1455
1456% start status callback to visualise results
1457if ~isempty(errormsg)
1458    display(errormsg)
1459    msgbox_uvmat('ERROR',errormsg)
1460elseif  isfield(handles,'status') %&& ~isequal(get(handles.ListPairMode,'Value'),3)
1461    set(handles.status,'Value',1);%suppress status display
1462    status_Callback(hObject, eventdata, handles)
1463end
1464
1465%------------------------------------------------------------------------
1466% --- Executes on button press in BATCH: remote processing
1467function BATCH_Callback(hObject, eventdata, handles)
1468% -----------------------------------------------------------------------
1469set(handles.BATCH, 'Enable','Off')
1470set(handles.BATCH,'BackgroundColor',[0.831 0.816 0.784])
1471batch=1;
1472errormsg=launch_jobs(hObject, eventdata, handles, batch);
1473set(handles.BATCH, 'Enable','On')
1474set(handles.BATCH,'BackgroundColor',[1 0 0])
1475
1476% start status callback to visualise results
1477if ~isempty(errormsg)
1478    display(errormsg)
1479    msgbox_uvmat('ERROR',errormsg)
1480elseif isfield(handles,'status')
1481    set(handles.status,'Value',1);%suppress status display
1482    status_Callback(hObject, eventdata, handles)
1483end
1484
1485%------------------------------------------------------------------------
1486% --- Lauch command called by RUN and BATCH: remote processing
1487function errormsg=launch_jobs(hObject, eventdata, handles, batch)
1488%-----------------------------------------------------------------------
1489errormsg='';%default
1490
1491%%read the civ GUI
1492Param=read_GUI(handles.civ)
1493%% check the selected list of operations:
1494operations={'CIV1','FIX1','PATCH1','CIV2','FIX2','PATCH2'};
1495box_test=[Param.CheckCiv1 Param.CheckFix1 Param.CheckPatch1 Param.CheckCiv2 Param.CheckFix2 Param.CheckPatch2];
1496
1497index_first=find(box_test==1,1);
1498if isempty(index_first)
1499    errormsg='no selected operation';
1500    return
1501end
1502index_last=find(box_test==1,1,'last');
1503box_used=box_test(index_first : index_last);
1504[box_missing,ind_missing]=min(box_used);
1505if isequal(box_missing,0); %there is a missing step in the sequence of operations
1506    errormsg=['missing' cell2mat(operations(ind_missing))];
1507    return
1508end
1509
1510%% check mask if selecetd
1511%could be included in get_mask callback ?
1512if isequal(get(handles.CheckMask,'Value'),1)
1513    maskname=get(handles.txt_MaskName,'String');
1514    if ~exist(maskname,'file')
1515        get_mask_civ1_Callback(hObject, eventdata, handles);
1516    end
1517end
1518if isequal(get(handles.CheckMask,'Value'),1)
1519    maskname=get(handles.txt_MaskName,'String');
1520    if ~exist(maskname,'file')
1521        get_mask_fix1_Callback(hObject, eventdata, handles);
1522    end
1523end
1524if isequal(get(handles.CheckMask,'Value'),1)
1525    maskname=get(handles.txt_Mask,'String');
1526    if ~exist(maskname,'file')
1527        get_mask_civ2_Callback(hObject, eventdata, handles);
1528    end
1529end
1530if isequal(get(handles.CheckMask,'Value'),1)
1531    maskname=get(handles.mask_fix2,'String');
1532    if ~exist(maskname,'file')
1533        get_mask_fix2_Callback(hObject, eventdata, handles);
1534    end
1535end
1536
1537%% reinitialise status callback
1538if isfield(handles,'status')
1539    set(handles.status,'Value',0);%suppress status display
1540    status_Callback(hObject, eventdata, handles)
1541end
1542
1543%% set the list of files and check them
1544display('checking the files...')
1545[ref_i,ref_j,errormsg]=find_ref_indices(handles);
1546if ~isempty(errormsg)
1547    return
1548end
1549[filecell,num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2,nom_type_nc,xx,yy,compare]=...
1550    set_civ_filenames(handles,ref_i,ref_j,box_test);
1551% ADDED
1552   [Rootbat,Filebat]=fileparts(filecell.nc.civ1{1,1});%output netcdf file (without extention)
1553%ADDED
1554set(handles.civ,'UserData',filecell);%store for futur use of status callback
1555if isempty(filecell)% (error message displayed in fct set_civ_filenames)
1556    return
1557end
1558nbfield=numel(num1_civ1);
1559nbslice=numel(num_a_civ1);
1560
1561%% read the PARAM.xml file to get the binaries (and batch_mode if batch)
1562path_civ=fileparts(which('civ')); %path to the source directory of uvmat
1563xmlfile='PARAM.xml';
1564if exist(xmlfile,'file')% search parameter xml file in the whole matlab path
1565    t=xmltree(xmlfile);
1566    s=convert(t);
1567else
1568    errormsg=['no file ' xmlfile];
1569    return
1570end
1571
1572test_interp=0; %eviter les variables test_ (LG)
1573
1574if batch
1575    if isfield(s,'BatchParam')
1576        Param.xml=s.BatchParam;
1577        if isfield(Param.xml,'BatchMode')
1578            batch_mode=Param.xml.BatchMode;
1579            if ~ismember(batch_mode,{'sge','oar'})
1580                errormsg=['batch mode ' batch_mode ' not supported by UVMAT'];
1581                return
1582            end
1583        end
1584    else
1585        errormsg='no batch civ binaries defined in PARAM.xml';
1586        return
1587    end
1588else % run
1589    if isfield(s,'RunParam')
1590        Param.xml=s.RunParam;
1591    else
1592        msgbox_uvmat('ERROR','no run civ binaries defined in PARAM.xml')
1593        return
1594    end
1595end
1596
1597%% check batch mode supported
1598if batch
1599    switch batch_mode
1600        case 'sge'
1601            test_command='qstat';
1602        case 'oar'
1603            test_command='oartat';
1604    end   
1605    [s,w]=system(test_command);
1606    if ~isequal(s,0)
1607        msgbox_uvmat('ERROR',[batch_mode ' batch system not available'])
1608        return
1609    end
1610end
1611
1612%% check if the binaries exist
1613if isequal(get(handles.Matlab,'checked'),'on')
1614    CivMode='Matlab';
1615else
1616    CivMode='CivX';
1617end
1618binary_list={};
1619switch CivMode
1620    case 'CivX'
1621        binary_list={'Civ1Bin','Civ2Bin','PatchBin','FixBin'};
1622    case 'CivAll'
1623        binary_list={'Civ'}
1624    case 'Matlab'
1625        if batch
1626            % vérifier Matlab installé sur le cluster
1627            % difficile a faire a priori
1628        end         
1629end
1630for bin_name=binary_list
1631    if isfield(Param.xml,bin_name{1})
1632        if ~exist(Param.xml.(bin_name{1}),'file')%look for the full path if the file name has been defined with a relative path in PARAM.xml
1633            fullname=fullfile(path_civ,Param.xml.(bin_name{1}))
1634            if exist(fullname,'file')
1635                Param.xml.(bin_name{1})=fullname;
1636            else
1637                msgbox_uvmat('ERROR',['Binary ' Param.xml.(bin_name{1}) ' defined in PARAM.xml does not exist'])
1638                return
1639            end
1640        else
1641            [path,name,ext]=fileparts(Param.xml.(bin_name{1}));
1642            currentdir=pwd;
1643            cd(path);
1644            binpath=pwd;%path of the binary
1645            Param.xml.(bin_name{1})=fullfile(binpath,[name ext]);
1646            %                      display(Param.xml.(bin_name{1}));
1647            cd(currentdir);
1648        end
1649       
1650    end
1651end
1652
1653display('files OK, processing...')
1654
1655%% get checkciv1 parameters
1656if Param.CheckCiv1
1657%     Param.Civ1=read_param_civ1(handles,filecell);
1658end
1659
1660%% get checkfix1 parameters
1661if Param.CheckFix1
1662    param.fix1=read_param_fix1(handles,filecell);
1663end
1664
1665%% get checkpatch1 parameters
1666if Param.CheckPatch1
1667    param.Patch1=read_panel(handles.Patch1);
1668    %param.checkpatch1=read_param_patch1(handles)
1669end
1670
1671%% get checkciv2 parameters:
1672if Param.CheckCiv2
1673    param.civ2=read_param_civ2(handles,cell2mat(filecell.ima1.civ2(1,1)));
1674end
1675
1676%% get checkfix2 parameters
1677if Param.CheckFix2
1678    flagindex2(1)=get(handles.vec_Fmin2_2, 'Value');
1679    flagindex2(2)=get(handles.vec_F3_2, 'Value');
1680    flagindex2(3)=get(handles.vec_F4, 'Value');
1681    thresh_vec2C=str2double(get(handles.thresh_vec2C,'String'));%threshold on image correlation vec_C
1682    thresh_vel2=str2double(get(handles.thresh_vel2,'String'));%threshold on velocity modulus
1683    test_mask=get(handles.CheckMask,'Value');
1684    nbslice_mask=get(handles.mask_fix2,'UserData'); % get the number of slices (= number of masks)
1685    %%%%%%%%%%%%%COMPLETER LE PROGRAMME FIX AVEC REF FILE ET OPTION inf_sup=2
1686    %     inf_sup=get(handles.inf_sup2,'Value');
1687    %     ref=get(handles.ref_fix2,'UserData');
1688   
1689    %%%%%%%%%%%%%%%%%%%
1690end
1691
1692%% get checkpatch2 parameters
1693if Param.CheckPatch2==1
1694    param.Patch2=read_panel(handles.Patch1);
1695end
1696
1697%%
1698
1699
1700%% MAIN LOOP
1701time=get(handles.ImaDoc,'UserData'); %get the set of times
1702
1703super_cmd=[];
1704batch_file_list=[];
1705   
1706for ifile=1:nbfield
1707    for j=1:nbslice
1708        % initiate system command
1709        switch CivMode
1710            case 'CivX'
1711                if isunix % check: necessaire aussi en RUN?
1712                    cmd=['#!/bin/bash \n '...
1713                        '#$ -cwd \n '...
1714                        'hostname && date \n '...
1715                        'umask 002 \n'];%allow writting access to created files for user group
1716                else
1717                    cmd=[];
1718                end
1719            case 'CivAll'
1720                %         if CivAll
1721                CivAllxml=xmltree;% xml contents,  all parameters
1722                CivAllCmd='';
1723                CivAllxml=set(CivAllxml,1,'name','CivDoc');
1724        end
1725       
1726       
1727        % define output file name
1728        if Param.CheckCiv2==1 || Param.CheckFix2==1 || Param.CheckPatch2==1
1729            OutputFile=filecell.nc.civ2{ifile,j};
1730        else
1731            OutputFile=filecell.nc.civ1{ifile,j};
1732        end
1733        OutputFile=regexprep(OutputFile,'.nc','');
1734
1735       
1736        %CheckCiv1
1737        if Param.CheckCiv1
1738            % read image-dependent parameters
1739            Param.Civ1.filename_ima_a=filecell.ima1.civ1{ifile,j};
1740            Param.Civ1.filename_ima_b=filecell.ima2.civ1{ifile,j};
1741            Param.Civ1.Dt=(time(num2_civ1(ifile),num_b_civ1(j))-time(num1_civ1(ifile),num_a_civ1(j)));
1742            Param.Civ1.T0=((time(num2_civ1(ifile),num_b_civ1(j))+time(num1_civ1(ifile),num_a_civ1(j)))/2);
1743            Param.Civ1.term_a=num2stra(num_a_civ1(j),nom_type_nc);%UTILITE?
1744            Param.Civ1.term_b=num2stra(num_b_civ1(j),nom_type_nc);%
1745Param.Civ1.pxcmx=1; %velocities are expressed in pixel dispalcement
1746Param.Civ1.pxcmy=1;
1747% if exist('file_ima','var')
1748A=imread(filecell.ima1.civ1{1,1});%read the first image to get the size
1749sizim=size(A);
1750Param.Civ1.npx=(sizim(2));
1751Param.Civ1.npy=(sizim(1));
1752%TODO : civ should not need npx and npy
1753
1754
1755           
1756            % read mask parameters
1757%             dummy=get(handles.CheckMask,'Value');
1758            if Param.Civ1.CheckMask % the lines below should be changed with the new gui
1759                %matbe they when check_mask the Maskname is read
1760                maskdispl=get(handles.txt_MaskName,'String');
1761                if exist(maskdispl,'file')
1762                    Param.Civ1.maskname=maskdispl;
1763                    Param.Civ1.maskflag='y';
1764                else
1765                    maskbase=[filecell.filebase '_' maskdispl]; %
1766                    nbslice_mask=str2double(maskdispl(1:end-4)); %
1767                    num1_mask=mod(num1_civ1(ifile)-1,nbslice_mask)+1;
1768                    Param.Civ1.maskname=name_generator(maskbase,num1_mask,1,'.png','_i');
1769                    if exist(Param.Civ1.maskname,'file')
1770                        Param.Civ1.maskflag='y';
1771                    else
1772                        Param.Civ1.maskname='noFile use default';
1773                        Param.Civ1.maskflag='n';
1774                    end
1775                end
1776            else
1777                Param.Civ1.maskname='noFile use default';
1778                Param.Civ1.maskflag='n';
1779            end
1780           
1781            % read grid parameters=
1782           
1783            if Param.Civ1.CheckGrid
1784                Caram.Civ1.gridflag='y';
1785                gridname=get(handles.txt_GridName,'String');
1786                if isequal(gridname(end-3:end),'grid')
1787                    nbslice_grid=str2double(gridname(1:end-4)); %
1788                    if ~isnan(nbslice_grid)
1789                        num1_grid=mod(num1_civ1(ifile)-1,nbslice_grid)+1;
1790                        Param.Civ1.gridname=[filecell.filebase '_' name_generator(gridname,num1_grid,1,'.grid','_i')];
1791                        if ~exist(Param.Civ1.gridname,'file')
1792                            msgbox_uvmat('ERROR','grid file absent for civ1')
1793                        end
1794                    elseif exist(gridname,'file')
1795                        Param.Civ1.gridname=gridname;
1796                    else
1797                        msgbox_uvmat('ERROR','grid file absent for civ1')
1798                    end
1799                end
1800            else
1801                Param.Civ1.gridname='noFile use default';
1802                Param.Civ1.gridflag='n';
1803            end
1804           
1805            switch CivMode
1806                case 'CivX'
1807                    cmd=[cmd...
1808                        cmd_civ1(filecell.nc.civ1{ifile,j},Param) '\n'];
1809                case 'CivAll'
1810                    CivAllCmd=[CivAllCmd ' civ1 '];
1811                    str=CIV1_CMD_Unified(filecell.nc.civ1{ifile,j},'',Param.Civ1);
1812                    fieldnames=fields(str);
1813                    [CivAllxml,uid_civ1]=add(CivAllxml,1,'element','civ1');
1814                    for ilist=1:length(fieldnames)
1815                        val=eval(['str.' fieldnames{ilist}]);
1816                        if ischar(val)
1817                            [CivAllxml,uid_t]=add(CivAllxml,uid_civ1,'element',fieldnames{ilist});
1818                            [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
1819                        end
1820                    end
1821            end
1822        end
1823       
1824        % CheckFix1
1825        if Param.CheckFix1
1826           switch CivMode
1827                case 'CivX'
1828                    if isunix %unix system
1829                        cmd_FIX=[Param.xml.FixBin ' -f ' filecell.nc.civ1{ifile,j} ' -fi1 ' num2str(param.fix1.flagindex1(1)) ...
1830                            ' -fi2 ' num2str(param.fix1.flagindex1(2)) ' -fi3 ' num2str(param.fix1.flagindex1(3)) ...
1831                            ' -threshC ' num2str(param.fix1.thresh_vecC1) ' -threshV ' num2str(param.fix1.thresh_vel1) ' -maskName ' param.fix1.maskname];
1832                    else %windows system
1833                        cmd_FIX=['"' Param.xml.FixBin '" -f "' filecell.nc.civ1{ifile,j} '" -fi1 ' num2str(param.fix1.flagindex1(1)) ...
1834                            ' -fi2 ' num2str(param.fix1.flagindex1(2)) ' -fi3 ' num2str(param.fix1.flagindex1(3)) ...
1835                            ' -threshC ' num2str(param.fix1.thresh_vecC1) ' -threshV ' num2str(param.fix1.thresh_vel1) ' -maskName "' param.fix1.maskname '"'];
1836                        cmd_FIX=regexprep(cmd_FIX,'\\','\\\\');
1837                    end
1838                    cmd=[cmd cmd_FIX '\n'];
1839                case 'CivAll'
1840                    fix1.inputFileName=filecell.nc.civ1{ifile,j} ;
1841                    fix1.fi1=num2str(param.fix1.flagindex1(1));
1842                    fix1.fi2=num2str(param.fix1.flagindex1(2));
1843                    fix1.fi3=num2str(param.fix1.flagindex1(3));
1844                    fix1.threshC=num2str(param.fix1.thresh_vecC1);
1845                    fix1.threshV=num2str(param.fix1.thresh_vel1);
1846                    fieldnames=fields(fix1);
1847                    [CivAllxml,uid_fix1]=add(CivAllxml,1,'element','fix1');
1848                    for ilist=1:length(fieldnames)
1849                        val=eval(['fix1.' fieldnames{ilist}]);
1850                        if ischar(val)
1851                            [CivAllxml,uid_t]=add(CivAllxml,uid_fix1,'element',fieldnames{ilist});
1852                            [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
1853                        end
1854                    end
1855                    CivAllCmd=[CivAllCmd ' fix1 '];
1856            end
1857        end
1858       
1859        %CheckPatch1
1860        if Param.CheckPatch1==1
1861            switch CivMode
1862                case 'CivX'
1863                    cmd_PATCH=cmd_patch(filecell.nc.civ1{ifile,j},param.Patch1,Param.xml.PatchBin);
1864                    cmd=[cmd cmd_PATCH '\n'];
1865                case 'CivAll'
1866                    patch1.inputFileName=filecell.nc.civ1{ifile,j} ;
1867                    patch1.nopt=subdomain_patch1;
1868                    patch1.maxdiff=thresh_patch1;
1869                    patch1.ro=rho_patch1;
1870                    test_grid=get(handles.get_gridpatch1,'Value');
1871                    if test_grid
1872                        patch1.gridflag='y';
1873                        gridname=get(handles.grid_patch1,'String');
1874                        if isequal(gridname(end-3:end),'grid')
1875                            nbslice_grid=str2double(gridname(1:end-4)); %
1876                            if ~isnan(nbslice_grid)
1877                                num1_grid=mod(num1_civ1(ifile)-1,nbslice_grid)+1;
1878                                patch1.gridPatch=[filecell.filebase '_' name_generator(gridname,num1_grid,1,'.grid','_i')];
1879                                if ~exist(patch1.gridPatch,'file')
1880                                    msgbox_uvmat('ERROR','grid file absent for patch1')
1881                                end
1882                            elseif exist(gridname,'file')
1883                                patch1.gridPatch=gridname;
1884                            else
1885                                msgbox_uvmat('ERROR','grid file absent for patch1')
1886                            end
1887                        end
1888                    else
1889                        patch1.gridPatch='none';
1890                        patch1.gridflag='n';
1891                        patch1.m=nx_patch1;
1892                        patch1.n=ny_patch1;
1893                    end
1894                    patch1.convectFlow='n';
1895                    fieldnames=fields(patch1);
1896                    [CivAllxml,uid_patch1]=add(CivAllxml,1,'element','patch1');
1897                    for ilist=1:length(fieldnames)
1898                        val=eval(['patch1.' fieldnames{ilist}]);
1899                        if ischar(val)
1900                            [CivAllxml,uid_t]=add(CivAllxml,uid_patch1,'element',fieldnames{ilist});
1901                            [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
1902                        end
1903                    end
1904                    CivAllCmd=[CivAllCmd ' patch1 '];
1905            end
1906        end
1907        if Param.CheckCiv2==1
1908            param.civ2.filename_ima_a=filecell.ima1.civ2{ifile,j};
1909            param.civ2.filename_ima_b=filecell.ima2.civ2{ifile,j};
1910            param.civ2.Dt=num2str(time(num2_civ2(ifile),num_b_civ2(j))-time(num1_civ2(ifile),num_a_civ2(j)));
1911            param.civ2.T0=num2str((time(num2_civ1(ifile),num_b_civ2(j))+time(num1_civ2(ifile),num_a_civ2(j)))/2);
1912            param.civ2.term_a=num2stra(num_a_civ2(j),nom_type_nc);
1913            param.civ2.term_b=num2stra(num_b_civ2(j),nom_type_nc);
1914            param.civ2.filename_nc1=filecell.nc.civ1{ifile,j};
1915            param.civ2.filename_nc1(end-2:end)=[]; % remove '.nc'
1916            test_mask=get(handles.CheckMask,'Value');
1917            if test_mask==0
1918                param.civ2.maskname='noFile use default';
1919                param.civ2.maskflag='n';
1920            else
1921                maskdispl=get(handles.txt_Mask,'String');
1922                if exist(maskdispl,'file')
1923                    param.civ2.maskname=maskdispl;
1924                    param.civ2.maskflag='y';
1925                else
1926                    maskbase=[filecell.filebase '_' maskdispl]; %
1927                    nbslice_mask=str2double(maskdispl(1:end-4)); %
1928                    num1_mask=mod(num1_civ2(ifile)-1,nbslice_mask)+1;
1929                    param.civ2.maskname=name_generator(maskbase,num1_mask,1,'.png','_i');
1930                    if exist(param.civ2.maskname,'file')
1931                        param.civ2.maskflag='y';
1932                    else
1933                        param.civ2.maskname='noFile use default';
1934                        param.civ2.maskflag='n';
1935                    end
1936                end
1937            end
1938            gridname=get(handles.txt_GridName,'String');
1939            if numel(gridname)>=4 && isequal(gridname(end-3:end),'grid')
1940                nbslice_grid=str2double(gridname(1:end-4)); %
1941                if ~isnan(nbslice_grid)
1942                    param.civ2.gridflag='y';
1943                    num1_grid=mod(num1_civ2(ifile)-1,nbslice_grid)+1;
1944                    param.civ2.gridname=[filecell.filebase '_' name_generator(gridname,num1_grid,1,'.grid','_i')];
1945                    if exist(param.civ2.gridname,'file')
1946                        param.civ2.gridflag='y';
1947                    else
1948                        param.civ2.gridname='noFile use default';
1949                        param.civ2.gridflag='n';
1950                    end
1951                elseif exist(gridname,'file')
1952                    param.civ2.gridflag='y';
1953                else
1954                    param.civ2.gridname='noFile use default';
1955                    param.civ2.gridflag='n';
1956                end
1957            end
1958            switch CivMode
1959                case 'CivX'
1960                    cmd_CIV2=CIV2_CMD(filecell.nc.civ2{ifile,j},param);%creates the cmx file [fullfile(Rootbat,Filebat) '.civ2.cmx]
1961                    cmd=[cmd cmd_CIV2 '\n'];
1962                case 'CivAll'
1963                    CivAllCmd=[CivAllCmd ' civ2 '];
1964                    str=CIV2_CMD_Unified(filecell.nc.civ2{ifile,j},'',param.civ2);
1965                    fieldnames=fields(str);
1966                    [CivAllxml,uid_civ2]=add(CivAllxml,1,'element','civ2');
1967                    for ilist=1:length(fieldnames)
1968                        val=eval(['str.' fieldnames{ilist}]);
1969                        if ischar(val)
1970                            [CivAllxml,uid_t]=add(CivAllxml,uid_civ2,'element',fieldnames{ilist});
1971                            [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
1972                        end
1973                    end
1974            end
1975        end
1976       
1977        % CheckFix2
1978        if Param.CheckFix2==1
1979            test_mask=get(handles.CheckMask,'Value');
1980            if test_mask==0
1981                maskname=''; %no mask used
1982            else
1983                maskdispl=get(handles.mask_fix2,'String');
1984                maskbase=[filecell.filebase '_' maskdispl]; %
1985                nbslice_mask=str2double(maskdispl(1:end-4)); %
1986                num1_mask=mod(num1_civ2(ifile)-1,nbslice_mask)+1;
1987                maskname =name_generator(maskbase,num1_mask,1,'.png','_i');
1988            end
1989            switch CivMode
1990                case 'CivX'
1991                    if isunix
1992                        cmd_FIX=[Param.xml.FixBin ' -f ' filecell.nc.civ2{ifile,j} ' -fi1 ' num2str(flagindex2(1)) ...
1993                            ' -fi2 ' num2str(flagindex2(2)) ' -fi3 ' num2str(flagindex2(3)) ...
1994                            ' -threshC ' num2str(thresh_vec2C) ' -threshV ' num2str(thresh_vel2) ' -maskName ' maskname];
1995                    else
1996                        cmd_FIX=['"' Param.xml.FixBin '" -f "' filecell.nc.civ2{ifile,j} '" -fi1 ' num2str(flagindex2(1)) ...
1997                            ' -fi2 ' num2str(flagindex2(2)) ' -fi3 ' num2str(flagindex2(3)) ...
1998                            ' -threshC ' num2str(thresh_vec2C) ' -threshV ' num2str(thresh_vel2) ' -maskName "' maskname '"'];
1999                        cmd_FIX=regexprep(cmd_FIX,'\\','\\\\');
2000                    end
2001                    cmd=[cmd cmd_FIX '\n'];
2002                case 'CivAll'
2003                    fix2.inputFileName=filecell.nc.civ2{ifile,j} ;
2004                    fix2.fi1=num2str(flagindex2(1));
2005                    fix2.fi2=num2str(flagindex2(2));
2006                    fix2.fi3=num2str(flagindex2(3));
2007                    fix2.threshC=num2str(thresh_vec2C);
2008                    fix2.threshV=num2str(thresh_vel2);
2009                    fieldnames=fields(fix2);
2010                    [CivAllxml,uid_fix2]=add(CivAllxml,1,'element','fix2');
2011                    for ilist=1:length(fieldnames)
2012                        val=eval(['fix2.' fieldnames{ilist}]);
2013                        if ischar(val)
2014                            [CivAllxml,uid_t]=add(CivAllxml,uid_fix2,'element',fieldnames{ilist});
2015                            [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
2016                        end
2017                    end
2018                    CivAllCmd=[CivAllCmd ' fix2 '];
2019            end
2020        end
2021       
2022        %CheckPatch2
2023        if Param.CheckPatch2==1
2024            switch CivMode
2025                case 'CivX'
2026                    cmd_PATCH=cmd_patch(filecell.nc.civ2{ifile,j},param.Patch2,Param.xml.PatchBin);
2027                    cmd=[cmd cmd_PATCH '\n'];
2028                case 'CivAll'
2029                    patch2.inputFileName=filecell.nc.civ1{ifile,j} ;
2030                    patch2.nopt=subdomain_patch2;
2031                    patch2.maxdiff=thresh_patch2;
2032                    patch2.ro=rho_patch2;
2033                    test_grid=get(handles.get_gridpatch2,'Value');
2034                    if test_grid
2035                        patch2.gridflag='y';
2036                        gridname=get(handles.grid_patch2,'String');
2037                        if isequal(gridname(end-3:end),'grid')
2038                            nbslice_grid=str2double(gridname(1:end-4)); %
2039                            if ~isnan(nbslice_grid)
2040                                num1_grid=mod(num1_civ2(ifile)-1,nbslice_grid)+1;
2041                                patch2.gridPatch=[filecell.filebase '_' name_generator(gridname,num1_grid,1,'.grid','_i')];
2042                                if ~exist(patch2.gridPatch,'file')
2043                                    msgbox_uvmat('ERROR','grid file absent for patch2')
2044                                end
2045                            elseif exist(gridname,'file')
2046                                patch2.gridPatch=gridname;
2047                            else
2048                                msgbox_uvmat('ERROR','grid file absent for patch2')
2049                            end
2050                        end
2051                    else
2052                        patch2.gridPatch='none';
2053                        patch2.gridflag='n';
2054                        patch2.m=nx_patch2;
2055                        patch2.n=ny_patch2;
2056                    end
2057                    patch2.convectFlow='n';
2058                    fieldnames=fields(patch2);
2059                    [CivAllxml,uid_patch2]=add(CivAllxml,1,'element','patch2');
2060                    for ilist=1:length(fieldnames)
2061                        val=eval(['patch2.' fieldnames{ilist}]);
2062                        if ischar(val)
2063                            [CivAllxml,uid_t]=add(CivAllxml,uid_patch2,'element',fieldnames{ilist});
2064                            [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
2065                        end
2066                    end
2067                    CivAllCmd=[CivAllCmd ' patch2 '];
2068            end
2069        end
2070 
2071        switch CivMode
2072            case {'CivX','CivAll'}
2073                if isequal(CivMode,'CivAll')
2074                    save(CivAllxml,[OutputFile '.xml']);
2075                    cmd=[cmd sparam.CivBin ' -f ' OutputFile '.xml '  CivAllCmd ' >' OutputFile '.log' '\n'];
2076                end
2077               
2078                % create the .bat file used in run or batch
2079                filename_bat=[OutputFile '.bat'];
2080                [fid,message]=fopen(filename_bat,'w');
2081                if isequal(fid,-1)
2082                    msgbox_uvmat('ERROR', ['creation of .bat file: ' message])
2083                    return
2084                end
2085                fprintf(fid,cmd);
2086                fclose(fid);
2087               
2088                if isunix
2089                    system(['chmod +x ' filename_bat]);
2090                end
2091               
2092                batch_file_list{length(batch_file_list)+1}=filename_bat;
2093               
2094%                 if batch
2095%                     switch batch_mode
2096%                         case 'sge'                           
2097%                             display(['!qsub -p ' pvalue ' -q civ.q -e ' flname '.errors -o ' flname '.log' ' ' filename_bat]);
2098%                             eval(  ['!qsub -p ' pvalue ' -q civ.q -e ' flname '.errors -o ' flname '.log' ' ' filename_bat]);
2099%                         case 'oar'
2100% %                             eval(  ['!chmod +x ' filename_bat]);
2101% %                             %eval(  ['!oarsub -n CIVX -l /core=1,walltime=00:10:00  ' filename_bat]);                   
2102% %                             eval(  ['!oarsub -n CIVX -l "/core=1+{type = ''smalljob''}/licence=1,walltime=00:10:00"   ' filename_bat]);
2103%
2104%                     cmd_str=['sh ' filename_bat];
2105%                     super_cmd{length(super_cmd)+1}=cmd_str;
2106%                           
2107%                     end
2108%                 else
2109%                     %% to lauch the jobs locally :
2110%                     if(isunix)
2111%                         cmd_str=['. ' filename_bat];
2112%                     else %case of Windows
2113%                         cmd_str=['@call "' regexprep(filename_bat,'\\','\\\\') '"'];
2114%                     end
2115%                     super_cmd=[super_cmd cmd_str '\n'];
2116%                     disp(cmd_str);
2117%                 end
2118            case 'Matlab'
2119                drawnow
2120                if Param.CheckCiv1==1
2121                    Param.Civ1=Param.Civ1;
2122                end
2123                if Param.CheckFix1==1
2124                    fix1.WarnFlags=[];
2125                    if get(handles.CheckFmin2,'Value')
2126                        fix1.WarnFlags=[fix1.WarnFlags -2];
2127                    end
2128                    if get(handles.CheckF3,'Value')
2129                        fix1.WarnFlags=[fix1.WarnFlags 3];
2130                    end
2131                    fix1.LowerBoundCorr=thresh_vecC1;
2132                    if get(handles.inf_sup1,'Value')
2133                        fix1.UppperBoundVel=thresh_vel1;
2134                    else
2135                        fix1.LowerBoundVel=thresh_vel1;
2136                    end
2137                    if get(handles.CheckMask,'Value')
2138                        fix1.MaskName=maskname;
2139                    end
2140                    Param.Fix1=fix1;
2141                end
2142                if Param.CheckPatch1==1
2143                    if strcmp(compare,'stereo PIV')
2144                        filebase_A=filecell.filebase;
2145                        [pp,ff]=fileparts(filebase_A);
2146                        filebase_B=fullfile(pp,get(handles.RootName_1,'String'));
2147                        RUN_STLIN(filecell.ncA.civ1{ifile,j},filecell.nc.civ1{ifile,j},'civ1',filecell.st{ifile,j},...
2148                            str2num(nx_patch1),str2num(ny_patch1),str2num(thresh_patch1),[filebase_A '.xml'],[filebase_B '.xml'])
2149                    else
2150                        Param.Patch1.Rho=rho_patch1;
2151                        Param.Patch1.Threshold=thresh_patch1;
2152                        Param.Patch1.SubDomain=subdomain_patch1;
2153                    end
2154                end
2155                if Param.CheckCiv2==1
2156                    Param.Civ2=param.civ2;
2157                end
2158                if Param.CheckFix2==1
2159                    fix2.WarnFlags=[];
2160                    if get(handles.vec_Fmin2_2,'Value')
2161                        fix2.WarnFlags=[fix2.WarnFlags -2];
2162                    end
2163                    if get(handles.vec_F4,'Value')
2164                        fix2.WarnFlags=[fix2.WarnFlags 4];
2165                    end
2166                    if get(handles.vec_F3_2,'Value')
2167                        fix2.WarnFlags=[fix2.WarnFlags 3];
2168                    end
2169                    fix2.LowerBoundCorr=thresh_vec2C;
2170                    if get(handles.inf_sup2,'Value')
2171                        fix2.UppperBoundVel=thresh_vel2;
2172                    else
2173                        fix2.LowerBoundVel=thresh_vel2;
2174                    end
2175                    if get(handles.CheckMask,'Value')
2176                        fix2.MaskName=maskname;
2177                    end
2178                    Param.Fix2=fix2;
2179                end
2180                if Param.CheckPatch2==1
2181                    if strcmp(compare,'stereo PIV')
2182                        filebase_A=filecell.filebase;
2183                        [pp,ff]=fileparts(filebase_A);
2184                        filebase_B=fullfile(pp,get(handles.RootName_1,'String'));
2185                        RUN_STLIN(filecell.ncA.civ2{ifile,j},filecell.nc.civ2{ifile,j},'civ2',filecell.st{ifile,j},...
2186                            str2num(nx_patch2),str2num(ny_patch2),str2num(thresh_patch2),[filebase_A '.xml'],[filebase_B '.xml'])
2187                    else
2188                        Param.Patch2.Rho=rho_patch2;
2189                        Param.Patch2.Threshold=thresh_patch2;
2190                        Param.Patch2.SubDomain=subdomain_patch2;
2191                    end
2192                end
2193                if ~strcmp(compare,'stereo PIV')
2194                    [Data,erromsg]=civ_uvmat(Param,filecell.nc.civ1{ifile,j});
2195                    if isempty(errormsg)
2196                        display([filecell.nc.civ1{ifile,j} ' written'])
2197                    else
2198                        msgbox_uvmat('ERROR',errormsg)
2199                    end
2200
2201                end
2202        end
2203    end
2204end
2205
2206if batch
2207    switch batch_mode                       
2208        case 'sge'
2209            for p=1:length(batch_file_list)
2210                cmd=['!qsub -p ' pvalue ' -q civ.q -e ' flname '.errors -o ' flname '.log' ' ' batch_file_list{p}];
2211                display(cmd);eval(cmd);
2212            end
2213        case 'oar_old'
2214                for p=1:length(batch_file_list)
2215                    oar_command=['!oarsub -n CIVX -q nicejob '...
2216                   '-E ' regexprep(batch_file_list{p},'.bat','.errors') ' -O ' regexprep(batch_file_list{p},'.bat','.log ')...
2217                    '-l "/core=1+{type = ''smalljob''}/licence=1,walltime=00:60:00"   ' batch_file_list{p}];
2218                display(oar_command);eval(oar_command);
2219                end
2220       
2221           
2222       case 'oar_perl'
2223                filename_joblist=fullfile(Rootbat,'job_list.txt');
2224                fid=fopen(filename_joblist,'w');
2225
2226                for p=1:length(batch_file_list)
2227                    oar_command=['oarsub -n CIVX '...
2228                        '-l "/core=1,walltime=00:10:00"   ' batch_file_list{p}];
2229                    fprintf(fid,[oar_command '\n']);
2230                end
2231                fclose(fid)
2232                oar_command=['oarsub -t container -n civx-container -l /core=24,walltime=00:35:00 "/fsnet/data/legi/calcul9/home/gostiaux/Documents/MATLAB/uvmat_dev/oar-dispatch -f '...
2233                    filename_joblist '"'];
2234                filename_oarcommand=fullfile(Rootbat,'oar_command');
2235                fid=fopen(filename_oarcommand,'w');
2236                fprintf(fid,[oar_command '\n']);
2237                fclose(fid);
2238%                 display(oar_command);
2239                eval(['! . ' filename_oarcommand]);
2240
2241        case 'oar'%'oar_mpi'
2242                filename_joblist=fullfile(Rootbat,'job_list.txt');
2243                fid=fopen(filename_joblist,'w');
2244
2245                for p=1:length(batch_file_list)
2246                    fprintf(fid,[batch_file_list{p} '\n']);
2247                end
2248                fclose(fid)
2249               
2250               
2251%                 oarsub -S ./oar.sub
2252
2253               
2254               
2255
2256               
2257            %             for p=0:floor(length(batch_file_list)/6);               
2258%                 filename_batch_group=fullfile(Rootbat,['job_list_' num2str(p) '.bat']);
2259%                 fid=fopen(filename_batch_group,'w');
2260%                 if fid==-1
2261%                     msgbox_uvmat('ERROR',['cannot create the command file ' filename_superbat])
2262%                     return
2263%                 end
2264%                 if p==floor(length(batch_file_list)/6)
2265%                     kmax=mod(length(batch_file_list),6);
2266%                 else
2267%                     kmax=6;
2268%                 end
2269%                 for k=1:kmax
2270%                     fprintf(fid,['sh ' batch_file_list{p*6+k} '\n']);
2271%                 end
2272%                 fclose(fid);
2273%                 system(['chmod +x ' filename_batch_group]);
2274%                 eval(  ['!oarsub -n CIVX -q nicejob '...
2275%                     '-E '  flname '.errors -O ' flname '.log '...
2276%                     '-l "/core=1+{type = ''smalljob''}/licence=1,walltime=00:60:00"   ' filename_batch_group]);
2277%             end
2278        case 'oar_new' % to be develloped with Patrick Begou
2279                filename_joblist=fullfile(Rootbat,'job_list.txt');
2280                fid=fopen(filename_superbat,'w');
2281                if fid==-1
2282                    msgbox_uvmat('ERROR',['cannot create the command file ' filename_superbat])
2283                    return
2284                end
2285                for p=1:length(batch_file_list)
2286                    fprintf(fid,[batch_file_list{p} '\n']);
2287                end
2288                fclose(fid);
2289                walltime=datestr(length(super_cmd)*10/24/60,13);
2290                eval(  ['!oarsub -n CIVX -q nicejob -l "/core=1+{type = ''smalljob''}/licence=1,walltime=' walltime '"   ' filename_superbat]);
2291    end
2292else
2293    if ~isequal(CivMode,'Matlab')
2294        filename_superbat=fullfile(Rootbat,'job_list.bat');
2295        fid=fopen(filename_superbat,'w');
2296        if fid==-1
2297            msgbox_uvmat('ERROR',['cannot create the command file ' filename_superbat])
2298            return
2299        end
2300        for p=1:length(batch_file_list)
2301            if isunix
2302                fprintf(fid,['sh ' batch_file_list{p} '\n']);
2303            else
2304                fprintf(fid,['@call "' regexprep(filename_bat,'\\','\\\\') '"' '\n']);
2305            end
2306        end
2307        fclose(fid);
2308        if(isunix)
2309            system(['chmod +x ' filename_superbat]);
2310        end
2311        system([filename_superbat ' &']);% execute main commmand
2312    end
2313   
2314end
2315
2316
2317%% save interface state
2318if isfield(filecell,'nc')
2319    if isfield(filecell.nc,'civ2')
2320        fileresu=filecell.nc.civ2{1,1};
2321    else
2322        fileresu=filecell.nc.civ1{1,1};
2323    end
2324end
2325[RootPath,RootFile,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fileresu);
2326namedoc=fullfile(RootPath,subdir,RootFile);
2327detect=1;
2328while detect==1
2329    namefigfull=[namedoc '.fig'];
2330    hh=dir(namefigfull);
2331    if ~isempty(hh)
2332        detect=1;
2333        namedoc=[namedoc '.0'];
2334    else
2335        detect=0;
2336    end
2337end
2338saveas(gcbf,namefigfull);%save the interface with name namefigfull (A CHANGER EN FICHIER  .xml)
2339
2340%Save info in personal profile (initiate browser next time) TODO
2341MenuFile={};
2342dir_perso=prefdir;
2343profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
2344if exist(profil_perso,'file')
2345    hh=load (profil_perso);
2346      if isfield(hh,'MenuFile')
2347          MenuFile=hh.MenuFile;
2348      end
2349      MenuFile=[filecell.ima1.civ1(1,1); MenuFile];
2350      save (profil_perso,'MenuFile','-append'); %store the file names for future opening of uvmat
2351else
2352    MenuFile=filecell.ima1.civ1(1,1);
2353    save (profil_perso,'MenuFile')
2354end
2355
2356
2357%------------------------------------------------------------------------
2358% --- determine the list of reference indices of processing file
2359function [ref_i,ref_j,errormsg]=find_ref_indices(handles)
2360%------------------------------------------------------------------------
2361errormsg=''; %default error message
2362first_i=str2double(get(handles.first_i,'String'));%first index i
2363last_i=str2double(get(handles.last_i,'String'));%last index i
2364incr_i=str2double(get(handles.incr_i,'String'));% increment
2365if isequal(get(handles.first_j,'Visible'),'on')
2366    first_j=str2double(get(handles.first_j,'String'));%first index j
2367    last_j=str2double(get(handles.last_j,'String'));%last index j
2368    incr_j=str2double(get(handles.incr_j,'String'));% increment
2369else
2370    first_j=1;
2371    last_j=1;
2372    incr_j=1;
2373end
2374ref_i=first_i:incr_i:last_i;% list of i indices (reference values for each pair)
2375ref_j=first_j:incr_j:last_j;% list of j indices (reference values for each pair)
2376if isnan(first_i)||isnan(first_j)
2377    errormsg='first field number not defined';
2378elseif isnan(last_i)||isnan(last_j)
2379    errormsg='last field number not defined';
2380elseif isnan(incr_i)||isnan(incr_j)
2381    errormsg='increment in field number not defined';
2382elseif last_i < first_i || last_j < first_j
2383    errormsg='last field number must be larger than the first one';
2384end
2385
2386%------------------------------------------------------------------------
2387% --- determine the list of filenames and indices needed for launch_job
2388function [filecell,num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2,nom_type_nc,file_ref_fix1,file_ref_fix2,compare]=...
2389    set_civ_filenames(handles,ref_i,ref_j,checkbox)
2390%------------------------------------------------------------------------
2391filecell=[];%default
2392
2393%% get the root names nomenclature and numbers
2394filebase=get(handles.RootName,'String');
2395
2396if isempty(filebase)||isequal(filebase,'')
2397    msgbox_uvmat('ERROR','no input files')
2398    return
2399end
2400
2401%filebase=regexprep(filebase,'\.fsnet','fsnet');% temporary fix for cluster Coriolis
2402filecell.filebase=filebase;
2403
2404browse=get(handles.RootName,'UserData');
2405compare_list=get(handles.ListCompareMode,'String');
2406val=get(handles.ListCompareMode,'Value');
2407compare=compare_list{val};
2408if strcmp(compare,'displacement')
2409    mode='displacement';
2410else
2411    mode_list=get(handles.ListPairMode,'String');
2412    mode_value=get(handles.ListPairMode,'Value');
2413    mode=mode_list{mode_value};
2414end
2415%time=get(handles.RootName,'UserData'); %get the set of times
2416ext_ima=get(handles.ImaExt,'String');
2417nom_type_nc=browse.nom_type_nc;
2418if isfield(browse,'nom_type_ima')
2419    nom_type_ima2=browse.nom_type_ima;
2420end
2421if isempty(nom_type_ima2),nom_type_ima2='1';end; %default
2422if isempty(nom_type_nc),nom_type_nc='_i1-i2';end; %default
2423[num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2]=...
2424    find_pair_indices(handles,ref_i,ref_j,mode);
2425%determine the new filebase for 'displacement' ListPairMode (comparison of two series)
2426filebase_B=filebase;% root name of the second field series for stereo
2427if strcmp(compare,'displacement') || strcmp(compare,'stereo PIV')
2428%     test_disp=1;
2429    nom_type_ima1=browse.nom_type_ima_1; %nomenclature type of the second file series
2430    [Path2,Name2]=fileparts(filebase_B);
2431    Path1=Path2;
2432    Name1=get(handles.RootName_1,'String');% root name of the first field series for stereo
2433    filebase_A=fullfile(Path1,Name1);
2434    if length(Name1)>6
2435        Name1=Name1(end-5:end);
2436    end
2437    if length(Name2)>6
2438        Name2=Name2(end-5:end);
2439    end
2440    filebase_AB=fullfile(Path2,[Name2 '-' Name1]);
2441else
2442%     test_disp=0;
2443    filebase_A=filebase;
2444    nom_type_ima1=nom_type_ima2;
2445    filebase_AB=filebase;
2446end
2447if strcmp(compare,'displacement')
2448    filebase_ima1=filebase_A;
2449    filebase_ima2=filebase_B;
2450    filebase_nc=filebase_AB; %root name for the result of civ2
2451else
2452    filebase_ima1=filebase_B;
2453    filebase_ima2=filebase_B;
2454    filebase_nc=filebase_B;
2455end
2456
2457%determine reference files for fix:
2458file_ref_fix1={};%default
2459file_ref_fix2={};
2460nbfield=length(num1_civ1);
2461nbslice=length(num_a_civ1);
2462if checkbox(2)==1% fix1 performed
2463    ref=get(handles.ref_fix1,'UserData');%read data on the ref file stored by get_ref_fix1_Callback
2464    if ~isempty(ref)
2465        first_i=str2double(get(handles.first_i,'String'));
2466        last_i=str2double(get(handles.last_i,'String'));
2467        incr_i=str2double(get(handles.incr_i,'String'));
2468        first_j=str2double(get(handles.first_j,'String'));
2469        last_j=str2double(get(handles.last_j,'String'));
2470        incr_j=str2double(get(handles.incr_j,'String'));
2471        num_i_ref=first_i:incr_i:last_i;
2472        num_j_ref=first_j:incr_j:last_j;
2473        if isequal(mode,'displacement')
2474            num_i1=num_i_ref;
2475            num_i2=num_i_ref;
2476            num_j1=num_j_ref;
2477            num_j2=num_j_ref;
2478        elseif isequal(mode,'pair j1-j2')% isequal(mode,'st_pair j1-j2')
2479            num_i1=num_i_ref;
2480            num_i2=num_i1;
2481            num_j1=ref.num_a*ones(size(num_i_ref));
2482            num_j2=ref.num_b*ones(size(num_i_ref));
2483        elseif isequal(mode,'series(Di)') % isequal(mode,'st_series(Di)')
2484            delta1=floor((ref.num2-ref.num1)/2);
2485            delta2=ceil((ref.num2-ref.num1)/2);
2486            num_i1=num_i_ref-delta1*ones(size(num_i_ref));
2487            num_i2=num_i_ref+delta2*ones(size(num_i_ref));
2488            if isempty(ref.num_a)
2489                ref.num_a=1;
2490            end
2491            num_j1=ref.num_a*ones(size(num_i1));
2492            num_j2=num_j1;
2493        elseif isequal(mode,'series(Dj)')%| isequal(mode,'st_series(Dj)')
2494            delta1=floor((ref.num_b-ref.num_a)/2);
2495            delta2=ceil((ref.num_b-ref.num_a)/2);
2496            num_i1=ref.num1*ones(size(num_i_ref));
2497            num_i2=num_i1;
2498            num_j1=num_j_ref-delta1*ones(size(num_j_ref));
2499            num_j2=num_j_ref+delta2*ones(size(num_j_ref));
2500        end
2501        for ifile=1:nbfield
2502            for j=1:nbslice
2503                file_ref=name_generator(ref.filebase,num_i1(ifile),num_j1(j),'.nc',ref.nom_type,1,num_i2(ifile),num_j2(j),ref.subdir);%
2504                file_ref_fix1(ifile,j)={file_ref};
2505                if ~exist(file_ref,'file')
2506                    msgbox_uvmat('ERROR',['reference file ' file_ref ' not found for fix1'])
2507                    filecell=[];
2508                    return
2509                end
2510            end
2511        end
2512    end
2513end
2514
2515%determine reference files for checkfix2:
2516if checkbox(5)==1% fix2 performed
2517    ref=get(handles.ref_fix2,'UserData');
2518    if ~isempty(ref)
2519        first_i=str2double(get(handles.first_i,'String'));
2520        last_i=str2double(get(handles.last_i,'String'));
2521        incr_i=str2double(get(handles.incr_i,'String'));
2522        first_j=str2double(get(handles.first_j,'String'));
2523        last_j=str2double(get(handles.last_j,'String'));
2524        incr_j=str2double(get(handles.incr_j,'String'));
2525        num_i_ref=first_i:incr_i:last_i;
2526        num_j_ref=first_j:incr_j:last_j;
2527        if isequal(mode,'displacement')
2528            num_i1=num_i_ref;
2529            num_i2=num_i_ref;
2530            num_j1=num_j_ref;
2531            num_j2=num_j_ref;
2532        elseif isequal(mode,'pair j1-j2')
2533            num_i1=num_i_ref;
2534            num_i2=num_i1;
2535            num_j1=ref.num_a;
2536            num_j2=ref.num_b;
2537        elseif isequal(mode,'series(Di)')
2538            delta1=floor((ref.num2-ref.num1)/2);
2539            delta2=ceil((ref.num2-ref.num1)/2);
2540            num_i1=num_i_ref-delta1*ones(size(num_i_ref));
2541            num_i2=num_i_ref+delta2*ones(size(num_i_ref));
2542            num_j1=ref.num_a*ones(size(num_i1));
2543            num_j2=num_j1;
2544        elseif isequal(mode,'series(Dj)')
2545            delta1=floor((ref.num_b-ref.num_a)/2);
2546            delta2=ceil((ref.num_b-ref.num_a)/2);
2547            num_i1=ref.num1*ones(size(num_i_ref));
2548            num_i2=num_i1;
2549            num_j1=num_j_ref-delta1*ones(size(num_j_ref));
2550            num_j2=num_j_ref+delta2*ones(size(num_j_ref));
2551        end
2552        for ifile=1:nbfield
2553            for j=1:nbslice
2554                file_ref=name_generator(ref.filebase,num_i1(ifile),num_j1(j),'.nc',ref.nom_type,1,num_i2(ifile),num_j2(j),ref.subdir);%
2555                file_ref_fix2(ifile,j)={file_ref};
2556                if ~exist(file_ref,'file')
2557                    msgbox_uvmat('ERROR',['reference file ' file_ref ' not found for fix2'])
2558                    filecell={};
2559                    return
2560                end
2561            end
2562        end
2563    end
2564end
2565
2566%check dir
2567subdir_civ1=get(handles.subdir_civ1,'String');%subdirectory subdir_civ1 for the netcdf output data
2568subdir_civ2=get(handles.subdir_civ2,'String');
2569if isequal(subdir_civ1,''),subdir_civ1='CIV'; end% put default subdir
2570if isequal(subdir_civ2,''),subdir_civ2=subdir_civ1; end% put default subdir
2571% currentdir=pwd;%store the current working directory
2572[Path_ima,Name]=fileparts(filebase);%Path of the image files (.civ)
2573if ~exist(Path_ima,'dir')
2574    msgbox_uvmat('ERROR',['path to images ' Path_ima ' not found'])
2575    filecell={};
2576    return
2577end
2578[xx,message]=fileattrib(Path_ima);
2579if ~isempty(message) && ~isequal(message.UserWrite,1)
2580    msgbox_uvmat('ERROR',['No writting access to ' Path_ima])
2581    filecell={};
2582%     cd(currentdir);
2583    return
2584end
2585
2586%check the existence of the netcdf and image files involved
2587% %%%%%%%%%%%%  case CheckCiv1 activated   %%%%%%%%%%%%%
2588if checkbox(1)==1;
2589    detect=1;
2590    vers=0;
2591    subdir_civ1_new=subdir_civ1;
2592    while detect==1 %create a new subdir if the netcdf files already exist
2593        for ifile=1:nbfield
2594            for j=1:nbslice
2595                filename=name_generator(filebase_nc,num1_civ1(ifile),num_a_civ1(j),'.nc',nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1_new);
2596                detect=exist(filename,'file')==2;
2597                if detect% if a netcdf file already exists
2598                    indstr=regexp(subdir_civ1_new,'\D');
2599                    if indstr(end)<length(subdir_civ1_new) %subdir_civ1 ends by a number
2600                        vers=str2double(subdir_civ1_new(indstr(end)+1:end))+1;
2601                        subdir_civ1_new=[subdir_civ1_new(1:indstr(end)) num2str(vers)];
2602                    else
2603                        vers=vers+1;
2604                        subdir_civ1_new=[subdir_civ1_new(1:indstr(end)) '_' num2str(vers)];       
2605                    end
2606                    subdir_civ2=subdir_civ1_new;
2607                    break
2608                end
2609                filecell.nc.civ1(ifile,j)={filename};
2610            end
2611            if detect% if a netcdf file already exists
2612                break
2613            end
2614        end
2615 
2616        %create the new subdir_civ1
2617        if ~exist(fullfile(Path_ima,subdir_civ1_new),'dir')
2618%             cd(Path_ima);         
2619            [xx,msg1]=mkdir(fullfile(Path_ima,subdir_civ1_new));
2620
2621            if ~strcmp(msg1,'')
2622                msgbox_uvmat('ERROR',['cannot create ' subdir_civ1_new ': ' msg1])%error message for directory creation
2623                filecell={};
2624                return
2625            elseif isunix         
2626                [xx,msg2] = fileattrib(fullfile(Path_ima,subdir_civ1_new),'+w','g'); %yield writing access (+w) to user group (g)
2627                if ~strcmp(msg2,'')
2628                    msgbox_uvmat('ERROR',['pb of permission for  ' fullfile(Path_ima,subdir_civ1_new) ': ' msg2])%error message for directory creation
2629                    filecell={};
2630                    return
2631                end
2632            end
2633%             cd(currentdir);
2634        end
2635        if strcmp(compare,'stereo PIV')&&(strcmp(mode,'pair j1-j2')||strcmp(mode,'series(Dj)')||strcmp(mode,'series(Di)'))%check second nc series
2636            for ifile=1:nbfield
2637                for j=1:nbslice
2638                    filename=name_generator(filebase_A,num1_civ1(ifile),num_a_civ1(j),'.nc',nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1_new);%
2639                    detect=exist(filename,'file')==2;
2640                    if detect% if a netcdf file already exists
2641                       indstr=regexp(subdir_civ1_new,'\D');
2642                       if indstr(end)<length(subdir_civ1_new) %subdir_civ1 ends by a number
2643                           vers=str2double(subdir_civ1_new(indstr(end)+1:end))+1;
2644                           subdir_civ1_new=[subdir_civ1_new(1:indstr(end)) num2str(vers)];
2645                       else
2646                           vers=vers+1;
2647                           subdir_civ1_new=[subdir_civ1_new '_' num2str(vers)];
2648                       end
2649                       subdir_civ2=subdir_civ1;
2650                       break
2651                    end
2652                    filecell.ncA.civ1(ifile,j)={filename};
2653                end
2654                if detect% if a netcdf file already exists
2655                    break
2656                end
2657            end
2658            %create the new subdir_civ1
2659            if ~exist(fullfile(Path_ima,subdir_civ1_new),'dir')
2660%                    cd(Path_ima);         
2661                [xx,msg1]=mkdir(fullfile(Path_ima,subdir_civ1_new));
2662%                             cd(currentdir);
2663                if ~strcmp(msg1,'')
2664                    msgbox_uvmat('ERROR',['cannot create ' subdir_civ1_new ': ' msg1])
2665%                     cd(currentdir)
2666                    filecell={};
2667                    return
2668                else
2669                    [xx,msg2] = fileattrib(fullfile(Path_ima,subdir_civ1_new),'+w','g'); %yield writing access (+w) to user group (g)
2670                    if ~strcmp(msg2,'')
2671                        msgbox_uvmat('ERROR',['pb of permission for ' subdir_civ1_new ': ' msg2])%error message for directory creation
2672%                         cd(currentdir)
2673                        filecell={};
2674                        return
2675                    end
2676                end
2677            end
2678        end
2679    end
2680    subdir_civ1=subdir_civ1_new;
2681    % get image names
2682    for ifile=1:nbfield
2683        for j=1:nbslice
2684            filename=name_generator(filebase_ima1, num1_civ1(ifile),num_a_civ1(j),ext_ima,nom_type_ima1);
2685            idetect(j)=exist(filename,'file')==2;
2686            filecell.ima1.civ1(ifile,j)={filename}; %first image
2687            filename=name_generator(filebase_ima2, num2_civ1(ifile),num_b_civ1(j),ext_ima,nom_type_ima2);
2688            idetect_1(j)=exist(filename,'file')==2;
2689            filecell.ima2.civ1(ifile,j)={filename};%second image
2690        end
2691        [idetectmin,indexj]=min(idetect);
2692        if idetectmin==0,
2693            msgbox_uvmat('ERROR',[filecell.ima1.civ1{ifile,indexj} ' not found'])
2694            filecell={};
2695           % cd(currentdir)
2696            return
2697        end
2698        [idetectmin,indexj]=min(idetect_1);
2699        if idetectmin==0,
2700            msgbox_uvmat('ERROR',[filecell.ima2.civ1{ifile,indexj} ' not found'])
2701            filecell={};
2702            %cd(currentdir)
2703            return
2704        end
2705    end
2706    if strcmp(compare,'stereo PIV') && (strcmp(mode,'pair j1-j2') || strcmp(mode,'series(Dj)') || strcmp(mode,'series(Di)'))
2707        for ifile=1:nbfield
2708            for j=1:nbslice
2709                filename=name_generator(filebase_A, num1_civ1(ifile),num_a_civ1(j),ext_ima,nom_type_ima1);
2710                idetect(j)=exist(filename,'file')==2;
2711                filecell.imaA1.civ1(ifile,j)={filename} ;%first image
2712                filename=name_generator(filebase_A, num2_civ1(ifile),num_b_civ1(j),ext_ima,nom_type_ima2);
2713                idetect_1(j)=exist(filename,'file')==2;
2714                filecell.imaA2.civ1(ifile,j)={filename};%second image
2715            end
2716            [idetectmin,indexj]=min(idetect);
2717            if idetectmin==0,
2718                msgbox_uvmat('ERROR',[filecell.imaA1.civ1{ifile,indexj} ' not found'])
2719                filecell={};
2720               % cd(currentdir)
2721                return
2722            end
2723            [idetectmin,indexj]=min(idetect_1);
2724            if idetectmin==0,
2725                msgbox_uvmat('ERROR',[filecell.imaA2.civ1{ifile,indexj} ' not found'])
2726                filecell={};
2727               % cd(currentdir)
2728                return
2729            end
2730        end
2731    end
2732   
2733    %%%%%%%%%%%%%  checkfix1 or checkpatch1 activated but no checkciv1   %%%%%%%%%%%%%
2734elseif (checkbox(2)==1 || checkbox(3)==1);
2735    for ifile=1:nbfield
2736        for j=1:nbslice
2737            filename=name_generator(filebase_nc,num1_civ1(ifile),num_a_civ1(j),'.nc',...
2738                nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
2739            detect=exist(filename,'file')==2;
2740            if detect==0
2741                msgbox_uvmat('ERROR',[filename ' not found'])
2742                filecell={};
2743               % cd(currentdir)
2744                return
2745            end
2746            filecell.nc.civ1(ifile,j)={filename};
2747        end
2748    end
2749    if strcmp(compare,'stereo PIV')
2750        for ifile=1:nbfield
2751            for j=1:nbslice
2752                filename=name_generator(filebase_A,num1_civ1(ifile),num_a_civ1(j),'.nc',nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
2753                filecell.ncA.civ1(ifile,j)={filename};
2754                if ~exist(filename,'file')
2755                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2756                    set(handles.RUN, 'Enable','On')
2757                    set(handles.RUN,'BackgroundColor',[1 0 0])
2758                    filecell={};
2759                    %cd(currentdir)
2760                    return
2761                end
2762            end
2763        end
2764    end
2765end
2766
2767%%%%%%%%%%%%%  if checkciv2 performed with pairs different than checkciv1  %%%%%%%%%%%%%
2768testdiff=0;
2769if (checkbox(4)==1)&&...
2770        ((get(handles.ListPairCiv1,'Value')~=get(handles.ListPairCiv2,'Value'))||~strcmp(subdir_civ2,subdir_civ1))
2771    testdiff=1;
2772    detect=1;
2773    vers=0;
2774    subdir_civ2_new=subdir_civ2;
2775    while detect==1 %create a new subdir if the netcdf files already exist
2776        for ifile=1:nbfield
2777            for j=1:nbslice
2778                filename=name_generator(filebase_nc,num1_civ2(ifile),num_a_civ2(j),'.nc',nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2_new);%
2779                detect=exist(filename,'file')==2;
2780                if detect% if a netcdf file already exists
2781                    indstr=regexp(subdir_civ2,'\D');
2782                    if indstr(end)<length(subdir_civ2) %subdir_civ1 ends by a number
2783                        vers=str2double(subdir_civ2(indstr(end)+1:end))+1;
2784                        subdir_civ2_new=[subdir_civ2(1:indstr(end)) num2str(vers)];
2785                    else
2786                        vers=vers+1;
2787                        subdir_civ2_new=[subdir_civ1 '_' num2str(vers)];
2788                    end
2789                    break
2790                end
2791                filecell.nc.civ2(ifile,j)={filename};
2792            end
2793            if detect% if a netcdf file already exists
2794                break
2795            end
2796        end
2797        %create the new subdir_civ2_new
2798        if ~exist(fullfile(Path_ima,subdir_civ2_new),'dir')
2799            [xx,m2]=mkdir(fullfile(Path_ima,subdir_civ2_new));
2800            [xx,msg2] = fileattrib(fullfile(Path_ima,subdir_civ2_new),'+w','g'); %yield writing access (+w) to user group (g)
2801            if ~isequal(m2,'')
2802                msgbox_uvmat('ERROR',['cannot create ' fullfile(Path_ima,subdir_civ2_new) ': ' m2])
2803                filecell={};
2804               % cd(currentdir)
2805                return
2806            end
2807        end
2808        if strcmp(compare,'stereo PIV')%check second nc series
2809            for ifile=1:nbfield
2810                for j=1:nbslice
2811                    filename=name_generator(filebase_A,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2812                        nom_type_nc,1,num2_civ2(ifile),num_b_civ1(j),subdir_civ2_new);%
2813                    detect=exist(filename,'file')==2;
2814                    if detect% if a netcdf file already exists
2815                        indstr=regexp(subdir_civ2,'\D');
2816                        if indstr(end)<length(subdir_civ2) %subdir_civ1 ends by a number
2817                           vers=str2double(subdir_civ2(indstr(end)+1:end))+1;
2818                           subdir_civ2_new=[subdir_civ2(1:indstr(end)) num2str(vers)];
2819                        else
2820                           vers=vers+1;
2821                           subdir_civ2_new=[subdir_civ1 '_' num2str(vers)];
2822                        end
2823                        break
2824                    end
2825                    filecell.ncA.civ2(ifile,j)={filename};
2826                end
2827                if detect% if a netcdf file already exists
2828                    break
2829                end
2830            end
2831            subdir_civ2=subdir_civ2_new;
2832            %create the new subdir_civ1
2833            if ~exist(fullfile(Path_ima,subdir_civ2_new),'dir')
2834                [xx,m2]=mkdir(subdir_civ2_new);
2835                 [xx,msg2] = fileattrib(fullfile(Path_ima,subdir_civ2_new),'+w','g'); %yield writing access (+w) to user group (g)
2836                if ~isequal(m2,'')
2837                    msgbox_uvmat('ERROR', ['cannot create ' fullfile(Path_ima,subdir_civ2_new) ': ' m2])%error message for directory creation
2838                  %  cd(currentdir)
2839                    filecell={};
2840                    return
2841                end
2842            end
2843        end
2844    end
2845    subdir_civ2=subdir_civ2_new;
2846end
2847%cd(currentdir);%come back to the current working directory
2848
2849%%%%%%%%%%%%%  if checkciv2 results are obtained or used  %%%%%%%%%%%%%
2850if checkbox(4)==1 || checkbox(5)==1 || checkbox(6)==1 %civ2
2851    %check source netcdf file of checkciv1 estimates
2852    if checkbox(1)==0; %no civ1 performed
2853        for ifile=1:nbfield
2854            for j=1:nbslice
2855                filename=name_generator(filebase_nc,num1_civ1(ifile),num_a_civ1(j),'.nc',...
2856                    nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
2857                filecell.nc.civ1(ifile,j)={filename};% name of the civ1 file
2858                if ~exist(filename,'file')
2859                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2860                    filecell={};
2861                    return
2862                end
2863                if ~testdiff % civ2 or patch2 are written in the same file as civ1
2864                    if checkbox(4)==0 ; %check the existence of civ2 if it is not calculated
2865                        Data=nc2struct(filename,'ListGlobalAttribute','CivStage','civ2');
2866                        if ~isempty(Data.CivStage) && Data.CivStage<4 %test for civ files
2867                            msgbox_uvmat('ERROR',['no civ2 data in ' filename])
2868                            filecell=[];
2869                            return
2870                        elseif isempty(Data.civ2)||isequal(Data.civ2,0)
2871                            msgbox_uvmat('ERROR',['no civ2 data in ' filename])
2872                            filecell=[];
2873                            return
2874                        end
2875                    elseif checkbox(3)==0; %check the existence of patch if it is not calculated
2876                        Data=nc2struct(filename,'ListGlobalAttribute','CivStage','patch');
2877                        if ~isempty(Data.CivStage)
2878                            if Data.CivStage<3 %test for civ files
2879                                msgbox_uvmat('ERROR',['no patch data in ' filename])
2880                                filecell=[];
2881                                return
2882                            end
2883                        elseif isempty(Data.patch)||isequal(Data.patch,0)
2884                            msgbox_uvmat('ERROR',['no patch data in ' filename])
2885                            filecell=[];
2886                            return
2887                        end
2888                    end
2889                end
2890            end
2891        end
2892        if strcmp(compare,'stereo PIV')
2893            for ifile=1:nbfield
2894                for j=1:nbslice
2895                    filename=name_generator(filebase_A,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2896                        nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
2897                    filecell.ncA.civ2(ifile,j)={filename};
2898                    if ~exist(filename,'file')
2899                        msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2900                        set(handles.RUN, 'Enable','On')
2901                        set(handles.RUN,'BackgroundColor',[1 0 0])
2902                        return
2903                    end
2904                end
2905            end
2906        end
2907    end
2908   
2909    detect=1;
2910    %     while detect==1%creates a new subdir if the netcdf files already contain checkciv2 data
2911    for ifile=1:nbfield
2912        for j=1:nbslice
2913            filename=name_generator(filebase_nc,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2914                nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);
2915            detect=exist(filename,'file')==2;
2916            filecell.nc.civ2(ifile,j)={filename};
2917        end
2918    end
2919    %get first image names for checkciv2
2920    if checkbox(1)==1 && isequal(num1_civ1,num1_civ2) && isequal(num_a_civ1,num_a_civ2)
2921        filecell.ima1.civ2=filecell.ima1.civ1;
2922    elseif checkbox(4)==1
2923        for ifile=1:nbfield
2924            for j=1:nbslice
2925                filename=name_generator(filebase_ima1, num1_civ2(ifile),num_a_civ2(j),ext_ima,nom_type_ima1);
2926                idetect_2(j)=exist(filename,'file')==2;
2927                filecell.ima1.civ2(ifile,j)={filename};%first image
2928            end
2929            [idetectmin,indexj]=min(idetect_2);
2930            if idetectmin==0,
2931                msgbox_uvmat('ERROR',['input image ' filecell.ima1.civ2{ifile,indexj} ' not found'])
2932                filecell=[];
2933                return
2934            end
2935        end
2936    end
2937   
2938    %get second image names for checkciv2
2939    if checkbox(1)==1 && isequal(num2_civ1,num2_civ2) && isequal(num_b_civ1,num_b_civ2)
2940        filecell.ima2.civ2=filecell.ima2.civ1;
2941    elseif checkbox(4)==1
2942        for ifile=1:nbfield
2943            for j=1:nbslice
2944                filename=name_generator(filebase_ima2, num2_civ2(ifile),num_b_civ2(j),ext_ima,nom_type_ima2);
2945                idetect_3(j)=exist(filename,'file')==2;
2946                filecell.ima2.civ2(ifile,j)={filename};%first image
2947            end
2948            [idetectmin,indexj]=min(idetect_3);
2949            if idetectmin==0,
2950                msgbox_uvmat('ERROR',['input image ' filecell.ima2.civ2{ifile,indexj} ' not found'])
2951                filecell=[];
2952                return
2953            end
2954        end
2955    end
2956end
2957if (checkbox(5) || checkbox(6)) && ~checkbox(4)  % need to read an existing netcdf civ2 file
2958    if ~testdiff
2959        filecell.nc.civ2=filecell.nc.civ1;% file already checked
2960    else     % check the civ2 files
2961        for ifile=1:nbfield
2962            for j=1:nbslice
2963                filename=name_generator(filebase_nc,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2964                    nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
2965                filecell.nc.civ2(ifile,j)={filename};
2966                if ~exist(filename,'file')
2967                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2968                    filecell=[];
2969                    return
2970                else
2971                    Data=nc2struct(filename,'ListGlobalAttribute','CivStage','civ2');
2972                    if ~isempty(Data.CivStage) && Data.CivStage<4 %test for civ files
2973                            msgbox_uvmat('ERROR',['no civ2 data in ' filename])
2974                            filecell=[];
2975                            return
2976                    elseif isempty(Data.civ2)||isequal(Data.civ2,0)
2977                        msgbox_uvmat('ERROR',['no civ2 data in ' filename])
2978                        filecell=[];
2979                        return
2980                    end
2981                end
2982            end
2983        end
2984    end
2985end
2986
2987%%%%%%%%%%%%%  if stereo fields are calculated by PATCH %%%%%%%%%%%%%
2988if strcmp(compare,'stereo PIV')
2989    if  checkbox(3) && isequal(get(handles.test_stereo1,'Value'),1)
2990        for ifile=1:nbfield
2991            for j=1:nbslice
2992                filename=name_generator(filebase_AB,num1_civ1(ifile),num_a_civ1(j),'.nc',...
2993                    nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
2994                filecell.st(ifile,j)={filename};
2995            end
2996        end
2997    end
2998    if  checkbox(6) && isequal(get(handles.CheckStereo,'Value'),1)
2999        for ifile=1:nbfield
3000            for j=1:nbslice
3001                filename=name_generator(filebase_AB,num1_civ2(ifile),num_a_civ2(j),'.nc',...
3002                    nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
3003                filecell.st(ifile,j)={filename};
3004            end
3005        end
3006    end
3007end
3008set(handles.subdir_civ1,'String',subdir_civ1);%update the edit box
3009set(handles.subdir_civ2,'String',subdir_civ2);%update the edit box
3010browse.nom_type_nc=nom_type_nc;
3011set(handles.RootName,'UserData',browse); %update the nomenclature type for uvmat
3012
3013
3014%COPY IMAGES TO THE FORMAT .png IF NEEDED
3015if isequal(nom_type_ima1,'*')%case of movie files
3016    nom_type_imanew1='_i';
3017else
3018    nom_type_imanew1=nom_type_ima1;
3019end
3020if isequal(nom_type_ima2,'*')%case of movie files
3021    nom_type_imanew2='_i';
3022else
3023    nom_type_imanew2=nom_type_ima2;
3024end
3025if ~isequal(ext_ima,'.png')
3026    %%type of image file
3027    type_ima1='none';%default
3028    movieobject1=[];%default
3029    if strcmpi(ext_ima,'.avi')
3030        hhh=which('mmreader');
3031        if ~isequal(hhh,'')&& mmreader.isPlatformSupported()% if the mmreader function is found (recent version of matlab)
3032            type_ima1='movie';
3033            movieobject1=mmreader([filebase_ima2 ext_ima]);
3034        else
3035            type_ima1='avi';
3036        end
3037    elseif ischar(ext_ima) && ~isempty(ext_ima(2:end))
3038        form=imformats(ext_ima(2:end));
3039        if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
3040            if isequal(nom_type_ima1,'*');
3041                type_ima1='multimage';%image series in a single image file
3042            else
3043                type_ima1='image';
3044            end
3045        end
3046    end
3047    type_ima2='none';%default
3048    movieobject2=[];
3049    if strcmpi(ext_ima,'.avi')
3050        hhh=which('mmreader');
3051        if ~isequal(hhh,'')&& mmreader.isPlatformSupported()% if the mmreader function is found (recent version of matlab)
3052            type_ima2='movie';
3053            movieobject2=mmreader([filebase_ima2 ext_ima]);
3054        else
3055            type_ima2='avi';
3056        end
3057    elseif ischar(ext_ima) && ~isempty(ext_ima(2:end))
3058        form=imformats(ext_ima(2:end));
3059        if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
3060            if isequal(nom_type_ima1,'*');
3061                type_ima2='multimage';%image series in a single image file
3062            else
3063                type_ima2='image';
3064            end
3065        end
3066    end
3067    %npxy=get(handles.ImaExt,'UserData');
3068    % %     if numel(npxy)<2
3069    %
3070    %         filename=name_generator(filebase_ima1,num1_civ1(1),num_a_civ1(1),ImaExt,nom_type_ima1);
3071    %         A=imread(filename);
3072    %         npxy=size(A);
3073    % %     end
3074    %     npy=npxy(1);
3075    %     npx=npxy(2);
3076    if checkbox(1) %if civ1 is performed
3077        h = waitbar(0,'copy images to the .png format for civ1');% display a wait bar
3078        for ifile=1:nbfield
3079            waitbar(ifile/nbfield);
3080            for j=1:nbslice
3081                filename=name_generator(filebase_ima1,num1_civ1(ifile),num_a_civ1(j),'.png',nom_type_imanew1);
3082                if ~exist(filename,'file')
3083                    A=read_image(filecell.ima1.civ1{ifile,j},type_ima1,num1_civ1(ifile),movieobject1);
3084                    imwrite(A,filename,'BitDepth',16);
3085                end
3086                filecell.ima1.civ1(ifile,j)={filename};
3087                filename=name_generator(filebase_ima2, num2_civ1(ifile),num_b_civ1(j),'.png',nom_type_imanew2);
3088                if ~exist(filename,'file')
3089                    A=read_image(filecell.ima2.civ1{ifile,j},type_ima2,num2_civ1(ifile),movieobject2);
3090                    imwrite(A,filename,'BitDepth',16);
3091                end
3092                filecell.ima2.civ1(ifile,j)={filename};
3093            end
3094        end
3095        close(h)
3096    end
3097    if checkbox(4) %if civ2 is performed
3098        h = waitbar(0,'copy images to the .png format for civ2');% display a wait bar
3099        for ifile=1:nbfield
3100            waitbar(ifile/nbfield);
3101            for j=1:nbslice
3102                filename=name_generator(filebase_ima1,num1_civ2(ifile),num_a_civ2(j),'.png',nom_type_imanew1);
3103                if ~exist(filename,'file')
3104                    A=read_image(cell2mat(filecell.ima1.civ2(ifile,j)),type_ima2,num1_civ2(ifile));
3105                    imwrite(A,filename,'BitDepth',16);
3106                end
3107                filecell.ima1.civ2(ifile,j)={filename};
3108                filename=name_generator(filebase_ima2, num2_civ2(ifile),num_b_civ2(j),'.png',nom_type_imanew2);
3109                if ~exist(filename,'file')
3110                    A=read_image(cell2mat(filecell.ima2.civ2(ifile,j)),type_ima2,num2_civ2(ifile));
3111                    imwrite(A,filename,'BitDepth',16);
3112                end
3113                filecell.ima2.civ2(ifile,j)={filename};
3114            end
3115        end
3116        close(h);
3117    end
3118end
3119
3120%------------------------------------------------------------------------
3121% --- determine the list of index pairs of processing file
3122function [num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2]=...
3123    find_pair_indices(handles,ref_i,ref_j,mode)
3124%------------------------------------------------------------------------
3125
3126list_civ1=get(handles.ListPairCiv1,'String');
3127index_civ1=get(handles.ListPairCiv1,'Value');
3128str_civ1=list_civ1{index_civ1};%string defining the image pairs for civ1
3129if isempty(str_civ1)||isequal(str_civ1,'')
3130    msgbox_uvmat('ERROR','no image pair selected for civ1')
3131    return
3132end
3133list_civ2=get(handles.ListPairCiv2,'String');
3134index_civ2=get(handles.ListPairCiv2,'Value');
3135if index_civ2>length(list_civ2)
3136    list_civ2=list_civ1;
3137    index_civ2=index_civ1;
3138end
3139str_civ2=list_civ2{index_civ2};%string defining the image pairs for civ2
3140
3141if isequal (mode,'series(Di)')
3142    lastfield=str2double(get(handles.nb_field,'String'));
3143    num1_civ1=ref_i-floor(index_civ1/2)*ones(size(ref_i));% set of first image numbers
3144    num2_civ1=ref_i+ceil(index_civ1/2)*ones(size(ref_i));
3145    num_a_civ1=ref_j;
3146    num_b_civ1=ref_j;
3147    num1_civ2=ref_i-floor(index_civ2/2)*ones(size(ref_i));
3148    num2_civ2=ref_i+ceil(index_civ2/2)*ones(size(ref_i));
3149    num_a_civ2=ref_j;
3150    num_b_civ2=ref_j;   
3151   
3152    % adjust the first and last field number
3153    lastfield=str2double(get(handles.nb_field,'String'));
3154    if isnan(lastfield)
3155        indsel=find((num1_civ1 >= 1)&(num1_civ2 >= 1));
3156    else
3157        indsel=find((num2_civ1 <= lastfield)&(num2_civ2 <= lastfield)&(num1_civ1 >= 1)&(num1_civ2 >= 1));
3158    end
3159    if length(indsel)>=1
3160        firstind=indsel(1);
3161        lastind=indsel(end);
3162        set(handles.first_i,'String',num2str(ref_i(firstind)))%update the display of first and last fields
3163        set(handles.last_i,'String',num2str(ref_i(lastind)))
3164        ref_i=ref_i(indsel);
3165        num1_civ1=num1_civ1(indsel);
3166        num1_civ2=num1_civ2(indsel);
3167        num2_civ1=num2_civ1(indsel);
3168        num2_civ2=num2_civ2(indsel);
3169    end
3170elseif isequal (mode,'series(Dj)')
3171    lastfield_j=str2double(get(handles.nb_field2,'String'));
3172    num1_civ1=ref_i;% set of first image numbers
3173    num2_civ1=ref_i;
3174    num_a_civ1=ref_j-floor(index_civ1/2)*ones(size(ref_j));
3175    num_b_civ1=ref_j+ceil(index_civ1/2)*ones(size(ref_j));
3176    num1_civ2=ref_i;
3177    num2_civ2=ref_i;
3178    num_a_civ2=ref_j-floor(index_civ2/2)*ones(size(ref_j));
3179    num_b_civ2=ref_j+ceil(index_civ2/2)*ones(size(ref_j));
3180    % adjust the first and last field number
3181    if isnan(lastfield_j)
3182        indsel=find((num_a_civ1 >= 1)&(num_a_civ2 >= 1));
3183    else
3184        indsel=find((num_b_civ1 <= lastfield_j)&(num_b_civ2 <= lastfield_j)&(num_a_civ1 >= 1)&(num_a_civ2 >= 1));
3185    end
3186    if length(indsel)>=1
3187        firstind=indsel(1);
3188        lastind=indsel(end);
3189        set(handles.first_j,'String',num2str(ref_j(firstind)))%update the display of first and last fields
3190        set(handles.last_j,'String',num2str(ref_j(lastind)))
3191        ref_j=ref_j(indsel);
3192        num_a_civ1=num_a_civ1(indsel);
3193        num_b_civ1=num_b_civ1(indsel);
3194        num_a_civ2=num_a_civ2(indsel);
3195        num_b_civ2=num_b_civ2(indsel);
3196    end
3197elseif isequal(mode,'pair j1-j2') %case of bursts (png_old or png_2D)
3198    displ_num=get(handles.ListPairCiv1,'UserData');
3199    num1_civ1=ref_i;
3200    num2_civ1=ref_i;
3201    num_a_civ1=displ_num(1,index_civ1);
3202    num_b_civ1=displ_num(2,index_civ1);
3203    num1_civ2=ref_i;
3204    num2_civ2=ref_i;
3205    num_a_civ2=displ_num(1,index_civ2);
3206    num_b_civ2=displ_num(2,index_civ2);
3207elseif isequal(mode,'displacement')
3208    num1_civ1=ref_i;
3209    num2_civ1=ref_i;
3210    num_a_civ1=ref_j;
3211    num_b_civ1=ref_j;
3212    num1_civ2=ref_i;
3213    num2_civ2=ref_i;
3214    num_a_civ2=ref_j;
3215    num_b_civ2=ref_j;
3216end
3217
3218
3219%------------------------------------------------------------------------
3220% --- PATCH
3221function cmd=cmd_patch(filename_nc,Param,PatchBin)
3222%------------------------------------------------------------------------
3223namelog=[filename_nc(1:end-3) '_patch.log'];
3224
3225% if test_interp==0
3226    if isunix
3227    cmd=[PatchBin...
3228        ' -f ' filename_nc ' -m ' num2str(Param.Nx)...
3229        ' -n ' num2str(Param.Ny) ' -ro ' num2str(Param.SmoothingParam)...
3230        ' -nopt ' num2str(Param.SubdomainSize) ...
3231        '  > ' namelog ' 2>&1']; % redirect standard output to the log file
3232    else
3233      cmd=['"' Param.xml.PatchBin...
3234          '" -f "' filename_nc '" -m ' param.Patch1.Nx...
3235          ' -n ' param.Patch1.Ny ' -ro ' param.Patch1.SmoothParam...
3236          ' -nopt ' Param.Patch1.SubdomainSize ...
3237        '  > "' namelog '" 2>&1']; % redirect standard output to the log file
3238    end
3239% else %nouveau programme patch
3240%     cmd=[PatchBin ' -f ' filename_nc ' -m ' nx_patch  ' -n ' ny_patch ' -ro ' rho_patch ...
3241%         ' -max ' thresh_value ' -nopt ' subdomain_patch  '  > ' namelog ' 2>&1']; % redirect standard output to the log file
3242% end
3243cmd=regexprep(cmd,'\\','\\\\');
3244
3245%------------------------------------------------------------------------
3246% --- STEREO Interp
3247function cmd=RUN_STINTERP(stinterpBin,filename_A_nc,filename_B_nc,filename_nc,nx_patch,ny_patch,rho_patch,subdomain_patch,thresh_value,xmlA,xmlB)
3248%------------------------------------------------------------------------
3249namelog=[filename_nc(1:end-3) '_stinterp.log'];
3250cmd=[stinterpBin ' -f1 ' filename_A_nc  ' -f2 ' filename_B_nc ' -f  ' filename_nc ...
3251    ' -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
3252
3253%------------------------------------------------------------------------
3254% --- Executes on button press in CheckCiv1.
3255function CheckCiv1_Callback(hObject, eventdata, handles)
3256%------------------------------------------------------------------------
3257state=get(handles.CheckCiv1,'Value');
3258enable_civ1(handles,state)
3259if state
3260    enable_pair1(handles,'on')
3261end
3262find_netcpair_civ1(hObject, eventdata, handles);
3263
3264%------------------------------------------------------------------------
3265% --- Executes on button press in CheckFix1.
3266function CheckFix1_Callback(hObject, eventdata, handles)
3267%------------------------------------------------------------------------
3268enable_fix1(handles,get(handles.CheckFix1,'Value'))
3269
3270%------------------------------------------------------------------------
3271% --- Executes on button press in CheckPatch1.
3272function CheckPatch1_Callback(hObject, eventdata, handles)
3273%------------------------------------------------------------------------
3274enable_patch1(handles,get(handles.CheckPatch1,'Value'))
3275
3276%------------------------------------------------------------------------
3277% --- Executes on button press in CheckCiv2.
3278function CheckCiv2_Callback(hObject, eventdata, handles)
3279%------------------------------------------------------------------------
3280state=get(handles.CheckCiv2,'Value');
3281enable_civ2(handles,state)
3282if state
3283    find_netcpair_civ2(hObject, eventdata, handles)
3284    enable_pair1(handles,'on')
3285end
3286
3287%------------------------------------------------------------------------
3288% --- Executes on button press in CheckFix2.
3289function CheckFix2_Callback(hObject, eventdata, handles)
3290%------------------------------------------------------------------------
3291state=get(handles.CheckFix2,'Value');
3292enable_fix2(handles,state)
3293if state
3294    find_netcpair_civ2(hObject, eventdata, handles) % select the available netcdf files
3295end
3296
3297%------------------------------------------------------------------------
3298% --- Executes on button press in CheckPatch2.
3299function CheckPatch2_Callback(hObject, eventdata, handles)
3300%------------------------------------------------------------------------
3301state=get(handles.CheckPatch2,'Value');
3302enable_patch2(handles,state)
3303if state
3304    find_netcpair_civ2(hObject, eventdata, handles) % select the available netcdf files
3305end
3306
3307%------------------------------------------------------------------------
3308function first_i_Callback(hObject, eventdata, handles)
3309%------------------------------------------------------------------------
3310% last_i_Callback(hObject, eventdata, handles)
3311first_i=str2double(get(handles.first_i,'String'));
3312% last_i=str2num(get(handles.last_i,'String'));
3313% ref_i=ceil((first_i+last_i)/2);
3314set(handles.ref_i,'String', num2str(first_i))% reference index for pair dt = first index
3315%set(handles.ref_i_civ2,'String', num2str(first_i))% reference index for pair dt = first index
3316ref_i_Callback(hObject, eventdata, handles)%refresh dispaly of dt for pairs (in case of non constant dt)
3317
3318%------------------------------------------------------------------------
3319function first_j_Callback(hObject, eventdata, handles)
3320%------------------------------------------------------------------------
3321first_j=str2num(get(handles.first_j,'String'));
3322set(handles.ref_j,'String', num2str(first_j))% reference index for pair dt = first index
3323ref_j_Callback(hObject, eventdata, handles)%refresh dispaly of dt for pairs (in case of non constant dt)
3324
3325%------------------------------------------------------------------------
3326% --- Executes on button press in SearchRange: determine the search range num_Sx,num_Sy
3327function SearchRange_Callback(hObject, eventdata, handles)
3328%------------------------------------------------------------------------
3329%determine pair numbers
3330if strcmp(get(handles.umin,'Visible'),'off')
3331    set(handles.u_title,'Visible','on')
3332    set(handles.v_title,'Visible','on')
3333    set(handles.umin,'Visible','on')
3334    set(handles.umax,'Visible','on')
3335    set(handles.vmin,'Visible','on')
3336    set(handles.vmax,'Visible','on')
3337    set(handles.CoordUnit,'Visible','on')
3338    set(handles.TimeUnit,'Visible','on')
3339    set(handles.slash_title,'Visible','on')
3340    set(handles.min_title,'Visible','on')
3341    set(handles.max_title,'Visible','on')
3342    set(handles.unit_title,'Visible','on')
3343else
3344    get_search_range(hObject, eventdata, handles)
3345end
3346
3347%------------------------------------------------------------------------
3348% ---  determine the search range num_Sx,num_Sy and shift
3349function get_search_range(hObject, eventdata, handles)
3350umin=str2double(get(handles.umin,'String'));
3351umax=str2double(get(handles.umax,'String'));
3352vmin=str2double(get(handles.umin,'String'));
3353vmax=str2double(get(handles.vmax,'String'));
3354%switch min_title and max_title in case of error
3355if umax<=umin
3356    umin_old=umin;
3357    umin=umax;
3358    umax=umin_old;
3359    set(handles.umin,'String', num2str(umin))
3360    set(handles.umax,'String', num2str(umax))
3361end
3362if vmax<=vmin
3363    vmin_old=vmin;
3364    vmin=vmax;
3365    vmax=vmin_old;
3366    set(handles.vmin,'String', num2str(vmin))
3367    set(handles.vmax,'String', num2str(vmax))
3368end   
3369if ~(isnan(umin)||isnan(umax)||isnan(vmin)||isnan(vmax))
3370    list_pair=get(handles.ListPairCiv1,'String');%get the menu of image pairs
3371    index=get(handles.ListPairCiv1,'Value');
3372    displ_num=get(handles.ListPairCiv1,'UserData');
3373    time=get(handles.ImaDoc,'UserData'); %get the set of times
3374    pxcm_xy=get(handles.SearchRange,'UserData');
3375    pxcmx=pxcm_xy(1);
3376    pxcmy=pxcm_xy(2);
3377    mode_list=get(handles.ListPairMode,'String');
3378    mode_value=get(handles.ListPairMode,'Value');
3379    mode=mode_list{mode_value};
3380    if isequal (mode, 'series(Di)' )
3381        ref_i=str2double(get(handles.ref_i,'String'));
3382        num1=ref_i-floor(index/2);%  first image numbers
3383        num2=ref_i+ceil(index/2);
3384        num_a=1;
3385        num_b=1;
3386    elseif isequal (mode, 'series(Dj)')
3387        num1=1;
3388        num2=1;
3389        ref_j=str2double(get(handles.ref_j,'String'));
3390        num_a=ref_j-floor(index/2);%  first image numbers
3391        num_b=ref_j+ceil(index/2);
3392    elseif isequal(mode,'pair j1-j2') %case of bursts (png_old or png_2D)
3393        ref_i=str2double(get(handles.ref_i,'String'));
3394        num1=ref_i;
3395        num2=ref_i;
3396        num_a=displ_num(1,index);
3397        num_b=displ_num(2,index);
3398    end
3399    dt=time(num2,num_b)-time(num1,num_a);
3400    ibx=str2double(get(handles.num_Bx,'String'));
3401    iby=str2double(get(handles.num_By,'String'));
3402    umin=dt*pxcmx*umin;
3403    umax=dt*pxcmx*umax;
3404    vmin=dt*pxcmy*vmin;
3405    vmax=dt*pxcmy*vmax;
3406    shiftx=round((umin+umax)/2);
3407    shifty=round((vmin+vmax)/2);
3408    isx=(umax+2-shiftx)*2+ibx;
3409    isx=2*ceil(isx/2)+1;
3410    isy=(vmax+2-shifty)*2+iby;
3411    isy=2*ceil(isy/2)+1;
3412    set(handles.num_Shiftx,'String',num2str(shiftx));
3413    set(handles.num_Shifty,'String',num2str(shifty));
3414    set(handles.num_Sx,'String',num2str(isx));
3415    set(handles.num_Sy,'String',num2str(isy));
3416end
3417
3418%------------------------------------------------------------------------
3419% --- Executes on carriage return on the subdir checkciv1 edit window
3420function subdir_civ1_Callback(hObject, eventdata, handles)
3421%------------------------------------------------------------------------
3422subdir=get(handles.subdir_civ1,'String');
3423set(handles.subdir_civ2,'String',subdir);% set civ2 directory the same as civ1 by default
3424menu_str=get(handles.ListSubdirCiv1,'String');% read the list of subdirectories for update
3425ichoice=find(strcmp(subdir,menu_str),1);
3426if isempty(ichoice)
3427    ilist=numel(menu_str); %select 'new...' in the menu
3428else
3429    ilist=ichoice;
3430end
3431set(handles.ListSubdirCiv1,'Value',ilist)% select the selected subdir in the menu
3432if get(handles.CheckCiv1,'Value')==0
3433    find_netcpair_civ1(hObject, eventdata, handles); %update the list of available pairs from netcdf files in the new directory
3434end
3435
3436%------------------------------------------------------------------------
3437% --- Executes on carriage return on the subdir checkciv1 edit window
3438function subdir_civ2_Callback(hObject, eventdata, handles)
3439%------------------------------------------------------------------------
3440subdir=get(handles.subdir_civ1,'String');
3441menu_str=get(handles.ListSubDirCiv2,'String');% read the list of subdirectories for update
3442ichoice=find(strcmp(subdir,menu_str),1);
3443if isempty(ichoice)
3444    ilist=numel(menu_str); %select 'new...' in the menu
3445else
3446    ilist=ichoice;
3447end
3448set(handles.ListSubDirCiv2,'Value',ilist)% select the selected subdir in the menu
3449%update the list of available pairs from netcdf files in the new directory
3450if ~get(handles.CheckCiv2,'Value') && ~get(handles.CheckCiv1,'Value') && ~get(handles.CheckFix1,'Value') && ~get(handles.CheckPatch1,'Value')
3451    find_netcpair_civ2(hObject, eventdata, handles);
3452end
3453
3454% %------------------------------------------------------------------------
3455% % --- Executes on button press in CheckMask: select box for mask option
3456% function check_Mask_Callback(hObject, eventdata, handles)
3457% %------------------------------------------------------------------------
3458% maskval=get(handles.CheckMask,'Value');
3459% if isequal(maskval,0)
3460%     set(handles.txt_MaskName,'Visible','off')
3461% %     set(handles.txt_MaskName,'String','')
3462% else
3463%     mask_displ='no mask'; %default
3464%     filebase=get(handles.RootName,'String');
3465%     [ nbslice_mask, flag_mask]=get_mask(filebase,handles);
3466%     if isequal(flag_mask,1)
3467%         mask_displ=[num2str(nbslice_mask) 'mask'];
3468%     elseif get(handles.ListCompareMode,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3469%         common_path=fileparts(filebase);
3470%         filebase_a=fullfile(common_path,get(handles.RootName_1,'String'));
3471%         [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3472%         if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice_mask)
3473%             mask_displ='no mask';
3474%         end
3475%     end
3476%     if isequal(mask_displ,'no mask')
3477%         [FileName, PathName, filterindex] = uigetfile( ...
3478%             {'*.png', ' (*.png)';
3479%             '*.png',  '.png files '; ...
3480%             '*.*', 'All Files (*.*)'}, ...
3481%             'Pick a mask file *.png',filebase);
3482%         mask_displ=fullfile(PathName,FileName);
3483%         if exist(mask_displ,'file')
3484%             set(handles.txt_MaskName,'Visible','on')
3485%             set(handles.txt_MaskName,'String',mask_displ)
3486% %             mask_displ='no mask';
3487%         end
3488%     end
3489%     if isequal(mask_displ,'no mask')
3490%         set(handles.CheckMask,'Value',0)
3491%         set(handles.CheckMask,'Value',0)
3492%         set(handles.CheckMask,'Value',0)
3493%         set(handles.CheckMask,'Value',0)
3494%     else
3495%         set(handles.CheckMask,'Value',1)
3496%         set(handles.CheckMask,'Value',1)
3497%     end
3498%     set(handles.txt_MaskName,'String',mask_displ)
3499%     set(handles.txt_MaskName,'String',mask_displ)
3500%     set(handles.txt_Mask,'String',mask_displ)
3501%     set(handles.mask_fix2,'String',mask_displ)
3502% end
3503% set(handles.CheckMask,'Value',maskval)%update the checkciv2 mask with the same option as checkciv1
3504
3505%------------------------------------------------------------------------
3506% --- Executes on button press in CheckMask.
3507function get_mask_fix1_Callback(hObject, eventdata, handles)
3508%------------------------------------------------------------------------
3509maskval=get(handles.CheckMask,'Value');
3510if isequal(maskval,0)
3511    set(handles.txt_MaskName,'String','')
3512else
3513    mask_displ='no mask'; %default
3514    filebase=get(handles.RootName,'String');
3515    [nbslice, flag_mask]=get_mask(filebase,handles);
3516    if isequal(flag_mask,1)
3517        mask_displ=[num2str(nbslice) 'mask'];
3518    elseif get(handles.ListCompareMode,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3519        filebase_a=get(handles.RootName_1,'String');
3520        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3521        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice)
3522            mask_displ='no mask';
3523        end
3524    end
3525    if isequal(mask_displ,'no mask')
3526        [FileName, PathName, filterindex] = uigetfile( ...
3527            {'*.png', ' (*.png)';
3528            '*.png',  '.png files '; ...
3529            '*.*', 'All Files (*.*)'}, ...
3530            'Pick a mask file *.png',filebase);
3531        mask_displ=fullfile(PathName,FileName);
3532        if ~exist(mask_displ,'file')
3533            mask_displ='no mask';
3534        end
3535    end
3536    if isequal(mask_displ,'no mask')
3537        set(handles.CheckMask,'Value',0)
3538        set(handles.CheckMask,'Value',0)
3539        set(handles.CheckMask,'Value',0)
3540    else
3541        %set(handles.CheckMask,'Value',1)
3542        set(handles.CheckMask,'Value',1)
3543    end
3544    set(handles.txt_MaskName,'String',mask_displ)
3545    set(handles.txt_Mask,'String',mask_displ)
3546    set(handles.mask_fix2,'String',mask_displ)
3547end
3548
3549%------------------------------------------------------------------------
3550% --- Executes on button press in CheckMask: select box for mask option
3551function get_mask_civ2_Callback(hObject, eventdata, handles)
3552%------------------------------------------------------------------------
3553maskval=get(handles.CheckMask,'Value');
3554if isequal(maskval,0)
3555    set(handles.txt_Mask,'String','')
3556else
3557    mask_displ='no mask'; %default
3558    filebase=get(handles.RootName,'String');
3559    [nbslice, flag_mask]=get_mask(filebase,handles);
3560    if isequal(flag_mask,1)
3561        mask_displ=[num2str(nbslice) 'mask'];
3562    elseif get(handles.ListCompareMode,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3563        filebase_a=get(handles.RootName_1,'String');
3564        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3565        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice)
3566            mask_displ='no mask';
3567        end
3568    end
3569    if isequal(mask_displ,'no mask')
3570        [FileName, PathName, filterindex] = uigetfile( ...
3571            {'*.png', ' (*.png)';
3572            '*.png',  '.png files '; ...
3573            '*.*', 'All Files (*.*)'}, ...
3574            'Pick a mask file *.png',filebase);
3575        mask_displ=fullfile(PathName,FileName);
3576        if ~exist(mask_displ,'file')
3577            mask_displ='no mask';
3578        end
3579    end
3580    if isequal(mask_displ,'no mask')
3581        set(handles.CheckMask,'Value',0)
3582        set(handles.CheckMask,'Value',0)
3583    else
3584        set(handles.CheckMask,'Value',1)
3585    end
3586    set(handles.txt_Mask,'String',mask_displ)
3587    set(handles.mask_fix2,'String',mask_displ)
3588end
3589
3590%------------------------------------------------------------------------
3591% --- Executes on button press in CheckMask.
3592function get_mask_fix2_Callback(hObject, eventdata, handles)
3593%------------------------------------------------------------------------
3594maskval=get(handles.CheckMask,'Value');
3595if isequal(maskval,0)
3596    set(handles.mask_fix2,'String','')
3597else
3598    mask_displ='no mask'; %default
3599    filebase=get(handles.RootName,'String');
3600    [nbslice, flag_mask]=get_mask(filebase,handles);
3601    if isequal(flag_mask,1)
3602        mask_displ=[num2str(nbslice) 'mask'];
3603    elseif get(handles.ListCompareMode,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3604        filebase_a=get(handles.RootName_1,'String');
3605        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3606        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice)
3607            mask_displ='no mask';
3608        end
3609    end
3610    if isequal(mask_displ,'no mask')
3611        [FileName, PathName, filterindex] = uigetfile( ...
3612            {'*.png', ' (*.png)';
3613            '*.png',  '.png files '; ...
3614            '*.*', 'All Files (*.*)'}, ...
3615            'Pick a mask file *.png',filebase);
3616        mask_displ=fullfile(PathName,FileName);
3617        if ~exist(mask_displ,'file')
3618            mask_displ='no mask';
3619        end
3620    end
3621    if isequal(mask_displ,'no mask')
3622        set(handles.CheckMask,'Value',0)
3623    end
3624    set(handles.mask_fix2,'String',mask_displ)
3625end
3626
3627%------------------------------------------------------------------------
3628% --- function called to look for mask files
3629function [nbslice, flag_mask]=get_mask(filebase,handles)
3630%------------------------------------------------------------------------
3631%detect mask files, images with appropriate file base
3632%[filebase '_' xx 'mask'], xx=nbslice
3633%flag_mask=1 indicates detection
3634
3635flag_mask=0;%default
3636nbslice=1;
3637
3638% subdir=get(handles.subdir_civ1,'String');
3639[Path,Name]=fileparts(filebase);
3640if ~isdir(Path)
3641    msgbox_uvmat('ERROR','no path for input files')
3642    return
3643end
3644% currentdir=pwd;
3645% cd(Path);%move in the dir of the root name filebase
3646maskfiles=dir(fullfile(Path,[Name '_*mask_*.png']));%look for mask files
3647% cd(currentdir);%come back to the current working directory
3648if ~isempty(maskfiles)
3649    %     msgbox_uvmat('ERROR','no mask available, to create it use Tools/Make mask in the upper menu bar of uvmat')
3650    % else
3651    flag_mask=1;
3652    maskname=maskfiles(1).name;% take the first mask file in the list
3653    [Path2,Name,ext]=fileparts(maskname);
3654    Namedouble=double(Name);
3655    val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
3656    ind_mask=findstr('mask',Name);
3657    i=ind_mask-1;
3658    while val(i)==0 && i>0
3659        i=i-1;
3660    end
3661    nbslice=str2double(Name(i+1:ind_mask-1));
3662    if ~isnan(nbslice) && Name(i)=='_'
3663        flag_mask=1;
3664    else
3665        msgbox_uvmat('ERROR',['bad mask file ' Name ext ' found in ' Path2])
3666        return
3667        nbslice=1;
3668    end
3669end
3670
3671%------------------------------------------------------------------------
3672% --- function called to look for grid files
3673function [nbslice, flag_mask]=get_grid(filebase,handles)
3674%------------------------------------------------------------------------
3675flag_mask=0;%default
3676nbslice=1;
3677[Path,Name]=fileparts(filebase);
3678currentdir=pwd;
3679cd(Path);%move in the dir of the root name filebase
3680maskfiles=dir([Name '_*grid_*.grid']);%look for mask files
3681cd(currentdir);%come back to the current working directory
3682if ~isempty(maskfiles)
3683    flag_mask=1;
3684    maskname=maskfiles(1).name;% take the first mask file in the list
3685    [Path2,Name,ext]=fileparts(maskname);
3686    Namedouble=double(Name);
3687    val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
3688    ind_mask=findstr('grid',Name);
3689    i=ind_mask-1;
3690    while val(i)==0 && i>0
3691        i=i-1;
3692    end
3693    nbslice=str2double(Name(i+1:ind_mask-1));
3694    if ~isnan(nbslice) && Name(i)=='_'
3695        flag_mask=1;
3696    else
3697        msgbox_uvmat('ERROR',['bad grid file ' Name ext ' found in ' Path2])
3698        return
3699        nbslice=1;
3700    end
3701end
3702
3703%------------------------------------------------------------------------
3704% --- transform numbers to letters
3705function str=num2stra(num,nom_type)
3706%------------------------------------------------------------------------
3707if isempty(nom_type)
3708    str='';
3709elseif strcmp(nom_type(end),'a')
3710    str=char(96+num);
3711elseif strcmp(nom_type(end),'A')
3712    str=char(96+num);
3713elseif isempty(nom_type(2:end))%a single index
3714    str='';
3715else
3716    str=num2str(num);
3717end
3718
3719%------------------------------------------------------------------------
3720% --- Executes on button press in ListSubdirCiv1.
3721function ListSubdirCiv1_Callback(hObject, eventdata, handles)
3722%------------------------------------------------------------------------
3723list_subdir_civ1=get(handles.ListSubdirCiv1,'String');
3724val=get(handles.ListSubdirCiv1,'Value');
3725subdir=list_subdir_civ1{val};
3726if strcmp(subdir,'new...')
3727    subdir='CIV'; %default subdirectory
3728end
3729set(handles.subdir_civ1,'String',subdir);
3730find_netcpair_civ1(hObject, eventdata, handles)
3731% end
3732
3733%------------------------------------------------------------------------
3734% --- Executes on button press in ListSubDirCiv2.
3735function ListSubDirCiv2_Callback(hObject, eventdata, handles)
3736%------------------------------------------------------------------------
3737list_subdir_civ2=get(handles.ListSubDirCiv2,'String');
3738val=get(handles.ListSubDirCiv2,'Value');
3739subdir=list_subdir_civ2{val};
3740if strcmp(subdir,'new...')
3741    subdir='CIV'; %default subdirectory
3742end
3743set(handles.subdir_civ2,'String',subdir);
3744%     set(handles.ListSubDirCiv2,'Value',1);
3745
3746
3747%------------------------------------------------------------------------
3748% --- Executes on button press in CheckGrid.
3749function CheckGrid_Callback(hObject, eventdata, handles)
3750%------------------------------------------------------------------------
3751value=get(hObject,'Value');
3752hparent=get(hObject,'parent');
3753hchildren=get(hparent,'children');
3754handle_txtbox=findobj(hchildren,'tag','txt_GridName');
3755handle_dx=findobj(hchildren,'tag','num_Dx');
3756handle_dy=findobj(hchildren,'tag','num_Dy');
3757testgrid=0;
3758filegrid='';
3759if value
3760    filebase=get(handles.RootName,'String');
3761    [nbslice, flag_grid]=get_grid(filebase,handles);% look for a grid with appropriate name
3762    if isequal(flag_grid,1)
3763        filegrid=[num2str(nbslice) 'grid'];
3764        testgrid=1;
3765    else % browse for a grid
3766        filegrid=get(hObject,'UserData');%look for previous grid name stored as UserData
3767        if exist(filegrid,'file')
3768            filebase=filegrid;
3769        end
3770        [FileName, PathName, filterindex] = uigetfile( ...
3771            {'*.grid', ' (*.grid)';
3772            '*.grid',  '.grid files '; ...
3773            '*.*', 'All Files (*.*)'}, ...
3774            'Pick a file',filebase);
3775        filegrid=fullfile(PathName,FileName);
3776        set(hObject,'UserData',filegrid);%store for future use
3777        if ~(isempty(FileName)||isempty(PathName)||isequal(FileName,0)||~exist(filegrid,'file'))
3778            testgrid=1;
3779        end
3780    end
3781end
3782if testgrid
3783    set(handle_dx,'Visible','off');
3784    set(handle_dy,'Visible','off');
3785    set(handle_txtbox,'Visible','on')
3786    set(handle_txtbox,'String',filegrid)
3787else
3788    set(hObject,'Value',0);
3789    set(handle_dx,'Visible','on');
3790    set(handle_dy,'Visible','on');
3791    set(handle_txtbox,'Visible','off')
3792end
3793
3794%% if hObject is on the checkciv1 frame, duplicate action for checkciv2 frame
3795PanelName=get(hparent,'tag');
3796if strcmp(PanelName,'panel_Civ1')
3797    hchildren=get(handles.Civ2,'children');
3798    handle_checkbox=findobj(hchildren,'tag','check_Grid');
3799    handle_txtbox=findobj(hchildren,'tag','txt_GridName');
3800    handle_dx=findobj(hchildren,'tag','num_Dx');
3801    handle_dy=findobj(hchildren,'tag','num_Dy');
3802    set(handle_checkbox,'UserData',filegrid);%store for future use
3803    if testgrid
3804        set(handle_checkbox,'Value',1);
3805        set(handle_dx,'Visible','off');
3806        set(handle_dy,'Visible','off');
3807        set(handle_txtbox,'Visible','on')
3808        set(handle_txtbox,'String',filegrid)
3809    else
3810        set(handle_checkbox,'Value',0);
3811        set(handles.CheckGrid,'Value',0);
3812        set(handle_dx,'Visible','on');
3813        set(handle_dy,'Visible','on');
3814        set(handle_txtbox,'Visible','off')
3815    end
3816end
3817%------------------------------------------------------------------------
3818% --- Executes on button press in CheckMask.
3819function CheckMask_Callback(hObject, eventdata, handles)
3820%------------------------------------------------------------------------
3821value=get(hObject,'Value');
3822testmask=0;
3823if value
3824    filebase=get(handles.RootName,'String');
3825    [nbslice, flag_mask]=get_mask(filebase,handles);% look for a mask with appropriate name
3826    if isequal(flag_mask,1)
3827        filemask=[num2str(nbslice) 'mask'];
3828        testmask=1;
3829    else % browse for a mask
3830        filemask=get(handles.CheckMask,'UserData');%look for previous mask name stored as UserData
3831        if exist(filemask,'file')
3832            filebase=filemask;
3833        end
3834        [FileName, PathName, filterindex] = uigetfile( ...
3835            {'*.png', ' (*.png)';
3836            '*.png',  '.png files '; ...
3837            '*.*', 'All Files (*.*)'}, ...
3838            'Pick a mask file *.png',filebase);
3839        filemask=fullfile(PathName,FileName);
3840        set(handles.CheckMask,'UserData',filemask);%store for future use
3841        if ~(isempty(FileName)||isempty(PathName)||isequal(FileName,0)||~exist(filemask,'file'))
3842            testmask=1;
3843        end
3844    end
3845end
3846if testmask
3847    set(handles.num_Dx,'Visible','off');
3848    set(handles.num_Dy,'Visible','off');
3849    set(handles.num_Dx,'Visible','off');
3850    set(handles.num_Dy,'Visible','off');
3851    set(handles.txt_MaskName,'Visible','on')
3852    set(handles.txt_MaskName,'String',filemask)
3853else
3854    set(handles.CheckMask,'Value',0);
3855    set(handles.CheckGrid,'Value',0);
3856    set(handles.num_Dx,'Visible','on');
3857    set(handles.num_Dy,'Visible','on');
3858    set(handles.num_Dx,'Visible','on');
3859    set(handles.num_Dy,'Visible','on');
3860    set(handles.txt_MaskName,'Visible','off')
3861end
3862
3863%------------------------------------------------------------------------
3864% --- Executes on button press in CheckGrid.
3865function browse_gridciv2_Callback(hObject, eventdata, handles)
3866%------------------------------------------------------------------------
3867value=get(handles.CheckGrid,'Value');
3868if value
3869    filebase=get(handles.RootName,'String');
3870    [nbslice, flag_grid]=get_grid(filebase,handles);
3871    if isequal(flag_grid,1)
3872        mask_displ=[num2str(nbslice) 'grid'];
3873        set(handles.txt_GridName,'String',mask_displ)
3874        set(handles.num_Dx,'Visible','off');
3875        set(handles.num_Dy,'Visible','off');
3876    else
3877        [FileName, PathName, filterindex] = uigetfile( ...
3878            {'*.grid', ' (*.grid)';
3879            '*.grid',  '.grid files '; ...
3880            '*.*', 'All Files (*.*)'}, ...
3881            'Pick a file',filebase);
3882        filegrid=fullfile(PathName,FileName);
3883        if isempty(FileName)||isempty(PathName)||isequal(FileName,0)||~exist(filegrid,'file')
3884            set(handles.CheckGrid,'Value',0);
3885            set(handles.txt_GridName,'string','');
3886            set(handles.num_Dx,'Visible','on');
3887            set(handles.num_Dy,'Visible','on');
3888            set(handles.txt_GridName,'string','');
3889        else
3890            set(handles.txt_GridName,'string',filegrid);
3891            set(handles.num_Dx,'Visible','off');
3892            set(handles.num_Dy,'Visible','off');
3893            set(handles.txt_GridName,'string',filegrid);
3894        end
3895    end
3896else
3897    set(handles.txt_GridName,'string','');
3898    set(handles.num_Dx,'Visible','on');
3899    set(handles.num_Dy,'Visible','on');
3900    set(handles.txt_GridName,'string','');
3901end
3902
3903% % --- Executes on button press in CheckGrid.
3904% function browse_gridciv2_Callback(hObject, eventdata, handles)
3905%
3906% filebase=get(handles.RootName,'String');
3907% [FileName, PathName, filterindex] = uigetfile( ...
3908%        {'*.grid', ' (*.grid)';
3909%         '*.grid',  '.grid files '; ...
3910%         '*.*', 'All Files (*.*)'}, ...
3911%         'Pick a file',filebase);
3912% filegrid=fullfile(PathName,FileName);
3913% set(handles.txt_GridName,'string',filegrid);
3914% set(handles.num_Dx,'String',' ');
3915% set(handles.num_Dy,'String',' ');
3916% % set(handles.grid_patch2,'string',filegrid);
3917
3918% --- Executes on button press in get_gridpatch1.
3919function get_gridpatch1_Callback(hObject, eventdata, handles)
3920% hObject    handle to get_gridpatch1 (see GCBO)
3921% eventdata  reserved - to be defined in a future version of MATLAB
3922% handles    structure with handles and user data (see GUIDATA)
3923
3924filebase=get(handles.RootName,'String');
3925[FileName, PathName, filterindex] = uigetfile( ...
3926    {'*.grid', ' (*.grid)';
3927    '*.grid',  '.grid files '; ...
3928    '*.*', 'All Files (*.*)'}, ...
3929    'Pick a file',filebase);
3930filegrid=fullfile(PathName,FileName);
3931set(handles.grid_patch1,'string',filegrid);
3932% set(handles.grid_patch2,'string',filegrid
3933
3934%------------------------------------------------------------------------
3935% --- Executes on button press in get_gridpatch2.
3936function get_gridpatch2_Callback(hObject, eventdata, handles)
3937%------------------------------------------------------------------------
3938
3939%------------------------------------------------------------------------
3940function enable_civ1(handles,state)
3941%------------------------------------------------------------------------
3942if state
3943    RootName=get(handles.RootName,'String');
3944    if isempty(RootName)
3945         msgbox_uvmat('ERROR','No input file')
3946        return
3947    end
3948    set(handles.Civ1,'Visible','on')   
3949    set(handles.PairIndices,'Visible','on')
3950%     set(handles.frame_civ1,'BackgroundColor',[1 1 0])
3951%     set(handles.frame_para_civ1,'BackgroundColor',[1 1 0])
3952%     set(handles.frame_grid_civ1,'BackgroundColor',[1 1 0])
3953else
3954    set(handles.Civ1,'Visible','off') 
3955%     set(handles.frame_civ1,'BackgroundColor',[0.831 0.816 0.784])
3956%     set(handles.frame_para_civ1,'BackgroundColor',[0.831 0.816 0.784])
3957%     set(handles.frame_grid_civ1,'BackgroundColor',[0.831 0.816 0.784])
3958end
3959
3960% set(handles.num_Bx,'Visible',state)
3961% set(handles.num_By,'Visible',state)
3962% set(handles.num_Sx,'Visible',state)
3963% set(handles.num_Sy,'Visible',state)
3964% set(handles.num_Shiftx,'Visible',state)
3965% set(handles.num_Shifty,'Visible',state)
3966% set(handles.num_Rho,'Visible',state)
3967% set(handles.num_Dx,'Visible',state)
3968% set(handles.num_Dy,'Visible',state)
3969% set(handles.SearchRange,'Visible',state)
3970% set(handles.u_title,'Visible',state)
3971% set(handles.v_title,'Visible',state)
3972% set(handles.min_title,'Visible',state)
3973% set(handles.max_title,'Visible',state)
3974% set(handles.umin,'Visible',state)
3975% set(handles.umax,'Visible',state)
3976% set(handles.vmin,'Visible',state)
3977% set(handles.vmax,'Visible',state)
3978% set(handles.txt_GridName,'Visible',state)
3979% set(handles.txt_MaskName,'Visible',state)
3980% set(handles.CheckGrid,'Visible',state)
3981% set(handles.CheckMask,'Visible',state)
3982% set(handles.parameters,'Visible',state)
3983% set(handles.grid,'Visible',state)
3984% set(handles.num_Dx,'Visible',state)
3985% set(handles.num_Dy,'Visible',state)
3986% set(handles.check_Threshold,'Visible',state)
3987% if isequal(state,'off')
3988%     set(handles.num_MinIma,'Visible','off')
3989%     set(handles.num_MaxIma,'Visible','off')
3990%     set(handles.check_Threshold,'Value',0)
3991% end
3992% set(handles.dx_civ1_title,'Visible',state)
3993% set(handles.dy_civ1_title,'Visible',state)
3994% set(handles.ImaThreshold_title,'Visible',state)
3995% set(handles.ib_title,'Visible',state)
3996% set(handles.is_title,'Visible',state)
3997% set(handles.shift_title,'Visible',state)
3998% set(handles.rho_title,'Visible',state)
3999% set(handles.TestCiv1,'Visible',state)
4000%set(handles.ListPairMode,'Visible',state)
4001
4002%------------------------------------------------------------------------
4003function enable_fix1(handles,state)
4004%------------------------------------------------------------------------
4005
4006if isequal(state,0)
4007    state='off';
4008end
4009if isequal(state,1)
4010    state='on';
4011    RootName=get(handles.RootName,'String');
4012    if isempty(RootName)
4013         msgbox_uvmat('ERROR','No input file')
4014        return
4015    end
4016end
4017if isequal(state,'on')
4018    set(handles.Fix1,'Visible','on')
4019%     set(handles.frame_fix1,'BackgroundColor',[1 1 0])
4020else
4021    set(handles.Fix1,'Visible','off')
4022    %set(handles.frame_fix1,'BackgroundColor',[0.7 0.7 0.7])
4023end
4024% % set(handles.REMOVE,'Visible',state)
4025% set(handles.CheckFmin2,'Visible',state)
4026% set(handles.CheckF2,'Visible',state)
4027% set(handles.CheckF3,'Visible',state)
4028% set(handles.num_MinCorr,'Visible',state)
4029% set(handles.thresh_vecC_title,'Visible',state)
4030% set(handles.num_MaxVel,'Visible',state)
4031% set(handles.thresh_vel_text,'Visible',state)
4032% set(handles.txt_MaskName,'Visible',state)
4033% set(handles.CheckMask,'Visible',state)
4034% set(handles.get_ref_fix1,'Visible',state)
4035% set(handles.ref_fix1,'Visible',state)
4036% set(handles.inf_sup1,'Visible',state)
4037% set(handles.field_ref1,'Visible',state)
4038
4039%------------------------------------------------------------------------
4040function enable_patch1(handles,state)
4041%------------------------------------------------------------------------
4042if state
4043    RootName=get(handles.RootName,'String');
4044    if isempty(RootName)
4045        msgbox_uvmat('ERROR','No input file')
4046        return
4047    end
4048    set(handles.Patch1,'Visible','on')
4049else
4050    set(handles.Patch1,'Visible','off')
4051end
4052return
4053set(handles.frame_patch1,'BackgroundColor',[1 1 0])
4054set(handles.rho_patch1,'Visible','on')
4055set(handles.rho_text1,'Visible','on')
4056if get(handles.ListPairMode,'Value')==2
4057    set(handles.thresh_patch1,'Visible','on')
4058    set(handles.thresh_text1,'Visible','on')
4059end
4060set(handles.subdomain_patch1,'Visible','on')
4061set(handles.subdomain_text1,'Visible','on')
4062set(handles.nx_patch1,'Visible','on')
4063set(handles.num_Ny,'Visible','on')
4064set(handles.nx_patch1_title,'Visible','on')
4065set(handles.ny_patch1_title,'Visible','on')
4066% if ~isempty(patch_newBin)
4067set(handles.test_interp,'Visible','off');
4068stereo_test=get(handles.ListCompareMode,'Value');
4069if stereo_test==3
4070    set(handles.test_stereo1,'Visible','on')
4071end
4072% end
4073%set(handles.get_gridpatch1,'Visible','on')
4074%set(handles.grid_patch1,'string','none');
4075%set(handles.grid_patch1,'Visible','on')
4076
4077% %------------------------------------------------------------------------
4078% function desable_patch1(handles)
4079% %------------------------------------------------------------------------
4080% return
4081% set(handles.frame_patch1,'BackgroundColor',[0.831 0.816 0.784])
4082% set(handles.rho_patch1,'Visible','off')
4083% set(handles.rho_text1,'Visible','off')
4084% set(handles.thresh_patch1,'Visible','off')
4085% set(handles.thresh_text1,'Visible','off')
4086% set(handles.subdomain_patch1,'Visible','off')
4087% set(handles.subdomain_text1,'Visible','off')
4088% set(handles.nx_patch1,'Visible','off')
4089% set(handles.num_Ny,'Visible','off')
4090% set(handles.nx_patch1_title,'Visible','off')
4091% set(handles.ny_patch1_title,'Visible','off')
4092% set(handles.test_stereo1,'Visible','off')
4093% %set(handles.test_interp,'Visible','off')
4094% %set(handles.get_gridpatch1,'Visible','off')
4095% %set(handles.grid_patch1,'Visible','off')
4096
4097%------------------------------------------------------------------------
4098function enable_civ2(handles,state)
4099%------------------------------------------------------------------------
4100if state
4101    RootName=get(handles.RootName,'String');
4102    if isempty(RootName)
4103         msgbox_uvmat('ERROR','No input file')
4104        return
4105    end
4106    set(handles.Civ2,'Visible','on')
4107else
4108    set(handles.Civ2,'Visible','off')
4109end
4110
4111%
4112% set(handles.ibx_civ2,'Visible',state)
4113% set(handles.iby_civ2,'Visible',state)
4114% set(handles.decimal,'Visible',state)
4115% set(handles.deformation,'Visible',state)
4116% set(handles.rho_civ2,'Visible',state)
4117% set(handles.num_Dx,'Visible',state)
4118% set(handles.num_Dy,'Visible',state)
4119% set(handles.CheckGrid,'Visible',state)
4120% set(handles.CheckMask,'Visible',state)
4121% set(handles.parameters,'Visible',state)
4122% set(handles.grid,'Visible',state)
4123% set(handles.parameters_text,'Visible',state)
4124% set(handles.grid_text,'Visible',state)
4125% set(handles.txt_GridName,'Visible',state)
4126% set(handles.txt_Mask,'Visible',state)
4127% set(handles.dx_civ2_title,'Visible',state)
4128% set(handles.dy_civ2_title,'Visible',state)
4129% set(handles.ibx_civ2_text,'Visible',state)
4130% set(handles.rho_civ2_title,'Visible',state)
4131% set(handles.ImaThreshold2,'Visible',state)
4132% set(handles.ImaThreshold_title2,'Visible',state)
4133% if isequal(state,'off')
4134%     set(handles.MinIma2,'Visible','off')
4135%     set(handles.MaxIma2,'Visible','off')
4136%     set(handles.ImaThreshold2,'Value',0)
4137%     if isequal(get(handles.CheckFix2,'Value'),0) & isequal(get(handles.CheckPatch2,'Value'),0)
4138%         set(handles.ListPairCiv2,'Visible','off')
4139%         set(handles.subdir_civ2,'Visible','off')
4140%         set(handles.subdir_civ2_text,'Visible','off')
4141%         set(handles.dt_unit_civ2,'Visible','off')
4142%         %set(handles.ref_i_civ2,'Visible','off')
4143%         set(handles.i_ref_civ2_title,'Visible','off')
4144%         set(handles.j_ref_civ2_title,'Visible','off')
4145%         set(handles.ref_j_civ2,'Visible','off')
4146%     end
4147% else
4148%     set(handles.ListPairCiv2,'Visible','on')
4149%     set(handles.subdir_civ2,'Visible','on')
4150%     set(handles.subdir_civ2_text,'Visible','on')
4151%     set(handles.dt_unit_civ2,'Visible','on')
4152%   %  set(handles.ref_i_civ2,'Visible','on')
4153%     set(handles.i_ref_civ2_title,'Visible','on')
4154%     set(handles.j_ref_civ2_title,'Visible','on')
4155%     set(handles.ref_j_civ2,'Visible','on')
4156% end
4157% set(handles.rho_civ2_title,'Visible',state)
4158
4159%------------------------------------------------------------------------
4160function enable_fix2(handles,state)
4161%------------------------------------------------------------------------
4162% if isequal(state,'on')
4163%     set(handles.Fix2,'Visible','on') 
4164%     
4165% %     set(handles.frame_civ2,'BackgroundColor',[1 1 0])
4166% %     set(handles.frame_para_civ2,'BackgroundColor',[1 1 0])
4167% %     set(handles.frame_grid_civ2,'BackgroundColor',[1 1 0])
4168% %     set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
4169% else
4170if state
4171    RootName=get(handles.RootName,'String');
4172    if isempty(RootName)
4173         msgbox_uvmat('ERROR','No input file')
4174        return
4175    end
4176    set(handles.Fix2,'Visible','on')
4177else
4178    set(handles.Fix2,'Visible','off')
4179end
4180
4181% set(handles.frame_fix2,'BackgroundColor',[1 1 0])
4182% set(handles.REMOVE2,'Visible','on')
4183% set(handles.vec_Fmin2_2,'Visible','on')
4184% set(handles.vec_F4,'Visible','on')
4185% set(handles.vec_F3_2,'Visible','on')
4186% set(handles.thresh_vec2C,'Visible','on')
4187% set(handles.thresh_vec2C_text,'Visible','on')
4188% set(handles.thresh_vel2,'Visible','on')
4189% set(handles.thresh_vel2_text,'Visible','on')
4190% set(handles.mask_fix2,'Visible','on')
4191% set(handles.CheckMask,'Visible','on')
4192% set(handles.ListPairCiv2,'Visible','on')
4193% set(handles.subdir_civ2,'Visible','on')
4194% set(handles.subdir_civ2_text,'Visible','on')
4195% set(handles.get_ref_fix2,'Visible','on')
4196% set(handles.ref_fix2,'Visible','on')
4197% set(handles.inf_sup2,'Visible','on')
4198% set(handles.field_ref2,'Visible','on')
4199
4200% %------------------------------------------------------------------------
4201% function desable_fix2(handles)
4202% %------------------------------------------------------------------------
4203% set(handles.frame_fix2,'BackgroundColor',[0.831 0.816 0.784])
4204% set(handles.REMOVE2,'Visible','off')
4205% set(handles.vec_Fmin2_2,'Visible','off')
4206% set(handles.vec_F4,'Visible','off')
4207% set(handles.vec_F3_2,'Visible','off')
4208% set(handles.thresh_vec2C,'Visible','off')
4209% set(handles.thresh_vec2C_text,'Visible','off')
4210% set(handles.thresh_vel2,'Visible','off')
4211% set(handles.thresh_vel2_text,'Visible','off')
4212% set(handles.mask_fix2,'Visible','off')
4213% set(handles.CheckMask,'Visible','off')
4214% set(handles.get_ref_fix2,'Visible','off')
4215% set(handles.ref_fix2,'Visible','off')
4216% set(handles.inf_sup2,'Visible','off')
4217% set(handles.field_ref2,'Visible','off')
4218% if isequal(get(handles.CheckCiv2,'Value'),0) & isequal(get(handles.CheckPatch2,'Value'),0)
4219%     set(handles.ListPairCiv2,'Visible','off')
4220%     set(handles.subdir_civ2,'Visible','off')
4221%     set(handles.subdir_civ2_text,'Visible','off')
4222% end
4223
4224%------------------------------------------------------------------------
4225function enable_patch2(handles,state)
4226%------------------------------------------------------------------------
4227if state
4228RootName=get(handles.RootName,'String');
4229    if isempty(RootName)
4230         msgbox_uvmat('ERROR','No input file')
4231        return
4232    end
4233    set(handles.Patch2,'Visible','on')
4234else
4235set(handles.Patch2,'Visible','off')
4236end
4237% set(handles.frame_patch2,'BackgroundColor',[1 1 0])
4238% set(handles.num_SmoothParam,'Visible','on')
4239% set(handles.num_SmoothingParam,'Visible','on')
4240% set(handles.num_MaxDiff,'Visible','on')
4241% set(handles.thresh_text2,'Visible','on')
4242% set(handles.num_SubdomainSize,'Visible','on')
4243% set(handles.subdomain_text2,'Visible','on')
4244% set(handles.num_Nx,'Visible','on')
4245% set(handles.num_Ny,'Visible','on')
4246% set(handles.nx_patch2_title,'Visible','on')
4247% set(handles.ny_patch2_title,'Visible','on')
4248% % set(handles.get_gridpatch2,'Visible','on')
4249% % set(handles.grid_patch2,'Visible','on')
4250% set(handles.ListPairCiv2,'Visible','on')
4251% set(handles.subdir_civ2,'Visible','on')
4252% set(handles.subdir_civ2_text,'Visible','on')
4253% stereo_test=get(handles.ListCompareMode,'Value');
4254% if stereo_test==3
4255%     set(handles.CheckStereo,'Visible','on')
4256% end
4257
4258% %------------------------------------------------------------------------
4259% function desable_patch2(handles)
4260% %------------------------------------------------------------------------
4261% %set(handles.checkpatch2,'Visible','off')
4262% return
4263% set(handles.frame_patch2,'BackgroundColor',[0.831 0.816 0.784])
4264% set(handles.num_SmoothParam,'Visible','off')
4265% set(handles.num_SmoothingParam,'Visible','off')
4266% set(handles.num_MaxDiff,'Visible','off')
4267% set(handles.thresh_text2,'Visible','off')
4268% set(handles.num_SubdomainSize,'Visible','off')
4269% set(handles.subdomain_text2,'Visible','off')
4270% set(handles.num_Nx,'Visible','off')
4271% set(handles.num_Ny,'Visible','off')
4272% set(handles.nx_patch2_title,'Visible','off')
4273% set(handles.ny_patch2_title,'Visible','off')
4274% % set(handles.get_gridpatch2,'Visible','off')
4275% % set(handles.grid_patch2,'Visible','off')
4276% if isequal(get(handles.CheckCiv2,'Value'),0) & isequal(get(handles.CheckFix2,'Value'),0)
4277%     set(handles.ListPairCiv2,'Visible','off')
4278%     set(handles.subdir_civ2,'Visible','off')
4279%     set(handles.subdir_civ2_text,'Visible','off')
4280% end
4281% set(handles.CheckStereo,'Visible','off')
4282%------------------------------------------------------------------------
4283function enable_pair1(handles,state)
4284%------------------------------------------------------------------------
4285set(handles.subdir_civ1,'Visible',state)
4286set(handles.ListSubdirCiv1,'Visible',state)
4287set(handles.SUBDIR_CIV1_txt,'Visible',state)
4288%set(handles.frame_subdirciv1,'Visible',state)
4289set(handles.ListPairCiv1,'Visible',state)
4290set(handles.PairCiv1_title,'Visible',state)
4291%set(handles.dt_unit,'Visible',state)
4292%set(handles.PAIR_frame,'Visible',state)
4293
4294%------------------------------------------------------------------------
4295% --- Read the parameters for checkciv1 on the interface
4296function par=read_param_civ1(handles,filecell)
4297%------------------------------------------------------------------------
4298ibx_val=str2double(get(handles.num_Bx,'String'));
4299par.ibx=num2str(ibx_val);
4300iby_val=str2double(get(handles.num_By,'String'));
4301par.iby=num2str(iby_val);
4302isx=get(handles.num_Sx,'String');
4303if isnan(str2double(isx)), isx='41'; set(handles.num_Sx,'String','41'), end; %default
4304if str2double(isx)<ibx_val+8,isx=num2str(ibx_val+8); set(handles.num_Sx,'String',num2str(ibx_val+8)); end
4305isy=get(handles.num_Sy,'String');
4306if isnan(str2double(isy)), isy='41'; set(handles.num_Sy,'String','41'), end;%default
4307if str2double(isy)<iby_val+8,isy=num2str(iby_val+8); set(handles.num_Sy,'String',num2str(iby_val+8)); end
4308par.isx=get(handles.num_Sx,'String');
4309par.isy=get(handles.num_Sy,'String');
4310par.shiftx=get(handles.num_Shiftx,'String');
4311par.shifty=get(handles.num_Shifty,'String');
4312if isnan(str2double(par.isx))
4313    par.isx='41';%default
4314    set(handles.num_Sx,'String','41');
4315end
4316if isnan(str2double(par.isy))
4317    par.isy='41'; %default
4318    set(handles.num_Sy,'String','41');
4319end
4320if isnan(str2double(par.shiftx))
4321    par.shiftx='0';%default
4322    set(handles.num_Shiftx,'String','0');
4323end
4324if isnan(str2double(par.shifty))
4325    par.shifty='0'; %default
4326    set(handles.num_Shifty,'String','0');
4327end
4328par.rho=get(handles.num_Rho,'String');
4329if isequal(get(handles.num_Rho,'Style'),'popupmenu')
4330    index=get(handles.num_Rho,'Value');
4331    par.rho=par.rho{index};
4332end
4333dummy=get(handles.num_Dx,'String');
4334par.dx=dummy{1};
4335dummy=get(handles.num_Dy,'String');
4336par.dy=dummy{1};
4337if isnan(str2double(par.dx))
4338    if isempty(get(handles.txt_GridName,'String'));
4339        par.dx='0'; %just read by civ program, not used
4340    else
4341        par.dx='20';%default
4342        set(handles.num_Dx,'String','20');
4343    end
4344end
4345if isnan(str2double(par.dy))
4346    if isempty(get(handles.txt_GridName,'String'));
4347        par.dy='0';%just read by civ program, not used
4348    else
4349        par.dy='20';%default
4350        set(handles.dy_civ1_title,'String','20');
4351    end
4352end
4353par.pxcmx='1'; %velocities are expressed in pixel dispalcement
4354par.pxcmy='1';
4355% if exist('file_ima','var')
4356A=imread(filecell.ima1.civ1{1,1});%read the first image to get the size
4357sizim=size(A);
4358par.npx=num2str(sizim(2));
4359par.npy=num2str(sizim(1));
4360%TODO : civ should not need npx and npy
4361
4362
4363% end
4364%time=get(handles.RootName,'UserData'); %get the set of times
4365dummy=get(handles.txt_GridName,'String');
4366par.gridname=dummy{1};
4367par.gridflag='y';
4368if strcmp(par.gridname,'')|| isempty(par.gridname)
4369    par.gridname='nogrid';
4370    par.gridflag='n';
4371end
4372
4373%------------------------------------------------------------------------
4374function par=read_param_fix1(handles,filecell)
4375%------------------------------------------------------------------------
4376    par.flagindex1(1)=get(handles.CheckFmin2, 'Value');
4377    par.flagindex1(2)=get(handles.CheckF3, 'Value');
4378    par.flagindex1(3)=get(handles.CheckF2, 'Value');
4379    par.thresh_vecC1=str2double(get(handles.num_MinCorr,'String'));%threshold on image correlation vec_C
4380    par.thresh_vel1=str2double(get(handles.num_MaxVel,'String'));%threshold on velocity modulus
4381    par.test_mask=get(handles.CheckMask,'Value');
4382    par.nbslice_mask=get(handles.txt_MaskName,'UserData'); % get the number of slices (= number of masks)
4383    %%%%%%%%%%%%%COMPLETER LE PROGRAMME FIX
4384    %     inf_sup=get(handles.inf_sup1,'Value');80
4385    %     fileref=get(handles.ref_fix1,'String');
4386    %     refpath=get(handles.ref_fix1,'UserData');
4387    %     fileref=fullfile(refpath,fileref);
4388    par.menu=get(handles.field_ref1,'String');
4389    par.index=get(handles.field_ref1,'Value');
4390    if isempty(par.menu)
4391        par.fieldchoice='';
4392    else
4393        par.fieldchoice=menu{index};
4394        msgbox_uvmat('WARNING','reference field is not used presently with batch, use RUN option')
4395    end
4396           if par.test_mask==0
4397                par.maskname='';
4398            else
4399                maskdispl=get(handles.txt_MaskName,'String');
4400                nbslice_mask=str2double(maskdispl(1:end-4)); %
4401                num1_mask=mod(num1_civ1(ifile)-1,nbslice_mask)+1;
4402                maskbase=[filecell.filebase '_' maskdispl];
4403                par.maskname=name_generator(maskbase,num1_mask,1,'.png','_i');
4404           end
4405 
4406%------------------------------------------------------------------------
4407function par=read_param_patch1(handles)
4408%------------------------------------------------------------------------
4409
4410par.rho_patch=str2double(get(handles.rho_patch1,'String'));
4411if isnan(par.rho_patch)
4412    par.rho_patch='1000';
4413    set(handles.rho_patch1,'String','1')
4414else
4415    par.rho_patch=num2str(1000*par.rho_patch);
4416end
4417par.nx_patch=get(handles.nx_patch1,'String');
4418par.ny_patch=get(handles.ny_patch1,'String');
4419if isnan(str2double(par.nx_patch))
4420    par.nx_patch='50' ;%default
4421    set(handles.nx_patch1,'String','50');
4422end
4423if isnan(str2double(par.ny_patch))
4424    par.ny_patch='50' ;%default
4425    set(handles.ny_patch1,'String','50');
4426end
4427par.subdomain_patch=get(handles.subdomain_patch1,'String');
4428par.thresh_patch=get(handles.thresh_patch1,'String');
4429
4430           
4431%------------------------------------------------------------------------
4432function par=read_param_civ2(handles,file_ima)
4433%------------------------------------------------------------------------
4434par.ibx=get(handles.ibx_civ2,'String');
4435par.iby=get(handles.iby_civ2,'String');
4436par.rho=get(handles.rho_civ2,'String');
4437par.decimal=int2str(get(handles.decimal,'Value'));
4438par.deformation=int2str(get(handles.deformation,'Value'));
4439par.dx=get(handles.num_Dx,'String');
4440par.dy=get(handles.num_Dy,'String');
4441if isnan(str2double(par.dx))
4442    if isempty(get(handles.txt_GridName,'String'));
4443        par.dx='0'; %just read by civ program, not used
4444    else
4445        par.dx='20';%default
4446        set(handles.num_Dx,'String','20');
4447    end
4448end
4449if isnan(str2double(par.dy))
4450    if isempty(get(handles.txt_GridName,'String'));
4451        par.dy='0';%just read by civ program, not used
4452    else
4453        par.dy='20';%default
4454        set(handles.num_Dy,'String','20');
4455    end
4456end
4457par.pxcmx='1';
4458par.pxcmy='1';
4459A=imread(file_ima);%read the first image to get the size
4460sizim=size(A);
4461par.npx=num2str(sizim(2));
4462par.npy=num2str(sizim(1));
4463%time=get(handles.RootName,'UserData'); %get the set of times
4464par.gridname=get(handles.txt_GridName,'String');
4465par.gridflag='y';
4466if strcmp(par.gridname,'')|| isempty(par.gridname)
4467    par.gridname='nogrid';
4468    par.gridflag='n';
4469end
4470
4471function cmd_CIV1=CIV1_CMD(filename,param)
4472%TODO : include filename in par_civ1
4473%------------------------------------------------------------------------
4474%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4475
4476%changes : filename_cmx -> filename ( no extension )
4477
4478filename=regexprep(filename,'.nc','');
4479
4480if isequal(Param.Civ1.Dt,'0')
4481    Param.Civ1.Dt='1' ;%case of 'displacement' mode
4482end
4483Param.Civ1.filename_ima_a=regexprep(Param.Civ1.filename_ima_a,'.png','');
4484Param.Civ1.filename_ima_b=regexprep(Param.Civ1.filename_ima_b,'.png','');
4485fid=fopen([filename '.civ1.cmx'],'w');
4486fprintf(fid,['##############   CMX file' '\n' ]);
4487fprintf(fid,   ['FirstImage ' regexprep(Param.Civ1.filename_ima_a,'\\','\\\\') '\n' ]);% for windows compatibility
4488fprintf(fid,   ['LastImage  ' regexprep(Param.Civ1.filename_ima_b,'\\','\\\\') '\n' ]);% for windows compatibility
4489fprintf(fid,  ['XX' '\n' ]);
4490fprintf(fid,  ['Mask ' Param.Civ1.maskflag '\n' ]);
4491fprintf(fid,  ['MaskName ' regexprep(Param.Civ1.maskname,'\\','\\\\') '\n' ]);
4492fprintf(fid,   ['ImageSize ' Param.Civ1.npx ' ' Param.Civ1.npy '\n' ]);   %VERIFIER CAS GENERAL ?
4493fprintf(fid,   ['CorrelationBoxesSize ' Param.Civ1.ibx ' ' Param.Civ1.iby '\n' ]);
4494fprintf(fid,   ['SearchBoxeSize ' Param.Civ1.isx ' ' Param.Civ1.isy '\n' ]);
4495fprintf(fid,   ['RO ' Param.Civ1.rho '\n' ]);
4496fprintf(fid,   ['GridSpacing ' Param.Civ1.dx ' ' Param.Civ1.dy '\n' ]);
4497fprintf(fid,   ['XX 1.0' '\n' ]);
4498fprintf(fid,   ['Dt_TO ' Param.Civ1.Dt ' ' Param.Civ1.T0 '\n' ]);
4499fprintf(fid,  ['PixCmXY ' Param.Civ1.pxcmx ' ' Param.Civ1.pxcmy '\n' ]);
4500fprintf(fid,  ['XX 1' '\n' ]);
4501fprintf(fid,   ['ShiftXY ' Param.Civ1.shiftx ' '  Param.Civ1.shifty '\n' ]);
4502fprintf(fid,  ['Grid ' Param.Civ1.gridflag '\n' ]);
4503fprintf(fid,   ['GridName ' regexprep(Param.Civ1.gridname,'\\','\\\\') '\n' ]);
4504fprintf(fid,   ['XX 85' '\n' ]);
4505fprintf(fid,   ['XX 1.0' '\n' ]);
4506fprintf(fid,   ['XX 1.0' '\n' ]);
4507fprintf(fid,   ['Hart 1' '\n' ]);
4508fprintf(fid,  [ 'DecimalShift 0' '\n' ]);
4509fprintf(fid,   ['Deformation 0' '\n' ]);
4510fprintf(fid,  ['CorrelationMin 0' '\n' ]);
4511fprintf(fid,   ['IntensityMin 0' '\n' ]);
4512fprintf(fid,  ['SeuilImage n' '\n' ]);
4513fprintf(fid,   ['SeuilImageValues 0 4096' '\n' ]);
4514fprintf(fid,   ['ImageToUse ' Param.Civ1.term_a ' ' Param.Civ1.term_b '\n' ]); % VERIFIER ?
4515fprintf(fid,   ['ImageUsedBefore null null' '\n' ]);
4516fclose(fid);
4517
4518% cmd_CIV1=[sparam.Civ1Bin ' -f ' filename '.cmx >' filename '.log' ]; % redirect standard output to the log file
4519% cmd_CIV1=regexprep(cmd_CIV1,'\\','\\\\');
4520% namelog=regexprep(namelog,'\\','\\\\');
4521if(isunix)
4522    cmd_CIV1=['cp -f ' filename '.civ1.cmx ' filename '.cmx'];
4523    cmd_CIV1=[cmd_CIV1 '\n'...
4524        Param.xml.Civ1Bin ' -f ' filename '.cmx >' filename '.log' ]; % redirect standard output to the log file, the result file is named [filename '.nc'] by CIVx
4525    cmd_CIV1=[cmd_CIV1 '\n' 'mv ' filename '.log' ' ' filename '.civ1.log' '\n' 'chmod g+w ' filename '.civ1.log' '\n' 'chmod g+w ' filename '.nc'];%rename .log as .civ1.log and set the netcdf result file for group user writting
4526   % cmd_CIV1=[cmd_CIV1 '\n' 'mv ' filename '.cmx' ' ' filename '.checkciv1.cmx' '\n'];%rename .cmx as .checkciv1.cmx
4527else %Windows system
4528%                     flname=regexprep(flname,'\\','\\\\');
4529%                     cmd=[cmd 'copy /Y "' flname '.checkciv1.cmx" "' flname '.cmx"\n'];
4530%     filename=regexprep(filename,'\\','\\\\');
4531    cmd_CIV1=['copy /Y "' filename '.civ1.cmx" "' filename '.cmx"'];% copy the .civ1.cmx parameter file to .cmx
4532    cmd_CIV1=[cmd_CIV1 '\n "' regexprep(Param.xml.Civ1Bin,'\\','\\\\') '" -f "' filename '.cmx" >"' filename '.log"' ]; % redirect standard output to the log file
4533%     namelog=regexprep(namelog,'\\','\\\\');
4534    cmd_CIV1=[cmd_CIV1 '\n ' 'copy /Y "' filename '.log' '" "' filename '.civ1.log"']; %preserve the log file as .civ1.log
4535
4536    %  cmd_CIV1=[cmd_CIV1 '\n' 'copy /Y "' filename '.cmx' '" "' filename '.checkciv1.cmx"'];
4537end
4538
4539%------------------------------------------------------------------------
4540% --- CheckCiv1  Unified
4541function xml_civ1_parameters=CIV1_CMD_Unified(filename,namelog,par)
4542%------------------------------------------------------------------------
4543%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4544%global CivBin%name of the executable for checkciv1 calculation
4545
4546civ1.image1=par.filename_ima_a;
4547civ1.image2=par.filename_ima_b;
4548civ1.imageSize_X=par.npx;
4549civ1.imageSize_Y=par.npy;
4550civ1.outputFileName=[filename '.nc'];
4551civ1.correlationBoxesSize_X=par.ibx;
4552civ1.correlationBoxesSize_Y=par.iby;
4553civ1.searchBoxesSize_X=par.isx;
4554civ1.searchBoxesSize_Y=par.isy;
4555civ1.globalShift_X=par.shiftx;
4556civ1.globalShift_Y=par.shifty;
4557civ1.ro=par.rho;
4558civ1.hart='y';
4559if isequal(par.gridflag,'y')
4560    civ1.grid=par.gridname;
4561else
4562    civ1.grid='n';
4563    civ1.gridSpacing_X=par.dx;
4564    civ1.gridSpacing_Y=par.dy;
4565end
4566if isequal(par.maskflag,'y')
4567    civ1.mask=par.maskname;
4568end
4569civ1.dt=par.Dt;
4570civ1.unit='pixel';
4571civ1.absolut_time_T0=par.T0;
4572civ1.pixcmx=par.pxcmx;
4573civ1.pixcmy=par.pxcmy;
4574civ1.convectFlow='n';
4575
4576xml_civ1_parameters=civ1;
4577
4578%------------------------------------------------------------------------
4579% --- CheckCiv2  Unified
4580function civ2=CIV2_CMD_Unified(filename,namelog,par)
4581%------------------------------------------------------------------------
4582%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4583%global CivBin%name of the executable for checkciv1 calculation
4584
4585filename=regexprep(filename,'.nc','');
4586
4587civ2.image1=par.filename_ima_a;
4588civ2.image2=par.filename_ima_b;
4589civ2.imageSize_X=par.npx;
4590civ2.imageSize_Y=par.npy;
4591civ2.inputFileName=[par.filename_nc1 '.nc'];
4592civ2.outputFileName=[filename '.nc'];
4593civ2.correlationBoxesSize_X=par.ibx;
4594civ2.correlationBoxesSize_Y=par.iby;
4595civ2.ro=par.rho;
4596%checkciv2.decimalShift=par.decimal;
4597%checkciv2.deformation=par.deformation;
4598if isequal(par.decimal,'1')
4599    civ2.decimalShift='y';
4600else
4601    civ2.decimalShift='n';
4602end
4603if isequal(par.deformation,'1')
4604    civ2.deformation='y';
4605else
4606    civ2.deformation='n';
4607end
4608if isequal(par.gridflag,'y')
4609    civ2.grid=par.gridname;
4610else
4611    civ2.grid='n';
4612    civ2.gridSpacing_X=par.dx;
4613    civ2.gridSpacing_Y=par.dy;
4614end
4615civ2.gridSpacing_X='10';
4616civ2.gridSpacing_Y='10';%NOTE: faut mettre gridSpacing pourque ca tourne, meme si c'est la grille qui est utilisee
4617if isequal(par.maskflag,'y')
4618    civ2.mask=par.maskname;
4619else
4620    civ2.mask='n';
4621end
4622civ2.dt=par.Dt;
4623civ2.unit='pixel';
4624civ2.absolut_time_T0=par.T0;
4625civ2.pixcmx=par.pxcmx;
4626civ2.pixcmy=par.pxcmy;
4627civ2.convectFlow='n';
4628civ2.pixcmx=par.pxcmx;
4629civ2.pixcmy=par.pxcmy;
4630civ2.convectFlow='n';
4631
4632%------------------------------------------------------------------------
4633% --- CheckCiv2  CheckCiv2  CheckCiv2 CheckCiv2
4634function cmd_CIV2=CIV2_CMD(filename,param)
4635%------------------------------------------------------------------------
4636%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4637% global civ2Bin sge%name of the executable for checkciv1 calculation
4638filename=regexprep(filename,'.nc','');
4639if isequal(param.civ2.Dt,'0')
4640    param.civ2.Dt='1' ;%case of 'displacement' mode
4641end
4642param.civ2.filename_ima_a=regexprep(param.civ2.filename_ima_a,'.png','');
4643param.civ2.filename_ima_b=regexprep(param.civ2.filename_ima_b,'.png','');% bug : .png appears two times ?
4644[fid,errormsg]=fopen([filename '.civ2.cmx'],'w');
4645if isequal(fid,-1)
4646    msgbox_uvmat('ERROR',errormsg)
4647    cmd_CIV2='';
4648    return
4649end
4650fprintf(fid,['##############   CMX file' '\n' ]);
4651fprintf(fid,   ['FirstImage ' regexprep(param.civ2.filename_ima_a,'\\','\\\\') '\n' ]);% for windows compatibility
4652fprintf(fid,   ['LastImage  ' regexprep(param.civ2.filename_ima_b,'\\','\\\\') '\n' ]);% for windows compatibility
4653fprintf(fid,  ['XX' '\n' ]);
4654fprintf(fid, ['Mask ' param.civ2.maskflag '\n' ]);
4655fprintf(fid, ['MaskName ' regexprep(param.civ2.maskname,'\\','\\\\') '\n' ]);% for windows compatibility
4656fprintf(fid, ['ImageSize ' param.civ2.npx ' ' param.civ2.npy '\n' ]);   %VERIFIER CAS GENERAL ?
4657fprintf(fid, ['CorrelationBoxesSize ' param.civ2.ibx ' ' param.civ2.iby '\n' ]);
4658fprintf(fid, ['SearchBoxeSize ' param.civ2.ibx ' ' param.civ2.iby '\n']);
4659fprintf(fid, ['RO ' param.civ2.rho '\n']);
4660fprintf(fid, ['GridSpacing ' param.civ2.dx ' ' param.civ2.dy '\n']);
4661fprintf(fid, ['XX 1.0' '\n' ]);
4662fprintf(fid, ['Dt_TO ' param.civ2.Dt ' ' param.civ2.T0 '\n' ]);
4663fprintf(fid, ['PixCmXY ' param.civ2.pxcmx ' ' param.civ2.pxcmy '\n' ]);
4664fprintf(fid, ['XX 1' '\n' ]);
4665fprintf(fid, 'ShiftXY 0 0\n');
4666fprintf(fid, ['Grid ' param.civ2.gridflag '\n' ]);
4667fprintf(fid, ['GridName ' regexprep(param.civ2.gridname,'\\','\\\\') '\n']);
4668fprintf(fid, ['XX 85' '\n' ]);
4669fprintf(fid, ['XX 1.0' '\n' ]);
4670fprintf(fid, ['XX 1.0' '\n' ]);
4671fprintf(fid, ['Hart 1' '\n' ]);
4672fprintf(fid, ['DecimalShift ' param.civ2.decimal '\n']);
4673fprintf(fid, ['Deformation ' param.civ2.deformation '\n']);
4674fprintf(fid,  ['CorrelationMin 0' '\n' ]);
4675fprintf(fid,   ['IntensityMin 0' '\n' ]);
4676fprintf(fid,  ['SeuilImage n' '\n' ]);
4677fprintf(fid,   ['SeuilImageValues 0 4096' '\n' ]);
4678fprintf(fid,   ['ImageToUse ' param.civ2.term_a ' ' param.civ2.term_b '\n' ]); % VERIFIER ?
4679fprintf(fid, ['ImageUsedBefore ' regexprep(param.civ2.filename_nc1,'\\','\\\\') '\n']);
4680fclose(fid);
4681
4682if(isunix)
4683    cmd_CIV2=['cp -f ' filename '.civ2.cmx ' filename '.cmx\n'];
4684    cmd_CIV2=[cmd_CIV2 Param.xml.Civ2Bin ' -f ' filename  '.cmx >' filename '.log' ]; % redirect standard output to the log file, the result file is named [filename '.nc'] by CIVx
4685    cmd_CIV2=[cmd_CIV2 '\n' 'mv ' filename '.log' ' ' filename '.civ2.log' '\n' 'chmod g+w ' filename '.nc'];%preserve the log file as .civ2.log
4686%    cmd_CIV2=[cmd_CIV2 '\n' 'mv ' filename '.cmx' ' ' filename '.checkciv2.cmx' '\n'];%rename .cmx as .checkciv2.cmx, the result file is named [filename '.nc'] by CIVx
4687
4688else
4689    filename=regexprep(filename,'\\','\\\\');
4690    cmd_CIV2=['copy /Y "' filename '.civ2.cmx" "' filename '.cmx"'];
4691    cmd_CIV2=[cmd_CIV2 '\n "' regexprep(Param.xml.Civ2Bin,'\\','\\\\') '" -f "' filename  '.cmx" >"' filename '.log"' ]; % redirect standard output to the log file
4692    cmd_CIV2=[cmd_CIV2 '\n ' 'copy /Y "' filename '.log' '" "' filename '.civ2.log"'];
4693 %    cmd_CIV2=[cmd_CIV2 '\n' 'copy /Y "' filename '.cmx' '" "' filename '.checkciv2.cmx"'];
4694end
4695
4696
4697%------------------------------------------------------------------------
4698%--read images and convert them to the uint16 format used for PIV
4699function A=read_image(filename,type_ima,num,movieobject)
4700%------------------------------------------------------------------------
4701%num is the view number needed for an avi movie
4702switch type_ima
4703    case 'movie'
4704        A=read(movieobject,num);
4705    case 'avi'
4706        mov=aviread(filename,num);
4707        A=frame2im(mov(1));
4708    case 'multimage'
4709        A=imread(filename,num);
4710    case 'image'
4711        A=imread(filename);
4712end
4713siz=size(A);
4714if length(siz)==3;%color images
4715    A=sum(double(A),3);
4716    A=uint16(A);
4717end
4718
4719%------------------------------------------------------------------------
4720function ref_i_Callback(hObject, eventdata, handles)
4721%------------------------------------------------------------------------
4722mode_list=get(handles.ListPairMode,'String');
4723mode_value=get(handles.ListPairMode,'Value');
4724mode=mode_list{mode_value};
4725find_netcpair_civ1(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4726if isequal(mode,'series(Di)') || ...% we do patch2 only
4727        (get(handles.CheckCiv2,'Value')==0 && get(handles.CheckCiv1,'Value')==0 && get(handles.CheckFix1,'Value')==0 && get(handles.CheckPatch1,'Value')==0)
4728    find_netcpair_civ2(hObject, eventdata, handles);
4729end
4730
4731%------------------------------------------------------------------------
4732function ref_j_Callback(hObject, eventdata, handles)
4733%------------------------------------------------------------------------
4734mode_list=get(handles.ListPairMode,'String');
4735mode_value=get(handles.ListPairMode,'Value');
4736mode=mode_list{mode_value};
4737if isequal(get(handles.CheckCiv1,'Value'),0)|| isequal(mode,'series(Dj)')
4738    find_netcpair_civ1(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4739end
4740if isequal(mode,'series(Dj)') || ...
4741        (get(handles.CheckCiv2,'Value')==0 && get(handles.CheckCiv1,'Value')==0 && get(handles.CheckFix1,'Value')==0 && get(handles.CheckPatch1,'Value')==0)
4742    find_netcpair_civ2(hObject, eventdata, handles);
4743end
4744
4745%------------------------------------------------------------------------
4746function ref_i_civ2_Callback(hObject, eventdata, handles)
4747%------------------------------------------------------------------------
4748mode_list=get(handles.ListPairMode,'String');
4749mode_value=get(handles.ListPairMode,'Value');
4750mode=mode_list{mode_value};
4751find_netcpair_civ2(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4752
4753%------------------------------------------------------------------------
4754function ref_j_civ2_Callback(hObject, eventdata, handles)
4755%------------------------------------------------------------------------
4756mode_list=get(handles.ListPairMode,'String');
4757mode_value=get(handles.ListPairMode,'Value');
4758mode=mode_list{mode_value};
4759if  isequal(mode,'series(Dj)')
4760    find_netcpair_civ2(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4761end
4762
4763%------------------------------------------------------------------------
4764% --- Executes on button press in ListCompareMode.
4765function ListCompareMode_Callback(hObject, eventdata, handles)
4766%------------------------------------------------------------------------
4767test=get(handles.ListCompareMode,'Value');
4768if test==2 || test==3 % case 'dispalcemen' or 'stereo PIV'
4769    filebase=get(handles.RootName,'String');
4770    browse=get(handlesRootName,'Userdata');
4771    browse.nom_type_ima1=browse.nom_type_ima;
4772    set(handlesRootName,'UserData',browse);
4773    set(handles.sub_txt,'Visible','on')
4774    set(handles.RootName_1,'Visible','On');%mkes the second file input window visible
4775    mode_store=get(handles.ListPairMode,'String');%get the present 'mode'
4776    set(handles.ListCompareMode,'UserData',mode_store);%store the mode display
4777    set(handles.ListPairMode,'Visible','off')
4778    if test==2
4779        set(handles.ListPairMode,'Visible','off')
4780        set(handles.ListPairMode,'Value',1) % mode 'civX' selected by default
4781    else
4782        set(handles.ListPairMode,'Visible','on')
4783        set(handles.ListPairMode,'Value',3) % mode 'Matlab' selected for stereo
4784    end
4785   
4786    %% menuopen an image file with the browser
4787    ind_opening=1;%default
4788    browse.incr_pair=[0 0]; %default
4789    oldfile=get(handles.RootName,'String');
4790     menu={'*.xml;*.civ;*.png;*.jpg;*.tif;*.avi;*.AVI;*.nc;', ' (*.xml,*.civ,*.png,*.jpg ,.tif, *.avi,*.nc)';
4791       '*.xml',  '.xml files '; ...
4792        '*.civ',  '.civ files '; ...
4793        '*.png','.png image files'; ...
4794        '*.jpg',' jpeg image files'; ...
4795        '*.tif','.tif image files'; ...
4796        '*.avi;*.AVI','.avi movie files'; ...
4797        '*.nc','.netcdf files'; ...
4798        '*.*',  'All Files (*.*)'};
4799    [FileName, PathName, filtindex] = uigetfile( menu, 'Pick a file of the second series',oldfile);
4800    fileinput=[PathName FileName];%complete file name
4801    sizf=size(fileinput);
4802    if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
4803    [path,name,ext]=fileparts(fileinput);
4804    [path1]=fileparts(filebase);
4805    if isunix
4806        [status,path]=system(['readlink ' path])
4807        [status,path1]=system(['readlink ' path1])% look for the true path in case of symbolic paths
4808    end
4809    if ~strcmp(path1,path)
4810        msgbox_uvmat('ERROR','The second image series must be in the same directory as the first one')
4811        return
4812     end
4813%     set(handles.RootName_1,'String',name);
4814    [RootPath,RootFile,field_count,str2,str_a,str_b,xx,nom_type,subdir]=name2display(name);
4815    set(handles.RootName_1,'String',RootFile);
4816    browse=get(handlesRootName,'UserData');
4817    browse.nom_type_ima_1=nom_type;
4818    set(handlesRootName,'UserData',browse)
4819   
4820    %check image extension
4821    if ~strcmp(ext,get(handles.ImaExt,'String'))
4822        msgbox_uvmat('ERROR','The second image series must have the same extension name as the first one')
4823        return
4824    end
4825   
4826    %% check coincidence of image sizes
4827%     ref_i=get(handles.ref_i,'string');
4828%     ref_j=get(handles.ref_j,'string');
4829%     [filecell,num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2,nom_type_nc]=set_civ_filenames(handles,ref_i,ref_j,[1 0 0 0 0 0]);
4830%     A=imread(filecell.ima1.checkciv1{1});
4831%     A_1=imread(fileinput);
4832%     npxy=size(A);
4833%     npxy_1=size(A_1);
4834%     if ~isequal(size(A),size(A_1))
4835%         msgbox_uvmat('ERROR','The two input image series do not have the same size')
4836%         return
4837%     end
4838else
4839    set(handles.ListPairMode,'Visible','on')
4840    set(handles.RootName_1,'Visible','Off');
4841    set(handles.sub_txt,'Visible','off')
4842    set(handles.RootName_1,'String',[]);
4843    mode_store=get(handles.ListCompareMode,'UserData');
4844    set(handles.ListPairMode,'Value',1)
4845    set(handles.ListPairMode,'String',mode_store)
4846    set(handles.test_stereo1,'Value',0)
4847    set(handles.CheckStereo,'Value',0)
4848    set(handles.ListPairMode,'Value',1) % mode 'civX' selected by default
4849end
4850if test==3 && get(handles.CheckPatch1,'Value')
4851    set(handles.test_stereo1,'Visible','on')
4852else
4853    set(handles.test_stereo1,'Visible','off')
4854end
4855if test==3 && get(handles.CheckPatch2,'Value')
4856    set(handles.CheckStereo,'Visible','on')
4857else
4858    set(handles.CheckStereo,'Visible','off')
4859end
4860mode_Callback(hObject, eventdata, handles)
4861
4862%------------------------------------------------------------------------
4863% --- Executes on button press in get_ref_fix1.
4864function get_ref_fix1_Callback(hObject, eventdata, handles)
4865%------------------------------------------------------------------------
4866filebase=get(handles.RootName,'String');
4867[FileName, PathName, filterindex] = uigetfile( ...
4868    {'*.nc', ' (*.nc)';
4869    '*.nc',  'netcdf files '; ...
4870    '*.*', 'All Files (*.*)'}, ...
4871    'Pick a file',filebase);
4872
4873fileinput=[PathName FileName];
4874sizf=size(fileinput);
4875if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
4876[Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
4877ref.filebase=fullfile(Path,File);
4878ref.num_a=stra2num(str_a);
4879ref.num_b=stra2num(str_b);
4880ref.num1=str2double(field_count);
4881ref.num2=str2double(str2);
4882browse=[];%initialisation
4883if ~isequal(ref.ext,'.nc')
4884    msgbox_uvmat('ERROR','the reference file must be in netcdf format (*.nc)')
4885    return
4886end
4887set(handles.ref_fix1,'String',[fullfile(ref.subdir,File) '....nc']);
4888set(handles.ref_fix1,'UserData',ref)
4889menu_field{1}='civ1';
4890Data=nc2struct(fileinput,[]);
4891if isfield(Data,'patch') && isequal(Data.patch,1)
4892    menu_field{2}='filter1';
4893end
4894if isfield(Data,'civ2') && isequal(Data.civ2,1)
4895    menu_field{3}='civ2';
4896end
4897if isfield(Data,'patch2') && isequal(Data.patch2,1)
4898    menu_field{4}='filter2';
4899end
4900set(handles.field_ref1,'String',menu_field);
4901set(handles.field_ref1,'Value',length(menu_field));
4902set(handles.inf_sup1,'Value',2);
4903set(handles.num_MaxVel,'String','1');%default threshold
4904set(handles.ref_fix1,'Enable','on')
4905
4906%------------------------------------------------------------------------
4907% --- Executes on button press in get_ref_fix2.
4908function get_ref_fix2_Callback(hObject, eventdata, handles)
4909%------------------------------------------------------------------------
4910if isequal(get(handles.get_ref_fix2,'Value'),1)
4911    filebase=get(handles.RootName,'String');
4912    [FileName, PathName, filterindex] = uigetfile( ...
4913        {'*.nc', ' (*.nc)';
4914        '*.nc',  'netcdf files '; ...
4915        '*.*', 'All Files (*.*)'}, ...
4916        'Pick a file',filebase);
4917    fileinput=[PathName FileName];
4918    sizf=size(fileinput);
4919    if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
4920    [Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
4921    ref.filebase=fullfile(Path,File);
4922    ref.num_a=stra2num(str_a);
4923    ref.num_b=stra2num(str_b);
4924    ref.num1=str2num(field_count);
4925    ref.num2=str2num(str2);
4926    browse=[];%initialisation
4927    if ~isequal(ref.ext,'.nc')
4928        msgbox_uvmat('ERROR','the reference file must be in netcdf format (*.nc)')
4929        return
4930    end
4931    set(handles.ref_fix2,'String',[fullfile(ref.subdir,File) '....nc']);
4932    set(handles.ref_fix2,'UserData',ref)
4933    menu_field{1}='civ1';
4934    Data=nc2struct(fileinput,[]);
4935    if isfield(Data,'patch') & isequal(Data.patch,1)
4936        menu_field{2}='filter1';
4937    end
4938    if isfield(Data,'civ2') & isequal(Data.civ2,1)
4939        menu_field{3}='civ2';
4940    end
4941    if isfield(Data,'patch2') & isequal(Data.patch2,1)
4942        menu_field{4}='filter2';
4943    end
4944    set(handles.field_ref2,'String',menu_field);
4945    set(handles.field_ref2,'Value',length(menu_field));
4946    set(handles.inf_sup2,'Value',2);
4947    set(handles.thresh_vel2,'String','1');%default threshold
4948    set(handles.ref_fix2,'Enable','on')
4949    set(handles.ref_fix2,'Visible','on')
4950    set(handles.field_ref2,'Visible','on')
4951else
4952    set(handles.ref_fix2,'Visible','off')
4953    set(handles.field_ref2,'Visible','off')
4954end
4955
4956%------------------------------------------------------------------------
4957function ref_fix1_Callback(hObject, eventdata, handles)
4958%------------------------------------------------------------------------
4959set(handles.inf_sup1,'Value',1);
4960set(handles.field_ref1,'Value',1)
4961set(handles.field_ref1,'String',{' '})
4962set(handles.ref_fix1,'UserData',[]);
4963set(handles.ref_fix1,'String','');
4964set(handles.thresh_vel1,'String','0');
4965
4966%------------------------------------------------------------------------
4967function ref_fix2_Callback(hObject, eventdata, handles)
4968%------------------------------------------------------------------------
4969set(handles.inf_sup2,'Value',1);
4970set(handles.field_ref2,'Value',1)
4971set(handles.field_ref2,'String',{' '})
4972set(handles.ref_fix2,'UserData',[]);
4973set(handles.ref_fix2,'String','');
4974set(handles.thresh_vel2,'String','0');
4975
4976%------------------------------------------------------------------------
4977% --- Executes on button press in test_stereo1.
4978function test_stereo1_Callback(hObject, eventdata, handles)
4979%------------------------------------------------------------------------
4980if isequal(get(handles.test_stereo1,'Value'),0)
4981    set(handles.subdomain_patch1,'Visible','on')
4982    set(handles.rho_patch1,'Visible','on')
4983else
4984    set(handles.subdomain_patch1,'Visible','off')
4985    set(handles.rho_patch1,'Visible','off')
4986end
4987
4988%------------------------------------------------------------------------
4989% --- Executes on button press in CheckStereo.
4990function StereoCheck_Callback(hObject, eventdata, handles)
4991%------------------------------------------------------------------------
4992if isequal(get(handles.CheckStereo,'Value'),0)
4993    set(handles.num_SubdomainSize,'Visible','on')
4994    set(handles.num_SmoothParam,'Visible','on')
4995else
4996    set(handles.num_SubdomainSize,'Visible','off')
4997    set(handles.num_SmoothParam,'Visible','off')
4998end
4999
5000%------------------------------------------------------------------------
5001% --- Executes on button press in check_Threshold.
5002function ImaThreshold_Callback(hObject, eventdata, handles)
5003%------------------------------------------------------------------------
5004if isequal(get(handles.check_Threshold,'Value'),1)
5005    set(handles.num_MinIma,'Visible','on')
5006    set(handles.num_MaxIma,'Visible','on')
5007else
5008    set(handles.num_MinIma,'Visible','off')
5009    set(handles.num_MaxIma,'Visible','off')
5010end
5011
5012%------------------------------------------------------------------------
5013% --- Executes on button press in ImaThreshold2.
5014function ImaThreshold2_Callback(hObject, eventdata, handles)
5015%------------------------------------------------------------------------
5016if isequal(get(handles.ImaThreshold2,'Value'),1)
5017    set(handles.MinIma2,'Visible','on')
5018    set(handles.MaxIma2,'Visible','on')
5019else
5020    set(handles.MinIma2,'Visible','off')
5021    set(handles.MaxIma2,'Visible','off')
5022end
5023
5024%------------------------------------------------------------------------
5025% --- Executes on button press in TestCiv1: display image correlation function
5026function TestCiv1_Callback(hObject, eventdata, handles)
5027%------------------------------------------------------------------------
5028set(handles.TestCiv1,'BackgroundColor',[1 1 0])
5029drawnow
5030test_civ1=get(handles.TestCiv1,'Value');
5031if test_civ1
5032    ref_i=str2double(get(handles.ref_i,'String'));
5033    if strcmp(get(handles.ref_j,'Visible'),'on')
5034        ref_j=str2double(get(handles.ref_j,'String'));
5035    else
5036        ref_j=1;%default
5037    end
5038    [filecell,num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2,nom_type_nc,file_ref_fix1,file_ref_fix2]=...
5039        set_civ_filenames(handles,ref_i,ref_j,[1 0 0 0 0 0]);
5040    Data.ListVarName={'ny','nx','A'};
5041    Data.VarDimName={'ny','nx',{'ny','nx'}};
5042    Data.A=imread(filecell.ima1.civ1{1});
5043    Data.ny=[size(Data.A,1) 1];
5044    Data.nx=[1 size(Data.A,2)];
5045    par_civ1=read_param_civ1(handles,filecell.ima1.civ1{1});
5046    par_civ1.filename_ima_a=filecell.ima1.civ1{1};
5047    par_civ1.filename_ima_b=filecell.ima2.civ1{1};
5048    par_civ1.T0=0;
5049    par_civ1.Dt=1;
5050    Param.Civ1=par_civ1;
5051    Data=civ_uvmat(Param);
5052    Data.ListVarName=[Data.ListVarName {'ny','nx','A'}];
5053    Data.VarDimName=[Data.VarDimName {'ny','nx',{'ny','nx'}}];
5054    Data.A=imread(filecell.ima1.civ1{1});
5055    Data.ny=[size(Data.A,1) 1];
5056    Data.nx=[1 size(Data.A,2)];
5057    hview_field=view_field(Data);
5058    set(0,'CurrentFigure',hview_field)
5059    hhview_field=guihandles(hview_field);
5060    set(hview_field,'CurrentAxes',hhview_field.axes3)
5061    ViewData=get(hview_field,'UserData');
5062    ViewData.CivHandle=handles.civ;% indicate the handle of the civ GUI in view_field
5063    ViewData.axes3.B=imread(filecell.ima2.civ1{1});%store the second image in the UserData of the GUI view_field
5064    ViewData.axes3.X=Data.Civ1_X; %keep the set of points in memeory
5065    ViewData.axes3.Y=Data.Civ1_Y;
5066    set(hview_field,'UserData',ViewData)
5067    corrfig=findobj(allchild(0),'tag','corrfig');% look for a current figure for image correlation display
5068    if isempty(corrfig)
5069        corrfig=figure;
5070        set(corrfig,'tag','corrfig')
5071        set(corrfig,'name','image correlation')
5072        set(corrfig,'DeleteFcn',{@closeview_field})%
5073    end
5074    set(handles.TestCiv1,'BackgroundColor',[1 0 0])
5075else
5076    corrfig=findobj(allchild(0),'tag','corrfig');% look for a current figure for image correlation display
5077    if ~isempty(corrfig)
5078        delete(corrfig)
5079    end
5080    hview_field=findobj(allchild(0),'tag','view_field');% look for view_field   
5081    if ~isempty(hview_field)
5082        delete(hview_field)
5083    end
5084end
5085
5086function closeview_field(gcbo,eventdata)
5087hview_field=findobj(allchild(0),'tag','view_field');% look for view_field   
5088    if ~isempty(hview_field)
5089        delete(hview_field)
5090    end
5091%-------------------------------------------------------------------
5092% --- Executes on button press in status.
5093function status_Callback(hObject, eventdata, handles)
5094%-------------------------------------------------------------------
5095val=get(handles.status,'Value');
5096if val==0
5097    set(handles.status,'BackgroundColor',[0 1 0])
5098    hfig=findobj(allchild(0),'name','civ_status');
5099    if ~isempty(hfig)
5100        delete(hfig)
5101    end
5102    return
5103end
5104set(handles.status,'BackgroundColor',[1 1 0])
5105drawnow
5106listtype={'civ1','fix1','patch1','civ2','fix2','patch2'};
5107Param.CheckCiv1=get(handles.CheckCiv1,'Value');
5108Param.CheckFix1=get(handles.CheckFix1,'Value');
5109Param.CheckPatch1=get(handles.CheckPatch1,'Value');
5110Param.CheckCiv2=get(handles.CheckCiv2,'Value');
5111Param.CheckFix2=get(handles.CheckFix2,'Value');
5112Param.CheckPatch2=get(handles.CheckPatch2,'Value');
5113box_test=[Param.CheckCiv1 Param.CheckFix1 Param.CheckPatch1 Param.CheckCiv2 Param.CheckFix2 Param.CheckPatch2];
5114
5115option_civ=find(box_test,1,'last');%last selected option (non-zero index of box_test)
5116filecell=get(handles.civ,'UserData');%retrieve the list of output files expected for PIV
5117test_new=0;
5118if ~isfield(filecell,'nc')
5119    test_new=1;
5120    [ref_i,ref_j,errormsg]=find_ref_indices(handles);
5121    if ~isempty(errormsg)
5122        msgbox_uvmat('ERROR',errormsg)
5123        return
5124    end
5125    filecell=set_civ_filenames(handles,ref_i,ref_j,box_test);%determine the output file expected from the GUI status
5126end
5127if ~isequal(box_test(4:6),[0 0 0])
5128    civ_files=filecell.nc.civ2;%case of civ2 operations
5129else
5130    civ_files=filecell.nc.civ1;
5131end
5132[root,filename,ext]=fileparts(civ_files{1});
5133[rootroot,subdir,extdir]=fileparts(root);
5134hfig=findobj(allchild(0),'name','civ_status');
5135if isempty(hfig)
5136    hfig=figure('DeleteFcn',@stop_status);
5137    set(hfig,'name','civ_status')
5138    hlist=uicontrol('Style','listbox','Units','normalized', 'Position',[0.05 0.09 0.9 0.71], 'Callback', @open_view_field,'tag','list');
5139    uicontrol('Style','edit','Units','normalized', 'Position', [0.05 0.87 0.9 0.1],'tag','msgbox','Max',2,'String','checking files...');
5140    uicontrol('Style','frame','Units','normalized', 'Position', [0.05 0.81 0.9 0.05]);
5141    uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.7 0.01 0.2 0.07],'String','OK','FontWeight','bold','FontUnits','normalized','FontSize',0.9,'Callback',@close_GUI);
5142    BarPosition=[0.05 0.81 0.01 0.05];
5143    hwaitbar=uicontrol('Style','frame','Units','normalized', 'Position',BarPosition ,'BackgroundColor',[1 0 0],'tag','waitbar');
5144    drawnow
5145end
5146% datnum=[];
5147Tabchar={};
5148nbfiles=numel(civ_files);
5149count=0;
5150testrecent=0;
5151while count<nbfiles
5152    count=0;
5153    datnum=zeros(1,nbfiles);
5154    for ifile=1:nbfiles
5155        detect=exist(civ_files{ifile},'file'); % check the existence of the file
5156        option=0;
5157        if detect==0
5158            option_str='not created';
5159        else
5160            datfile=dir(civ_files{ifile});
5161            if isfield(datfile,'datenum')
5162                datnum(ifile)=datfile.datenum;%only available in recent matlab versions
5163                testrecent=1;
5164            end
5165            filefound(ifile)={datfile.name};
5166            lastfield='';
5167            % check the content  netcdf file
5168            Data=nc2struct(civ_files{ifile},'ListGlobalAttribute','patch2','fix2','civ2','patch','fix');
5169            if ~isempty(Data.patch2) && isequal(Data.patch2,1)
5170                option=6;
5171                option_str='patch2';
5172            elseif ~isempty(Data.fix2) && isequal(Data.fix2,1)
5173                option=5;
5174                option_str='fix2';
5175            elseif ~isempty(Data.civ2) && isequal(Data.civ2,1);
5176                option=4;
5177                option_str='civ2';
5178            elseif ~isempty(Data.patch) && isequal(Data.patch,1);
5179                option=3;
5180                option_str='patch1';
5181            elseif ~isempty(Data.fix) && isequal(Data.fix,1);
5182                option=2;
5183                option_str='fix1';
5184            else
5185                option=1;
5186                option_str='civ1';
5187            end
5188        end
5189        if option >= option_civ
5190            count=count+1;
5191        end
5192        [rr,filename,ext]=fileparts(civ_files{ifile});
5193        Tabchar{ifile,1}=[fullfile([subdir extdir],filename) ext  '...' option_str];
5194    end
5195    datnum=datnum(datnum~=0);%keep the non zero values corresponding to existing files
5196    if isempty(datnum)
5197        if testrecent
5198            message='no civ result created yet';
5199        else
5200            message='';
5201        end
5202    else
5203        datnum=datnum(datnum~=0);%keep the non zero values corresponding to existing files
5204        [first,ind]=min(datnum);
5205        [last,indlast]=max(datnum);
5206        if test_new
5207            message='existing file status, no processing launched yet';
5208        else
5209        message={[num2str(count) ' file(s) done over ' num2str(nbfiles)] ;['oldest modification:  ' cell2mat(filefound(ind)) ' : ' datestr(first)];...
5210            ['latest modification:  ' cell2mat(filefound(indlast)) ' : ' datestr(last)]};
5211        end
5212    end
5213    hfig=findobj(allchild(0),'name','civ_status');
5214    if isempty(hfig)% the status list has been deleted
5215        return
5216    else
5217        hlist=findobj(hfig,'tag','list');
5218        hmsgbox=findobj(hfig,'tag','msgbox');
5219        hwaitbar=findobj(hfig,'tag','waitbar');
5220        set(hlist,'String',Tabchar)
5221        set(hmsgbox,'String', message)
5222        if count>0 && ~test_new
5223            BarPosition(3)=0.9*count/nbfiles;
5224            set(hwaitbar,'Position',BarPosition)
5225        end
5226    end
5227    set(hlist,'UserData',rootroot)
5228    pause(10)% wait 10 seconds for next check
5229end
5230
5231   
5232%------------------------------------------------------------------------   
5233% call 'view_field.fig' to display the  field selected in the list of 'status'
5234function open_view_field(hObject, eventdata)
5235%------------------------------------------------------------------------
5236list=get(hObject,'String');
5237index=get(hObject,'Value');
5238rootroot=get(hObject,'UserData');
5239filename=list{index};
5240ind_dot=findstr(filename,'...');
5241filename=filename(1:ind_dot-1);
5242filename=fullfile(rootroot,filename);
5243delete(get(hObject,'parent'))%delete the display figure to stop the check process
5244if exist(filename,'file')%visualise the vel field if it exists
5245    uvmat(filename)
5246    set(gcbo,'Value',1)
5247end
5248
5249%------------------------------------------------------------------------   
5250% launched by pressing OK on the status figure
5251function close_GUI(hObject, eventdata)
5252%------------------------------------------------------------------------
5253    delete(gcbf)
5254   
5255%------------------------------------------------------------------------   
5256% launched by deleting the status figure
5257function stop_status(hObject, eventdata)
5258%------------------------------------------------------------------------
5259hciv=findobj(allchild(0),'tag','civ');
5260hhciv=guidata(hciv);
5261set(hhciv.status,'value',0) %reset the status uicontrol in the GUI civ
5262set(hhciv.status,'BackgroundColor',[0 1 0])
5263
5264%------------------------------------------------------------------------
5265% % --- Executes on button press in ListPairMode.
5266% function CivMode_Callback(hObject, eventdata, handles)
5267% %------------------------------------------------------------------------
5268% Listprog=get(handles.ListPairMode,'String');
5269% index=get(handles.ListPairMode,'Value');
5270% prog=Listprog{index};
5271% switch prog
5272%     case 'CivX'
5273%         set(handles.thresh_patch1,'Visible','off')
5274%         set(handles.thresh_text1,'Visible','off')
5275%         set(handles.num_MaxDiff,'Visible','off')
5276%         set(handles.thresh_text2,'Visible','off')
5277%         set(handles.num_Rho,'Style','edit')
5278%         set(handles.num_Rho,'String','1')
5279%         set(handles.BATCH,'Enable','on')
5280%     case 'CivAll'
5281%         if get(handles.CheckPatch1,'Value')
5282%             set(handles.thresh_patch1,'Visible','on')
5283%             set(handles.thresh_text1,'Visible','on')
5284%         end
5285%         set(handles.num_Rho,'Style','edit')
5286%         set(handles.num_Rho,'String','1')
5287%         set(handles.BATCH,'Enable','on')
5288%     case 'CivUvmat'
5289%       
5290% end
5291
5292
5293% --- Executes on button press in CheckStereo.
5294function CheckStereo_Callback(hObject, eventdata, handles)
5295% hObject    handle to CheckStereo (see GCBO)
5296% eventdata  reserved - to be defined in a future version of MATLAB
5297% handles    structure with handles and user data (see GUIDATA)
5298
5299% Hint: get(hObject,'Value') returns toggle state of CheckStereo
5300
5301
5302
5303function num_SubdomainSize_Callback(hObject, eventdata, handles)
5304% hObject    handle to num_SubdomainSize (see GCBO)
5305% eventdata  reserved - to be defined in a future version of MATLAB
5306% handles    structure with handles and user data (see GUIDATA)
5307
5308% Hints: get(hObject,'String') returns contents of num_SubdomainSize as text
5309%        str2double(get(hObject,'String')) returns contents of num_SubdomainSize as a double
5310
5311
5312
5313function num_SmoothingParam_Callback(hObject, eventdata, handles)
5314% hObject    handle to num_SmoothingParam (see GCBO)
5315% eventdata  reserved - to be defined in a future version of MATLAB
5316% handles    structure with handles and user data (see GUIDATA)
5317
5318% Hints: get(hObject,'String') returns contents of num_SmoothingParam as text
5319%        str2double(get(hObject,'String')) returns contents of num_SmoothingParam as a double
5320
5321
5322
5323function num_MaxDiff_Callback(hObject, eventdata, handles)
5324% hObject    handle to num_MaxDiff (see GCBO)
5325% eventdata  reserved - to be defined in a future version of MATLAB
5326% handles    structure with handles and user data (see GUIDATA)
5327
5328% Hints: get(hObject,'String') returns contents of num_MaxDiff as text
5329%        str2double(get(hObject,'String')) returns contents of num_MaxDiff as a double
5330
5331
5332function num_Nx_Callback(hObject, eventdata, handles)
5333% hObject    handle to num_Nx (see GCBO)
5334% eventdata  reserved - to be defined in a future version of MATLAB
5335% handles    structure with handles and user data (see GUIDATA)
5336
5337% Hints: get(hObject,'String') returns contents of num_Nx as text
5338%        str2double(get(hObject,'String')) returns contents of num_Nx as a double
5339
5340
5341function num_Ny_Callback(hObject, eventdata, handles)
5342% hObject    handle to num_Ny (see GCBO)
5343% eventdata  reserved - to be defined in a future version of MATLAB
5344% handles    structure with handles and user data (see GUIDATA)
5345
5346% Hints: get(hObject,'String') returns contents of num_Ny as text
5347%        str2double(get(hObject,'String')) returns contents of num_Ny as a double
5348
5349
5350% --------------------------------------------------------------------
5351function MenuHelp_Callback(hObject, eventdata, handles)
5352path_civ=fileparts(which ('civ'));
5353helpfile=fullfile(path_civ,'uvmat_doc','uvmat_doc.html');
5354if isempty(dir(helpfile))
5355    msgbox_uvmat('ERROR','Please put the help file uvmat_doc.html in the sub-directory /uvmat_doc of the UVMAT package')
5356else
5357    addpath (fullfile(path_civ,'uvmat_doc'))
5358    web([helpfile '#civ'])
5359end
5360
5361% --------------------------------------------------------------------
5362function CivX_Callback(hObject, eventdata, handles)
5363%set(handles.thresh_patch1,'Visible','off')
5364set(handles.thresh_text1,'Visible','off')
5365set(handles.num_MaxDiff,'Visible','off')
5366set(handles.thresh_text2,'Visible','off')
5367set(handles.num_Rho,'Style','edit')
5368set(handles.num_Rho,'String','1')
5369set(handles.BATCH,'Enable','on')
5370
5371% --------------------------------------------------------------------
5372function Matlab_Callback(hObject, eventdata, handles)
5373set(handles.Matlab,'checked','on')
5374set(handles.CivX,'checked','off')
5375if get(handles.CheckPatch1,'Value')
5376    set(handles.thresh_patch1,'Visible','on')
5377    set(handles.thresh_text1,'Visible','on')
5378end
5379if get(handles.CheckPatch2,'Value')
5380    set(handles.num_MaxDiff,'Visible','on')
5381    set(handles.thresh_text2,'Visible','on')
5382end
5383set(handles.num_Rho,'Style','popupmenu')
5384set(handles.num_Rho,'Value',1)
5385set(handles.num_Rho,'String',{'1';'2'})
5386set(handles.BATCH,'Enable','off')
5387
5388% --------------------------------------------------------------------
5389% --- read a panel with handle 'handle' producing a structure 'struct'
5390function struct=read_panel(handle)
5391hchild=get(handle,'children');
5392for ichild=1:numel(hchild)
5393    if strcmp(get(hchild(ichild),'Visible'),'on')
5394        object_style=get(hchild(ichild),'Style');
5395        tag=get(hchild(ichild),'tag');
5396        check_input=1;%default
5397        switch object_style
5398            case 'edit'
5399                switch(tag(1:4))
5400                    case 'num_'
5401                        input=str2double(get(hchild(ichild),'String'));
5402                        %deal with undefined input: retrieve the default value stored as UserData
5403                        if isnan(input)
5404                            input=get(hchild(ichild),'UserData');
5405                            set(hchild(ichild),'String',num2str(input))
5406                        end
5407                    case 'txt_'
5408                        input=get(hchild(ichild),'String');
5409                end
5410                key=tag(5:end);
5411            case 'checkbox'
5412                input=get(hchild(ichild),'Value');
5413                key=tag(7:end);
5414            otherwise
5415                check_input=0;
5416        end
5417        if check_input
5418            eval(['struct.' key '=input;'])
5419        end
5420    end
5421end
5422
5423
5424
5425function cmd=cmd_civ1(filename,Param)
5426%TODO : include filename in par_civ1
5427%------------------------------------------------------------------------
5428%pixels per cm and matrix of the image times, read from the .civ file by uvmat
5429
5430%changes : filename_cmx -> filename ( no extension )
5431
5432filename=regexprep(filename,'.nc','')
5433
5434if isequal(Param.Civ1.Dt,'0')
5435    Param.Civ1.Dt='1' ;%case of 'displacement' mode
5436end
5437Param.Civ1.filename_ima_a=regexprep(Param.Civ1.filename_ima_a,'.png','');
5438Param.Civ1.filename_ima_b=regexprep(Param.Civ1.filename_ima_b,'.png','');
5439fid=fopen([filename '.civ1.cmx'],'w');
5440fprintf(fid,['##############   CMX file' '\n' ]);
5441fprintf(fid,   ['FirstImage ' regexprep(Param.Civ1.filename_ima_a,'\\','\\\\') '\n' ]);% for windows compatibility
5442fprintf(fid,   ['LastImage  ' regexprep(Param.Civ1.filename_ima_b,'\\','\\\\') '\n' ]);% for windows compatibility
5443fprintf(fid,  ['XX' '\n' ]);
5444fprintf(fid,  ['Mask ' Param.Civ1.maskflag '\n' ]);
5445fprintf(fid,  ['MaskName ' regexprep(Param.Civ1.maskname,'\\','\\\\') '\n' ]);
5446fprintf(fid,   ['ImageSize ' num2str(Param.Civ1.npx) ' ' num2str(Param.Civ1.npy) '\n' ]);   %VERIFIER CAS GENERAL ?
5447fprintf(fid,   ['CorrelationBoxesSize ' num2str(Param.Civ1.Bx) ' ' num2str(Param.Civ1.By) '\n' ]);
5448fprintf(fid,   ['SearchBoxeSize ' num2str(Param.Civ1.Sx) ' ' num2str(Param.Civ1.Sy) '\n' ]);
5449fprintf(fid,   ['RO ' num2str(Param.Civ1.Rho) '\n' ]);
5450fprintf(fid,   ['GridSpacing ' num2str(Param.Civ1.Dx) ' ' num2str(Param.Civ1.Dy) '\n' ]);
5451fprintf(fid,   ['XX 1.0' '\n' ]);
5452fprintf(fid,   ['Dt_TO ' num2str(Param.Civ1.Dt) ' ' num2str(Param.Civ1.T0) '\n' ]);
5453fprintf(fid,  ['PixCmXY ' num2str(Param.Civ1.pxcmx) ' ' num2str(Param.Civ1.pxcmy) '\n' ]);
5454fprintf(fid,  ['XX 1' '\n' ]);
5455fprintf(fid,   ['ShiftXY ' num2str(Param.Civ1.Shiftx) ' '  num2str(Param.Civ1.Shifty) '\n' ]);
5456fprintf(fid,  ['Grid ' Param.Civ1.gridflag '\n' ]);
5457fprintf(fid,   ['GridName ' regexprep(Param.Civ1.gridname,'\\','\\\\') '\n' ]);
5458fprintf(fid,   ['XX 85' '\n' ]);
5459fprintf(fid,   ['XX 1.0' '\n' ]);
5460fprintf(fid,   ['XX 1.0' '\n' ]);
5461fprintf(fid,   ['Hart 1' '\n' ]);
5462fprintf(fid,  [ 'DecimalShift 0' '\n' ]);
5463fprintf(fid,   ['Deformation 0' '\n' ]);
5464fprintf(fid,  ['CorrelationMin 0' '\n' ]);
5465fprintf(fid,   ['IntensityMin 0' '\n' ]);
5466fprintf(fid,  ['SeuilImage n' '\n' ]);
5467fprintf(fid,   ['SeuilImageValues 0 4096' '\n' ]);
5468fprintf(fid,   ['ImageToUse ' Param.Civ1.term_a ' ' Param.Civ1.term_b '\n' ]); % VERIFIER ?
5469fprintf(fid,   ['ImageUsedBefore null null' '\n' ]);
5470fclose(fid);
5471
5472% cmd_CIV1=[sparam.Civ1Bin ' -f ' filename '.cmx >' filename '.log' ]; % redirect standard output to the log file
5473% cmd_CIV1=regexprep(cmd_CIV1,'\\','\\\\');
5474% namelog=regexprep(namelog,'\\','\\\\');
5475if(isunix)
5476    cmd=['cp -f ' filename '.civ1.cmx ' filename '.cmx \n '];% the cmx file gives the name to the nc file
5477    cmd=[cmd Param.xml.Civ1Bin ' -f ' filename '.cmx >' filename '.civ1.log \n ' ]; % redirect standard output to the log file, the result file is named [filename '.nc'] by CIVx
5478%     cmd=[cmd '\n' 'mv ' filename '.log' ' ' filename '.civ1.log' '\n' 'chmod g+w ' filename '.civ1.log' '\n' 'chmod g+w ' filename '.nc'];%rename .log as .civ1.log and set the netcdf result file for group user writting
5479%    cmd=[cmd '\n' 'mv ' filename '.cmx' ' ' filename '.civ1.cmx' '\n'];%rename .cmx as .civ1.cmx
5480    cmd=[cmd 'rm ' filename '.cmx'];
5481else %Windows system
5482    filename=regexprep(filename,'\\','\\\\');
5483    cmd=['copy /Y "' filename '.civ1.cmx" "' filename '.cmx" \n '];
5484    cmd=[cmd '"' regexprep(Param.xml.Civ1Bin,'\\','\\\\')...
5485        '" -f "' filename '.cmx" >"' filename '.civ1.log" \n ' ]; % redirect standard output to the log file
5486%     namelog=regexprep(namelog,'\\','\\\\');
5487%     cmd=[cmd '\n ' 'copy /Y "' filename '.log' '" "' filename '.civ1.log"']; %preserve the log file as .civ1.log
5488    cmd=[cmd 'del "' filename '.cmx'];
5489end
Note: See TracBrowser for help on using the repository browser.