% Script reads in CCD bias data from a spreadsheet and fits a polynomial
% to model the temperature variation of bias for each individual pixel.
% For 2000 pixels and a 1st 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 BIAS_LOOKUP.MAT, which contains a matrix of polynomial 
%				coefficients (row # = pixel #, columns give poly coefficients)

% LAST UPDATE: July 6th, 00 	by James Anstey
%
% COMMENTS:
%	- note, this script doesn't do any checking of input file length. It 
%	  expects a file that has n_pix rows.

clear T bias order p n_pix
clear m T_pts

n_pix = 2000						% # of pixels on the CCD

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

order = 3;							% order of polynomial to fit

tic
bias = wk1read('bias.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, bias(m,1:T_pts), order);
end
toc									% check the time

save bias_lookup p				% save polynomials in file BIAS_LOOKUP.MAT
clear all							% save memory by getting rid of stuff