source: trunk/src/civ.m @ 300

Last change on this file since 300 was 300, checked in by sommeria, 12 years ago

improve again the input to the civ GUI: a few bufg corrections

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