
%% Need to standardize each record - mainly to ensure that the varainces
% are similar, as the ANOVA will take care of the means 
load Varve_Locs
load timeline_varve
load Varve_Data_Matrix 
load Varve_LogData_Matrix 

Varve_LogData_Normed_Matrix=NaN(size(Varve_Data_Matrix));

%Records [1:4, 7:9] are complete from [1400 1969]. 
%Set means to zero and stds to 1 for these records over this interval:
prox_inds_long=[1:1:4, 7:9];
prox_inds_short=setdiff(1:1:length(Varve_Locs(:,1)), prox_inds_long);

inds_nonan=find(isnan(sum(Varve_Data_Matrix(prox_inds_long,:)))==0);

for kk=1:1:length(prox_inds_long)
    Varve_LogData_Normed_Matrix(prox_inds_long(kk),:)=(Varve_LogData_Matrix(prox_inds_long(kk),:)-mean(Varve_LogData_Matrix(prox_inds_long(kk),inds_nonan)))/std(Varve_LogData_Matrix(prox_inds_long(kk),inds_nonan));
end

%for each of the others, find the intersection with the [1400, 1969] interval. Find
%the means and stds of the longs over this interval, take averages, and
%scale the record to these. 

for kk=1:1:length(prox_inds_short)
    inds_inter=intersect(find(isnan(Varve_Data_Matrix(prox_inds_short(kk),:))==0), inds_nonan);
    
    %and for the logs:
    mean_scale=mean(mean(Varve_LogData_Normed_Matrix(prox_inds_long,inds_inter),2));
    std_scale=mean(std(Varve_LogData_Normed_Matrix(prox_inds_long,inds_inter),0,2));
    %and set the mean and std of the current record:
    Varve_LogData_Normed_Matrix(prox_inds_short(kk),:)=(Varve_LogData_Matrix(prox_inds_short(kk),:)-mean(Varve_LogData_Matrix(prox_inds_short(kk),inds_inter)))/std(Varve_LogData_Matrix(prox_inds_short(kk),inds_inter))*std_scale+mean_scale;
    [mean_scale, mean(Varve_LogData_Normed_Matrix(prox_inds_short(kk),inds_inter))]
    [std_scale, std(Varve_LogData_Normed_Matrix(prox_inds_short(kk),inds_inter))]
end



save Varve_LogData_Normed_Matrix Varve_LogData_Normed_Matrix
save Varve_LogData_Normed_Matrix Varve_LogData_Normed_Matrix -ASCII

