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');

%%% Temp Old Monthly Lower
fin=['/cpc/home/sparker/lrf/temp/monstemp_lowerlimit_8110'];
fid= fopen(fin,'r','ieee-le');
for m=1:12
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    temp_old_monthly_lower(:,:,m)=reshape(b,unx,uny)';
end
fclose(fid);

%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_lower(:,:,1)); shading flat; colorbar('vert');

%%% Temp Old Monthly Upper
fin=['/cpc/home/sparker/lrf/temp/monstemp_upperlimit_8110'];
fid= fopen(fin,'r','ieee-le');
for m=1:12
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    temp_old_monthly_upper(:,:,m)=reshape(b,unx,uny)';
end
fclose(fid);

%%% Temp New Monthly Lower
fin=['/cpc/home/sparker/lrf/temp/monstemp_lowerlimit_9120'];
fid= fopen(fin,'r','ieee-le');
for m=1:12
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    temp_new_monthly_lower(:,:,m)=reshape(b,unx,uny)';
end
fclose(fid);


%%% Temp New Monthly Upper
fin=['/cpc/home/sparker/lrf/temp/monstemp_upperlimit_9120'];
fid= fopen(fin,'r','ieee-le');
for m=1:12
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    temp_new_monthly_upper(:,:,m)=reshape(b,unx,uny)';
end
fclose(fid);


%%% Temp Old Season Lower
fin=['/cpc/home/sparker/lrf/temp/seastemp_lowerlimit_8110'];
fid= fopen(fin,'r','ieee-le');
for m=1:12
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    temp_old_seasons_lower(:,:,m)=reshape(b,unx,uny)';
end
fclose(fid);


%%% Temp Old Season Upper
fin=['/cpc/home/sparker/lrf/temp/seastemp_upperlimit_8110'];
fid= fopen(fin,'r','ieee-le');
for m=1:12
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    temp_old_seasons_upper(:,:,m)=reshape(b,unx,uny)';
end
fclose(fid);


%%% Temp New Season Lower
fin=['/cpc/home/sparker/lrf/temp/seastemp_lowerlimit_9120'];
fid= fopen(fin,'r','ieee-le');
for m=1:12
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    temp_new_seasons_lower(:,:,m)=reshape(b,unx,uny)';
end
fclose(fid);


%%% Temp New Season Upper
fin=['/cpc/home/sparker/lrf/temp/seastemp_upperlimit_9120'];
fid= fopen(fin,'r','ieee-le');
for m=1:12
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    temp_new_seasons_upper(:,:,m)=reshape(b,unx,uny)';
end
fclose(fid);


%%% Prec Old Monthly Lower
fin=['/cpc/home/sparker/lrf/precip/monsprec_lowerlimit8110'];
fid= fopen(fin,'r','ieee-le');
for m=1:12
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    prec_old_monthly_lower(:,:,m)=reshape(b,unx,uny)';
end
fclose(fid);


%%% Prec Old Monthly Upper
fin=['/cpc/home/sparker/lrf/precip/monsprec_upperlimit8110'];
fid= fopen(fin,'r','ieee-le');
for m=1:12
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    prec_old_monthly_upper(:,:,m)=reshape(b,unx,uny)';
end
fclose(fid);


%%% Prec New Monthly Lower
fin=['/cpc/home/sparker/lrf/precip/monsprec_lowerlimit9120_nn'];
fid= fopen(fin,'r','ieee-le');
for m=1:12
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    prec_new_monthly_lower(:,:,m)=reshape(b,unx,uny)';
end
fclose(fid);


%%% Prec New Monthly Upper
fin=['/cpc/home/sparker/lrf/precip/monsprec_upperlimit9120_nn'];
fid= fopen(fin,'r','ieee-le');
for m=1:12
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    prec_new_monthly_upper(:,:,m)=reshape(b,unx,uny)';
end
fclose(fid);


%%% Prec Old Season Lower
fin=['/cpc/home/sparker/lrf/precip/seasprec_lowerlimit8110'];
fid= fopen(fin,'r','ieee-le');
for m=1:12
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    prec_old_seasons_lower(:,:,m)=reshape(b,unx,uny)';
end
fclose(fid);

%%% Prec Old Season Upper
fin=['/cpc/home/sparker/lrf/precip/seasprec_upperlimit8110'];
fid= fopen(fin,'r','ieee-le');
for m=1:12
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    prec_old_seasons_upper(:,:,m)=reshape(b,unx,uny)';
end
fclose(fid);


%%% Prec New Season Lower
fin=['/cpc/home/sparker/lrf/precip/seasprec_lowerlimit9120_nn'];
fid= fopen(fin,'r','ieee-le');
for m=1:12
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    prec_new_seasons_lower(:,:,m)=reshape(b,unx,uny)';
end
fclose(fid);


%%% Prec New Season Upper
fin=['/cpc/home/sparker/lrf/precip/seasprec_upperlimit9120_nn'];
fid= fopen(fin,'r','ieee-le');
for m=1:12
    b=fread(fid,unx*uny,'single');
    b(find(b<-99))=NaN;
    prec_new_seasons_upper(:,:,m)=reshape(b,unx,uny)';
end
fclose(fid);

%%% Difference between base periods
temp_diff_monthly_lower=temp_new_monthly_lower - temp_old_monthly_lower
temp_diff_monthly_upper=temp_new_monthly_upper - temp_old_monthly_upper
temp_diff_seasons_lower=temp_new_seasons_lower - temp_old_seasons_lower
temp_diff_seasons_upper=temp_new_seasons_upper - temp_old_seasons_upper
prec_diff_monthly_lower=prec_new_monthly_lower - prec_old_monthly_lower
prec_diff_monthly_upper=prec_new_monthly_upper - prec_old_monthly_upper
prec_diff_seasons_lower=prec_new_seasons_lower - prec_old_seasons_lower
prec_diff_seasons_upper=prec_new_seasons_upper - prec_old_seasons_upper



%%% Perform difference fields loop
for m=1:12
    b=reshape(temp_diff_monthly_lower(:,:,m),36*19,1) % reshapes grid at some timestep back to a single length equalling 36*19
    temp_diff_monthly_lower_meanvalue(m,:) = nanmean(b);
    b=reshape(temp_diff_monthly_upper(:,:,m),36*19,1) % reshapes grid at some timestep back to a single length equalling 36*19
    temp_diff_monthly_upper_meanvalue(m,:) = nanmean(b);
    b=reshape(temp_diff_seasons_lower(:,:,m),36*19,1) % reshapes grid at some timestep back to a single length equalling 36*19
    temp_diff_seasons_lower_meanvalue(m,:) = nanmean(b);
    b=reshape(temp_diff_seasons_upper(:,:,m),36*19,1) % reshapes grid at some timestep back to a single length equalling 36*19
    temp_diff_seasons_upper_meanvalue(m,:) = nanmean(b);
    b=reshape(prec_diff_monthly_lower(:,:,m),36*19,1) % reshapes grid at some timestep back to a single length equalling 36*19
    prec_diff_monthly_lower_meanvalue(m,:) = nanmean(b);
    b=reshape(prec_diff_monthly_upper(:,:,m),36*19,1) % reshapes grid at some timestep back to a single length equalling 36*19
    prec_diff_monthly_upper_meanvalue(m,:) = nanmean(b);
    b=reshape(prec_diff_seasons_lower(:,:,m),36*19,1) % reshapes grid at some timestep back to a single length equalling 36*19
    prec_diff_seasons_lower_meanvalue(m,:) = nanmean(b);
    b=reshape(prec_diff_seasons_upper(:,:,m),36*19,1) % reshapes grid at some timestep back to a single length equalling 36*19
    prec_diff_seasons_upper_meanvalue(m,:) = nanmean(b);
end



%%% Display single numerical value for annual average difference fields
%%% monthly and seasons
disp(temp_diff_monthly_lower_meanvalue(m,:))
disp(temp_diff_monthly_upper_meanvalue(m,:))
disp(temp_diff_seasons_lower_meanvalue(m,:))
disp(temp_diff_seasons_upper_meanvalue(m,:))
disp(prec_diff_monthly_lower_meanvalue(m,:))
disp(prec_diff_monthly_upper_meanvalue(m,:))
disp(prec_diff_seasons_lower_meanvalue(m,:))
disp(prec_diff_seasons_upper_meanvalue(m,:))


%%% Prints out of matlab
%print('-dpng',['test.png']);