%	Hycode 2000
%  function corr_data=apply_cal_offset_to_data_array(hdr_in,data_in,calfname_ac9one,calfname_ac9two)
%	Apply field calibration offset to data array with 1 or 2 ac9
%	for 1 ac9 call the function with an empty string ('') for calfname_ac9two
%	input data_in:	data array
%			calfname_ac9one:	name of file containing offset for first a9 in infile	
%			calfname_ac9two:	name of file containing offset for second a9 in infile	
%	
%	format for cal file (calfname_ac9one and calfname_ac9two): example
%			a412	a440	a488	a532	a555	a650	a676	a715	a780	c412	c440	c488	c532	c555	c650	c676	c715	c780
%			0.019	0.067	0.667	0.556	0.444	0.005	0.006	0.018	0.0144	0.09	0.013	0.025	0.026	0.033	0.001	0.86	0.36	0.53
%

function corr_data=apply_cal_offset_to_data_array(hdr_in,data_in,calfname_ac9one,calfname_ac9two)

[hdrcal1_in,cal1_in]=rdctd(calfname_ac9one);
if cal1_in==-1
   corr_data=-1;return;
end

if isempty(calfname_ac9two)==0
[hdrcal2_in,cal2_in]=rdctd(calfname_ac9two);
if cal1_in==-1
   corr_data=-1;return;
end
end

ind_a412=strmatch('a412',hdr_in);
ind_c412=strmatch('c412',hdr_in);


ind_aone=ind_a412(1);
ind_cone=ind_c412(1);   
if isempty(calfname_ac9two)==0
ind_atwo=ind_a412(2);
ind_ctwo=ind_c412(2);  
end

data_out=data_in;

for i=ind_aone:(ind_aone+17)
   data_out(i,:)=data_in(i,:)-cal1_in(i-ind_aone+1);
end
if isempty(calfname_ac9two)==0
for i=ind_atwo:(ind_atwo+17)
   data_out(i,:)=data_in(i,:)-cal2_in(i-ind_atwo+1);
end
end

corr_data=data_out;
return
