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

Implement range to moc function #45

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 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
32 changes: 32 additions & 0 deletions healpix_alchemy/tests/test_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import numpy as np
from mocpy import MOC
import itertools
from astropy_healpix import HEALPix
from astropy.coordinates import Angle, Longitude, Latitude
import astropy.units as u

from .. import types


def to_ranges(iterable):
iterable = sorted(set(iterable))
for key, group in itertools.groupby(enumerate(iterable),
lambda t: t[1] - t[0]):
group = list(group)
yield group[0][1], group[-1][1]+1


def test_to_moc():
depth = 10
rng = np.random.default_rng()
lon = Longitude(360 * rng.random() * u.deg)
lat = Latitude((180 * rng.random() - 90) * u.deg)
radius = Angle(rng.normal(loc=10, scale=2.5) * u.deg)
hp = HEALPix(nside=2**depth, order='nested')
healpix_list = hp.cone_search_lonlat(lon, lat, radius)
depth_list = depth * np.ones(np.shape(healpix_list))
moc = MOC.from_healpix_cells(healpix_list, depth_list)
nested_hpx_ranges = [item for item in to_ranges(healpix_list)]
lpsinger marked this conversation as resolved.
Show resolved Hide resolved

assert types.Point.to_moc(rangeSet=nested_hpx_ranges,
nside=2**depth) == moc
7 changes: 7 additions & 0 deletions healpix_alchemy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ def process_bind_param(self, value, dialect):
value = int(value)
return value

@classmethod
def to_moc(cls, rangeSet, nside):
lpsinger marked this conversation as resolved.
Show resolved Hide resolved
depth = int(np.log2(nside))
MOCstr = f'{depth}/' + ' '.join(map(lambda x: f'{x[0]}-{x[1]-1}',
rangeSet))
return MOC.from_string(MOCstr)


class Tile(sa.TypeDecorator):

Expand Down