Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function for HiPS to HEALPix conversion #132

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 56 additions & 3 deletions hips/draw/healpix.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from pathlib import Path
import numpy as np
from pathlib import Path
from typing import Union, List
from astropy.io import fits
from astropy_healpix import healpy as hp
import healpy as hp

from ..tiles import HipsTile, HipsTileMeta, HipsSurveyProperties
from ..utils.healpix import hips_tile_healpix_ipix_array

__all__ = [
'healpix_to_hips_tile',
'healpix_to_hips',
'hips_to_healpix'
]


def healpix_to_hips_tile(hpx_data, tile_width, tile_idx, file_format) -> HipsTile:
def healpix_to_hips_tile(hpx_data: np.ndarray, tile_width: int,
tile_idx: int, file_format: str) -> HipsTile:
"""Create single hips tile from healpix data given a tile index.

Parameters
Expand Down Expand Up @@ -55,7 +61,8 @@ def healpix_to_hips_tile(hpx_data, tile_width, tile_idx, file_format) -> HipsTil
return HipsTile.from_numpy(meta=meta, data=data)


def healpix_to_hips(hpx_data, tile_width, base_path, file_format='fits'):
def healpix_to_hips(hpx_data: np.ndarray, tile_width: int,
base_path: Union[str, Path], file_format='fits') -> None:
"""Convert HEALPix image to HiPS.

Parameters
Expand Down Expand Up @@ -88,3 +95,49 @@ def healpix_to_hips(hpx_data, tile_width, base_path, file_format='fits'):

properties = HipsSurveyProperties(data=data)
properties.write(base_path / 'properties')

def hips_to_healpix(hips_url: str, hips_tiles: List[HipsTile], healpix_pixels: np.ndarray) -> np.ndarray:
"""Given a HiPS survey, generate a HEALPix map.

Parameters
----------
hips_tiles : List[HipsTile]
List of HiPS tiles.
hips_url : str
URL of HiPS survey.
healpix_pixels : `~numpy.ndarray`
HEALPix pixel numbers.

Returns
-------
healpix_map : `~numpy.ndarray`
HEALPix map.
"""
# Query the HiPS survey properties.
hips_properties = HipsSurveyProperties.fetch(hips_url + "/properties")

hips_tile_width = hips_properties.tile_width
order = int(np.log2(hips_tile_width))

# xy_to_healpix = hips_tile_healpix_ipix_array(shift_order=order).flatten()

healpix_map = np.empty(hp.nside2npix(2 ** 12), dtype=np.double) # 2 ** 12?
healpix_map[:] = hp.pixelfunc.UNSEEN

for index, tile in enumerate(hips_tiles):
has_blank_value = False

if 'BLANK' in tile.data:
has_blank_value = True
blank_value_scaled = bscale * tile[0].header['BLANK'] + bzero

# healpix_index = xy_to_healpix[index]
pixel_value = tile.data

if has_blank_value and np.fabs(pixel_value - blank_value_scaled) < 1e-6:
pixel_value = hp.pixelfunc.UNSEEN

healpix_map_range = index * (hips_tile_width ** 2)
healpix_map[healpix_map_range: healpix_map_range + tile.meta.width] = pixel_value[0]

return healpix_map
22 changes: 20 additions & 2 deletions hips/draw/tests/test_healpix.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import numpy as np
from PIL import Image
from astropy.io import fits
from numpy.testing import assert_allclose
from astropy_healpix import healpy as hp
from ..healpix import healpix_to_hips
from numpy.testing import assert_allclose

from ..healpix import healpix_to_hips, healpix_to_hips_tile
from ..healpix import hips_to_healpix


@pytest.mark.parametrize('file_format', ['fits', 'png'])
Expand Down Expand Up @@ -36,3 +38,19 @@ def test_healpix_to_hips(tmpdir, file_format):

properties = (tmpdir / 'properties').read_text(encoding=None)
assert file_format in properties

def test_hips_to_healpix(tmpdir):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer a simple test that doesn't use real data for two reasons:

  • Hitting the network and disk is slow, and for the network error-prone
  • Your test should end with an assert value on one or a few pixel values; if you take real data, it's not clear from looking at the test if the code works, you get an arbitrary value. If you set up a test case where you see what goes in from reading the test code, it's clear what should come out.

nside, tile_width = 4, 2
npix = hp.nside2npix(nside)
healpix_data = np.arange(npix, dtype='uint8')

n_tiles = healpix_data.size // tile_width ** 2

tiles = []
for tile_idx in range(n_tiles):
tiles.append(healpix_to_hips_tile(hpx_data=healpix_data, tile_width=tile_width,
tile_idx=tile_idx, file_format='fits'))

healpix_map = hips_to_healpix(hips_url='http://alasky.u-strasbg.fr/Pan-STARRS/DR1/g',
hips_tiles=tiles,
healpix_pixels=healpix_data)