function x = read_csv_slit_fns(dir_path, wv_fn)
% read_csv_slit_fns(dir_path)
%	This function reads in data from a series of .csv files (the 
%	raw data files produced by the spectrometer) to use for 
%	generating the total slit function for our spectrometer.

%	ARGUMENTS:
%		dir_path			path to directory containing the data files
%		wv_fn				polynomial giving the relation between the CCD's
%							wavelength centre and pixel centre (where does
%							it come from? There are some who call him...Kruger).
%
%	INPUT FILES:
%		csv_lis.lis		file containing a list with the filenames of csv
%							files to read.
%		ddmmyyhh.csv	data files (name gives date and time)
%	Required directory structure for input files:
%		DIRECTORY:				CONTENTS:
%		  slit					scripts: slit_fn.m, find_centre.m, read_csv_slit_fns.m
%			|						the list file, csv_lis.lis
%			|
%			|__[data 
%				directories]
%			
%	OUTPUT:
%		x = # of slit functions. Also sets the global variable coords to
%			contain the starting pixel of each slit function.
%		If the function fails, it returns x = -1
%
%	VARIABLES:
%			wv_cent		wavelength centre of slit fn
%			p_cent		pixel centre of slit fn (derived from wv_cent using wv_fn)
%	(g)	int_time		integration time of spectra
%	(g)	mean_TCCD	mean temperature of CCD while spectra was taken
%	(g)	n_pix			# of pixels on the CCD
%			T_min			min temperature for calculating dc and bias noise
%			T_max			max temperature for calculating dc and bias noise
%
%	CALLS:				none
%
%	CALLED BY:			slit_fn.m
%
%	COMMENTS:
%		- the script assumes 2000 pixels, ie there is no global n_pix variable
%		July 13 - modify so that functions are averaged over all spectra in the
%					 csv files, rather than using just the first one in the file.

clear filename wv_cent
clear i n
clear meas_fn len coord

global n_pix
global meas_fn
global len
global coord
global int_time
global mean_TCCD
%global dc_noise

wid = 60;	% Half width of slit functions to read in (just my guess
				% at a good number)
% Max and min temperatures for which we can calculate the noise:            
T_max = 249;
T_min = 238;

% open list file:
fid = fopen('csv_lis.lis','r');
if fid == -1
   disp('READ_CSV_SLIT_FN.M: Error, file CSV_LIS.LIS could not be opened.');
   x = -1; 
   return
end

i = 0;
while ~feof(fid)	% while not end of file
   i = i + 1;
   filename(i,1:8) = fscanf(fid,'%8s',1);
   filename(i,9:12) = '.csv';
   fgetl(fid);
end
fclose(fid);

n_fns = i;	% assume only one slit fn in each csv file
				% note, this is a LOCAL n_fns variable - not the same as 
            % the one in SLIT_FN.M
            
            
% Preallocate memory to speed up the code:
dc_noise 	= zeros(n_fns,n_pix);
mean_TCCD 	= zeros(1,n_fns);
int_time 	= zeros(1,n_fns);
wv_cent 		= zeros(1,n_fns);
cent 			= zeros(1,n_fns);

%for i = 1:1:n_fns
i = 1;
while i <= n_fns

   % Open data file:
   fid = fopen(strcat(dir_path,filename(i,1:12)),'r');
   if fid == -1
   	disp(strcat('READ_CSV_SLIT_FN.M: Error, file ',filename(i,1:12),' could not be opened.'));
   	x = -1; 
   	return
   end
   
   
   for m = 1:1:22
      fgetl(fid);
   end
   
   
   % The following code retrives the centre coordinate, integration time,
   % and mean CCD temperature from the csv file:
   fgetl(fid);
   fgetl(fid);
   
   fscanf(fid,'%44s',1)	% skip date and time fields
   for m = 1:1:7
      fscanf(fid,'%5f',1);	% skip number
      fscanf(fid,'%1s',1);	% skip comma
   end
   wv_cent(i) = fscanf(fid,'%5f',1);	% get wavelength centre
   fscanf(fid,'%1s',1);						% skip comma
   int_time(i) = fscanf(fid,'%5f',1);	% get integration time
   fscanf(fid,'%1s',1);						% skip comma
   fscanf(fid,'%5f',1);						% skip number
   fscanf(fid,'%1s',1);						% skip comma
   mean_TCCD(i) = fscanf(fid,'%5f',1);	% get mean CCD temperature
   
   global swc
   global scent
   swc = wv_cent;
   
   % Convert wv_cent to a pixel centre:
   cent(i) = polyval(wv_fn, wv_cent(i));
   % and round off:
   if mod(cent(i),1) < 0.5   
      cent(i) = cent(i) - mod(cent(i),1);
   else
      cent(i) = cent(i) +1 - mod(cent(i),1);
	end
   
   scent = cent;
   
   % Validate temperature range:
   continue = 1;
   if (mean_TCCD(i) < T_min) | (mean_TCCD(i) > T_max) continue = 0; end
   % Pixel range validation:
   %if (cent(i) < 1) | (cent(i) > n_pix) continue = 0; end
   
   if continue		% If cent and mean_TCCD are in range
      % Now read in the i'th slit function. We will read in values in a window
   	% that is 2*wid+1 pixels wide, centred around the centre one (i.e. +/- wid 
   	% pixels of cent(i)).
      
      % Preallocate memory for the meas_fn array:
   	meas_fn(i,1:(2.*wid + 1)) = zeros(1,(2.*wid + 1));
      
      fgetl(fid);
   
   	y = mod((cent(i) - wid), 200);	% cent(i) - wid = the starting pixel of the slit fn
   	z = (cent(i) - wid) - y;
   
   	for n = 1:1:(z/200)
      	fgetl(fid);
   	end
   	for n = 1:1:(y-1)
   	   fscanf(fid,'%8f',1);		% skip number
      	fscanf(fid,'%1s',1);		% skip comma
   	end	
   	[t1 t2] = min([120 201-y]);
   	for n = 1:1:t1
      	meas_fn(i,n) = fscanf(fid,'%8f',1);
      	fscanf(fid,'%1s',1);		% skip comma
   	end
   	if t2 == 2
      	fgetl(fid);
      	if feof(fid) 
      	   display('READ_CSV_SLIT_FNS: Error, function has fallen off end of pixel range!');
      	   return
      	end
      	n = n + 1;
      	while n < 121
      	   meas_fn(i,n) = fscanf(fid,'%8f',1);
      		fscanf(fid,'%1s',1);		% skip comma
      	   n = n + 1;   
      	end
   	end
      fclose(fid);   
      i = i + 1;
   else	% continue == 0 => temperature was out of range
      disp(strcat(filename(i,1:12),': temperature out of range.',...
         ' Discarding function...'));
      n_fns = n_fns - 1;
      filename(i,:) = [];		% remove filename from list
   end	% if continue
end	% for i = 1:1:n_fns
   
i = 1:1:n_fns;
coord(i) = cent(i) - wid;	% coord(i) = starting pixel of each slit function
len(i) = 2.*wid + 1;			% len(i) = length of each slit function (these are
									% actually all the same).
x = n_fns;


