source: trunk/src/find_file_series.m @ 350

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

series modified using 'uitable'. Bugs need to be corrected.

File size: 8.9 KB
Line 
1%'find_file_series': check the content onf an input field and find the corresponding file series
2%--------------------------------------------------------------------------
3% function [RootPath,RootFile,i1_series,i2_series,j1_series,j2_series,NomType,FileType,Object]=find_file_series(fileinput)
4%
5% OUTPUT:
6% RootPath,RootFile: root path and root name detected in fileinput, possibly modified for movies (indexing is then done on image view, not file)
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)
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
20% fileinput: name (including path)  of the input file
21%
22%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
23%  Copyright  2011, LEGI / CNRS-UJF-INPG, joel.sommeria@legi.grenoble-inp.fr
24%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
25%     This file is part of the toolbox UVMAT.
26%
27%     UVMAT is free software; you can redistribute it and/or modify
28%     it under the terms of the GNU General Public License as published by
29%     the Free Software Foundation; either version 2 of the License, or
30%     (at your option) any later version.
31%
32%     UVMAT is distributed in the hope that it will be useful,
33%     but WITHOUT ANY WARRANTY; without even the implied warranty of
34%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
36%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
37
38function [RootPath,RootFile,i1_series,i2_series,j1_series,j2_series,NomType,FileType,Object]=find_file_series(fileinput)
39%------------------------------------------------------------------------
40
41%% get input root name and nomenclature type
42[RootPath,SubDir,RootFile,~,i2_input,j1_input,j2_input,FileExt,NomType]=fileparts_uvmat(fileinput);
43
44%% check for particular file types: images, movies, civ data
45FileType='';
46Object=[];
47i1_series=zeros(1,1,1);
48i2_series=zeros(1,1,1);
49j1_series=zeros(1,1,1);
50j2_series=zeros(1,1,1);
51switch FileExt
52    % ancillary files, no field indexing
53    case {'.civ','.log','.cmx','.cmx2','.txt','.bat'}
54        FileType='txt';
55        NomType='';
56    case '.fig'
57        FileType='figure';
58        NomType='';
59    case '.xml'
60        FileType='xml';
61        NomType='';
62    case '.xls'
63        FileType='xls';
64        NomType='';
65    otherwise
66        if ~isempty(FileExt)&& ~isempty(imformats(FileExt(2:end)))
67            try
68                imainfo=imfinfo(fileinput);
69                FileType='image';
70                if length(imainfo) >1 %case of image with multiple frames
71                    NomType='*';
72                    FileType='multimage';
73                    i1_series=1:length(imainfo);
74                    [RootPath,RootFile]=fileparts(fileinput);
75                end
76            end
77        else
78            try
79                Data=nc2struct(fileinput,'ListGlobalAttribute','absolut_time_T0','Conventions');
80                if ~isempty(Data.absolut_time_T0')
81                    FileType='civx'; % test for civx velocity fields
82                elseif strcmp(Data.Conventions','uvmat/civdata')
83                    FileType='civdata'; % test for civx velocity fields
84                else
85                    FileType='netcdf';
86                end
87            end
88            try
89                Object=VideoReader(fileinput);
90                NomType='*';
91                FileType='video';
92                i1_series=[1:get(Object,'NumberOfFrames')];
93                [RootPath,RootFile]=fileparts(fileinput);
94            end
95        end
96end
97
98%% get the list of existing files when relevant
99if strcmp(NomType,'')||strcmp(NomType,'*')
100    if exist(fileinput,'file')
101    [RootPath,RootFile]=fileparts(fileinput);% case of constant name (no indexing)
102    else
103       RootPath='';
104       RootFile='';
105    end
106else
107    if strcmp(SubDir,'')
108        filebasesub=fullfile(RootPath,RootFile);
109    else
110        filebasesub=fullfile(RootPath,SubDir,RootFile);
111    end
112    detect_string=regexprep(NomType,'\d','*');%replace numbers by '*'
113    old_string='';
114    detect_string=regexprep(detect_string,'[ab]$','*');%suppress the possible letter ab at the end
115    detect_string=regexprep(detect_string,'[AB]$','*');%suppress the possible letter ab at the end
116    detect_string=regexprep(detect_string,'[a|A]$','*');%suppress a possible second letter a,A at the end
117    while ~strcmp(detect_string,old_string)%removes multiple '*'
118        old_string=detect_string;
119        detect_string=regexprep(detect_string,'**','*');
120    end
121    dirpair=dir([filebasesub detect_string FileExt]);
122    nbpair=numel(dirpair);
123    ref_i_list=zeros(1,nbpair);
124    ref_j_list=zeros(1,nbpair);
125    if nbpair==0% no detected file
126        RootPath='';
127        RootFile='';
128    end
129    for ifile=1:nbpair
130        [~,~,~,i1,i2,j1,j2]=fileparts_uvmat(dirpair(ifile).name);
131        ref_i=i1;
132        if isempty(i2_input)
133            if ~isempty(i2)% invalid file name if i2 does not exist in the input file
134                break
135            end
136        else
137            ref_i=floor((i1+i2)/2);
138        end
139        ref_j=1;
140        if isempty(j1_input)
141            if  ~isempty(j1)% invalid file name if j1 does not exist in the input file
142                break
143            end
144        else %j1_input is not empty
145            if isempty(j1)% the detected name does not fit with the input
146                break
147            else
148                ref_j=j1;
149                if isempty(j2_input)
150                    if  ~isempty(j2)% invalid file name if j2 does not exist in the input file
151                        break
152                    end
153                else
154                    ref_j=floor((j1+j2)/2);
155                end
156            end
157        end
158        % update the detected index series
159        ref_i_list(ifile)=ref_i;
160        ref_j_list(ifile)=ref_j;
161        nb_pairs=0;
162        if ~isempty(i2_input)|| ~isempty(j2_input) %deals with  pairs
163            if size(i1_series,1)>=ref_i+1 && size(i1_series,2)>=ref_j+1
164                nb_pairs=numel(find(i1_series(ref_i+1,ref_j+1,:)~=0));
165            end
166        end
167        i1_series(ref_i+1,ref_j+1,nb_pairs+1)=i1;
168        if ~isempty(i2_input)
169            i2_series(ref_i+1,ref_j+1,nb_pairs+1)=i2;
170        end
171        if ~isempty(j1_input)
172            j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
173        end
174        if ~isempty(j2_input)
175            j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
176            j2_series(ref_i+1,ref_j+1,nb_pairs+1)=j2;
177        end
178    end
179    % look for the numerical string of the first files to update the NomType (take into account the 0 before the number)
180    max_j=max(ref_j_list);
181    if isempty(max_j)
182        ref_ij=ref_i_list;
183    else
184        ref_ij=ref_i_list*max_j+ref_j_list; % ordered by index i, then by j for a given i.
185    end
186    [~,ifile]=min(ref_ij(ref_ij>0));
187    if isempty(ifile)
188        RootPath='';
189        RootFile='';
190        NomType='';
191    else
192    [~,~,~,~,~,~,~,~,NomType]=fileparts_uvmat(dirpair(ifile).name);
193    end
194end
195
196%% set to empty array the irrelevant index series
197if isequal(i1_series,0), i1_series=[]; end
198if isequal(i2_series,0), i2_series=[]; end
199if isequal(j1_series,0), j1_series=[]; end
200if isequal(j2_series,0), j2_series=[]; end
201
202%% sort pairs by decreasing index differences in case of multiple pairs at the same reference index
203if size(i2_series,3)>1 %pairs i1 -i2
204    diff_index=abs(i2_series-i1_series);
205    [~,ind_pair]=sort(diff_index,3,'descend');
206    for ref_i=1:size(i1_series,1)
207        for ref_j=1:size(j1_series,2)
208            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
209            i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
210            if ~isempty(j1_series)
211                j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
212            end
213        end
214    end
215elseif size(j2_series,3)>1 %pairs j1 -j2
216    diff_index=abs(j2_series-j1_series);
217    [~,ind_pair]=sort(diff_index,3,'descend');
218    for ref_i=1:size(i1_series,1)
219        for ref_j=1:size(j1_series,2)
220            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
221            if ~isempty(i2_series)
222                i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
223            end
224            j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
225            j2_series(ref_i,ref_j,:)=j2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
226        end
227    end
228end
229
Note: See TracBrowser for help on using the repository browser.