def calc_psf_scaling(pupil_grid, ndim, maxdim):
    """Calculate the input and output grid spacings.

    Args:
        pupil_grid: A RayGrid instance
        ndim: The sampling across the wavefront
        maxdim: The total width of the sampling grid

    Returns:
        delta_x: The linear grid spacing on the entrance pupil
        delta_xp: The linear grid spacing on the image plane
    """
    opt_model = pupil_grid.opt_model
    fod = opt_model['analysis_results']['parax_data'].fod
    wl = opt_model.nm_to_sys_units(pupil_grid.wvl)

    fill_factor = ndim/maxdim
    max_D = 2 * fod.enp_radius / fill_factor
    delta_x = max_D / maxdim
    C = wl/fod.exp_radius

    delta_theta = (fill_factor * C) / 2
    ref_sphere_radius = pupil_grid.fld.ref_sphere[2]
    delta_xp = delta_theta * ref_sphere_radius

    return delta_x, delta_xp