% Script reads in dark current data from a spreadsheet and fits a polynomial
% to model the temperature variation of dark current for each individual pixel.
% For 2000 pixels and a 3rd order polynomial, this takes about 25 seconds to
% run, so the results are stored in a lookup table for use by the slit_fn
% program.

% INPUT: 	temperature values, no. of pixels, dc values in wk1 spreadsheet
%				format, and order of polynomial to fit.
%
% OUTPUT:	file DC_LOOKUP.MAT, which contains a matrix of polynomial 
%				coefficients (row # = pixel #, columns give poly coefficients)

% LAST UPDATE: July 6th, 00 	by James Anstey

clear T dc order p n_pix
clear m

n_pix = 2000						% # of pixels on the CCD

T = [240 243 244 247 251];		% temperature values
T_pts = length(T);

order = 3;							% order of polynomial to fit

tic
dc = wk1read('dc.wk1');			% reads in 2000 x 5 matrix
toc									% what time is it?

p = zeros(n_pix,order + 1);	% initialize poly coefficient matrix

tic
% Do least squares fit for each pixel:
for m = 1:1:n_pix
   p(m,:) = polyfit(T, dc(m,1:T_pts), order);
end
toc									% check the time

save dc_lookup p					% save polynomials in file DC_LOOKUP.MAT
clear p dc							% save memory by getting rid of these