%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% parameters for the itrdbmatrix %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all; close all;
%-- 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 = 1;  % start year of the data_matrix
ieyr = 1998; % end year of the data_matrix

isamp = 1;   % 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 %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


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

%%-------- input tree data file_names -------
%dataf = 'D:\scozztemann\dodist\allproxyname.txt';
dataf = 'D:\scozztemann\trimdata\1227proxynamenolu.txt';

%%-- 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\dodist\allproxy\'; aa='.ppd';
cc=lower(filee{i}); dd='D:\scozztemann\dodist\allproxy1\'; 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/2+2),3)>0 & cf(4,3)==cf(round(m/2+2),3)
        bbyr = cf(4,1)+icut;
        eeyr = cf(m,1);
    elseif cf(4,3)+cf(round(m/2+2),3)>0 & cf(4,2)~=cf(round(m/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

%y1=x(1859:2001,:); %get data from 1856-1998
y1=x(1803:2001,:); %get data from 1800-1998
[mmm,nnn]=size(y1);
%y=y1(:,2:1261);
y=y1(:,2:nnn);
size(y)
size(y1)
%t=[y1(:,2) y(:,1) y1(:,1261) y(:,1260)];
    keepers=y;
    [nrows,ncols]=size(keepers)

    means=nanmean(keepers); % normalization over 1800-1998/ or whatever intervals
    stdevs=nanstd(keepers);
    stdkeepers=keepers-repmat(means,nrows,1);
    stdkeepers=stdkeepers./repmat(stdevs,nrows,1);

save('d:\scozztemann\trimdata\means','means','-ascii')
save('d:\scozztemann\trimdata\stdevs','stdevs','-ascii')
save('d:\scozztemann\trimdata\trimforinfill1227','stdkeepers','-ascii')

% then this dataset is used for infilling with regem
% note: four short series from mxd are removed



