function A = IMDC_Read_LT_Data(strFileToRead, nMetadataLines, strColsFormat, strDelimiter);
%IMDC_Read_LT_Data  Reads ASCII files with long term data according to IMDC standard format.
%   These files have three parts: a header (lines starting with "* "), a metadata block
%   (lines with information about the data. The information in the lines can be strings or numbers) and
%   the columns of information.
%   A = IMDC_READ__LT_Data(strFileToRead, nMetadataLines, strColsFormat) reads ASCII Data files
%   and returns a 1 column x (nMetadataLines+1) rows cell array with the
%   metadata in the file (first (nMetadataLines+1) rows in the cell array) and a matrix with the read values.
%   Lines at the beginning of the file starting with '* ' are seen as header and are skipped.
%   strFileToRead: File name as string of the file to be opened.
%   nMetadataLines: number of expected metadata lines.
%   strColsFormat: string with the expected format for the data columns as used by the
%      textread command, e.g.: '%u %u %f %f %f %f %f %f %f'. 
%   strDelimiter: Defines what the delimiter is in your actual data (eg. '
%   ' (space) default; '\t'(tab);','(comma),etc...)
%   A: cell array with the metadata lines in the same order (ALL of them are STRINGS, even if it a numerical value);
%      and the last element in the cell array is a matrix with the read values according to the specified format
%      (strColsFormat).
%      LIMITATIONS: Columns of values are separated by spaces. All values in
%      the columns are numeric

% International Marine and Dredging Consultants, IMDC
% Antwerp Belgium
%
% Written by: Fernando Toro
% Date: March 2005
% Modified by: Mark & Fernando Star Matflap Team
% Date: October 2005

% Modified by MBO and FTO, May 2006 % strdelimiter bug removed
% IMDC


% ********************
% Check input argument
% ********************

if ~ischar(strFileToRead)
    error('Input argument must be a character array!')
end


% ********************************************************
% Replace substrings '\' and '\\' with '/' for convinience
% ********************************************************

FileToRead = strrep(strFileToRead,'\','/');
FileToRead = strrep(strFileToRead,'\\','\');
FileToRead = strrep(strFileToRead,'//','/');


% ***************************
% Read number of header lines
% ***************************

NumLines = IMDC_Read_FileHeader(strFileToRead);

% *******************
% Read metadata lines
% *******************

% *********
% Open file
% *********
fid = fopen(strFileToRead, 'r');
if (fid == -1)
   % The file did not open
   error(['Problems opening file ' strFileToRead])
end;
% Skip the header
for I =1:NumLines
    line = fgetl(fid);
end
% Read metadata
A = cell((nMetadataLines+1), 1);
for I = 1:nMetadataLines
    A{I,1} = fgetl(fid);
end
% Check that the following line is in fact a line with data in columns and
% that the number of columns coincides with the defined number of columns in strColsFormat 
line = fgetl(fid);

if nargin==3
%    mValues = strread(line,'%s','delimiter',' ');
    strDelimiter=' '; % 
%else
   % mValues = strread(line,'%s','delimiter',strDelimiter);
end
mValues = strread(line,'%s','delimiter',strDelimiter);

nCols = size(mValues,1);
mCols = strread(strColsFormat,'%s','delimiter',' ');
%mCols=strread(mValues,strColsFormat,'delimiter',strDelimiter);

nExpectedCols = size(mCols,1);
if (nCols ~= nExpectedCols)
    % Expected and read number of columns do not coincide
    error('The expected number of columns does not coincide the read values in the file')
    fclose(fid);
end

NumLines = NumLines + nMetadataLines;
fclose(fid);

% ************
% Read Columns
% ************

strMyCommand = '[B(:,1) ';
for I = 2:nCols
    % Built part of the command to read file
    strMyCommand = [strMyCommand 'B(:,' num2str(I) ') '];
end
%strMyCommand = [strMyCommand '] = textread(strFileToRead, strColsFormat, ''headerlines'', NumLines);'];
strMyCommand = [strMyCommand '] = textread(strFileToRead, strColsFormat, ''headerlines'', NumLines, ''delimiter'',strDelimiter);'];

eval(strMyCommand);

% **************************
% Check if B is empty matrix
% **************************

if isempty(B)
    error(['No data in file ' strFileToRead])
end

A{(nMetadataLines+1),1} = B;

return;
