source: trunk/src/get_file_type.m @ 771

Last change on this file since 771 was 771, checked in by sommeria, 10 years ago
File size: 5.4 KB
Line 
1%'get_file_type': determine info about a file (image, multimage, civdata,...) .
2%------------------------------------------------------------------------
3% [FileType,FileInfo,Object]=get_file_type(fileinput)
4%
5% OUTPUT:
6% FileType: type of file
7% FileInfo: structure containing info on the file (case of images or video), in particular
8%      .Height: image height in pixels
9%      .Width:  image width in pixels
10%      .BitDepth: nbre of bits per pixel  (8 of 16)
11%      .ColorType: 'greyscale' or 'color'
12%      .NumberOfFrames
13%      .FrameRate: nbre of frames per second, =[] for images
14% Object: in case of video
15%
16% INPUT:
17% fileinput: name, including path, of the file to analyse
18function [FileType,FileInfo,VideoObject]=get_file_type(fileinput)
19%%%% TODO: suppress the output argument FileType, contained in FileInfo %%%%
20FileInfo=[];% will remain empty in the absence of input file
21VideoObject=[];
22if exist(fileinput,'file')
23    FileInfo.FileName=fileinput;
24    FileInfo.FileType='txt'; %default
25    FileType='txt';%default, text file
26else
27    FileType='';
28    return
29end
30[tild,tild,FileExt]=fileparts(fileinput);
31
32switch FileExt
33    case '.fig'
34        FileInfo.FileType='figure';
35        FileType='figure';
36    case '.xml'
37        FileInfo.FileType='xml';
38        FileType='xml';
39    case '.xls'
40        FileInfo.FileType='xls';
41        FileType='xls';
42    otherwise
43        if ~isempty(FileExt)% exclude empty extension
44            FileExt=regexprep(FileExt,'^.','');% eliminate the dot of the extension
45            if ~isempty(FileExt)
46                if ~isempty(imformats(FileExt))%case of images
47                    try
48                        imainfo=imfinfo(fileinput);
49                        if length(imainfo) >1 %case of image with multiple frames
50                            FileType='multimage';
51                            FileInfo=imainfo(1);%take info from the first frame
52                            FileInfo.NumberOfFrames=length(imainfo);
53                        else
54                            FileType='image';
55                            FileInfo=imainfo;
56                            FileInfo.NumberOfFrames=1;
57                        end
58                        FileInfo.FileName=FileInfo.Filename; %correct the info given by imfinfo
59                        FileInfo.FileType=FileType;
60                    end
61                else
62                    error_nc=0;
63                    try
64                      %  [Data,tild,tild,errormsg]=nc2struct(fileinput,'ListGlobalAttribute','absolut_time_T0','Conventions',...
65                       %     'CivStage','patch2','fix2','civ2','patch','fix','hart');
66                       [Data,tild,tild,errormsg]=nc2struct(fileinput,[]);
67                        if ~isempty(errormsg)
68                            error_nc=1;
69                        else
70                            if isfield(Data,'absolut_time_T0') && isfield(Data,'hart') && ~isempty(Data.absolut_time_T0) && ~isempty(Data.hart)
71                                FileInfo.FileType='civx';
72                                FileType='civx'; % test for civx velocity fields
73                                if isfield(Data,'patch2') && isequal(Data.patch2,1)
74                                    FileInfo.CivStage=6;
75                                elseif isfield(Data,'fix2') && isequal(Data.fix2,1)
76                                    FileInfo.CivStage=5;
77                                elseif  isfield(Data,'civ2')&& isequal(Data.civ2,1)
78                                    FileInfo.CivStage=4;
79                                elseif isfield(Data,'patch')&&isequal(Data.patch,1)
80                                    FileInfo.CivStage=3;
81                                elseif isfield(Data,'fix')&&isequal(Data.fix,1)
82                                    FileInfo.CivStage=2;
83                                else
84                                    FileInfo.CivStage=1;
85                                end
86                            elseif isfield(Data,'Conventions') && strcmp(Data.Conventions,'uvmat/civdata')
87                                FileInfo.FileType='civdata'; % test for civx velocity fields
88                                FileType='civdata'; % test for civx velocity fields
89                                FileInfo.CivStage=Data.CivStage;
90                            else
91                                FileInfo.FileType='netcdf';
92                                FileType='netcdf';
93                                FileInfo.ListVarName=Data.ListVarName;
94                            end
95                        end
96                    catch ME
97                        error_nc=1;
98                    end
99                    if error_nc
100                        try
101                            if exist('VideoReader.m','file')%recent version of Matlab
102                                VideoObject=VideoReader(fileinput);
103                                FileInfo=get(VideoObject);
104                                FileType='video';
105                            elseif exist('mmreader.m','file')% Matlab 2009a
106                                VideoObject=mmreader(fileinput);
107                                FileInfo=get(VideoObject);
108                                FileType='mmreader';
109                            end
110                            FileInfo.FileName=fileinput;
111                            FileInfo.FileType=FileType;
112                            FileInfo.BitDepth=FileInfo.BitsPerPixel/3;
113                        end
114                    end
115                end
116            end
117        end
118end
Note: See TracBrowser for help on using the repository browser.