Skip to content

Commit

Permalink
Regions
Browse files Browse the repository at this point in the history
  • Loading branch information
lpsinger committed Nov 10, 2022
1 parent a3120c6 commit 22be0af
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 16 deletions.
40 changes: 25 additions & 15 deletions healpix_alchemy/tests/benchmarks/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
"""
import io
import functools

from astropy.coordinates import SkyCoord, uniform_spherical_random_surface
from astropy import units as u
from mocpy import MOC
import numpy as np
import pytest
from regions import Regions

from ...constants import HPX, LEVEL, PIXEL_AREA
from ...types import Tile
Expand All @@ -25,19 +27,18 @@


def get_ztf_footprint_corners():
"""Return the corner offsets of the ZTF footprint.
Notes
-----
This polygon is smaller than the spatial extent of the true ZTF field of
view, but has approximately the same area because the real ZTF field of
view has chip gaps.
"""Return the corner offsets of the ZTF footprint."""
url = '/Users/lpsinger/src/skyportal/data/ZTF_Region.reg'
regions = Regions.read(url, cache=True)

vertices = SkyCoord(
[region.vertices.ra for region in regions],
[region.vertices.dec for region in regions]
).transform_to(
SkyCoord(0 * u.deg, 0 * u.deg).skyoffset_frame()
)

For the real ZTF footprint, use the region file
https://github.com/skyportal/skyportal/blob/main/data/ZTF_Region.reg.
"""
x = 6.86 / 2
return [-x, +x, +x, -x] * u.deg, [-x, -x, +x, +x] * u.deg
return vertices.lon, vertices.lat


def get_footprints_grid(lon, lat, offsets):
Expand All @@ -62,7 +63,7 @@ def get_footprints_grid(lon, lat, offsets):
"""
lon = np.repeat(lon[np.newaxis, :], len(offsets), axis=0)
lat = np.repeat(lat[np.newaxis, :], len(offsets), axis=0)
result = SkyCoord(lon, lat, frame=offsets[:, np.newaxis].skyoffset_frame())
result = SkyCoord(lon, lat, frame=offsets.reshape((-1,) + (1,) * (lon.ndim - 1)).skyoffset_frame())
return result.icrs


Expand All @@ -82,10 +83,19 @@ def get_random_galaxies(n, cursor):
return points


def union_moc_func(a, b):
return a.union(b)


def get_union_moc(footprints):
mocs = (MOC.from_polygon_skycoord(footprint) for footprint in footprints)
return functools.reduce(union_moc_func, mocs)


def get_random_fields(n, cursor):
centers = SkyCoord(get_random_points(n, RANDOM_FIELDS_SEED))
footprints = get_footprints_grid(*get_ztf_footprint_corners(), centers)
mocs = [MOC.from_polygon_skycoord(footprint) for footprint in footprints]
mocs = [get_union_moc(footprint) for footprint in footprints]

f = io.StringIO('\n'.join(f'{i}' for i in range(len(mocs))))
cursor.copy_from(f, Field.__tablename__)
Expand All @@ -103,7 +113,7 @@ def get_random_fields(n, cursor):

def get_random_sky_map(n, cursor):
rng = np.random.default_rng(RANDOM_SKY_MAP_SEED)
for skymap_id in range(1, 0, -1):
for skymap_id in range(10, 0, -1):
# Make a randomly subdivided sky map
npix = HPX.npix
tiles = np.arange(0, npix + 1, 4 ** LEVEL).tolist()
Expand Down
1 change: 1 addition & 0 deletions healpix_alchemy/tests/benchmarks/test_benchmarks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from functools import reduce
import time

import numpy as np
import sqlalchemy as sa
Expand Down
42 changes: 41 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pytest = "*"
pytest-benchmark = "*"
pytest-doctestplus = "*"
pytest-postgresql = "<4.0.0" # pytest-postgresql 4.0.0 uses psycopg3, but sqlalchemy does not yet support it
regions = ">=0.6"

[tool.pytest.ini_options]
addopts = "--doctest-glob *.md"
Expand Down

0 comments on commit 22be0af

Please sign in to comment.