def get_spatial_scale(wcs, assert_square=True):

    # Code adapted from APLpy

    wcs = wcs.sub([WCSSUB_CELESTIAL])
    cdelt = np.matrix(wcs.wcs.get_cdelt())
    pc = np.matrix(wcs.wcs.get_pc())
    scale = np.array(cdelt * pc)

    if assert_square:
        try:
            np.testing.assert_almost_equal(abs(cdelt[0,0]), abs(cdelt[0,1]))
            np.testing.assert_almost_equal(abs(pc[0,0]), abs(pc[1,1]))
            np.testing.assert_almost_equal(abs(scale[0,0]), abs(scale[0,1]))
        except __HOLE__:
            raise ValueError("Non-square pixels.  Please resample data.")

    return abs(scale[0,0]) * u.Unit(wcs.wcs.cunit[0])
dataset/ETHPy150Open glue-viz/glue/glue/external/pvextractor/utils/wcs_utils.py/get_spatial_scale