source: trunk/src/find_file_series.m @ 356

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

civ updated with new functions for opening files, consistently with uvmat
Bugs to be expected (use previous version then)

File size: 9.1 KB
Line 
1%'find_file_series': check the content of an input file 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,tild,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                if exist('VideoReader','file')%recent version of Matlab
90                    Object=VideoReader(fileinput);   
91                else
92                    Object=mmreader(fileinput);%older Matlab function for movies               
93                end
94                NomType='*';
95                FileType='video'; 
96                i1_series=(1:get(Object,'NumberOfFrames'))';
97            end
98        end
99end
100
101%% get the list of existing files when relevant
102if strcmp(NomType,'')||strcmp(NomType,'*')
103    if exist(fileinput,'file')
104    [RootPath,RootFile]=fileparts(fileinput);% case of constant name (no indexing)
105    else
106       RootPath='';
107       RootFile='';
108    end
109else
110    if strcmp(SubDir,'')
111        filebasesub=fullfile(RootPath,RootFile);
112    else
113        filebasesub=fullfile(RootPath,SubDir,RootFile);
114    end
115    detect_string=regexprep(NomType,'\d','*');%replace numbers by '*'
116    old_string='';
117    detect_string=regexprep(detect_string,'[ab]$','*');%suppress the possible letter ab at the end
118    detect_string=regexprep(detect_string,'[AB]$','*');%suppress the possible letter ab at the end
119    detect_string=regexprep(detect_string,'[a|A]$','*');%suppress a possible second letter a,A at the end
120    while ~strcmp(detect_string,old_string)%removes multiple '*'
121        old_string=detect_string;
122        detect_string=regexprep(detect_string,'**','*');
123    end
124    dirpair=dir([filebasesub detect_string FileExt]);
125    nbpair=numel(dirpair);
126    ref_i_list=zeros(1,nbpair);
127    ref_j_list=zeros(1,nbpair);
128    if nbpair==0% no detected file
129        RootPath='';
130        RootFile='';
131    end
132    for ifile=1:nbpair
133        [tild,tild,tild,i1,i2,j1,j2]=fileparts_uvmat(dirpair(ifile).name);
134        ref_i=i1;
135        if isempty(i2_input)
136            if ~isempty(i2)% invalid file name if i2 does not exist in the input file
137                break
138            end
139        else
140            ref_i=floor((i1+i2)/2);
141        end
142        ref_j=1;
143        if isempty(j1_input)
144            if  ~isempty(j1)% invalid file name if j1 does not exist in the input file
145                break
146            end
147        else %j1_input is not empty
148            if isempty(j1)% the detected name does not fit with the input
149                break
150            else
151                ref_j=j1;
152                if isempty(j2_input)
153                    if  ~isempty(j2)% invalid file name if j2 does not exist in the input file
154                        break
155                    end
156                else
157                    ref_j=floor((j1+j2)/2);
158                end
159            end
160        end
161        % update the detected index series
162        ref_i_list(ifile)=ref_i;
163        ref_j_list(ifile)=ref_j;
164        nb_pairs=0;
165        if ~isempty(i2_input)|| ~isempty(j2_input) %deals with  pairs
166            if size(i1_series,1)>=ref_i+1 && size(i1_series,2)>=ref_j+1
167                nb_pairs=numel(find(i1_series(ref_i+1,ref_j+1,:)~=0));
168            end
169        end
170        i1_series(ref_i+1,ref_j+1,nb_pairs+1)=i1;
171        if ~isempty(i2_input)
172            i2_series(ref_i+1,ref_j+1,nb_pairs+1)=i2;
173        end
174        if ~isempty(j1_input)
175            j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
176        end
177        if ~isempty(j2_input)
178            j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
179            j2_series(ref_i+1,ref_j+1,nb_pairs+1)=j2;
180        end
181    end
182    % look for the numerical string of the first files to update the NomType (take into account the 0 before the number)
183    max_j=max(ref_j_list);
184    if isempty(max_j)
185        ref_ij=ref_i_list;
186    else
187        ref_ij=ref_i_list*max_j+ref_j_list; % ordered by index i, then by j for a given i.
188    end
189    [tild,ifile]=min(ref_ij(ref_ij>0));
190    if isempty(ifile)
191        RootPath='';
192        RootFile='';
193        NomType='';
194    else
195    [tild,tild,tild,tild,tild,tild,tild,tild,NomType]=fileparts_uvmat(dirpair(ifile).name);
196    end
197end
198
199%% set to empty array the irrelevant index series
200if isequal(i1_series,0), i1_series=[]; end
201if isequal(i2_series,0), i2_series=[]; end
202if isequal(j1_series,0), j1_series=[]; end
203if isequal(j2_series,0), j2_series=[]; end
204
205%% sort pairs by decreasing index differences in case of multiple pairs at the same reference index
206if size(i2_series,3)>1 %pairs i1 -i2
207    diff_index=abs(i2_series-i1_series);
208    [tild,ind_pair]=sort(diff_index,3,'descend');
209    for ref_i=1:size(i1_series,1)
210        for ref_j=1:size(j1_series,2)
211            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
212            i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
213            if ~isempty(j1_series)
214                j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
215            end
216        end
217    end
218elseif size(j2_series,3)>1 %pairs j1 -j2
219    diff_index=abs(j2_series-j1_series);
220    [tild,ind_pair]=sort(diff_index,3,'descend');
221    for ref_i=1:size(i1_series,1)
222        for ref_j=1:size(j1_series,2)
223            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
224            if ~isempty(i2_series)
225                i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
226            end
227            j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
228            j2_series(ref_i,ref_j,:)=j2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
229        end
230    end
231end
232
Note: See TracBrowser for help on using the repository browser.