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 |
---|
18 | function [FileType,FileInfo,VideoObject]=get_file_type(fileinput) |
---|
19 | %%%% TODO: suppress the output argument FileType, contained in FileInfo %%%% |
---|
20 | FileInfo=[];% will remain empty in the absence of input file |
---|
21 | VideoObject=[]; |
---|
22 | if exist(fileinput,'file') |
---|
23 | FileInfo.FileName=fileinput; |
---|
24 | FileInfo.FileType='txt'; %default |
---|
25 | FileType='txt';%default, text file |
---|
26 | else |
---|
27 | FileType=''; |
---|
28 | return |
---|
29 | end |
---|
30 | [tild,tild,FileExt]=fileparts(fileinput); |
---|
31 | |
---|
32 | switch 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 | case '.dat' |
---|
43 | FileInfo.FileType='dat'; |
---|
44 | FileType='dat'; |
---|
45 | otherwise |
---|
46 | if ~isempty(FileExt)% exclude empty extension |
---|
47 | FileExt=regexprep(FileExt,'^.','');% eliminate the dot of the extension |
---|
48 | if ~isempty(FileExt) |
---|
49 | if ~isempty(imformats(FileExt))%case of images |
---|
50 | try |
---|
51 | imainfo=imfinfo(fileinput); |
---|
52 | if length(imainfo) >1 %case of image with multiple frames |
---|
53 | FileType='multimage'; |
---|
54 | FileInfo=imainfo(1);%take info from the first frame |
---|
55 | FileInfo.NumberOfFrames=length(imainfo); |
---|
56 | else |
---|
57 | FileType='image'; |
---|
58 | FileInfo=imainfo; |
---|
59 | FileInfo.NumberOfFrames=1; |
---|
60 | end |
---|
61 | FileInfo.FileName=FileInfo.Filename; %correct the info given by imfinfo |
---|
62 | FileInfo.FileType=FileType; |
---|
63 | end |
---|
64 | else |
---|
65 | error_nc=0; |
---|
66 | try |
---|
67 | % [Data,tild,tild,errormsg]=nc2struct(fileinput,'ListGlobalAttribute','absolut_time_T0','Conventions',... |
---|
68 | % 'CivStage','patch2','fix2','civ2','patch','fix','hart'); |
---|
69 | [Data,tild,tild,errormsg]=nc2struct(fileinput,[]); |
---|
70 | if ~isempty(errormsg) |
---|
71 | error_nc=1; |
---|
72 | else |
---|
73 | if isfield(Data,'absolut_time_T0') && isfield(Data,'hart') && ~isempty(Data.absolut_time_T0) && ~isempty(Data.hart) |
---|
74 | FileInfo.FileType='civx'; |
---|
75 | FileType='civx'; % test for civx velocity fields |
---|
76 | if isfield(Data,'patch2') && isequal(Data.patch2,1) |
---|
77 | FileInfo.CivStage=6; |
---|
78 | elseif isfield(Data,'fix2') && isequal(Data.fix2,1) |
---|
79 | FileInfo.CivStage=5; |
---|
80 | elseif isfield(Data,'civ2')&& isequal(Data.civ2,1) |
---|
81 | FileInfo.CivStage=4; |
---|
82 | elseif isfield(Data,'patch')&&isequal(Data.patch,1) |
---|
83 | FileInfo.CivStage=3; |
---|
84 | elseif isfield(Data,'fix')&&isequal(Data.fix,1) |
---|
85 | FileInfo.CivStage=2; |
---|
86 | else |
---|
87 | FileInfo.CivStage=1; |
---|
88 | end |
---|
89 | elseif isfield(Data,'Conventions') && strcmp(Data.Conventions,'uvmat/civdata') |
---|
90 | FileInfo.FileType='civdata'; % test for civx velocity fields |
---|
91 | FileType='civdata'; % test for civx velocity fields |
---|
92 | FileInfo.CivStage=Data.CivStage; |
---|
93 | else |
---|
94 | FileInfo.FileType='netcdf'; |
---|
95 | FileType='netcdf'; |
---|
96 | FileInfo.ListVarName=Data.ListVarName; |
---|
97 | end |
---|
98 | end |
---|
99 | catch ME |
---|
100 | error_nc=1; |
---|
101 | end |
---|
102 | if error_nc |
---|
103 | try |
---|
104 | if exist('VideoReader.m','file')%recent version of Matlab |
---|
105 | VideoObject=VideoReader(fileinput); |
---|
106 | FileInfo=get(VideoObject); |
---|
107 | FileType='video'; |
---|
108 | elseif exist('mmreader.m','file')% Matlab 2009a |
---|
109 | VideoObject=mmreader(fileinput); |
---|
110 | FileInfo=get(VideoObject); |
---|
111 | FileType='mmreader'; |
---|
112 | end |
---|
113 | FileInfo.FileName=fileinput; |
---|
114 | FileInfo.FileType=FileType; |
---|
115 | FileInfo.BitDepth=FileInfo.BitsPerPixel/3; |
---|
116 | end |
---|
117 | end |
---|
118 | end |
---|
119 | end |
---|
120 | end |
---|
121 | end |
---|