function [xbt] = read_hires_woce_line(fname) ; 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION
%   A program to read the high-resolution WOCE XBT line (PX37) data.
%   Data were obtained from Dean Roemmich, http://www-hrx.ucsd.edu
%
% INPUT
%   fname	The file name, as a string enclosed in quotes
%
% OUTPUT
%   xbt 	A structure containing the xbt data 
%
% EXAMPLE 
%   xbt = read_hires_woce_line(fname) ; 
%   xbt = read_hires_woce_line('p379910a.10') ; 
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Kate Edwards 8/2002
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Filename: Line designation, year, and starting month
fid = fopen(fname,'rt') ; 

% The first line of the file gives the number of profiles contained in that file
num_profiles = str2num(fgetl(fid)) ;

% Each profile is preceeded by a header line containing:
% latitude, east longitude, date (day/month/year), time (gmt), profile number. 
xbt.depth = -(5:10:895) ; % Depth (m), same for all profiles
for pro = 1:num_profiles
    % Read header
    hdr = fgetl(fid) ; 
    xbt.lat(pro) = str2num(hdr(1:11)) ; % Latitude 
    xbt.lon1(pro) = str2num(hdr(12:19)) ; % Longitude
    xbt.day(pro) = str2num(hdr(19:21)) ; 
    xbt.mon(pro) = str2num(hdr(23:24)) ; 
    xbt.year(pro) = str2num(hdr(26:27)) ; 
    % Convert to 4-digit year
    if xbt.year(pro) > 50 xbt.year(pro) = xbt.year(pro) + 1900 ; 
    else xbt.year(pro) = xbt.year(pro) + 2000 ;
    end
    xbt.hour(pro) = str2num(hdr(29:30)) ; % GMT
    xbt.min(pro) = str2num(hdr(32:33)) ; 
    xbt.sec(pro) = str2num(hdr(35:36)) ; 
    xbt.profile_number(pro) = str2num(hdr(38:40)) ;  
    
    % Read data
    % Each profile contains 90 values of temperature (degC), written as 12I6, in thousandths of a degree. 
    % Each data value is the average over a 10-meter interval, beginning with 0-10 m through 890-900 m. 
    % Missing values (below bottom strikes, wire breaks or other end of profile) are marked -999.

    old_row = 0 ; 
    for j = 1:8
        line = fgetl(fid) ; % Read in data line
        [data, ncols] = sscanf(line, '%f'); 
	% Temperature (C)
        xbt.T((old_row + 1):(old_row + ncols),pro) = data./1000 ; %
        old_row = old_row + ncols ; 
    end
end
fclose(fid) ; 

% Matlab time convention for use with axdate
xbt.time = datenum(xbt.year,xbt.mon,xbt.day,xbt.hour,xbt.min,xbt.sec) ;
xbt.T(xbt.T == -0.999) = NaN ; % Convert bad data flags to NaNs
% Convert longitude so that negative values = western hemisphere 
xbt.lon2 = xbt.lon1 ;
xbt.lon2(xbt.lon2 >= 180) = -(360 - xbt.lon2(xbt.lon2 >= 180)) ; 
