%###########################################################################################
%%%%%%%% for selecting annually and decadally-resolved Full/Screened proxy network %%%%%%%%%%%%
%###########################################################################################

%%% Last modified October 2007 by skm

clear all; close all;

region='GL'
period='la100' %% 'whole' for full period, 'la100' for 1896-1995 calibration, 
               %% 'ea100' for 1850-1949 calibration.
	       
%% default values -- can also use for specific regions
ilon1 = -180; ilon2 = 180; 
ilat1 = -90; ilat2 = 90;   


if (period == 'ea100') 
  load('data/rtable1209late');
  z=rtable1209late;
else
  load('data/rtable1209');
  z=rtable1209;
end  
%-----------  r-table description --------------
%    row 1:  longitude
%    row 2:  latitude
%    row 3:  proxy ID (e.g. 8000 annually-resolved ice)
%    row 4:  r -value for raw data over 1850-1995(entire),    
%    row 5:  r -value for low-pass data over 1850-1995,        
%    row 6:  r -value for raw data over 1850-1949(early half),  
%    row 7:  r -value for low-pass data over 1850-1949,        
%    row 8:  r -value for raw data over 1896-1995(late half)
%    row 9:  r -value for low-pass data over 1896-1995

%---------------------------------------------------
%---------------------------------------------------

% this is raw proxy dataset
load('data/itrdbmatrix');
x=itrdbmatrix;
[n1,m1]=size(x);

%-- select all these chronologies that located within a certain area --
%                   (e.g. -120W~-60W, 20N~60N)

switch upper(region)
   case 'GL'
     %%-- global proxy network---
     ilon1 = -180; ilon2 = 180; 
     ilat1 = -90; ilat2 = 90;   
     %%--------------------------
   case 'NH'
     %%-- North hemisphere proxy network---
     ilon1 = -180; ilon2 = 180; 
     ilat1 = 0; ilat2 = 90;   
     %%--------------------------
   case 'SH'
     %%-- South hemisphere proxy network---
     ilon1 = -180; ilon2 = 180; 
     ilat1 = -90; ilat2 = 0;   
     %%--------------------------
   otherwise
     disp('non-hemisphere region')
end

switch upper(period)
   case 'WHOLE'
     ia=4; %% annually resolved
     ib=5; %% decadally resolved
     corra=0.106; %% 9000,8000,7500,4000,3000,2000
     corra2=-0.106; %% 7000
     corra3=0.136; %% 6000, 5000
     corrb=0.337; %% same as above +1
     corrb2=-0.337;
     corrb3=0.425;    
   case 'EA100'
     ia=6;
     ib=7;
     corra=0.128; %% 9000,8000,7500,4000,3000,2000
     corra2=-0.128; %% 7000
     corra3=0.164; %% 6000, 5000
     corrb=0.418; %% same as above +1
     corrb2=-0.418;
     corrb3=0.521;    
   case 'LA100'
     ia=6;
     ib=7;
     corra=0.128; %% 9000,8000,7500,4000,3000,2000
     corra2=-0.128; %% 7000
     corra3=0.164; %% 6000, 5000
     corrb=0.418; %% same as above +1
     corrb2=-0.418;
     corrb3=0.521;    
   case 'FULL_'
     ia=4;
     ib=5;
     corra=0;
     corra2=0;
     corra3=0;
     corrb=0;
     corrb2=0;
     corrb3=0;
   otherwise
     disp('something amiss')
end

%------------------------------------------------------------------

%------------------------------------------------------------------
%% below is for selecting full/screened/1856-1925 screened/1926-1995 screened proxy-network
%% replacing "abs(z(4,i))>=0.165"/"abs(z(5,i))>=0.513" in line 75/84 with the followings for your expected proxy-network
%%        abs(z(4,i))>=0   / abs(z(5,i))>=0          (full proxy-network)
%%        abs(z(4,i))>=0.165 / abs(z(5,i))>=0.513    (screening over 1856-1995)
%%        abs(z(6,i))>=0.234 / abs(z(7,i))>=0.705    (screening over 1856-1925)
%%        abs(z(8,i))>=0.234 / abs(z(9,i))>=0.705    (screening over 1926-1995)

%------------------------------------------------------------------

%----------------------------------------------------------------
%                                                               %
%  If you want a non-dendro dataset, then you just need to      %
%  delete "z(3,i)==9000 | " from line 73 (9000 is tree-ring ID) %
%                                                               %
%----------------------------------------------------------------
for istep=1:19 % note: istep1=back to 1800AD, istep2=back to 1700AD.....istep19=back to 0AD

byr=1800-(istep-1)*100;
eyr=1995;

kk=find(x(:,1)==byr);
kkk=find(x(:,1)==eyr); 

n=0;yc=[];locc=[];
for i=1:m1-1 % This is for searching annually-resolved proxies
    if (z(3,i)==9000 |  z(3,i)==8000 |  z(3,i)==7500 |  z(3,i)==4000 |  z(3,i)==3000 |  z(3,i)==2000)  &...
         x(kk,i+1)>-99999 & x(kkk,i+1)>-99999 &...
            z(1,i)>=ilon1 & z(1,i)<=ilon2 & z(2,i)>=ilat1 & z(2,i)<=ilat2 & z(ia,i)>=corra
        n=n+1;
%%%% low pass filter to 0.1
	temp=x(kk:kkk,i+1)*sign(z(ia,i));
	[smoot,icb,ice,mse0]=lowpassmin(temp,0.1);
        yc(1:kkk-kk+1,n)=smoot;
        locc(1:2,n)=z(1:2,i);
    end
end
n2=0;yc2=[];locc2=[];
for i=1:m1-1 % This is for searching annually-resolved proxies
    if ( z(3,i)==7000)  &...
         x(kk,i+1)>-99999 & x(kkk,i+1)>-99999 &...
            z(1,i)>=ilon1 & z(1,i)<=ilon2 & z(2,i)>=ilat1 & z(2,i)<=ilat2 & z(ia,i)<=corra2
        n2=n2+1;
%%%% low pass filter to 0.1
	temp=x(kk:kkk,i+1)*sign(z(ia,i));
	[smoot,icb,ice,mse0]=lowpassmin(temp,0.1);
        yc2(1:kkk-kk+1,n2)=smoot;
        locc2(1:2,n2)=z(1:2,i);
    end
end
n3=0;yc3=[];locc3=[];
for i=1:m1-1 % This is for searching annually-resolved proxies
    if (z(3,i)==6000 |  z(3,i)==5000)  &...
         x(kk,i+1)>-99999 & x(kkk,i+1)>-99999 &...
            z(1,i)>=ilon1 & z(1,i)<=ilon2 & z(2,i)>=ilat1 & z(2,i)<=ilat2 & abs(z(ia,i))>=corra3
        n3=n3+1;
%%%% low pass filter to 0.1
	temp=x(kk:kkk,i+1)*sign(z(ia,i));
	[smoot,icb,ice,mse0]=lowpassmin(temp,0.1);
        yc3(1:kkk-kk+1,n3)=smoot;
        locc3(1:2,n3)=z(1:2,i);
    end
end
nd=0; yd=[]; locd=[];
for i=1:m1-1 % This is for searching decadally-resolved proxies
    if (z(3,i) == 9001 | z(3,i)==8001 |  z(3,i)==7501 | z(3,i)==4001 |  z(3,i)==3001 |  z(3,i)==2001)  &...
         x(kk,i+1)>-99999 & x(kkk,i+1)>-99999 &...
            z(1,i)>=ilon1 & z(1,i)<=ilon2 & z(2,i)>=ilat1 & z(2,i)<=ilat2 & z(ib,i) >=corrb
        nd=nd+1;
%%% low pass filter to 0.05
        temp=x(kk:kkk,i+1)*sign(z(ib,i));
	[smoot,icb,ice,mse0]=lowpassmin(temp,0.05);
        yd(1:kkk-kk+1,nd)=smoot;
        locd(1:2,nd)=z(1:2,i);	
    end
end
nd2=0; yd2=[]; locd2=[];
for i=1:m1-1 % This is for searching decadally-resolved proxies
    if (z(3,i)==7001)  &...
         x(kk,i+1)>-99999 & x(kkk,i+1)>-99999 &...
            z(1,i)>=ilon1 & z(1,i)<=ilon2 & z(2,i)>=ilat1 & z(2,i)<=ilat2 & z(ib,i) <=corrb2
        nd2=nd2+1;
%%% low pass filter to 0.05
        temp=x(kk:kkk,i+1)*sign(z(ib,i));
	[smoot,icb,ice,mse0]=lowpassmin(temp,0.05);
        yd2(1:kkk-kk+1,nd2)=smoot;
        locd2(1:2,nd2)=z(1:2,i);	
    end
end
nd3=0; yd3=[]; locd3=[];
for i=1:m1-1 % This is for searching decadally-resolved proxies
    if (z(3,i) == 6001 | z(3,i)==5001 )  &...
         x(kk,i+1)>-99999 & x(kkk,i+1)>-99999 &...
            z(1,i)>=ilon1 & z(1,i)<=ilon2 & z(2,i)>=ilat1 & z(2,i)<=ilat2 & abs(z(ib,i)) >=corrb3
        nd3=nd3+1;
%%% low pass filter to 0.05
        temp=x(kk:kkk,i+1)*sign(z(ib,i));
	[smoot,icb,ice,mse0]=lowpassmin(temp,0.05);
        yd3(1:kkk-kk+1,nd3)=smoot;
        locd3(1:2,nd3)=z(1:2,i);	
    end
end

y=[yc yc2 yc3 yd yd2 yd3]; % get annually + decadally-resolved proxy network
loc=[locc locc2 locc3 locd locd2 locd3];

save(strcat('data/cps/',period,'/grid_',region,'_proxy',num2str(istep)),'y','loc');
clear y yc yc2 yc3 yd yd2 yd3 locc locc2 locc3 locd locd2 locd3
end %% for istep


%clear all;


