
clear all 

FCLM5IN='/cluster/shared/noresm/inputdata/lnd/clm2/surfdata_map/surfdata_1.9x2.5_16pfts_CMIP6_simyr1850_c170706.nc'; 
FCLM4IN='/cluster/shared/noresm/inputdata/lnd/clm2/surfdata/surfdata_1.9x2.5_simyr1850_c110114.nc';
FCLM4OUT='/cluster/shared/noresm/inputdata/lnd/clm2/surfdata/surfdata_1.9x2.5_simyr1850_c110114_CMIP6.nc';

% do PFT 
PCT_PFT=ncread(FCLM4OUT,'PCT_PFT');
PCT_NAT_PFT=ncread(FCLM5IN,'PCT_NAT_PFT');
PCT_CFT=ncread(FCLM5IN,'PCT_CFT');
PCT_NATVEG=ncread(FCLM5IN,'PCT_NATVEG');
PCT_CROP=ncread(FCLM5IN,'PCT_CROP');
for n=1:size(PCT_NAT_PFT,3)
  PCT_NAT_PFT(:,:,n)=PCT_NAT_PFT(:,:,n).*PCT_NATVEG/100;
end
for n=1:size(PCT_CFT,3)
  PCT_CFT(:,:,n)=PCT_CFT(:,:,n).*PCT_CROP/100;
end
PCT_CFT(:,:,1)=PCT_CFT(:,:,1)+PCT_CFT(:,:,2);PCT_CFT(:,:,2)=0; % no irregated crops!
PCT_PFT=cat(3,PCT_NAT_PFT,PCT_CFT);

% do urban ancillaries
CANYON_HWR=ncread(FCLM4OUT,'CANYON_HWR');
HT_ROOF=ncread(FCLM4OUT,'HT_ROOF');
NLEV_IMPROAD=ncread(FCLM4OUT,'NLEV_IMPROAD');
PCT_URBAN=ncread(FCLM4OUT,'PCT_URBAN');
THICK_ROOF=ncread(FCLM4OUT,'THICK_ROOF');
THICK_WALL=ncread(FCLM4OUT,'THICK_WALL');
T_BUILDING_MIN=ncread(FCLM4OUT,'T_BUILDING_MIN');
WIND_HGT_CANYON=ncread(FCLM4OUT,'WIND_HGT_CANYON');
WTLUNIT_ROOF=ncread(FCLM4OUT,'WTLUNIT_ROOF');
WTROAD_PERV=ncread(FCLM4OUT,'WTROAD_PERV'); 
%
TK_ROOF=ncread(FCLM4OUT,'TK_ROOF');
TK_WALL=ncread(FCLM4OUT,'TK_WALL');
TK_IMPROAD=ncread(FCLM4OUT,'TK_IMPROAD');
CV_ROOF=ncread(FCLM4OUT,'CV_ROOF');
CV_WALL=ncread(FCLM4OUT,'CV_WALL');
CV_IMPROAD=ncread(FCLM4OUT,'CV_IMPROAD');
EM_ROOF=ncread(FCLM4OUT,'EM_ROOF');
EM_WALL=ncread(FCLM4OUT,'EM_WALL');
EM_IMPROAD=ncread(FCLM4OUT,'EM_IMPROAD');
EM_PERROAD=ncread(FCLM4OUT,'EM_PERROAD');
ALB_ROOF=ncread(FCLM4OUT,'ALB_ROOF');
ALB_WALL=ncread(FCLM4OUT,'ALB_WALL');
ALB_IMPROAD=ncread(FCLM4OUT,'ALB_IMPROAD');
ALB_PERROAD=ncread(FCLM4OUT,'ALB_PERROAD');
%
CANYON_HWR_CLM5=ncread(FCLM5IN,'CANYON_HWR');
HT_ROOF_CLM5=ncread(FCLM5IN,'HT_ROOF');
NLEV_IMPROAD_CLM5=ncread(FCLM5IN,'NLEV_IMPROAD');
PCT_URBAN_CLM5=ncread(FCLM5IN,'PCT_URBAN');
THICK_ROOF_CLM5=ncread(FCLM5IN,'THICK_ROOF');
THICK_WALL_CLM5=ncread(FCLM5IN,'THICK_WALL');
T_BUILDING_MIN_CLM5=ncread(FCLM5IN,'T_BUILDING_MIN');
WIND_HGT_CANYON_CLM5=ncread(FCLM5IN,'WIND_HGT_CANYON');
WTLUNIT_ROOF_CLM5=ncread(FCLM5IN,'WTLUNIT_ROOF');
WTROAD_PERV_CLM5=ncread(FCLM5IN,'WTROAD_PERV');
%
TK_ROOF_CLM5=ncread(FCLM5IN,'TK_ROOF');
TK_WALL_CLM5=ncread(FCLM5IN,'TK_WALL');
TK_IMPROAD_CLM5=ncread(FCLM5IN,'TK_IMPROAD');
CV_ROOF_CLM5=ncread(FCLM5IN,'CV_ROOF');
CV_WALL_CLM5=ncread(FCLM5IN,'CV_WALL');
CV_IMPROAD_CLM5=ncread(FCLM5IN,'CV_IMPROAD');
EM_ROOF_CLM5=ncread(FCLM5IN,'EM_ROOF');
EM_WALL_CLM5=ncread(FCLM5IN,'EM_WALL');
EM_IMPROAD_CLM5=ncread(FCLM5IN,'EM_IMPROAD');
EM_PERROAD_CLM5=ncread(FCLM5IN,'EM_PERROAD');
ALB_ROOF_CLM5=ncread(FCLM5IN,'ALB_ROOF_DIF');
ALB_WALL_CLM5=ncread(FCLM5IN,'ALB_WALL_DIF');
ALB_IMPROAD_CLM5=ncread(FCLM5IN,'ALB_IMPROAD_DIF');
ALB_PERROAD_CLM5=ncread(FCLM5IN,'ALB_PERROAD_DIF');
%
CANYON_HWR=CANYON_HWR_CLM5(:,:,end);
HT_ROOF=HT_ROOF_CLM5(:,:,end);
NLEV_IMPROAD=NLEV_IMPROAD_CLM5(:,:,end);
PCT_URBAN=sum(PCT_URBAN_CLM5,3);
THICK_ROOF=THICK_ROOF_CLM5(:,:,end);
THICK_WALL=THICK_WALL_CLM5(:,:,end);
T_BUILDING_MIN=T_BUILDING_MIN_CLM5(:,:,end);
WIND_HGT_CANYON=WIND_HGT_CANYON_CLM5(:,:,end);
WTLUNIT_ROOF=WTLUNIT_ROOF_CLM5(:,:,end);
WTROAD_PERV=WTROAD_PERV_CLM5(:,:,end);
%
for k=1:size(TK_ROOF,3)
  TK_ROOF(:,:,k)=TK_ROOF_CLM5(:,:,end,1); 
  TK_WALL(:,:,k)=TK_WALL_CLM5(:,:,end,1); 
  TK_IMPROAD(:,:,k)=TK_IMPROAD_CLM5(:,:,end,1); 
  CV_ROOF(:,:,k)=CV_ROOF_CLM5(:,:,end,1); 
  CV_WALL(:,:,k)=CV_WALL_CLM5(:,:,end,1); 
  CV_IMPROAD(:,:,k)=CV_IMPROAD_CLM5(:,:,end,1); 
end 
EM_ROOF=EM_ROOF_CLM5(:,:,end,1); 
EM_WALL=EM_WALL_CLM5(:,:,end,1); 
EM_IMPROAD=EM_IMPROAD_CLM5(:,:,end,1); 
EM_PERROAD=EM_PERROAD_CLM5(:,:,end,1); 
for n=1:size(ALB_IMPROAD,4)
  ALB_ROOF(:,:,:,n)=squeeze(ALB_ROOF_CLM5(:,:,end,:));
  ALB_WALL(:,:,:,n)=squeeze(ALB_WALL_CLM5(:,:,end,:));
  ALB_IMPROAD(:,:,:,n)=squeeze(ALB_IMPROAD_CLM5(:,:,end,:));
  ALB_PERROAD(:,:,:,n)=squeeze(ALB_PERROAD_CLM5(:,:,end,:));
end 

% update output file
ncwrite(FCLM4OUT,'PCT_PFT',PCT_PFT);
ncwrite(FCLM4OUT,'CANYON_HWR',CANYON_HWR);
ncwrite(FCLM4OUT,'HT_ROOF',HT_ROOF);
ncwrite(FCLM4OUT,'NLEV_IMPROAD',NLEV_IMPROAD);
ncwrite(FCLM4OUT,'PCT_URBAN',PCT_URBAN);
ncwrite(FCLM4OUT,'THICK_ROOF',THICK_ROOF);
ncwrite(FCLM4OUT,'THICK_WALL',THICK_WALL);
ncwrite(FCLM4OUT,'T_BUILDING_MIN',T_BUILDING_MIN);
ncwrite(FCLM4OUT,'WIND_HGT_CANYON',WIND_HGT_CANYON);
ncwrite(FCLM4OUT,'WTLUNIT_ROOF',WTLUNIT_ROOF);
ncwrite(FCLM4OUT,'WTROAD_PERV',WTROAD_PERV);
ncwrite(FCLM4OUT,'TK_ROOF',TK_ROOF);
ncwrite(FCLM4OUT,'TK_WALL',TK_WALL);
ncwrite(FCLM4OUT,'TK_IMPROAD',TK_IMPROAD);
ncwrite(FCLM4OUT,'CV_ROOF',CV_ROOF);
ncwrite(FCLM4OUT,'CV_WALL',CV_WALL);
ncwrite(FCLM4OUT,'CV_IMPROAD',CV_IMPROAD);
ncwrite(FCLM4OUT,'EM_ROOF',EM_ROOF);
ncwrite(FCLM4OUT,'EM_WALL',EM_WALL);
ncwrite(FCLM4OUT,'EM_IMPROAD',EM_IMPROAD);
ncwrite(FCLM4OUT,'EM_PERROAD',EM_PERROAD);
ncwrite(FCLM4OUT,'ALB_ROOF',ALB_ROOF);
ncwrite(FCLM4OUT,'ALB_WALL',ALB_WALL);
ncwrite(FCLM4OUT,'ALB_IMPROAD',ALB_IMPROAD);
ncwrite(FCLM4OUT,'ALB_PERROAD',ALB_PERROAD);

