source: trunk/src/find_file_series.m @ 439

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

bugs repairs testing .tif multimage

File size: 11.5 KB
RevLine 
[356]1%'find_file_series': check the content of an input file and find the corresponding file series
[332]2%--------------------------------------------------------------------------
[398]3% function [RootPath,RootFile,i1_series,i2_series,j1_series,j2_series,NomType,FileType,Object,i1_input,i2_input,j1_input,j2_input]=find_file_series(fileinput)
[332]4%
5% OUTPUT:
[371]6% RootFile: root file detected in fileinput, possibly modified for movies (indexing is then done on image view, not file)
[343]7% i1_series(ref_i+1, ref_j+1,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
8%  (ref_i+1 is used to deal with the image index zero sometimes used)
[332]9% NomType: nomenclature type corrected after checking the first file (problem of 0 before the number string)
10% FileType: type of file, =
11%       = 'image', usual image as recognised by Matlab
12%       = 'multimage', image series stored in a single file
13%       = 'civx', netcdf file with civx convention
14%       = 'civdata', civ data with new convention
15%       = 'netcdf' other netcdf files
16%       = 'video': movie recognised by VideoReader (e;g. avi)
17% Object: video object (=[] otherwise)
18%
19%INPUT
[371]20% RootPath: path to the directory to be scanned
21% fileinput: name (without path) of the input file sample
[332]22%
23%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
24%  Copyright  2011, LEGI / CNRS-UJF-INPG, joel.sommeria@legi.grenoble-inp.fr
25%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
26%     This file is part of the toolbox UVMAT.
27%
28%     UVMAT is free software; you can redistribute it and/or modify
29%     it under the terms of the GNU General Public License as published by
30%     the Free Software Foundation; either version 2 of the License, or
31%     (at your option) any later version.
32%
33%     UVMAT is distributed in the hope that it will be useful,
34%     but WITHOUT ANY WARRANTY; without even the implied warranty of
35%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
37%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
38
[398]39function [RootPath,SubDir,RootFile,i1_series,i2_series,j1_series,j2_series,NomType,FileType,Object,i1_input,i2_input,j1_input,j2_input]=find_file_series(FilePath,fileinput)
[332]40%------------------------------------------------------------------------
[398]41
[332]42%% get input root name and nomenclature type
[398]43fullfileinput=fullfile(FilePath,fileinput);% input file name with path
44[RootPath,SubDir,RootFile,i1_input,i2_input,j1_input,j2_input,FileExt,NomType]=fileparts_uvmat(fullfileinput);
[332]45
[398]46
[332]47%% check for particular file types: images, movies, civ data
[339]48i1_series=zeros(1,1,1);
49i2_series=zeros(1,1,1);
50j1_series=zeros(1,1,1);
51j2_series=zeros(1,1,1);
[386]52% ifile_min=1;%default
[376]53[FileType,FileInfo,Object]=get_file_type(fullfileinput);
[439]54switch FileType
55    case 'multimage'
[388]56    NomType='*';
[439]57    i1_series=(1:FileInfo.NbFrame)';
58    case {'video','mmreader'}
59    NomType='*';
[397]60    i1_series=(1:FileInfo.NumberOfFrames)';
[439]61end
62% if strcmp( FileType,'multimage')||strcmp( FileType,'video')||strcmp( FileType,'mmreader')
63%     NomType='*';
64%     i1_series=(1:FileInfo.NumberOfFrames)';
65% end
[339]66
[398]67if strcmp(NomType,'')||strcmp(NomType,'*')
[371]68    if exist(fullfileinput,'file')
[398]69        [tild,RootFile]=fileparts(fileinput);% case of constant name (no indexing), get the filename without its extension
[388]70    else
[360]71        RootFile='';
[339]72    end
[398]73    i1_input=1;% the index now refer to the frame in the movie, choose 1 at opening
74    i2_input=[];
75    j1_input=[];
76    j2_input=[];
[388]77else
[395]78    %% possibly include the first index in the root name, if there exists a corresponding xml file
[384]79    NomTypePref='';
[385]80    r=regexp(NomType,'^(?<tiretnum>_?\d+)','names');%look for a number or _1 at the beginning of NomType
[384]81    if ~isempty(r)
[388]82        fileinput_end=regexprep(fileinput,['^' RootFile],'');%remove RootFile at the beginning of fileinput
[395]83        if isempty(regexp(r.tiretnum,'^_','once'))% if a separator '_' is not  detected
[388]84            rr=regexp(fileinput_end,'^(?<i1>\d+)','names');
85        else% if a separator '_' is  detected
86            rr=regexp(fileinput_end,'^(?<i1>_\d+)','names');
[384]87        end
[388]88        if ~isempty(rr)
89            RootFileNew=[RootFile rr.i1];
[395]90            checkpair=~isempty(regexp(NomType,'-','once'))||~isempty(regexp(NomType,'ab$','once'))||~isempty(regexp(NomType,'AB$','once'));%case of PIV results
[398]91            if exist(fullfile(RootPath,[RootFileNew '.xml']),'file') %|| (checkpair && exist(fullfile(fileparts(RootPath),[RootFileNew '.xml']),'file'))
[388]92                RootFile=RootFileNew;
[391]93                NomTypePref=r.tiretnum;
[388]94                NomType=regexprep(NomType,['^'  NomTypePref],'');
[398]95                i1_input=j1_input;
[388]96                i2_input=j2_input;
97                j1_input=[];
98                j2_input=[];
[395]99            end       
[384]100        end
101    end
[360]102    %% analyse the list of existing files when relevant
103    sep1='';
[388]104    i1_str='(?<i1>)';%will set i1=[];
[360]105    i1_star='';
[388]106    %     r.sep2='';
107    i2_str='(?<i2>)';%will set i2=[];
[360]108    i2_star='';
[388]109    %     sep3='';
110    j1_str='(?<j1>)';%will set j1=[];
[360]111    j1_star='';
[388]112    %     sep4='';
113    j2_str='(?<j2>)';%will set j2=[];
[360]114    j2_star='';
[388]115    %   NomTypeStr=NomType;
116    %Look for cases with letter indexing for the second index
117    r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<j1>[a|A])(?<j2>[b|B]?)$','names');
[360]118    if ~isempty(r)
[388]119        sep1=r.sep1;
[399]120        i1_str='(?<i1>\d+)';
121        if strcmp(lower(r.j1),r.j1)% lower case index
[388]122            j1_str='(?<j1>[a-z])';
123        else
[399]124           j1_str='(?<j1>[A-Z])'; % upper case index
[360]125        end
[388]126        j1_star='*';
127        if ~isempty(r.j2)
128           if strcmp(lower(r.j1),r.j1)
129            j2_str='(?<j2>[a-z])';
130            else
131           j2_str='(?<j2>[A-Z])';
132           end
133            j2_star='*';
[360]134        end
[388]135    else %numerical indexing
136        r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<i2>(-\d+)?)(?<j1>(_\d+)?)(?<j2>(-\d+)?)$','names');
137        if ~isempty(r)
138            sep1=r.sep1;
139            i1_str='(?<i1>\d+)';
140            i1_star='*';
141            if ~isempty(r.i2)
142                i2_str='(?<i2>-\d+)';
143                i2_star='-*';
[360]144            end
[388]145            if ~isempty(r.j1)
146                j1_str='(?<j1>_\d+)';
147                j1_star='_*';
[360]148            end
[388]149            if ~isempty(r.j2)
150                j2_str='(?<j2>-\d+)';
151                j2_star='-*';
[360]152            end
153        end
[332]154    end
[388]155    detect_string=['^' RootFile sep1 i1_str i2_str j1_str j2_str FileExt '$'];%string used in regexp to detect file indices
[360]156    %find the string used to extract the relevant files with the command dir
[388]157    star_string=[RootFile sep1 i1_star i2_star  j1_star j2_star FileExt];
[360]158    wd=pwd;%current working directory
[398]159    cd (FilePath)% move to the local dir to save time in the operation dir.
[388]160    dirpair=dir(star_string);% look for relevant files in the file directory
[360]161    cd(wd)
[339]162    nbpair=numel(dirpair);
163    ref_i_list=zeros(1,nbpair);
164    ref_j_list=zeros(1,nbpair);
165    if nbpair==0% no detected file
166        RootFile='';
167    end
[360]168    % scan the list of relevant files, extract the indices
[339]169    for ifile=1:nbpair
[360]170        rr=regexp(dirpair(ifile).name,detect_string,'names');
[362]171        if ~isempty(rr)
[388]172            i1=str2num(rr.i1);
173            i2=str2num(regexprep(rr.i2,'^-',''));
174            j1=stra2num(regexprep(rr.j1,'^_',''));
175            j2=stra2num(regexprep(rr.j2,'^-',''));
176            ref_i=i1;
177            if isempty(i2_input)
178                if ~isempty(i2)% invalid file name if i2 does not exist in the input file
179                    break
180                end
181            else
182                ref_i=floor((i1+i2)/2);
[339]183            end
[388]184            ref_j=1;
185            if isempty(j1_input)
186                if  ~isempty(j1)% invalid file name if j1 does not exist in the input file
187                    break
188                end
189            else %j1_input is not empty
190                if isempty(j1)% the detected name does not fit with the input
191                    break
192                else
193                    ref_j=j1;
194                    if isempty(j2_input)
195                        if  ~isempty(j2)% invalid file name if j2 does not exist in the input file
196                            break
197                        end
198                    else
199                        ref_j=floor((j1+j2)/2);
[342]200                    end
[339]201                end
202            end
[388]203            % update the detected index series
204            ref_i_list(ifile)=ref_i;
205            ref_j_list(ifile)=ref_j;
206            nb_pairs=0;
207            if ~isempty(i2_input)|| ~isempty(j2_input) %deals with  pairs
208                if size(i1_series,1)>=ref_i+1 && size(i1_series,2)>=ref_j+1
209                    nb_pairs=numel(find(i1_series(ref_i+1,ref_j+1,:)~=0));
210                end
[339]211            end
[388]212            i1_series(ref_i+1,ref_j+1,nb_pairs+1)=i1;
213            if ~isempty(i2_input)
214                i2_series(ref_i+1,ref_j+1,nb_pairs+1)=i2;
215            end
216            if ~isempty(j1_input)
217                j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
218            end
219            if ~isempty(j2_input)
220                j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
221                j2_series(ref_i+1,ref_j+1,nb_pairs+1)=j2;
222            end
[334]223        end
[339]224    end
225    % look for the numerical string of the first files to update the NomType (take into account the 0 before the number)
226    max_j=max(ref_j_list);
[350]227    if isempty(max_j)
228        ref_ij=ref_i_list;
229    else
230        ref_ij=ref_i_list*max_j+ref_j_list; % ordered by index i, then by j for a given i.
231    end
[421]232    %[tild,ifile_min]=min(ref_ij(ref_ij>0));
233    ifile_min=find(ref_ij>0 , 1);
[360]234    if isempty(ifile_min)
[388]235        %         RootPath='';
[350]236        RootFile='';
237        NomType='';
238    else
[372]239        [tild,tild,tild,tild,tild,tild,tild,tild,NomType]=fileparts_uvmat(dirpair(ifile_min).name);% update the representation of indices (number of 0 before the number)
[384]240        NomType=regexprep(NomType,['^' NomTypePref],'');
[350]241    end
[386]242    %% update the file type if the input file does not exist (pb of 0001)
243    if isempty(FileType)
244        [FileType,tild,Object]=get_file_type(dirpair(ifile_min).name);
245    end
[339]246end
247
248%% set to empty array the irrelevant index series
249if isequal(i1_series,0), i1_series=[]; end
250if isequal(i2_series,0), i2_series=[]; end
251if isequal(j1_series,0), j1_series=[]; end
252if isequal(j2_series,0), j2_series=[]; end
253
254%% sort pairs by decreasing index differences in case of multiple pairs at the same reference index
255if size(i2_series,3)>1 %pairs i1 -i2
256    diff_index=abs(i2_series-i1_series);
[353]257    [tild,ind_pair]=sort(diff_index,3,'descend');
[339]258    for ref_i=1:size(i1_series,1)
259        for ref_j=1:size(j1_series,2)
260            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
261            i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
262            if ~isempty(j1_series)
263                j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
264            end
265        end
[334]266    end
[339]267elseif size(j2_series,3)>1 %pairs j1 -j2
268    diff_index=abs(j2_series-j1_series);
[353]269    [tild,ind_pair]=sort(diff_index,3,'descend');
[339]270    for ref_i=1:size(i1_series,1)
271        for ref_j=1:size(j1_series,2)
272            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
273            if ~isempty(i2_series)
274                i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
275            end
276            j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
277            j2_series(ref_i,ref_j,:)=j2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
278        end
279    end
280end
281
Note: See TracBrowser for help on using the repository browser.