% read black rock forest temperature dataset
function D = brfread(filename)

mname = { 'Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep' 'Oct' 'Nov' 'Dec' };
mnumber = { '  1' '  2' '  3' '  4' '  5' '  6' '  7' '  8' '  9' ' 10' ' 11' ' 12' };

% count lines in file
fid = fopen(filename);
N = 0;
while 1
    tline = fgetl(fid);
    if ( ~ischar(tline) )
        break;
    end;
    N=N+1;
end
fclose(fid);

% initialize data
D=zeros(N,1);

% read data, presuming lines look like
% '0700-0759 1 Jan 1997	-19.35'
fid = fopen(filename);
for i = [1:N]
    tline = fgetl(fid);
    j =strfind(tline,'-'); % delete dash between first two numbers
    tline(j(1))=' ';
    % replace month name with month number
    for j = [1:12]
        k=strfind(tline,char(mname(j)));
        if( length(k) > 0 )
            tline(k(1):k(1)+2)=char(mnumber(j));
            break;
        end
    end
    f = sscanf(tline,'%f'); % break out hr and min from hhmm-style number
    h1 = floor( f(1)/100 );
    m1 = f(1)-h1*100;

    h2 = floor( f(2)/100 ); % break out hr and min from hhmm-style number
    m2 = f(2)-h1*100;
    
    jday = julian( f(5), f(4), f(3) ); % convert to epock time in seconds
    e1 = htoe(f(5), jday, h1, m1, 0.0, 0.0);
    e2 = htoe(f(5), jday, h2, m2, 0.0, 0.0);
    
    d1 = e1 / (24*60*60); % convert to epoch time in hours
    d2 = e2 / (24*60*60);
    
    D(i,1) = 0.5*(d1+d2);  % time is mean time of two times given
    D(i,2) = f(6); % temperature
  
end

D(:,1) = D(:,1) - D(1,1); % shift time so that dataset beings at t=0

fclose(fid);
