Index: trunk/src/find_file_series.m
===================================================================
--- trunk/src/find_file_series.m	(revision 935)
+++ trunk/src/find_file_series.m	(revision 961)
@@ -77,189 +77,231 @@
 end
 if checkfileindexing
-NomTypePref='';
-if isempty(NomType)||strcmp(NomType,'*')
-    if exist(fullfileinput,'file')
-        [tild,RootFile]=fileparts(fileinput);% case of constant name (no indexing), get the filename without its extension
+    NomTypePref='';
+    if isempty(NomType)||strcmp(NomType,'*')
+        if exist(fullfileinput,'file')
+            [tild,RootFile]=fileparts(fileinput);% case of constant name (no indexing), get the filename without its extension
+        else
+            RootFile='';
+        end
     else
-        RootFile='';
-    end
-else
-    %% if checkxml=1, possibly include the first index in the root name, if there exists a corresponding xml file 
-    if ~exist('checkxml','var')||checkxml
-        r=regexp(NomType,'^(?<tiretnum>_|\d+)','names');%look for a number or _1 at the beginning of NomType
-        if ~isempty(r) %if NomType begins by a number or _1
-            fileinput_end=regexprep(fileinput,['^' RootFile],'');%remove RootFile at the beginning of fileinput
-            if isempty(regexp(r.tiretnum,'^_','once'))% if a separator '_' is not  detected
-                rr=regexp(fileinput_end,'^(?<i1>\d+)','names');
-            else% if a separator '_' is  detected
-                rr=regexp(fileinput_end,'^(?<i1>_\d+)','names');
-            end
-            if ~isempty(rr)
-                RootFile_i=[RootFile rr.i1];% new root file
-                %look for an xml file correspoonding to the new root name
-                if exist(fullfile(RootPath,SubDir,[RootFile_i '.xml']),'file') || (strcmp(FileExt,'.nc') && exist(fullfile(RootPath,[RootFile_i '.xml']),'file'))
-                    RootFile=RootFile_i;
-                    NomTypePref=r.tiretnum;
-                    NomType=regexprep(NomType,['^'  NomTypePref],'');
-                    i1_input=j1_input;
-                    i2_input=j2_input;
-                    j1_input=[];
-                    j2_input=[];
-                end
-            end
-        end
-    end
-    %% analyse the list of existing files when relevant
-    sep1='';
-    sep2='';
-    i1_str='(?<i1>)';%will set i1=[];
-    i1_star='';
-    i2_str='(?<i2>)';%will set i2=[];
-    i2_star='';
-    j1_str='(?<j1>)';%will set j1=[];
-    j1_star='';
-    j2_str='(?<j2>)';%will set j2=[];
-    j2_star='';
-    %Look for cases with letter indexing for the second index
-    r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<sep2>_?)(?<j1>[a|A])(?<j2>[b|B]?)$','names');
-    if ~isempty(r)
-        sep1=r.sep1;
-        sep2=r.sep2;
-        i1_str='(?<i1>\d+)';
-        i1_star='*';
-        if strcmp(lower(r.j1),r.j1)% lower case index
-            j1_str='(?<j1>[a-z])';
-        else
-           j1_str='(?<j1>[A-Z])'; % upper case index
-        end
-        j1_star='*';
-        if ~isempty(r.j2)
-           if strcmp(lower(r.j1),r.j1)
-            j2_str='(?<j2>[a-z])';
-            else
-           j2_str='(?<j2>[A-Z])'; 
-           end
-            j2_star='*';
-        end
-    else %numerical indexing
-        r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<i2>(-\d+)?)(?<j1>(_\d+)?)(?<j2>(-\d+)?)$','names');
+        %% if checkxml=1, possibly include the first index in the root name, if there exists a corresponding xml file
+        if ~exist('checkxml','var')||checkxml
+            r=regexp(NomType,'^(?<tiretnum>_|\d+)','names');%look for a number or _1 at the beginning of NomType
+            if ~isempty(r) %if NomType begins by a number or _1
+                fileinput_end=regexprep(fileinput,['^' RootFile],'');%remove RootFile at the beginning of fileinput
+                if isempty(regexp(r.tiretnum,'^_','once'))% if a separator '_' is not  detected
+                    rr=regexp(fileinput_end,'^(?<i1>\d+)','names');
+                else% if a separator '_' is  detected
+                    rr=regexp(fileinput_end,'^(?<i1>_\d+)','names');
+                end
+                if ~isempty(rr)
+                    RootFile_i=[RootFile rr.i1];% new root file
+                    %look for an xml file correspoonding to the new root name
+                    if exist(fullfile(RootPath,SubDir,[RootFile_i '.xml']),'file') || (strcmp(FileExt,'.nc') && exist(fullfile(RootPath,[RootFile_i '.xml']),'file'))
+                        RootFile=RootFile_i;
+                        NomTypePref=r.tiretnum;
+                        NomType=regexprep(NomType,['^'  NomTypePref],'');
+                        i1_input=j1_input;
+                        i2_input=j2_input;
+                        j1_input=[];
+                        j2_input=[];
+                    elseif exist([RootPath '.xml'],'file')% new convention with j indices in sub-folders level0, 1...
+                        rj=regexp(SubDir,'^level(?<j1>\d+)$','names');
+                        if ~isempty(rj)
+                            j1_input=rj.j1;
+                            NomType='level';
+                            [RootPath,SubDir]=fileparts(RootPath);
+                        end
+                    end
+                end
+            end
+        end
+        
+        %% analyse the list of existing files when relevant
+        sep1='';
+        sep2='';
+        i1_str='(?<i1>)';%will set i1=[];
+        i1_star='';
+        i2_str='(?<i2>)';%will set i2=[];
+        i2_star='';
+        j1_str='(?<j1>)';%will set j1=[];
+        j1_star='';
+        j2_str='(?<j2>)';%will set j2=[];
+        j2_star='';
+        %Look for cases with letter indexing for the second index
+        r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<sep2>_?)(?<j1>[a|A])(?<j2>[b|B]?)$','names');
         if ~isempty(r)
             sep1=r.sep1;
+            sep2=r.sep2;
             i1_str='(?<i1>\d+)';
             i1_star='*';
-            if ~isempty(r.i2)
-                i2_str='(?<i2>-\d+)';
-                i2_star='-*';
-            end
-            if ~isempty(r.j1)
-                j1_str='(?<j1>_\d+)';
-                j1_star='_*';
-            end
+            if strcmp(lower(r.j1),r.j1)% lower case index
+                j1_str='(?<j1>[a-z])';
+            else
+                j1_str='(?<j1>[A-Z])'; % upper case index
+            end
+            j1_star='*';
             if ~isempty(r.j2)
-                j2_str='(?<j2>-\d+)';
-                j2_star='-*';
-            end
-        end
-    end
-    detect_string=['^' RootFile sep1 i1_str i2_str sep2 j1_str j2_str FileExt '$'];%string used in regexp to detect file indices
-    %find the string used to extract the relevant files with the command dir
-    star_string=[RootFile sep1 i1_star i2_star sep2 j1_star j2_star FileExt];
-    wd=pwd;%current working directory
-    cd (FilePath)% move to the local dir to save time in the operation dir.
-    dirpair=dir(star_string);% look for relevant files in the file directory
-    cd(wd)
-    nbpair=numel(dirpair);
-    ref_i_list=zeros(1,nbpair);
-    ref_j_list=zeros(1,nbpair);
-    if nbpair==0% no detected file
-        RootFile='';
-    end
-    % scan the list of relevant files, extract the indices
-    for ifile=1:nbpair
-        rr=regexp(dirpair(ifile).name,detect_string,'names');
-        if ~isempty(rr)
-            i1=str2num(rr.i1);
-            i2=str2num(regexprep(rr.i2,'^-',''));
-            j1=stra2num(regexprep(rr.j1,'^_',''));
-            j2=stra2num(regexprep(rr.j2,'^-',''));
-            ref_i=i1;
-            if isempty(i2_input)
-                if ~isempty(i2)% invalid file name if i2 does not exist in the input file
-                    break
-                end
+                if strcmp(lower(r.j1),r.j1)
+                    j2_str='(?<j2>[a-z])';
+                else
+                    j2_str='(?<j2>[A-Z])';
+                end
+                j2_star='*';
+            end
+        else %numerical indexing
+            r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<i2>(-\d+)?)(?<j1>(_\d+)?)(?<j2>(-\d+)?)$','names');
+            if ~isempty(r)
+                sep1=r.sep1;
+                i1_str='(?<i1>\d+)';
+                i1_star='*';
+                if ~isempty(r.i2)
+                    i2_str='(?<i2>-\d+)';
+                    i2_star='-*';
+                end
+                if ~isempty(r.j1)
+                    j1_str='(?<j1>_\d+)';
+                    j1_star='_*';
+                end
+                if ~isempty(r.j2)
+                    j2_str='(?<j2>-\d+)';
+                    j2_star='-*';
+                end
+            end
+        end
+        if strcmp(NomType,'level')
+            star_string=[RootFile '*' FileExt];
+            detect_string=['^' RootFile '(?<i1>\d+)' FileExt '$'];%string used in regexp to detect file indices
+             wd=pwd;%current working directory
+            cd (FilePath)% move to the local dir to save time in the operation dir.
+            dirpair=dir(star_string);% look for relevant files in the file directory
+            cd(wd)
+            nbpair=numel(dirpair);
+            i1_series=zeros(1,nbpair);
+            if nbpair==0% no detected file
+                RootFile='';
+            end
+            % scan the list of relevant files, extract the indices
+            for ifile=1:nbpair
+                rr=regexp(dirpair(ifile).name,detect_string,'names');
+                if ~isempty(rr)
+                    i1_series(ifile)=str2num(rr.i1);                 
+                end
+            end
+            % look for the list of subfolders level#
+            cd (fullfile(RootPath,SubDir))% move to the local dir to save time in the operation dir.
+            dirpair=dir('level*');% look for relevant files in the file directory
+            cd(wd)
+            nbpair=numel(dirpair);
+            jfile=0;
+            for ifile=1:nbpair
+                rr=regexp(dirpair(ifile).name,'^level(?<i1>\d+)$','names');
+                if ~isempty(rr)
+                    jfile=jfile+1;
+                    j1_series(jfile)=str2num(rr.i1);              
+                end
+            end
+            [i1_series,j1_series]=meshgrid(i1_series,j1_series);
+            i1_series=reshape(i1_series,1,[]);
+            j1_series=reshape(j1_series,1,[]);
+        else
+            detect_string=['^' RootFile sep1 i1_str i2_str sep2 j1_str j2_str FileExt '$'];%string used in regexp to detect file indices
+            %find the string used to extract the relevant files with the command dir
+            star_string=[RootFile sep1 i1_star i2_star sep2 j1_star j2_star FileExt];
+            wd=pwd;%current working directory
+            cd (FilePath)% move to the local dir to save time in the operation dir.
+            dirpair=dir(star_string);% look for relevant files in the file directory
+            cd(wd)
+            nbpair=numel(dirpair);
+            ref_i_list=zeros(1,nbpair);
+            ref_j_list=zeros(1,nbpair);
+            if nbpair==0% no detected file
+                RootFile='';
+            end
+            % scan the list of relevant files, extract the indices
+            for ifile=1:nbpair
+                rr=regexp(dirpair(ifile).name,detect_string,'names');
+                if ~isempty(rr)
+                    i1=str2num(rr.i1);
+                    i2=str2num(regexprep(rr.i2,'^-',''));
+                    j1=stra2num(regexprep(rr.j1,'^_',''));
+                    j2=stra2num(regexprep(rr.j2,'^-',''));
+                    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
+                        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 %j1_input is not empty
+                        if isempty(j1)% the detected name does not fit with the input
+                            break
+                        else
+                            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
+                    end
+                    % update the detected index series
+                    if ~isempty(ref_i)&&~isempty(ref_j)
+                        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+1 && size(i1_series,2)>=ref_j+1
+                                nb_pairs=numel(find(i1_series(ref_i+1,ref_j+1,:)~=0));
+                            end
+                        end
+                        if i1==0
+                            i1=-1;% set index 0 to -1 to distinguish from the absent index (set to 0)
+                        end
+                        if j1==0
+                            j1=-1;% set index 0 to -1 to distinguish from the absent index (set to 0)
+                        end
+                        i1_series(ref_i+1,ref_j+1,nb_pairs+1)=i1;
+                        if ~isempty(i2_input)
+                            i2_series(ref_i+1,ref_j+1,nb_pairs+1)=i2;
+                        end
+                        if ~isempty(j1_input)
+                            j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
+                        end
+                        if ~isempty(j2_input)
+                            j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
+                            j2_series(ref_i+1,ref_j+1,nb_pairs+1)=j2;
+                        end
+                    end
+                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);
+            if isempty(max_j)
+                ref_ij=ref_i_list;
             else
-                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 %j1_input is not empty
-                if isempty(j1)% the detected name does not fit with the input
-                    break
-                else
-                    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
-            end
-            % update the detected index series
-            if ~isempty(ref_i)&&~isempty(ref_j)
-                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+1 && size(i1_series,2)>=ref_j+1
-                        nb_pairs=numel(find(i1_series(ref_i+1,ref_j+1,:)~=0));
-                    end
-                end
-                if i1==0
-                    i1=-1;% set index 0 to -1 to distinguish from the absent index (set to 0)
-                end
-                if j1==0
-                    j1=-1;% set index 0 to -1 to distinguish from the absent index (set to 0)
-                end
-                i1_series(ref_i+1,ref_j+1,nb_pairs+1)=i1;
-                if ~isempty(i2_input)
-                    i2_series(ref_i+1,ref_j+1,nb_pairs+1)=i2;
-                end
-                if ~isempty(j1_input)
-                    j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
-                end
-                if ~isempty(j2_input)
-                    j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
-                    j2_series(ref_i+1,ref_j+1,nb_pairs+1)=j2;
-                end
-            end
-        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);
-    if isempty(max_j)
-        ref_ij=ref_i_list;
-    else
-        ref_ij=ref_i_list*max_j+ref_j_list; % ordered by index i, then by j for a given i.
-    end
-    ind_select=find(ref_ij>0);
-    
-    if isempty(ind_select)
-%         RootFile='';
-%         NomType='';
-    else
-        [tild,ifile_min]=min(ref_ij(ind_select));
-        [tild,tild,tild,tild,tild,tild,tild,tild,NomType]=fileparts_uvmat(dirpair(ind_select(ifile_min)).name);% update the representation of indices (number of 0 before the number)
-        NomType=regexprep(NomType,['^' NomTypePref],'');
-        %% update the file type if the input file does not exist (pb of 0001)
-        if isempty(FileInfo.FileType)
-            [FileInfo,MovieObject]=get_file_info(fullfile(FilePath,dirpair(ifile_min).name));
-        end
-    end
-end
+                ref_ij=ref_i_list*max_j+ref_j_list; % ordered by index i, then by j for a given i.
+            end
+            ind_select=find(ref_ij>0);
+            
+            if ~isempty(ind_select)
+                [tild,ifile_min]=min(ref_ij(ind_select));
+                [tild,tild,tild,tild,tild,tild,tild,tild,NomType]=fileparts_uvmat(dirpair(ind_select(ifile_min)).name);% update the representation of indices (number of 0 before the number)
+                NomType=regexprep(NomType,['^' NomTypePref],'');
+                %% update the file type if the input file does not exist (pb of 0001)
+                if isempty(FileInfo.FileType)
+                    [FileInfo,MovieObject]=get_file_info(fullfile(FilePath,dirpair(ifile_min).name));
+                end
+            end
+        end
+    end
 
 %% set to empty array the irrelevant index series
