function [data,tt2,SN,INTtt2] = readOCR_3(fnamdark,fnamlight)

%Read data into Matlab.  Data files are assumed to have already been
%converted to comma-delimited text and calibration and immersion
%coefficients applied in SatCon.  One header line is assumed (to ensure this,
%UNcheck "extra output header information" in SatCon parameters), and tt2
%tags are assumed to have been processed and formatted (should be 2 last
%columns of .dat file).

%Inputs:  fnamdark = string containing the path/filename of the dark-frame
%               data file for a single sensor
%fnamlight = string containing path/filename of the corresponding light-
%               frame data file

%Outputs:  data = radiometric data with tt2-interpolated dark-frame values
%               subtracted
%tt2 = Matlab datenumber-formated tt2 vector (sensor-specific)
%SN = Sensor serial number
%INTtt2 = vector of measurement integration tt2s (seconds)


%read dark-frame datafile
% fnamdark=HED;
fid = fopen(fnamdark);

% create a text string of conversion specifiers that textscan to use when it
% reads in the data file:
formatpattern = [];
number_of_floating_point_columns = 146;
number_of_string_columns= 2;
for ii = 1:number_of_floating_point_columns
    formatpattern = [formatpattern '%f'];
end
for ii = 1:number_of_string_columns
    formatpattern = [formatpattern '%s'];
end

A = textscan(fid,formatpattern,'headerlines',1,'delimiter',',');
fclose(fid);
% fnamdark
%read numeric data (not all are output; see "rawheader" variables in
%OCRstuff.mat)

if isempty(A{1})==1
    data=NaN;
    tt2=NaN;
    SN=NaN;
    INTtt2=NaN;
else
    
    for i = 1:number_of_floating_point_columns;
        darkdata(:,i) = A{1,i};
    end;
    
    %read date and tt2 data as formatted by SatCon
    for i = 1:2
        tt2(:,i) = A{1,number_of_floating_point_columns+i};  % tt2 is the renamed time
    end;
    
    %Convert SatCon date/tt2 to Matlab datenumber format
    for i = 1:size(tt2,1);
        B = [sscanf(tt2{i,1},'%f-%f')' sscanf(tt2{i,2},'%f:%f:%f')'];
        C = datevec(datenum([B(1) 0 B(2)]));
        C(4:6) = [B(3) B(4) B(5)];
        darkdate(i,1) = datenum(C);
    end;
    
    clear tt2 A B C;
    
    %repeat all of the above for light-frame datafile
    
    % fnamlight=HSE;
    fid = fopen(fnamlight);
    A = textscan(fid,formatpattern,'headerlines',1,'delimiter',',');
    fclose(fid);
    
    if isempty(A{1})==1
        data=NaN;
        tt2=NaN;
        SN=NaN;
        INTtt2=NaN;
    else
        
        for i = 1:number_of_floating_point_columns;
            lightdata(:,i) = A{1,i};
        end;
        
        for i = 1:2;
            tt2(:,i) = A{1,number_of_floating_point_columns+i};
        end;
        
        for i = 1:size(tt2,1);
            B = [sscanf(tt2{i,1},'%f-%f')' sscanf(tt2{i,2},'%f:%f:%f')'];
            C = datevec(datenum([B(1) 0 B(2)]));
            C(4:6) = [B(3) B(4) B(5)];
            lightdate(i,1) = datenum(C);
        end;
        
        clear tt2 A B C;
        
        %Prior to interpolating dark-frame data to light-frame tt2scale, discard
        %light frames collected before the first or after the last dark frame
        first = darkdate(1,1);
        last = darkdate(length(darkdate),1);
        good = intersect(find(lightdate>first),find(lightdate<last));
        
        
        % retrieve the data. user must specify the columns where the
        % radiometer data are located:
        start_col = 4; % first colunm of radiometric data
        end_col = 140; % last column of radiometric data
        lightdata = lightdata(:,start_col:end_col);
        darkdata = darkdata(:,start_col:end_col);
        
        % retrieve the serial number of the OCR sensor:
        SN = lightdata(1,1);
        
        % retrieve the vector of measurement integration tt2s (seconds)
        INTtt2 = lightdata(good,2);
        
        % loop through each of the data columns and interpolate dark-frame
        % data to light-frame tt2scale
        for i = 1:size(lightdata,2);
            darkfill(:,i) = interp1(darkdate,darkdata(:,i),lightdate(good),'linear');
        end;
        
        data = lightdata(good,:)-darkfill;
        tt2 = lightdate(good);
        
    end
    
end

end