function output=g2n(input_series, oldest14Cdate)

% Use to update a GISP2 based chronology to NGRIP.
% Input series: 2-column in k.y. BP (before 1950).
% Max conversion age is 100.8 ka. Outside of this range,
% Input series age converted to b2k (before 2000) by adding 50 years.
% Oldest radiocarbon date is optional input.
% Conversion begins from radiocarbon date if specified.
% Output is 'GICC05modelext' (GICC05 with ss09sea) in ka b2k
% USE WITH CAUTION ONLY ON RECORDS BASED ON GISP2 M/S94!
%
% Version 1.0; 2013-08-28
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
% 
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>

if(nargin<1), error('No input series specified'); end

% input series in kiloyears
x=input_series(:,1);
y=input_series(:,2);

% test for input in kiloyears
if(max(x)>10000)
	warning('Input series appears to be in years. Converting to kiloyears.');
	x=x/1000;
end

% load depth ties, ngrip and gisp2 data
fid=fopen('g2nties.txt');
c=textscan(fid,'%f %f %f %f %f %f %f %f %s %s','delimiter','\t','headerlines',1,'emptyvalue',-999);
fclose(fid);
[gisp2_old_depth,gisp2_old_age,gisp2_o18,ngrip_depth,ngrip_age,ngrip_o18,tie_gisp2_depth,tie_ngrip_depth,vers,ref]=deal(c{:});
clear ans fid c

% find and remove -999 values mapped to empty cells
gisp2_old_depth(find(gisp2_old_depth==-999))=[];
gisp2_old_age(find(gisp2_old_age==-999))=[];
gisp2_o18(find(gisp2_o18==-999))=[];
ngrip_depth(find(ngrip_depth==-999))=[];
ngrip_age(find(ngrip_age==-999))=[];
tie_gisp2_depth(find(tie_gisp2_depth==-999))=[];
tie_ngrip_depth(find(tie_ngrip_depth==-999))=[];

% find remove empty cells from cell arrays
ref(cellfun('isempty',ref))=[];
vers(2:length(vers))=[];
vers=char(vers);

% find and remove NaNs from GISP2
nanindex=[find(isnan(gisp2_o18))' find(isnan(gisp2_old_depth))' find(isnan(gisp2_old_age))'];
gisp2_o18(nanindex)=[];
gisp2_old_age(nanindex)=[];
gisp2_old_depth(nanindex)=[];

% create depth lookup table and remove NaNs
depth_pointers=horzcat(tie_gisp2_depth,tie_ngrip_depth);
depth_pointers(find(isnan(depth_pointers(:,1))),:)=[];
depth_pointers(find(isnan(depth_pointers(:,2))),:)=[];

% create new GISP2 depth scale based on NGRIP
gisp2_new_depth=interp1(depth_pointers(:,1),depth_pointers(:,2),gisp2_old_depth);

%find indexes bounding update interval
min_convert_index=find(gisp2_new_depth >= min(tie_ngrip_depth),1);
max_convert_index=max(find(gisp2_new_depth <= max(tie_ngrip_depth)));

% apply GICC05modelext chronology to new GISP2 depth scale
if exist('gisp2_new_age')~=1
	gisp2_new_age=interp1(ngrip_depth,ngrip_age,gisp2_new_depth);
end

% import ages younger than import interval and convert to b2k
if min_convert_index > 1
	gisp2_new_age(1:min_convert_index-1)=gisp2_old_age(1:min_convert_index-1)+.05;
	
	% Check for age reversal at concatenation point. Temporary issue with the 1.0 version data.
	if vers(1)=='1' & vers(3)=='0'
		if abs(gisp2_new_age(682)-8.2654)>0.0001
			error('Age reversal pre-correction age value %f inconsistent. Aborting.', gisp2_new_age(682));
		end
		gisp2_new_age(682) = 8.2497; %mean of indexes 683 and 681 in v 1.0 data
	end
end

% remove data older than update interval (these vectors become new lookup table)
gisp2_new_age(max_convert_index+1:length(gisp2_new_age))=[];
gisp2_old_age(max_convert_index+1:length(gisp2_old_age))=[];

% create new age scale for selected interval, import out-of-range ages and convert to b2k
x2=interp1(gisp2_old_age,gisp2_new_age,x);

% package for output to user
output=horzcat(x2,y);

% determine updated range and account for radiocarbon date
if min(x)>gisp2_old_age(min_convert_index)
	age_range(1)=min(x);
else
	age_range(1)=x(find(x >= gisp2_old_age(min_convert_index),1));
end

if exist('oldest14Cdate')
	if oldest14Cdate>gisp2_old_age(min_convert_index)
		start_index=find(x >= oldest14Cdate,1);
		output(1:start_index-1,1)=x(1:start_index-1)+.05;
		age_range(1)=x(find(x >= oldest14Cdate,1));
		if min(diff(output(:,1)))<=0
			warning(['Splicing original ages into GICC05 ages at ' num2str(oldest14Cdate) 'ka BP resulted in an age reversal. Please manually splice in younger ages']);
			output(1:start_index-1,1)=NaN;
		end
	end
end

if max(x)<gisp2_old_age(max_convert_index)
	age_range(2)=max(x);
else
	age_range(2)=gisp2_old_age(max_convert_index);
end

%echo outcome
disp([fprintf('\n') 'The interval between ' num2str(age_range(1)) ' and ' num2str(age_range(2)) 'ka (BP 1950) was updated to the GICC05modelext chronology'])
disp(['Total age range is ' num2str(min(x2)) ' to ' num2str(max(x2)) 'ka b2k'])
disp([fprintf('\n') 'references:'])
disp(ref)
disp([fprintf('\n') 'datafile version: ' vers])