function [data,tt2,SN,INTtt2] = readOCR_2(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)

load OCRstuff.mat formatpattern; %for reading in the file

%read dark-frame datafile
% fnamdark=HED;
fid = fopen(fnamdark);
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:146;   
    darkdata(:,i) = A{1,i};
end;

%read date and tt2 data as formatted by SatCon
for i = 1:2;  
    tt2(:,i) = A{1,146+i};  % tt2 is the renmaed 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:146;
    lightdata(:,i) = A{1,i};
end;

for i = 1:2;
    tt2(:,i) = A{1,146+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));

%Column numbers here are hard-coded
SN = lightdata(1,1);
INTtt2 = lightdata(good,2);
lightdata = lightdata(:,4:140);  %radiometer data are 
darkdata = darkdata(:,4:140);    %in these columns

%interpolate dark-frame data to light-frame tt2scale
for i = 1:137;
    darkfill(:,i) = interp1(darkdate,darkdata(:,i),lightdate(good),'linear');
end;

data = lightdata(good,:)-darkfill;
tt2 = lightdate(good);

end

end

end

