% I. GOCCP DATA
% Load data
ncload5('/Volumes/MyPassport/Obs/3D_CloudFraction_Phase330m_200609-200708_night_CFMIP2_sat_2.9.nc')

% Cleaning
clear liqgoccp icegoccp phagoccp phasegoccp tot

% Building phase matrix
tottmp(:,:,:,:,1)=clcalipso_ice;
tottmp(:,:,:,:,2)=clcalipso_liq;
tottmp(:,:,:,:,3)=clcalipso_un(:,:,:,1,:);  % only select undefined (=category 1)  
tottmp(:,:,:,:,4)=clcalipso_un(:,:,:,4,:);  % and horizontally oriented ice (= category 4)

tot=squeeze(nansum(tottmp,5));
ice=squeeze(nansum(tottmp(:,:,:,:,[1 3 4]),5));  % Set undefined as ice (here we assume undefined as dominated by ice)
ice(isnan(clcalipso_ice))=nan;

tot(isnan(ice) & isnan(clcalipso_liq))=nan;  % setting not a number
phasegoccp=ice./tot;  % calculating phase ratio
clear tottmp tot


liqgoccp=squeeze(nanmean(nanmean(clcalipso_liq,1),4))';
liqgoccp(41,:)=nan; % adding a dummy dimension for pcolor 
liqgoccp(:,91)=nan; % adding a dummy dimension for pcolor 

icegoccp=squeeze(nanmean(nanmean(ice,1),4))';
icegoccp(41,:)=nan;
icegoccp(:,91)=nan;

phagoccp=squeeze(nanmean(nanmean(phasegoccp,1),4))';
phagoccp(41,:)=nan;
phagoccp(:,91)=nan;

% Declaring dimension vectors
alt=0:0.48:19.2;
lat=-90:2:90;
altmid=0.24:.48:18.96;
latmid=-89:2:89;


% FIGURE
figure(41)
subplot(2,3,1)
pcolor(lat,alt,liqgoccp);shading flat
caxis([0 0.4])
axis([-82 82 0 12])
set(gca,'ydir','normal')
set(gca,'tickdir','out')
set(gcf,'color','white')
set(gca,'xtick',[-80:20:80],'fontname','times','fontsize',14)
set(gca,'ytick',[0:2:18],'fontname','times','fontsize',14)
title('a. GOCCP','fontname','times','fontweight','bold','fontsize',18)

figure(42)
subplot(2,3,1)
pcolor(lat,alt,icegoccp);shading flat
caxis([0 0.4])
axis([-82 82 0 19])
set(gca,'ydir','normal')
set(gca,'tickdir','out')
set(gcf,'color','white')
set(gca,'xtick',[-80:20:80],'fontname','times','fontsize',14)
set(gca,'ytick',[0:2:18],'fontname','times','fontsize',14)
title('a. GOCCP','fontname','times','fontweight','bold','fontsize',18)

figure(43)
subplot(3,1,1)
pcolor(lat,alt,phagoccp);shading flat
caxis([0 1])
axis([-82 82 0 14])
set(gca,'ydir','normal')
set(gca,'tickdir','out')
set(gcf,'color','white')
set(gca,'xtick',[-80:20:80],'fontname','times','fontsize',14)
set(gca,'ytick',[0:2:18],'fontname','times','fontsize',14)
title('a. GOCCP','fontname','times','fontweight','bold','fontsize',18)
hold on
contour(latmid,altmid,phagoccp(1:40,1:90),[0.5 0.5],'green-','linewidth',3);   % magenta


% II. Science Team DATA
clear clcalipso_ice clcalipso_liq
ncload5('/Volumes/MyPassport/Obs/CF3D330m_200609-200708_CFMIP2_ST.nc')
clear liqst icest phast phasest tot 

tottmp(:,:,:,:,1)=clcalipso_ice; % ICE
 tottmp(:,:,:,:,2)=clcalipso_liq; % LIQ
 tot=squeeze(nansum(tottmp,5));
 tot(isnan(clcalipso_ice) & isnan(clcalipso_liq))=nan;

 clear tottmp
 phasest=clcalipso_ice./tot;

liqst=squeeze(nanmean(nanmean(clcalipso_liq,3),4));
liqst(41,:)=nan;
liqst(:,91)=nan;

icest=squeeze(nanmean(nanmean(clcalipso_ice,3),4));
icest(41,:)=nan;
icest(:,91)=nan;

phast=squeeze(nanmean(nanmean(phasest,3),4));
phast(41,:)=nan;
phast(:,91)=nan;

alt=0:0.48:19.2;
lat=-90:2:90;
altmid=0.24:.48:18.96;
latmid=-89:2:89;

figure(41)
subplot(2,3,2)
pcolor(lat,alt,liqst);shading flat
caxis([0 0.4])
axis([-82 82 0 12])
set(gca,'ydir','normal')
set(gca,'tickdir','out')
set(gcf,'color','white')
set(gca,'xtick',[-80:20:80],'fontname','times','fontsize',14)
set(gca,'ytick',[0:2:18],'fontname','times','fontsize',14)
title('b. ST','fontname','times','fontweight','bold','fontsize',18)

figure(42)
subplot(2,3,2)
pcolor(lat,alt,icest);shading flat
caxis([0 0.4])
axis([-82 82 0 19])
set(gca,'ydir','normal')
set(gca,'tickdir','out')
set(gcf,'color','white')
set(gca,'xtick',[-80:20:80],'fontname','times','fontsize',14)
set(gca,'ytick',[0:2:18],'fontname','times','fontsize',14)
title('b. ST','fontname','times','fontweight','bold','fontsize',18)




% III. KYUSHU DATA
ncload5('/Volumes/MyPassport/Obs/CF3DJAXA_200609-200708_night_CFMIP2_9.nc')
clear iceku liqku phaku phaseku 
cf3d=double(cf3d);
cf3d(cf3d==-9999)=nan;

tottmp(:,:,:,:,1)=cf3d(:,:,:,2,:);
tottmp(:,:,:,:,2)=cf3d(:,:,:,3,:);
tot=squeeze(nansum(tottmp,5));
cf3dice=squeeze(cf3d(:,:,:,2,:)); % ice
cf3dliq=squeeze(cf3d(:,:,:,3,:)); % LIQ

tot(isnan(cf3dice) & isnan(cf3dliq))=nan;
phaseku=squeeze(cf3dice)./tot;
clear tottmp tot cf3dliq cf3dice

 phaseku1d=squeeze(nanmean(nanmean(nanmean(phaseku,2),2),4));

 liqku=squeeze(nanmean(nanmean(cf3d(:,:,:,3,:),2),5));
liqku(84,:)=nan;
liqku(:,91)=nan;

iceku=squeeze(nanmean(nanmean(cf3d(:,:,:,2,:),2),5));
iceku(84,:)=nan;
iceku(:,91)=nan;

phaku=squeeze(nanmean(nanmean(phaseku,2),4));
phaku(84,:)=nan;
phaku(:,91)=nan;

alt2=0:0.24:19.92;
lat=-90:2:90;
altmid2=0.12:.24:19.8;
latmid=-89:2:89;

figure(41)
subplot(2,3,3)
pcolor(lat,alt2,liqku);shading flat
caxis([0 0.4])
axis([-82 82 0 12])
set(gca,'ydir','normal')
set(gca,'tickdir','out')
set(gcf,'color','white')
set(gca,'xtick',[-80:20:80],'fontname','times','fontsize',14)
set(gca,'ytick',[0:2:18],'fontname','times','fontsize',14)
title('c. KU','fontname','times','fontweight','bold','fontsize',18)
 ylabel('Height (km)','fontname','times','fontweight','bold','fontsize',18)
xlabel('Latitude (\circN)','fontname','times','fontweight','bold','fontsize',18)
tutu=colorbar('east','ytick',[-0.25:2*0.5/20:0.25],'yticklabel',100*[-0.25:2*0.5/20:0.25])
ylabel(tutu,'Cloud Fraction difference (%)','fontname','times','fontweight','bold','fontsize',14)
tutu=colorbar('east','ytick',[-0.:2*0.3/20:0.3],'yticklabel',100*[-0.:2*0.3/20:0.3])
ylabel(tutu,'Cloud Fraction (%)','fontname','times','fontweight','bold','fontsize',14)

figure(42)
subplot(2,3,3)
pcolor(lat,alt2,iceku);shading flat
caxis([0 0.4])
axis([-82 82 0 19])
set(gca,'ydir','normal')
set(gca,'tickdir','out')
set(gcf,'color','white')
set(gca,'xtick',[-80:20:80],'fontname','times','fontsize',14)
set(gca,'ytick',[0:2:18],'fontname','times','fontsize',14)
title('c. KU','fontname','times','fontweight','bold','fontsize',18)
 ylabel('Height (km)','fontname','times','fontweight','bold','fontsize',18)
xlabel('Latitude (\circN)','fontname','times','fontweight','bold','fontsize',18)
tutu=colorbar('east','ytick',[-0.25:2*0.5/20:0.25],'yticklabel',100*[-0.25:2*0.5/20:0.25])
ylabel(tutu,'Cloud Fraction difference (%)','fontname','times','fontweight','bold','fontsize',14)
tutu=colorbar('east','ytick',[-0.:2*0.3/20:0.3],'yticklabel',100*[-0.:2*0.3/20:0.3])
ylabel(tutu,'Cloud Fraction (%)','fontname','times','fontweight','bold','fontsize',14)



% LINEAR INTERPOLATION TO GOCCP GRID FOR BIAS COMPUTATION
% 

%
lat=-89:2:89;
lev=0.12:.24:19.8;

lat_mod=lat;
lev_mod=0.24:.48:18.96;

[x2 y2]=meshgrid(lat_mod,lev_mod);
[x1 y1]=meshgrid(lat,lev);

liqku2=interp2(x1,y1,liqku(1:83,1:90),x2,y2);
iceku2=interp2(x1,y1,iceku(1:83,1:90),x2,y2);

iceku2(41,:)=nan;
iceku2(:,91)=nan;

liqku2(41,:)=nan;
liqku2(:,91)=nan;

lat=-90:2:90;
alt=0:0.48:19.2;

figure(41)
subplot(2,3,5)
pcolor(lat,alt,liqgoccp-liqst);shading flat
caxis([-0.25 0.25])
axis([-82 82 0 12])
set(gca,'ydir','normal')
set(gca,'tickdir','out')
set(gcf,'color','white')
set(gca,'xtick',[-80:20:80],'fontname','times','fontsize',14)
set(gca,'ytick',[0:2:18],'fontname','times','fontsize',14)
title('d. GOCCP - ST','fontname','times','fontweight','bold','fontsize',18)

figure(42)
subplot(2,3,5)
pcolor(lat,alt,icegoccp-icest);shading flat
caxis([-0.25 0.25])
axis([-82 82 0 19])
set(gca,'ydir','normal')
set(gca,'tickdir','out')
set(gcf,'color','white')
set(gca,'xtick',[-80:20:80],'fontname','times','fontsize',14)
set(gca,'ytick',[0:2:18],'fontname','times','fontsize',14)
title('d. GOCCP - ST','fontname','times','fontweight','bold','fontsize',18)
set(gcf,'color','none')
set(gcf,'renderer','zbuffer')

figure(41)
subplot(2,3,6)
pcolor(lat,alt,liqgoccp-liqku2);shading flat
caxis([-0.25 0.25])
axis([-82 82 0 12])
set(gca,'ydir','normal')
set(gca,'tickdir','out')
set(gcf,'color','white')
set(gca,'xtick',[-80:20:80],'fontname','times','fontsize',14)
set(gca,'ytick',[0:2:18],'fontname','times','fontsize',14)
title('e. GOCCP - KU','fontname','times','fontweight','bold','fontsize',18)
set(gcf,'color','none')
set(gcf,'renderer','zbuffer')

figure(42)
subplot(2,3,6)
pcolor(lat,alt,icegoccp-iceku2);shading flat
caxis([-0.25 0.25])
axis([-82 82 0 19])
set(gca,'ydir','normal')
set(gca,'tickdir','out')
set(gcf,'color','white')
set(gca,'xtick',[-80:20:80],'fontname','times','fontsize',14)
set(gca,'ytick',[0:2:18],'fontname','times','fontsize',14)
title('e. GOCCP - KU','fontname','times','fontweight','bold','fontsize',18)
set(gcf,'color','none')
set(gcf,'renderer','zbuffer')


