clear all
loc = '/cpc/home/sparker/lrf/analysis';

%%% SET DIMS
ulon=[-130:2:-60]; unx=length(ulon);
ulat=[20:2:56]; uny=length(ulat);

%%% SET DATES
yrs=[1995:2020]; % Native Time Series
startdate=['01Jan1995']; finaldate=['31Dec2020']; 
datesd=datestr(datevec((datenum(['' startdate ''])):(datenum(['' finaldate '']))),'yyyymmdd');
for d=1:length(datesd), x(d,:)=eval( datesd(d,1:6)); end; datesm=unique(x); datesm_temp=num2str(datesm);

yrs=[2011:2020]; x=[]; % Study/Target Time Series
startdate=['01Jan2011']; finaldate=['31Dec2020']; 
datesd=datestr(datevec((datenum(['' startdate ''])):(datenum(['' finaldate '']))),'yyyymmdd');
for d=1:length(datesd), x(d,:)=eval( datesd(d,1:6)); end; datesm=unique(x); datesm=num2str(datesm); 

fin=['/cpc/prod_tst/nick.novella/lrf/vf_glbt/library/mask_2x2.data'];
fid= fopen(fin,'r','ieee-le');
b=fread(fid,unx*uny,'single');
b(find(b~=1))=NaN;
mk=reshape(b,unx,uny)';
fclose(fid);

%%% plot  
%%% figure(1); clf(1); 
%%% plot(adm1lon,adm1lat,'-','LineWidth',1,'Color',[0 0 0]); hold on; axis([-130 -60 20 55])
%%% pcolorjw(ulon,ulat,mk); shading flat; colorbar('vert');

%%% Old temp Monthly
fin=['/cpc/prod_tst/nick.novella/lrf/ob_glbt/output/all_ranges/temp_mons_verif.bi'];
fid= fopen(fin,'r','ieee-le');
for t=1:length(datesm_temp)
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    temp_old_monthly(:,:,t)=reshape(b,unx,uny)';
end
fclose(fid);
temp_old_monthly=temp_old_monthly(:,:,strmatch(datesm(1,:),datesm_temp):strmatch(datesm(end,:),datesm_temp));

%%% figure(1); clf(1); 
%%% plot(adm1lon,adm1lat,'-','LineWidth',1,'Color',[0 0 0]); hold on; axis([-130 -60 20 55])
%%% pcolorjw(ulon,ulat,temp_old_monthly(:,:,1)); shading flat; colorbar('vert');

%%% New Temp Monthly
fin=['/cpc/home/sparker/lrf/reproc/ob_glbt/output/all_ranges/temp_mons_verif.bi'];
fid= fopen(fin,'r','ieee-le');
for t=1:length(datesm_temp)
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    temp_new_monthly(:,:,t)=reshape(b,unx,uny)';
end
fclose(fid);
temp_new_monthly=temp_new_monthly(:,:,strmatch(datesm(1,:),datesm_temp):strmatch(datesm(end,:),datesm_temp));

%%% Old Prec Monthly
fin=['/cpc/prod_tst/nick.novella/lrf/ob_glbt/output/all_ranges/prec_mons_verif.bi'];
fid= fopen(fin,'r','ieee-le');
for t=1:length(datesm_temp)
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    prec_old_monthly(:,:,t)=reshape(b,unx,uny)';
end
fclose(fid);
prec_old_monthly=prec_old_monthly(:,:,strmatch(datesm(1,:),datesm_temp):strmatch(datesm(end,:),datesm_temp));

%%% New Prec Monthly
fin=['/cpc/home/sparker/lrf/reproc/ob_glbt/output/all_ranges/prec_mons_verif.bi'];
fid= fopen(fin,'r','ieee-le');
for t=1:length(datesm_temp)
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    prec_new_monthly(:,:,t)=reshape(b,unx,uny)';
end
fclose(fid);
prec_new_monthly=prec_new_monthly(:,:,strmatch(datesm(1,:),datesm_temp):strmatch(datesm(end,:),datesm_temp));

%%% Old temp Seasons
fin=['/cpc/prod_tst/nick.novella/lrf/ob_glbt/output/all_ranges/temp_seas_verif.bi'];
fid= fopen(fin,'r','ieee-le');
for t=1:length(datesm_temp)
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    temp_old_seasons(:,:,t)=reshape(b,unx,uny)';
end
fclose(fid);
temp_old_seasons=temp_old_seasons(:,:,strmatch(datesm(1,:),datesm_temp):strmatch(datesm(end,:),datesm_temp));

%%% New Temp Seasons
fin=['/cpc/home/sparker/lrf/reproc/ob_glbt/output/all_ranges/temp_seas_verif.bi'];
fid= fopen(fin,'r','ieee-le');
for t=1:length(datesm_temp)
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    temp_new_seasons(:,:,t)=reshape(b,unx,uny)';
end
fclose(fid);
temp_new_seasons=temp_new_seasons(:,:,strmatch(datesm(1,:),datesm_temp):strmatch(datesm(end,:),datesm_temp));

%%% Old Prec Seasons
fin=['/cpc/prod_tst/nick.novella/lrf/ob_glbt/output/all_ranges/prec_seas_verif.bi'];
fid= fopen(fin,'r','ieee-le');
for t=1:length(datesm_temp)
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    prec_old_seasons(:,:,t)=reshape(b,unx,uny)';
end
fclose(fid);
prec_old_seasons=prec_old_seasons(:,:,strmatch(datesm(1,:),datesm_temp):strmatch(datesm(end,:),datesm_temp));

%%% New Prec Seasons
fin=['/cpc/home/sparker/lrf/reproc/ob_glbt/output/all_ranges/prec_seas_verif.bi'];
fid= fopen(fin,'r','ieee-le');
for t=1:length(datesm_temp)
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    prec_new_seasons(:,:,t)=reshape(b,unx,uny)';
end
fclose(fid);
prec_new_seasons=prec_new_seasons(:,:,strmatch(datesm(1,:),datesm_temp):strmatch(datesm(end,:),datesm_temp));




%%% Calc Distribution of A/N?B between base periods
a=temp_old_monthly(:,:,1); I=find(isnan(a)==0); nI=length(find(isnan(a)==0));
dist=[];
for m=1:length(datesm)
  for t=1:8
   if (t==1), gr=temp_old_monthly(:,:,m); end
   if (t==2), gr=temp_new_monthly(:,:,m); end
   if (t==3), gr=prec_old_monthly(:,:,m); end
   if (t==4), gr=prec_new_monthly(:,:,m); end
   if (t==5), gr=temp_old_seasons(:,:,m); end
   if (t==6), gr=temp_new_seasons(:,:,m); end
   if (t==7), gr=prec_old_seasons(:,:,m); end
   if (t==8), gr=prec_new_seasons(:,:,m); end
   dist(m,:,t)=[ length(find(gr(I)==1))/nI length(find(gr(I)==0))/nI length(find(gr(I)==-1))/nI ];
  end
end



%%% Pie chart variables named
X1=mean(dist(:,:,1));
X2=mean(dist(:,:,2));
X3=mean(dist(:,:,3));
X4=mean(dist(:,:,4));
X5=mean(dist(:,:,5));
X6=mean(dist(:,:,6));
X7=mean(dist(:,:,7));
X8=mean(dist(:,:,8));
labels={'Above','Normal','Below'};
%%%t=tiledlayout(1,2,'TileSpacing','compact');

%%% Pie charts
figure(1)
ax1=subplot(1,2,1);
pie(ax1,X1);
title('Old Monthly Temp','FontSize',18)
ax2=subplot(1,2,2);
pie(ax2,X2);
title('New Monthly Temp','FontSize',18)

%%% Legend
lgd=legend(labels,'Location','southwest');
fig.color='white';

colormap([1 0 0;
    .5 .5 .5;
    0 0 1])


