clear all
close all

load /holocene/s1/zuz10/work1/temann/data/clidata
trtest=clidata;

[n,m]=size(trtest)

llow(1:n,1:m)=NaN;
hhigh(1:n,1:m)=NaN;
llow(:,1)=trtest(:,1);
hhigh(:,1)=trtest(:,1);
llow(1:3,:)=trtest(1:3,:);
hhigh(1:3,:)=trtest(1:3,:);

keepers=trtest(4:n,2:m);
[nrows,ncols]=size(keepers)

    cutoff=20; 
	lowlim=1./(cutoff./2)
%%%%%%%%%%%%%%

%load ./prepped   %load the input file. This need only have the matrix to be imputed.

stdkeepers=keepers; % because standardization have done in doannsst.m


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[nrows,ncols]=size(stdkeepers) 
gotstart=zeros(1,ncols);
gotstop=zeros(1,ncols);
start=zeros(1,ncols);
stop=zeros(1,ncols);
stop(:)=nrows;
[lowa,lowb]=butter(5,lowlim,'low');

%%%%%%%%%%%%%%  
%%%% This section finds the starting and stopping year for each column. It assumes that
%%%%  there are no missing values between start and stop, otherwise filtering chokes.

for i=1:nrows
	for j=1:ncols
		if ~isnan(stdkeepers(i,j)) & gotstart(j)~=1
			start(j)=i;
			gotstart(j)=1;
		elseif isnan(stdkeepers(i,j)) & gotstart(j)==1 & gotstop(j)~=1
			stop(j)=i-1;
			gotstop(j)=1;	
		end
	end
end
%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%
%%%%	Apply the zero-phase butterworth filter but only to the proxies
	highf=zeros(nrows,ncols);
	lowf=zeros(nrows,ncols); 
	lowf=lowf./lowf;
	highf=highf./highf;
	for k=1:ncols
		clear y y2
		data=stdkeepers(start(k):stop(k),k);
%		lowf(start(k):stop(k),k)=lowpass(data,0.05,2,2);
        frequency = 0.05;
        [smoothed0,icb,ice,mse0] = lowpassmin(data,frequency);
        lowf(start(k):stop(k),k)= smoothed0;
%		lowf(start(k):stop(k),k)=filtfilt(lowa,lowb,data);
		highf(start(k):stop(k),k)=data-lowf(start(k):stop(k),k);
	end
%%%%%


llow(4:n,2:m)=lowf;
hhigh(4:n,2:m)=highf;


save('/holocene/s1/zuz10/work1/temann/data/clidatal','llow','-ascii')
save('/holocene/s1/zuz10/work1/temann/data/clidatah','hhigh','-ascii')



