def read_projection_from_fits(fitsfile, extname=None):
    """
    Load a WCS or HPX projection.
    """
    f = fits.open(fitsfile)
    nhdu = len(f)
    # Try and get the energy bounds
    try:
        ebins = find_and_read_ebins(f)
    except:
        ebins = None

    if extname is None:
        # If there is an image in the Primary HDU we can return a WCS-based
        # projection
        if f[0].header['NAXIS'] != 0:
            proj = WCS(f[0].header)
            return proj, f, f[0]
    else:
        if f[extname].header['XTENSION'] == 'IMAGE':
            proj = WCS(f[extname].header)
            return proj, f, f[extname]
        elif extname in ['SKYMAP', 'SKYMAP2']:
            proj = HPX.create_from_hdu(f[extname], ebins)
            return proj, f, f[extname]
        elif f[extname].header['XTENSION'] == 'BINTABLE':
            try:
                if f[extname].header['PIXTYPE'] == 'HEALPIX':
                    proj = HPX.create_from_hdu(f[extname], ebins)
                    return proj, f, f[extname]
            except:
                pass
        return None, f, None

    # Loop on HDU and look for either an image or a table with HEALPix data
    for i in range(1, nhdu):
        # if there is an image we can return a WCS-based projection
        if f[i].header['XTENSION'] == 'IMAGE':
            proj = WCS(f[i].header)
            return proj, f, f[i]
        elif f[i].header['XTENSION'] == 'BINTABLE':
            if f[i].name in ['SKYMAP', 'SKYMAP2']:
                proj = HPX.create_from_hdu(f[i], ebins)
                return proj, f, f[i]
            try:
                if f[i].header['PIXTYPE'] == 'HEALPIX':
                    proj = HPX.create_from_hdu(f[i], ebins)
                    return proj, f, f[i]
            except:
                pass

    return None, f, None
Load a WCS or HPX projection.