%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% this file is used to generate proxy matrix %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%-- because most tree-chronologies we have are standard chron. we choose STD or
%-- some unknown type of index, which are mostly STD files, to form itrdbmatrix
%-- the format of individual file is: 1st column yr; 2rd index; 3rd sample numbers

%ibyr = 0;  % start year of the data_matrix
ibyr = -5;  % start year of the data_matrix
ieyr = 1996; % end year of the data_matrix

isamp = 8;   % the numbers of sample needed for a single tree chronlogy
             % to be put in the matrix.
             % (e.g. when isamp=5, means the index values that were created 
             % with less than 5 samples will be removed (in the begining of chronologies)

icut = 0;   % the early values to be removed due to the possible small numbers of
             % sample for those tree chronologies which do not have enough 'sample' 
             % information (say, NaN, 1, 24 in the second column of chronologies)
             % (e.g. when icut=15, means 15 years' early values are removed)
% (Note: if you don't want to remove any values from indivadual chronology
%     just set isamp=1 and icut=0)
             
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% parameters for the sub_itrdbmatrix %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% change below for filelink.m
isyear = 1800; % select all these chronologies that longer then, say, 1300 
%isyear = 1900; % select all these chronologies that longer then, say, 1300 
               %(Note: isyear should be ibyr < isyear < ieyr )
isyearend = 1970; % select all these chronologies that have data in the year, say, 1969 
               %(Note: isyear should be ibyr < isyear < isyearend < ieyr )

ilon1 = -180; ilon2 = 180; % select all these chronologies that located within a certain area
ilat1 = -90; ilat2 = 90;   % (e.g. -120W~-60W, 20N~60N)

%-----------------------------------------------------------
%------------------ form the itrdbmatrix  ------------------
%-----------------------------------------------------------

%%-------- input tree data file_names -------
%dataf = 'D:\scozztemann\trimdata\allproxyname2.txt'; % for all proxies joint in this recon.
dataf = '/holocene/s1/zuz10/allproxy1209/1209proxyname.txt'; % for all proxies joint in this recon.
%dataf = 'D:\scozztemann\trimdata\allproxyname2noco.txt'; % for partial (no CO2) proxies joint in this recon.


%%-- The file_name.txt can be a full list of chronologies or part of most reliable
%%   and interesting chronologies in the itrdb. That means no matter how many chronologies
%%   in the itrdb, selected chronologies will be controlled by file_name.txt---

filee = textread(dataf,'%s','delimiter','\n','whitespace','');
[m1,n1] = size(filee);


x(1:3,1)=NaN; x(4:ieyr-ibyr+4,1)=(ibyr:1:ieyr)'; % set the first column for itrdbmatrix

for i =1:m1 
%cc=lower(filee{i}); dd='D:\scozztemann\trimdata\allproxy2\'; aa='.ppd';
cc=lower(filee{i}); dd='/holocene/s1/zuz10/allproxy1209/'; aa='.ppd';
ccc=strcat(dd,cc,aa);
cf = load(strtok(ccc));

    [m,n] = size(cf);
    
%--- put the selected ring index into data_matrix---
    xx=repmat(NaN,length(x),1);
    xx(1:3,1) = cf(1:3,2);
    if cf(4,3)+cf(round(m/1.2+2),3)>0 & cf(4,3)==cf(round(m/1.2+2),3)
        bbyr = cf(4,1)+icut;
        eeyr = cf(m,1);
    elseif cf(4,3)+cf(round(m/1.2+2),3)>0 & cf(4,2)~=cf(round(m/1.2+2),3)
        nn=3;
        for ii=1:m-3
            nn=nn+1;
            if cf(ii+3,3)>=isamp;break;
            end
        end
        bbyr = cf(nn,1);  % identify start and end yr for individual chron.
        eeyr = cf(m,1);   % according criteria for removing early small sample index
    else
        bbyr = cf(4,1)+icut;
        eeyr = cf(m,1);
    end
    %--
    
    if bbyr < ibyr & eeyr > ieyr
        k1=find(cf(:,1)==ibyr);k2=find(cf(:,1)==ieyr);
        xx(4:length(x),1)=cf(k1:k2,2);
    elseif bbyr < ibyr & eeyr <= ieyr
        k1=find(cf(:,1)==ibyr);k2=find(cf(:,1)==eeyr);
        xx(4:k2-k1+4,1)=cf(k1:k2,2);
    elseif bbyr >= ibyr & eeyr > ieyr
        k1=find(cf(:,1)==bbyr);k2=find(cf(:,1)==ieyr);
        xx(length(x)+k1-k2:length(x),1)=cf(k1:k2,2);
    else
        k1=find(cf(:,1)==bbyr);k2=find(cf(:,1)==eeyr);
        xx(bbyr-ibyr+4:k2-k1+bbyr-ibyr+4,1)=cf(k1:k2,2);
    end
    %--    
       x(:,i+1)=xx; 

end

save('/holocene/s1/zuz10/work1/temann/data/itrdbmatrixfull','x','-ASCII')

%---------------------------------------------------------------
%------------------ form the sub_itrdbmatrix  ------------------
%---------------------------------------------------------------

y(1:3,1)=NaN; y(4:ieyr-ibyr+4,1)=(ibyr:1:ieyr)'; % set the first column for sub_itrdbmatrix

kk=find(x(:,1)==isyear);
kkk=find(x(:,1)==isyearend); % added line 4/4/05
n=0;
for i=1:m1% high and low have diff. selection except for tree rings
%    if (x(3,i+1)<=8001 &  x(3,i+1)>=2000)  &...%upper for other's high, this for other's both high/low(actually for low)
%    if (x(3,i+1)==8000 |  x(3,i+1)==7000 |  x(3,i+1)==6000 |  x(3,i+1)==5000 |  x(3,i+1)==4000 |  x(3,i+1)==3000 |  x(3,i+1)==2000)  &...
%    if x(3,i+1)==9000 &... %both high/low tree rings
    if (x(3,i+1)<=9001 &  x(3,i+1)>=2000)  &...%select all proxy that have location info)
         x(kk,i+1)>-99999 & x(kkk,i+1)>-99999 &...
            x(1,i+1)>=ilon1 & x(1,i+1)<=ilon2 & x(2,i+1)>=ilat1 & x(2,i+1)<=ilat2
        n=n+1;
        y(:,n+1)=x(:,i+1);
    end
end

save('/holocene/s1/zuz10/work1/temann/data/sub_itrdbmatrix','y','-ASCII')
save('/holocene/s1/zuz10/work1/temann/data/itrdbmatrix','y','-ASCII')

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