function [data,time,SN,INTTIME] = readOCR(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 time 
%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 time-interpolated dark-frame values
%               subtracted
%time = Matlab datenumber-formated time vector (sensor-specific)
%SN = Sensor serial number
%INTTIME = vector of measurement integration times (seconds)

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

%read dark-frame datafile
fid = fopen(fnamdark);
A = textscan(fid,formatpattern,'headerlines',1,'delimiter',',');
fclose(fid);

%read numeric data (not all are output; see "rawheader" variables in
%OCRstuff.mat)
for i = 1:146;   
    darkdata(:,i) = A{1,i};
end;

%read date and time data as formatted by SatCon
for i = 1:2;  
    time(:,i) = A{1,146+i};
end;

%Convert SatCon date/time to Matlab datenumber format
for i = 1:length(time);  
    B = [sscanf(time{i,1},'%f-%f')' sscanf(time{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 time A B C;

%repeat all of the above for light-frame datafile

fid = fopen(fnamlight);
A = textscan(fid,formatpattern,'headerlines',1,'delimiter',',');
fclose(fid);

for i = 1:146;
    lightdata(:,i) = A{1,i};
end;

for i = 1:2;
    time(:,i) = A{1,146+i};
end;

for i = 1:length(time);
    B = [sscanf(time{i,1},'%f-%f')' sscanf(time{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 time A B C;

%Prior to interpolating dark-frame data to light-frame timescale, 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);
INTTIME = lightdata(good,2);
lightdata = lightdata(:,4:140);  %radiometer data are 
darkdata = darkdata(:,4:140);    %in these columns

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

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