function tel = cms_read_tel(telfile)
% CMS_READ_TEL Reads a CMS telescoping grid file
%
% Description:
%   Reads a CMS telescoping grid file
%   and output all variables to a structure array.
%
% Usage:
%   out = cms_read_tel(telfile);
%
% Input:
%   telfile - Telescoping grid file including path (*.tel)
%
% Output:
%   tel - Structure including the following fields
%     ncells - Number of cells
%     x0 - Grid origin in x-direction [m]
%     y0 - Grid origin in y-direction [m]
%     angle - Grid angle [deg]
%     id - Cell ID number
%     x - Grid cell-centered coordinate in x-direction [m]
%     y - Grid cell-centered coordinate in y-direction [m]
%     dx - Grid cell resolution in x-direction [m]
%     dy - Grid cell resolution in y-direction [m]
%     iloc - Cell connectivity
%     depth - Cell-centered depths (-999 = inactive cell) [m]
%
% Author: Alex Sanchez, USACE

fid = fopen(telfile,'r');
fgets(fid); %Skip header line
data = fscanf(fid,'%f %f %f %d',4);
tel.ncells = data(4);
tel.x0 = data(2);
tel.y0 = data(3);
tel.angle = data(1);
data = fscanf(fid,'%d %f %f %f %f %d %d %d %d %d %d %d %d %f',...
    [14,tel.ncells])';
fclose(fid);
tel.id = data(:,1);
tel.x = data(:,2);
tel.y = data(:,3);
tel.dx = data(:,4);
tel.dy = data(:,5);
tel.iloc = data(:,6:13);
tel.depth = data(:,14);

return
end
