clear all
close all

% do high/low pass for climate data (warm\ann share the same warm & sst=slp)
load ./data/clidata
%trtestt=coldslp(:,1:10);
%save('d:\database\tempupdated\trtestt','trtestt','-ascii')
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; %years
%	nproxies=1099;
	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('./data/clidatal','llow','-ascii')
save('./data/clidatah','hhigh','-ascii')

%%%%%%%

%load d:\scozztemann1\data\warmslpl;
%load d:\scozztemann1\data\warmslph;

%dd=80

%subplot(2,1,1); plot(1:141,llow(4:144,ddd),'r',1:141,warmslpl(4:144,ddd),'b')
%subplot(2,1,2); plot(1:141,hhigh(4:144,ddd),'r',1:141,warmslph(4:144,ddd),'b')




