clear all 

FNAME='/cluster/shared/noresm/inputdata/lnd/clm2/surfdata/surfdata_1.9x2.5_simyr1850_c110114_CMIP6.nc';

PCT_NAT_PFT=ncread(FNAME,'PCT_NAT_PFT');
PCT_CFT=ncread(FNAME,'PCT_CFT');
PCT_NATVEG=ncread(FNAME,'PCT_NATVEG');
PCT_CROP=ncread(FNAME,'PCT_CROP');

PCT_SAND=ncread(FNAME,'PCT_SAND');
PCT_CLAY=ncread(FNAME,'PCT_CLAY'); % use old PCT_URBAN and scale the rest such that PCTs sum up to 100%; use CLM5 (100-PCT_URBAN) for scaling 
PCT_WETLAND=ncread(FNAME,'PCT_WETLAND');
PCT_LAKE=ncread(FNAME,'PCT_LAKE');
PCT_GLACIER=ncread(FNAME,'PCT_GLACIER');
PCT_URBAN=ncread(FNAME,'PCT_URBAN');


PCT_CFT,PCT_CLAY,PCT_CROP,PCT_GLACIER,PCT_GLC_GIC,PCT_GLC_ICESHEET,PCT_GLC_MEC,PCT_GLC_MEC_GIC,PCT_GLC_MEC_ICESHEET,PCT_LAKE,PCT_NATVEG,PCT_NAT_PFT,PCT_SAND,PCT_WETLAND

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_PFT=cat(3,PCT_NAT_PFT,PCT_CFT); 

ncwrite(FNAME,'PCT_PFT',PCT_PFT);
ncwrite(FNAME,'PCT_URBAN',sum(PCT_URBAN,3));

