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

Support HEALpix-indexed AGASC HDF5 files and more #155

Merged
merged 34 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a518a2b
Initial commit to support HEALpix-indexed AGASC HDF5 files
taldcroft Sep 12, 2023
799e19f
Fix typo
taldcroft Sep 12, 2023
f9e2d23
Minimal modernization of create_mini_agasc_h5.py
taldcroft Sep 12, 2023
0be1563
Refactor and modernize
taldcroft Sep 13, 2023
e9fef74
Apply black formatting
taldcroft Sep 13, 2023
f64aebd
Apply black to create_near_neighbor_ids
taldcroft Sep 13, 2023
45b1533
Improvements to miniagasc creation helper scripts
taldcroft Sep 13, 2023
cbccf43
Support for caching and columns in get_agasc_cone
taldcroft Sep 18, 2023
0805567
Working get_agasc_cone for healpix
taldcroft Sep 18, 2023
c90685b
Remove columns as an option since slows performance
taldcroft Sep 18, 2023
cad21fe
Fix a couple of issues in create_near_neighbor_ids
taldcroft Sep 18, 2023
07b069b
Change epoch for computing near-neighbor to 2024.0
taldcroft Sep 18, 2023
fe9b554
Some fixes / improvements in file creation scripts
taldcroft Sep 19, 2023
065ed43
Implement new convention for agasc_file kwarg
taldcroft Sep 19, 2023
601fafa
Doc fixes
taldcroft Sep 19, 2023
3d11988
Fix tests
taldcroft Sep 19, 2023
9744866
Rework the file selection code
taldcroft Sep 22, 2023
238454c
Update filename resolution for latest spec
taldcroft Sep 23, 2023
df155b2
Update conf.py for numpydoc and autodoc typehints
taldcroft Sep 23, 2023
145567c
Flake8
taldcroft Sep 23, 2023
6176521
Add test of get_agasc_filename and fix bug in that function
taldcroft Sep 25, 2023
be7145e
Require either .h5 or * at end of `agasc_file` + other fixes
taldcroft Sep 26, 2023
350d91f
Rename and overhaul create_mini_agasc_h5
taldcroft Sep 26, 2023
a5d38c5
Add option to allow RC files for testing
taldcroft Sep 27, 2023
8ea5a53
Add version kwarg and fix tests
taldcroft Sep 27, 2023
3bfa5a2
Test get_agasc_cone, get_star, get_stars for HEALpix
taldcroft Sep 27, 2023
e314a63
Flake8
taldcroft Sep 28, 2023
743285d
Add new dev script for profiling
taldcroft Sep 29, 2023
ca4613b
Reduce memory from supplement
taldcroft Sep 30, 2023
775e156
Add dev scripts for memory profiling
taldcroft Oct 1, 2023
4a5f473
Fix name
taldcroft Oct 1, 2023
0194306
Add documentation on HEALpix ordering
taldcroft Oct 1, 2023
476e183
Add agasc_file to stars meta
taldcroft Oct 3, 2023
c41fe20
Add dev script to profile caching performance
taldcroft Oct 3, 2023
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
1 change: 0 additions & 1 deletion agasc/healpix.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import functools
from pathlib import Path
from typing import Optional

import astropy.units as u
import astropy_healpix as hpx
Expand Down
57 changes: 44 additions & 13 deletions create_mini_agasc_h5.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@

The output can be either ``miniagasc_<version>.h5`` or ``proseco_agasc_<version>.h5``.

The mini AGASC file is a subset of the full AGASC file that contains only the stars
satisfying ``stars["MAG_ACA"] - 3.0 * stars["MAG_ACA_ERR"] / 100.0 < 11.5``.

The proseco AGASC file is a subset of the mini AGASC file that contains only the columns
jeanconn marked this conversation as resolved.
Show resolved Hide resolved
needed for proseco. It also includes the near-neighbor stars (which are close to a
candidate guide or acq star) that got cut by the magnitude filter.

The script depends on the file ``agasc<version>_near_neighbor_ids.fits.gz`` which is
created using ``create_near_neighbor_ids.py``.

Since version 1.8 the mini AGASC file is sorted by HEALPix index and includes a
``healpix_index`` table.

Examples
--------

Expand All @@ -16,7 +29,9 @@
$ python create_mini_agasc_h5.py --version 1.8 --proseco
"""
import argparse
from pathlib import Path

import astropy.units as u
import numpy as np
import tables
from astropy.table import Table
Expand All @@ -25,7 +40,7 @@


def get_parser():
parser = argparse.ArgumentParser()
parser = argparse.ArgumentParser(description="Create mini AGASC HDF5 file")
parser.add_argument(
"--version",
type=str,
Expand All @@ -40,13 +55,22 @@ def get_parser():
parser.add_argument(
"--proseco",
action="store_true",
help="Create proseco_agasc (this implies --include-near-neighbors)",
help=(
"Create `proseco_agasc_<version>.h5` instead of `miniagasc_<version>.h5`. "
"This option implies --include-near-neighbors."
),
)
parser.add_argument(
"--healpix",
action="store_true",
help="Create AGASC file using HEALpix ordering with a healpix_index table",
)
parser.add_argument(
"--nside",
type=int,
default=64,
help="HEALPix nside parameter (default=64)",
)
return parser


Expand All @@ -59,28 +83,33 @@ def main():
rootname = "proseco_agasc" if args.proseco else "miniagasc"

filename_full = f"agasc{version}.h5"
filename_mini = f"{rootname}_{args.version}.h5"
filename_mini = f"{rootname}_{version}.h5"

stars = get_mini_agasc_stars(
filename_full,
version,
proseco=args.proseco,
include_near_neighbors=args.include_near_neighbors or args.proseco,
)

if args.proseco:
stars = filter_proseco_stars(stars)
args.healpix = True

if args.healpix:
print("Creating healpix_index table and sorting by healpix index")
healpix_index, idx_sort = get_healpix_index_table(stars)
print(
f"Creating healpix_index table for nside={args.nside} "
"and sorting by healpix index"
)
healpix_index, idx_sort = get_healpix_index_table(stars, args.nside)
else:
print("Sorting on DEC column")
idx_sort = np.argsort(stars["DEC"])
stars = stars.take(idx_sort)

write_mini_agasc(args, version_num, stars)
write_mini_agasc(filename_mini, stars, version)
if args.healpix:
write_healpix_index_table(filename_mini, healpix_index)
write_healpix_index_table(filename_mini, healpix_index, args.nside)


def write_healpix_index_table(filename: str, healpix_index: Table, nside: int):
Expand Down Expand Up @@ -139,17 +168,16 @@ def filter_proseco_stars(stars):
# fmt: on

names = [name for name in stars.dtype.names if name not in excludes]
print("Dtype before excluding:\n", stars.dtype)
stars = Table({name: stars[name] for name in names}, copy=False)
stars = stars.as_array()
print("Dtype after excluding:\n", stars.dtype)

return stars


def get_mini_agasc_stars(
filename: str,
agasc_full: str,
version_str: str,
proseco: bool,
include_near_neighbors: bool,
) -> np.ndarray:
"""
Expand All @@ -164,6 +192,8 @@ def get_mini_agasc_stars(
The path to the file containing the full AGASC data.
version_str : str
The version string to use for the near-neighbor file.
proseco : bool
If True, create proseco_agasc_<version>.h5 instead of miniagasc_<version>.h5.
include_near_neighbors : bool
If True, include the near-neighbor stars that got cut by the magnitude filter.

Expand All @@ -172,9 +202,9 @@ def get_mini_agasc_stars(
np.ndarray
The resulting array of miniagasc stars.
"""
print(f"Reading full AGASC {filename} and selecting useable stars")
print(f"Reading full AGASC {agasc_full} and selecting useable stars")

with tables.open_file(filename) as h5:
with tables.open_file(agasc_full) as h5:
stars = h5.root.data[:]

# Filter mags
Expand All @@ -183,7 +213,8 @@ def get_mini_agasc_stars(
# Put back near-neighbor stars that got cut by above mag filter. This file
# is made with create_near_neighbor_ids.py.
if proseco or include_near_neighbors:
near_file = f"near_neighbor_ids_{version_str}.fits.gz"
agasc_full_name = Path(agasc_full).with_suffix("").name
near_file = f"{agasc_full_name}_near_neighbor_ids.fits.gz"
near_table = Table.read(near_file, format="fits")
near_ids = set(near_table["near_id"])
print(f"Including {len(near_ids)} near neighbor stars")
Expand Down
31 changes: 22 additions & 9 deletions create_near_neighbor_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
$ python make_near_neighbor_ids.py --version=1p7
"""
import argparse
import os
from pathlib import Path

import tables
Expand All @@ -26,10 +25,21 @@
def get_parser():
parser = argparse.ArgumentParser()
parser.add_argument(
"--version",
"agasc_full",
type=str,
required=True,
help="Version (e.g. 1.7 or 1.8rc3)",
help="Input file full AGASC (e.g. ~/ska/data/agasc/agasc1p7.h5))",
)
parser.add_argument(
"--outdir",
type=str,
default=".",
help="Output directory (default='.')",
)
parser.add_argument(
"--max-rows",
type=int,
default=None,
help="Max rows to process (default=None)",
)
return parser

Expand All @@ -38,10 +48,7 @@ def main():
parser = get_parser()
args = parser.parse_args()

version = args.version

agasc_full = str(Path(os.environ["SKA"], "data", "agasc", f"agasc{version}.h5"))

agasc_full = Path(args.agasc_full).expanduser()
with tables.open_file(agasc_full) as h5:
stars = h5.root.data[:]

Expand All @@ -55,6 +62,8 @@ def main():

# Candidate acq/guide stars with a near neighbor that made ASPQ1 > 0
nears = stars[ok]
if args.max_rows is not None:
nears = nears[: args.max_rows]

radius = 60 / 3600
near_ids = set()
Expand All @@ -72,8 +81,12 @@ def main():
if id != sp["AGASC_ID"]:
near_ids.add(id)

outfile = (
Path(args.outdir) / f"{agasc_full.name[:-3]}_near_neighbor_ids.fits.gz"
)
t = Table([list(near_ids)], names=["near_id"])
t.write(f"near_neighbor_ids_{version}.fits.gz", format="fits", overwrite=True)
print(f"Writing {len(t)} near-neighbor IDs to {outfile}")
t.write(str(outfile), format="fits", overwrite=True)


if __name__ == "__main__":
Expand Down