source: trunk/src/civ.m @ 314

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

Conflict badly resolved at previous revision. Corrected now

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