Index: trunk/src/find_file_series.m
===================================================================
--- trunk/src/find_file_series.m	(revision 334)
+++ trunk/src/find_file_series.m	(revision 339)
@@ -1,8 +1,9 @@
 %'find_file_series': check the content onf an input field and find the corresponding file series
 %--------------------------------------------------------------------------
-% function [i1,i2,j1,j2,NomType,FileType,Object]=find_file_series(fileinput)
+% function [RootPath,RootFile,i1_series,i2_series,j1_series,j2_series,NomType,FileType,Object]=find_file_series(fileinput)
 %
 % OUTPUT:
-% i1,i2,j1,j2: set of i1 indices, respectively i2,j1,j2,  of the detected files 
+% RootPath,RootFile: root path and root name detected in fileinput, possibly modified for movies (indexing is then done on image view, not file)
+% i1_series(ref_i, ref_j,pair),i2_series,j1_series,j2_series: set of indices (i1,i2,j1,j2) sorted by ref index ref_i, ref_j, and pairindex in case of multiple pairs with the same ref
 % NomType: nomenclature type corrected after checking the first file (problem of 0 before the number string)
 % FileType: type of file, =
@@ -34,18 +35,18 @@
 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
 
-function [i1,i2,j1,j2,NomType,FileType,Object]=find_file_series(fileinput)
+function [RootPath,RootFile,i1_series,i2_series,j1_series,j2_series,NomType,FileType,Object]=find_file_series(fileinput)
 %------------------------------------------------------------------------
-i1=[];%default
-i2=[];%default
-j1=[];%default
-j2=[];%default
 
 %% get input root name and nomenclature type
 % [RootPath,RootFile,~,~,~,~,FileExt,NomType,SubDir]=name2display(fileinput);
-[RootPath,SubDir,RootFile,~,~,~,~,FileExt,NomType]=fileparts_uvmat(fileinput);
+[RootPath,SubDir,RootFile,~,i2_input,j1_input,j2_input,FileExt,NomType]=fileparts_uvmat(fileinput);
 
 %% check for particular file types: images, movies, civ data
 FileType='';
 Object=[];
+i1_series=zeros(1,1,1);
+i2_series=zeros(1,1,1);
+j1_series=zeros(1,1,1);
+j2_series=zeros(1,1,1);
 switch FileExt
     % ancillary files, no field indexing
@@ -64,12 +65,13 @@
     otherwise
         if ~isempty(FileExt)&& ~isempty(imformats(FileExt(2:end)))
-            imainfo=imfinfo(fileinput);
-            FileType='image';
-            if length(imainfo) >1 %case of image with multiple frames
-                NomType='*';
-                FileType='multimage';
-                i1=1;
-                i2=length(imainfo);
-                [RootPath,RootFile]=fileparts(fileinput);
+            try
+                imainfo=imfinfo(fileinput);
+                FileType='image';
+                if length(imainfo) >1 %case of image with multiple frames
+                    NomType='*';
+                    FileType='multimage';
+                    i1_series=1:length(imainfo);
+                    [RootPath,RootFile]=fileparts(fileinput);
+                end
             end
         else
@@ -88,13 +90,18 @@
                 NomType='*';
                 FileType='video';
-                i1=1;
-                i2=get(Object,'NumberOfFrames');
+                i1_series=[1:get(Object,'NumberOfFrames')];
                 [RootPath,RootFile]=fileparts(fileinput);
             end
         end
 end
+
+%% get the list of existing files when relevant
 if strcmp(NomType,'')||strcmp(NomType,'*')
+    if exist(fileinput,'file')
     [RootPath,RootFile]=fileparts(fileinput);% case of constant name (no indexing)
-%% get the list of existing files
+    else
+       RootPath='';
+       RootFile='';
+    end
 else
     if strcmp(SubDir,'')
@@ -113,37 +120,97 @@
     end
     dirpair=dir([filebasesub detect_string FileExt]);
-    for ifile=1:length(dirpair)
-       % [~,~,str_1,str_2,str_a,str_b]=name2display(dirpair(ifile).name);
-       dirpair(ifile).name
-        [~,~,~,i1_ifile,i2_ifile,j1_ifile,j2_ifile]=fileparts_uvmat(dirpair(ifile).name);  
-        if isempty(i1_ifile)
-            i1(ifile)=1;
+    nbpair=numel(dirpair);
+    ref_i_list=zeros(1,nbpair);
+    ref_j_list=zeros(1,nbpair);
+    if nbpair==0% no detected file
+        RootPath='';
+        RootFile='';
+    end
+    for ifile=1:nbpair
+        [~,~,~,i1,i2,j1,j2]=fileparts_uvmat(dirpair(ifile).name);
+        %             i1_series(ifile)=i1;
+        ref_i=i1;
+        if isempty(i2_input)
+            if ~isempty(i2)% invalid file name if i2 does not exist in the input file
+                break
+            end
         else
-            i1(ifile)=i1_ifile;
-        end
-        if isempty(i2_ifile)
-            i2(ifile)=i1(ifile);
+            ref_i=floor((i1+i2)/2);
+        end
+        ref_j=1;
+        if isempty(j1_input)
+            if  ~isempty(j1)% invalid file name if j1 does not exist in the input file
+                break
+            end
         else
-            i2(ifile)=i2_ifile;
-        end
-        end
-        if isempty(j1_ifile)
-            j1(ifile)=1;
-        else
-            j1(ifile)=j1_ifile;
-        end
-        if isempty(j2_ifile)
-            j2(ifile)=j1(ifile);
-        else
-            j2(ifile)=j2_ifile;
-        end
-    end  
-    % TODO : sort by reference index
-    % update the NomType from the minimal index detected (to deal with number strings beginning by 0)
-    [~,ifile]=min(i1);
-    %[~,~,~,~,~,~,~,NomType]=name2display(dirpair(ifile).name);
-    if ~isempty(i1)
-           [~,ifile]=min(i1);
+            ref_j=j1;
+            if isempty(j2_input)
+                if  ~isempty(j2)% invalid file name if j2 does not exist in the input file
+                    break
+                end
+            else
+                ref_j=floor((j1+j2)/2);
+            end
+        end
+        % update the detected index series
+        ref_i_list(ifile)=ref_i;
+        ref_j_list(ifile)=ref_j;
+        nb_pairs=0;
+        if ~isempty(i2_input)|| ~isempty(j2_input) %deals with  pairs
+            if size(i1_series,1)>=ref_i && size(i1_series,2)>=ref_j
+                nb_pairs=numel(find(i1_series(ref_i,ref_j,:)~=0));
+            end
+        end
+        i1_series(ref_i,ref_j,nb_pairs+1)=i1;
+        if ~isempty(i2_input)
+            i2_series(ref_i,ref_j,nb_pairs+1)=i2;
+        end
+        if ~isempty(j1_input)
+            j1_series(ref_i,ref_j,nb_pairs+1)=j1;
+        end
+        if ~isempty(j2_input)
+            j1_series(ref_i,ref_j,nb_pairs+1)=j1;
+            j2_series(ref_i,ref_j,nb_pairs+1)=j2;
+        end
+    end
+    % look for the numerical string of the first files to update the NomType (take into account the 0 before the number)
+    max_j=max(ref_j_list);
+    ref_ij=ref_i_list*max_j+ref_j_list; % ordered by index i, then by j for a given i.
+    [~,ifile]=min(ref_ij(ref_ij>0));
     [~,~,~,~,~,~,~,~,NomType]=fileparts_uvmat(dirpair(ifile).name);
-    end
 end
+
+%% set to empty array the irrelevant index series
+if isequal(i1_series,0), i1_series=[]; end
+if isequal(i2_series,0), i2_series=[]; end
+if isequal(j1_series,0), j1_series=[]; end
+if isequal(j2_series,0), j2_series=[]; end
+
+%% sort pairs by decreasing index differences in case of multiple pairs at the same reference index
+if size(i2_series,3)>1 %pairs i1 -i2
+    diff_index=abs(i2_series-i1_series);
+    [~,ind_pair]=sort(diff_index,3,'descend');
+    for ref_i=1:size(i1_series,1)
+        for ref_j=1:size(j1_series,2)
+            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
+            i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
+            if ~isempty(j1_series)
+                j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
+            end
+        end
+    end
+elseif size(j2_series,3)>1 %pairs j1 -j2
+    diff_index=abs(j2_series-j1_series);
+    [~,ind_pair]=sort(diff_index,3,'descend');
+    for ref_i=1:size(i1_series,1)
+        for ref_j=1:size(j1_series,2)
+            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
+            if ~isempty(i2_series)
+                i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
+            end
+            j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
+            j2_series(ref_i,ref_j,:)=j2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
+        end
+    end
+end
+
