function [data, Fs, NoSs, TITLE, TIMEPOS]=ReadData(file,flag)

%

% Reads a Data File from the Cintal DAQ System

%

%    [data, fs, NoSs, TITLE, TIMEPOS]=ReadData(file,flag)

%

% Where:

%     data: is a matrix [ NoChannels * Total No Samples ]

%     fs: sampling frequency

%     NoSs: Number of Channels

%     TITLE: Description of the experiment

%     TIMEPOS: Time/Position information of the data in the file

%

%     file: name of file to be read, empty variable will allow the selection

%       of the file to read, recognized extensions:

%                      * ".acust"          - Acoustic Data

%                      * ".tilt1"/".tilt2" - Array Inclination Data

%                      * ".pr1"/".pr2"     - Array Depth Data

%                      * ".temp"           - Temperature Data

%                      * ".dummy"          - Battery Voltage Data

%     flag: if greater than 0, return values of data will be converted

%       to its usable System Units ( volts, degrees of inclination, meters, etc )

%

if ( nargin<=1 )

    flag=0;

elseif ( ~isnumeric(flag) | isempty(flag) ) 

    flag=0;

end

if ( (nargin==0) | isempty(file) )   

   [fname fpath]=uigetfile('*.*','Choose a File:');

   if (size(fname)==size(fpath))

      if ((fname==0) | (fpath==0))         

         error('A file must be selected.');      

      end   

   end

   file=fname;

   filename=[fpath fname];

   

   if ( ~isnumeric(flag) | isempty(flag) ) 

    flag=0;

   end



else

   if ~ischar(file) 

      error('Parameter «file» must be a filename or must be empty');

   end   

   if strcmp(computer,'PCWIN')

      if isempty(findstr(file,'\'))

         filename=['.\' file];

      else

         filename=file;

      end

   else

      if isempty(findstr(file,'/'))

         filename=['./' file];

      else

         filename=file;

      end

   end

end

    

type=gettype(file);

ctype=0;

if strcmp(lower(type),'acust')

    ctype=1;

elseif strcmp(lower(type),'temp')

    ctype=2;

elseif ( strcmp(lower(type),'tilt1') | strcmp(lower(type),'tilt2') )

    ctype=3;

elseif ( strcmp(lower(type),'pr1') | strcmp(lower(type),'pr2') )

    ctype=4;

elseif strcmp(lower(type),'dummy')

    ctype=5;

end    



if ctype>0

    disp(['File Data Type: ' type ' ...'])

else

    error('Unknown File Type')

end    



disp(['Trying to open: ' filename ' !!!']);

fid = fopen(filename,'r');

if (fid==-1)

   error(['File:' filename 'could not be open!!']);

end

disp('File Openned!!');



TITLE = fgetl(fid);

teststr(TITLE);



TIMEPOS = fgetl(fid);

teststr(TIMEPOS);



datainfo = fgetl(fid);

teststr(datainfo);



Fs=str2num(datainfo(4:8));           % Sampling frequency

NoSs=str2num(datainfo(18:19));       % No of Channels

SpSz=str2num(datainfo(28:29));       % No of Bits per Samples

ToSp=str2num(datainfo(37:44));       % Total Number of Samples



testinfo(Fs); testinfo(NoSs); testinfo(SpSz); testinfo(ToSp); 



[data count]=fread(fid,[NoSs inf],['int' num2str(SpSz)]);

disp('Data Read!')

fclose(fid);

disp('File Closed..')



if count<ToSp

   disp('Not a Full File!');

end



if flag

    switch ctype

    case [1],

        data=data/(2^10); %shift data 10 bits right

        data=data.*4000/(2^22-1); % 4000mV/gratest 22 bit number! 

    case [2],

        data=data/100; % no offset, scale 100mV/ºC

    case [3],

        data=(data - 2500) / 34; % offset 2500mV , scale 34mV/º

    case [4],

        data=(data - 1) / 50; % offset 1mV , scale 50mV/º

    case [5],

        data=data/100; % no offset, 100mV/V

    otherwise,

        error('Unknown Data Type!')

    end

    disp('Data Converted');    

end



return





% ****************************************************************************

% **********    Auxiliary Functions ******************************************

% ****************************************************************************





function teststr(strarr)

if ~ischar(strarr)

   error('Not a Valid DATA file!!');

end   

return



function testinfo(valuearr)

if isempty(valuearr)

   error('Not a Valid DATA file!!');

end   

return



function out=gettype(strarr)



found=findstr(strarr,'.');

if isempty(found)

   error('No Filename extension!!!')

else

   out=strarr((found(end)+1):end);

end   



return