function fix_HYDRO1K_map()

% the original file
fname_inp = '/global/cfs/cdirs/e3sm/inputdata/lnd/clm2/mappingdata/maps/2026-incite-conus-1024x2-pg2/map_1km-merge-10min_HYDRO1K-merge-nomask_to_2026-incite-conus-1024x2-pg2_nomask_aave_da_c251211.nc';

% read data from the original file
row = double(ncread(fname_inp,'row'));
col = double(ncread(fname_inp,'col'));
S = ncread(fname_inp,'S');

% create the map
map = sparse(row, col, S);

% compute cummulative weights
cum_wts = sum(map,2);

tol = 1.e-4; % Tolerance used in mkgridmapMod.F90

% find rows for which cummulative weight exceeds tolerance
loc_row = find(cum_wts > 1.0 + tol);

% rescale weights for the problematic destination cells
for ii = 1:length(loc_row)
    disp(num2str(ii))
    r = loc_row(ii);
    idx = find(row == r);
    S(idx) = S(idx)/sum(S(idx));
end

% write the new weights (S) in an updated file
fname_out = '/global/cfs/cdirs/e3sm/inputdata/lnd/clm2/mappingdata/maps/2026-incite-conus-1024x2-pg2/map_1km-merge-10min_HYDRO1K-merge-nomask_to_2026-incite-conus-1024x2-pg2_nomask_aave_da_c251211.modified.nc';
ncwrite(fname_out,'S',S);

% also, fix 'frac_b'
frac_b = ncread(fname_inp, 'frac_b');
frac_b(loc_row) = 1.0;
ncwrite(fname_out,'frac_b',frac_b);

