[380] | 1 | %'get_file_type': determine info about a file (image, multimage, civdata,...) . |
---|
[376] | 2 | %------------------------------------------------------------------------ |
---|
[380] | 3 | % [FileType,FileInfo,Object]=get_file_type(fileinput) |
---|
[376] | 4 | % |
---|
| 5 | % OUTPUT: |
---|
| 6 | % FileType: type of file |
---|
[469] | 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 |
---|
[376] | 14 | % Object: in case of video |
---|
| 15 | % |
---|
| 16 | % INPUT: |
---|
| 17 | % fileinput: name, including path, of the file to analyse |
---|
[397] | 18 | function [FileType,FileInfo,VideoObject]=get_file_type(fileinput) |
---|
[771] | 19 | %%%% TODO: suppress the output argument FileType, contained in FileInfo %%%% |
---|
| 20 | FileInfo=[];% will remain empty in the absence of input file |
---|
[397] | 21 | VideoObject=[]; |
---|
[675] | 22 | if exist(fileinput,'file') |
---|
[771] | 23 | FileInfo.FileName=fileinput; |
---|
| 24 | FileInfo.FileType='txt'; %default |
---|
[675] | 25 | FileType='txt';%default, text file |
---|
| 26 | else |
---|
| 27 | FileType=''; |
---|
| 28 | return |
---|
| 29 | end |
---|
[376] | 30 | [tild,tild,FileExt]=fileparts(fileinput); |
---|
| 31 | |
---|
| 32 | switch FileExt |
---|
| 33 | case '.fig' |
---|
[771] | 34 | FileInfo.FileType='figure'; |
---|
[376] | 35 | FileType='figure'; |
---|
| 36 | case '.xml' |
---|
[771] | 37 | FileInfo.FileType='xml'; |
---|
[376] | 38 | FileType='xml'; |
---|
| 39 | case '.xls' |
---|
[771] | 40 | FileInfo.FileType='xls'; |
---|
[376] | 41 | FileType='xls'; |
---|
| 42 | otherwise |
---|
[664] | 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 |
---|
[771] | 58 | FileInfo.FileName=FileInfo.Filename; %correct the info given by imfinfo |
---|
| 59 | FileInfo.FileType=FileType; |
---|
[664] | 60 | end |
---|
[446] | 61 | else |
---|
[664] | 62 | error_nc=0; |
---|
| 63 | try |
---|
[771] | 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,[]); |
---|
[752] | 67 | if ~isempty(errormsg) |
---|
[664] | 68 | error_nc=1; |
---|
| 69 | else |
---|
[771] | 70 | if isfield(Data,'absolut_time_T0') && isfield(Data,'hart') && ~isempty(Data.absolut_time_T0) && ~isempty(Data.hart) |
---|
| 71 | FileInfo.FileType='civx'; |
---|
[664] | 72 | FileType='civx'; % test for civx velocity fields |
---|
[771] | 73 | if isfield(Data,'patch2') && isequal(Data.patch2,1) |
---|
[664] | 74 | FileInfo.CivStage=6; |
---|
[771] | 75 | elseif isfield(Data,'fix2') && isequal(Data.fix2,1) |
---|
[664] | 76 | FileInfo.CivStage=5; |
---|
[771] | 77 | elseif isfield(Data,'civ2')&& isequal(Data.civ2,1) |
---|
[664] | 78 | FileInfo.CivStage=4; |
---|
[771] | 79 | elseif isfield(Data,'patch')&&isequal(Data.patch,1) |
---|
[664] | 80 | FileInfo.CivStage=3; |
---|
[771] | 81 | elseif isfield(Data,'fix')&&isequal(Data.fix,1) |
---|
[664] | 82 | FileInfo.CivStage=2; |
---|
[752] | 83 | else |
---|
[664] | 84 | FileInfo.CivStage=1; |
---|
| 85 | end |
---|
[771] | 86 | elseif isfield(Data,'Conventions') && strcmp(Data.Conventions,'uvmat/civdata') |
---|
| 87 | FileInfo.FileType='civdata'; % test for civx velocity fields |
---|
[664] | 88 | FileType='civdata'; % test for civx velocity fields |
---|
| 89 | FileInfo.CivStage=Data.CivStage; |
---|
| 90 | else |
---|
[771] | 91 | FileInfo.FileType='netcdf'; |
---|
[664] | 92 | FileType='netcdf'; |
---|
[771] | 93 | FileInfo.ListVarName=Data.ListVarName; |
---|
[664] | 94 | end |
---|
[386] | 95 | end |
---|
[664] | 96 | catch ME |
---|
| 97 | error_nc=1; |
---|
[376] | 98 | end |
---|
[664] | 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 |
---|
[771] | 110 | FileInfo.FileName=fileinput; |
---|
| 111 | FileInfo.FileType=FileType; |
---|
[664] | 112 | FileInfo.BitDepth=FileInfo.BitsPerPixel/3; |
---|
| 113 | end |
---|
| 114 | end |
---|
[376] | 115 | end |
---|
| 116 | end |
---|
| 117 | end |
---|
| 118 | end |
---|