Ignore:
Timestamp:
Jun 28, 2016, 9:05:33 AM (8 years ago)
Author:
sommeria
Message:

NomType? level introduced

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/find_file_series.m

    r935 r961  
    7777end
    7878if checkfileindexing
    79 NomTypePref='';
    80 if isempty(NomType)||strcmp(NomType,'*')
    81     if exist(fullfileinput,'file')
    82         [tild,RootFile]=fileparts(fileinput);% case of constant name (no indexing), get the filename without its extension
     79    NomTypePref='';
     80    if isempty(NomType)||strcmp(NomType,'*')
     81        if exist(fullfileinput,'file')
     82            [tild,RootFile]=fileparts(fileinput);% case of constant name (no indexing), get the filename without its extension
     83        else
     84            RootFile='';
     85        end
    8386    else
    84         RootFile='';
    85     end
    86 else
    87     %% if checkxml=1, possibly include the first index in the root name, if there exists a corresponding xml file
    88     if ~exist('checkxml','var')||checkxml
    89         r=regexp(NomType,'^(?<tiretnum>_|\d+)','names');%look for a number or _1 at the beginning of NomType
    90         if ~isempty(r) %if NomType begins by a number or _1
    91             fileinput_end=regexprep(fileinput,['^' RootFile],'');%remove RootFile at the beginning of fileinput
    92             if isempty(regexp(r.tiretnum,'^_','once'))% if a separator '_' is not  detected
    93                 rr=regexp(fileinput_end,'^(?<i1>\d+)','names');
    94             else% if a separator '_' is  detected
    95                 rr=regexp(fileinput_end,'^(?<i1>_\d+)','names');
    96             end
    97             if ~isempty(rr)
    98                 RootFile_i=[RootFile rr.i1];% new root file
    99                 %look for an xml file correspoonding to the new root name
    100                 if exist(fullfile(RootPath,SubDir,[RootFile_i '.xml']),'file') || (strcmp(FileExt,'.nc') && exist(fullfile(RootPath,[RootFile_i '.xml']),'file'))
    101                     RootFile=RootFile_i;
    102                     NomTypePref=r.tiretnum;
    103                     NomType=regexprep(NomType,['^'  NomTypePref],'');
    104                     i1_input=j1_input;
    105                     i2_input=j2_input;
    106                     j1_input=[];
    107                     j2_input=[];
    108                 end
    109             end
    110         end
    111     end
    112     %% analyse the list of existing files when relevant
    113     sep1='';
    114     sep2='';
    115     i1_str='(?<i1>)';%will set i1=[];
    116     i1_star='';
    117     i2_str='(?<i2>)';%will set i2=[];
    118     i2_star='';
    119     j1_str='(?<j1>)';%will set j1=[];
    120     j1_star='';
    121     j2_str='(?<j2>)';%will set j2=[];
    122     j2_star='';
    123     %Look for cases with letter indexing for the second index
    124     r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<sep2>_?)(?<j1>[a|A])(?<j2>[b|B]?)$','names');
    125     if ~isempty(r)
    126         sep1=r.sep1;
    127         sep2=r.sep2;
    128         i1_str='(?<i1>\d+)';
    129         i1_star='*';
    130         if strcmp(lower(r.j1),r.j1)% lower case index
    131             j1_str='(?<j1>[a-z])';
    132         else
    133            j1_str='(?<j1>[A-Z])'; % upper case index
    134         end
    135         j1_star='*';
    136         if ~isempty(r.j2)
    137            if strcmp(lower(r.j1),r.j1)
    138             j2_str='(?<j2>[a-z])';
    139             else
    140            j2_str='(?<j2>[A-Z])';
    141            end
    142             j2_star='*';
    143         end
    144     else %numerical indexing
    145         r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<i2>(-\d+)?)(?<j1>(_\d+)?)(?<j2>(-\d+)?)$','names');
     87        %% if checkxml=1, possibly include the first index in the root name, if there exists a corresponding xml file
     88        if ~exist('checkxml','var')||checkxml
     89            r=regexp(NomType,'^(?<tiretnum>_|\d+)','names');%look for a number or _1 at the beginning of NomType
     90            if ~isempty(r) %if NomType begins by a number or _1
     91                fileinput_end=regexprep(fileinput,['^' RootFile],'');%remove RootFile at the beginning of fileinput
     92                if isempty(regexp(r.tiretnum,'^_','once'))% if a separator '_' is not  detected
     93                    rr=regexp(fileinput_end,'^(?<i1>\d+)','names');
     94                else% if a separator '_' is  detected
     95                    rr=regexp(fileinput_end,'^(?<i1>_\d+)','names');
     96                end
     97                if ~isempty(rr)
     98                    RootFile_i=[RootFile rr.i1];% new root file
     99                    %look for an xml file correspoonding to the new root name
     100                    if exist(fullfile(RootPath,SubDir,[RootFile_i '.xml']),'file') || (strcmp(FileExt,'.nc') && exist(fullfile(RootPath,[RootFile_i '.xml']),'file'))
     101                        RootFile=RootFile_i;
     102                        NomTypePref=r.tiretnum;
     103                        NomType=regexprep(NomType,['^'  NomTypePref],'');
     104                        i1_input=j1_input;
     105                        i2_input=j2_input;
     106                        j1_input=[];
     107                        j2_input=[];
     108                    elseif exist([RootPath '.xml'],'file')% new convention with j indices in sub-folders level0, 1...
     109                        rj=regexp(SubDir,'^level(?<j1>\d+)$','names');
     110                        if ~isempty(rj)
     111                            j1_input=rj.j1;
     112                            NomType='level';
     113                            [RootPath,SubDir]=fileparts(RootPath);
     114                        end
     115                    end
     116                end
     117            end
     118        end
     119       
     120        %% analyse the list of existing files when relevant
     121        sep1='';
     122        sep2='';
     123        i1_str='(?<i1>)';%will set i1=[];
     124        i1_star='';
     125        i2_str='(?<i2>)';%will set i2=[];
     126        i2_star='';
     127        j1_str='(?<j1>)';%will set j1=[];
     128        j1_star='';
     129        j2_str='(?<j2>)';%will set j2=[];
     130        j2_star='';
     131        %Look for cases with letter indexing for the second index
     132        r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<sep2>_?)(?<j1>[a|A])(?<j2>[b|B]?)$','names');
    146133        if ~isempty(r)
    147134            sep1=r.sep1;
     135            sep2=r.sep2;
    148136            i1_str='(?<i1>\d+)';
    149137            i1_star='*';
    150             if ~isempty(r.i2)
    151                 i2_str='(?<i2>-\d+)';
    152                 i2_star='-*';
    153             end
    154             if ~isempty(r.j1)
    155                 j1_str='(?<j1>_\d+)';
    156                 j1_star='_*';
    157             end
     138            if strcmp(lower(r.j1),r.j1)% lower case index
     139                j1_str='(?<j1>[a-z])';
     140            else
     141                j1_str='(?<j1>[A-Z])'; % upper case index
     142            end
     143            j1_star='*';
    158144            if ~isempty(r.j2)
    159                 j2_str='(?<j2>-\d+)';
    160                 j2_star='-*';
    161             end
    162         end
    163     end
    164     detect_string=['^' RootFile sep1 i1_str i2_str sep2 j1_str j2_str FileExt '$'];%string used in regexp to detect file indices
    165     %find the string used to extract the relevant files with the command dir
    166     star_string=[RootFile sep1 i1_star i2_star sep2 j1_star j2_star FileExt];
    167     wd=pwd;%current working directory
    168     cd (FilePath)% move to the local dir to save time in the operation dir.
    169     dirpair=dir(star_string);% look for relevant files in the file directory
    170     cd(wd)
    171     nbpair=numel(dirpair);
    172     ref_i_list=zeros(1,nbpair);
    173     ref_j_list=zeros(1,nbpair);
    174     if nbpair==0% no detected file
    175         RootFile='';
    176     end
    177     % scan the list of relevant files, extract the indices
    178     for ifile=1:nbpair
    179         rr=regexp(dirpair(ifile).name,detect_string,'names');
    180         if ~isempty(rr)
    181             i1=str2num(rr.i1);
    182             i2=str2num(regexprep(rr.i2,'^-',''));
    183             j1=stra2num(regexprep(rr.j1,'^_',''));
    184             j2=stra2num(regexprep(rr.j2,'^-',''));
    185             ref_i=i1;
    186             if isempty(i2_input)
    187                 if ~isempty(i2)% invalid file name if i2 does not exist in the input file
    188                     break
    189                 end
     145                if strcmp(lower(r.j1),r.j1)
     146                    j2_str='(?<j2>[a-z])';
     147                else
     148                    j2_str='(?<j2>[A-Z])';
     149                end
     150                j2_star='*';
     151            end
     152        else %numerical indexing
     153            r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<i2>(-\d+)?)(?<j1>(_\d+)?)(?<j2>(-\d+)?)$','names');
     154            if ~isempty(r)
     155                sep1=r.sep1;
     156                i1_str='(?<i1>\d+)';
     157                i1_star='*';
     158                if ~isempty(r.i2)
     159                    i2_str='(?<i2>-\d+)';
     160                    i2_star='-*';
     161                end
     162                if ~isempty(r.j1)
     163                    j1_str='(?<j1>_\d+)';
     164                    j1_star='_*';
     165                end
     166                if ~isempty(r.j2)
     167                    j2_str='(?<j2>-\d+)';
     168                    j2_star='-*';
     169                end
     170            end
     171        end
     172        if strcmp(NomType,'level')
     173            star_string=[RootFile '*' FileExt];
     174            detect_string=['^' RootFile '(?<i1>\d+)' FileExt '$'];%string used in regexp to detect file indices
     175             wd=pwd;%current working directory
     176            cd (FilePath)% move to the local dir to save time in the operation dir.
     177            dirpair=dir(star_string);% look for relevant files in the file directory
     178            cd(wd)
     179            nbpair=numel(dirpair);
     180            i1_series=zeros(1,nbpair);
     181            if nbpair==0% no detected file
     182                RootFile='';
     183            end
     184            % scan the list of relevant files, extract the indices
     185            for ifile=1:nbpair
     186                rr=regexp(dirpair(ifile).name,detect_string,'names');
     187                if ~isempty(rr)
     188                    i1_series(ifile)=str2num(rr.i1);                 
     189                end
     190            end
     191            % look for the list of subfolders level#
     192            cd (fullfile(RootPath,SubDir))% move to the local dir to save time in the operation dir.
     193            dirpair=dir('level*');% look for relevant files in the file directory
     194            cd(wd)
     195            nbpair=numel(dirpair);
     196            jfile=0;
     197            for ifile=1:nbpair
     198                rr=regexp(dirpair(ifile).name,'^level(?<i1>\d+)$','names');
     199                if ~isempty(rr)
     200                    jfile=jfile+1;
     201                    j1_series(jfile)=str2num(rr.i1);             
     202                end
     203            end
     204            [i1_series,j1_series]=meshgrid(i1_series,j1_series);
     205            i1_series=reshape(i1_series,1,[]);
     206            j1_series=reshape(j1_series,1,[]);
     207        else
     208            detect_string=['^' RootFile sep1 i1_str i2_str sep2 j1_str j2_str FileExt '$'];%string used in regexp to detect file indices
     209            %find the string used to extract the relevant files with the command dir
     210            star_string=[RootFile sep1 i1_star i2_star sep2 j1_star j2_star FileExt];
     211            wd=pwd;%current working directory
     212            cd (FilePath)% move to the local dir to save time in the operation dir.
     213            dirpair=dir(star_string);% look for relevant files in the file directory
     214            cd(wd)
     215            nbpair=numel(dirpair);
     216            ref_i_list=zeros(1,nbpair);
     217            ref_j_list=zeros(1,nbpair);
     218            if nbpair==0% no detected file
     219                RootFile='';
     220            end
     221            % scan the list of relevant files, extract the indices
     222            for ifile=1:nbpair
     223                rr=regexp(dirpair(ifile).name,detect_string,'names');
     224                if ~isempty(rr)
     225                    i1=str2num(rr.i1);
     226                    i2=str2num(regexprep(rr.i2,'^-',''));
     227                    j1=stra2num(regexprep(rr.j1,'^_',''));
     228                    j2=stra2num(regexprep(rr.j2,'^-',''));
     229                    ref_i=i1;
     230                    if isempty(i2_input)
     231                        if ~isempty(i2)% invalid file name if i2 does not exist in the input file
     232                            break
     233                        end
     234                    else
     235                        ref_i=floor((i1+i2)/2);
     236                    end
     237                    ref_j=1;
     238                    if isempty(j1_input)
     239                        if  ~isempty(j1)% invalid file name if j1 does not exist in the input file
     240                            break
     241                        end
     242                    else %j1_input is not empty
     243                        if isempty(j1)% the detected name does not fit with the input
     244                            break
     245                        else
     246                            ref_j=j1;
     247                            if isempty(j2_input)
     248                                if  ~isempty(j2)% invalid file name if j2 does not exist in the input file
     249                                    break
     250                                end
     251                            else
     252                                ref_j=floor((j1+j2)/2);
     253                            end
     254                        end
     255                    end
     256                    % update the detected index series
     257                    if ~isempty(ref_i)&&~isempty(ref_j)
     258                        ref_i_list(ifile)=ref_i;
     259                        ref_j_list(ifile)=ref_j;
     260                        nb_pairs=0;
     261                        if ~isempty(i2_input)|| ~isempty(j2_input) %deals with  pairs
     262                            if size(i1_series,1)>=ref_i+1 && size(i1_series,2)>=ref_j+1
     263                                nb_pairs=numel(find(i1_series(ref_i+1,ref_j+1,:)~=0));
     264                            end
     265                        end
     266                        if i1==0
     267                            i1=-1;% set index 0 to -1 to distinguish from the absent index (set to 0)
     268                        end
     269                        if j1==0
     270                            j1=-1;% set index 0 to -1 to distinguish from the absent index (set to 0)
     271                        end
     272                        i1_series(ref_i+1,ref_j+1,nb_pairs+1)=i1;
     273                        if ~isempty(i2_input)
     274                            i2_series(ref_i+1,ref_j+1,nb_pairs+1)=i2;
     275                        end
     276                        if ~isempty(j1_input)
     277                            j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
     278                        end
     279                        if ~isempty(j2_input)
     280                            j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
     281                            j2_series(ref_i+1,ref_j+1,nb_pairs+1)=j2;
     282                        end
     283                    end
     284                end
     285            end
     286            % look for the numerical string of the first files to update the NomType (take into account the 0 before the number)
     287            max_j=max(ref_j_list);
     288            if isempty(max_j)
     289                ref_ij=ref_i_list;
    190290            else
    191                 ref_i=floor((i1+i2)/2);
    192             end
    193             ref_j=1;
    194             if isempty(j1_input)
    195                 if  ~isempty(j1)% invalid file name if j1 does not exist in the input file
    196                     break
    197                 end
    198             else %j1_input is not empty
    199                 if isempty(j1)% the detected name does not fit with the input
    200                     break
    201                 else
    202                     ref_j=j1;
    203                     if isempty(j2_input)
    204                         if  ~isempty(j2)% invalid file name if j2 does not exist in the input file
    205                             break
    206                         end
    207                     else
    208                         ref_j=floor((j1+j2)/2);
    209                     end
    210                 end
    211             end
    212             % update the detected index series
    213             if ~isempty(ref_i)&&~isempty(ref_j)
    214                 ref_i_list(ifile)=ref_i;
    215                 ref_j_list(ifile)=ref_j;
    216                 nb_pairs=0;
    217                 if ~isempty(i2_input)|| ~isempty(j2_input) %deals with  pairs
    218                     if size(i1_series,1)>=ref_i+1 && size(i1_series,2)>=ref_j+1
    219                         nb_pairs=numel(find(i1_series(ref_i+1,ref_j+1,:)~=0));
    220                     end
    221                 end
    222                 if i1==0
    223                     i1=-1;% set index 0 to -1 to distinguish from the absent index (set to 0)
    224                 end
    225                 if j1==0
    226                     j1=-1;% set index 0 to -1 to distinguish from the absent index (set to 0)
    227                 end
    228                 i1_series(ref_i+1,ref_j+1,nb_pairs+1)=i1;
    229                 if ~isempty(i2_input)
    230                     i2_series(ref_i+1,ref_j+1,nb_pairs+1)=i2;
    231                 end
    232                 if ~isempty(j1_input)
    233                     j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
    234                 end
    235                 if ~isempty(j2_input)
    236                     j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
    237                     j2_series(ref_i+1,ref_j+1,nb_pairs+1)=j2;
    238                 end
    239             end
    240         end
    241     end
    242     % look for the numerical string of the first files to update the NomType (take into account the 0 before the number)
    243     max_j=max(ref_j_list);
    244     if isempty(max_j)
    245         ref_ij=ref_i_list;
    246     else
    247         ref_ij=ref_i_list*max_j+ref_j_list; % ordered by index i, then by j for a given i.
    248     end
    249     ind_select=find(ref_ij>0);
    250    
    251     if isempty(ind_select)
    252 %         RootFile='';
    253 %         NomType='';
    254     else
    255         [tild,ifile_min]=min(ref_ij(ind_select));
    256         [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)
    257         NomType=regexprep(NomType,['^' NomTypePref],'');
    258         %% update the file type if the input file does not exist (pb of 0001)
    259         if isempty(FileInfo.FileType)
    260             [FileInfo,MovieObject]=get_file_info(fullfile(FilePath,dirpair(ifile_min).name));
    261         end
    262     end
    263 end
     291                ref_ij=ref_i_list*max_j+ref_j_list; % ordered by index i, then by j for a given i.
     292            end
     293            ind_select=find(ref_ij>0);
     294           
     295            if ~isempty(ind_select)
     296                [tild,ifile_min]=min(ref_ij(ind_select));
     297                [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)
     298                NomType=regexprep(NomType,['^' NomTypePref],'');
     299                %% update the file type if the input file does not exist (pb of 0001)
     300                if isempty(FileInfo.FileType)
     301                    [FileInfo,MovieObject]=get_file_info(fullfile(FilePath,dirpair(ifile_min).name));
     302                end
     303            end
     304        end
     305    end
    264306
    265307%% set to empty array the irrelevant index series
Note: See TracChangeset for help on using the changeset viewer.