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 timezone and observing night calculations #121

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Add calculate_observing_night
  • Loading branch information
moeyensj committed Oct 8, 2024
commit 38a14ffb613d80c1f16a4c20919b76d01ebc8c09
1 change: 1 addition & 0 deletions src/adam_core/observers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# flake8: noqa: F401
from .observers import Observers
from .state import get_observer_state
from .utils import calculate_observing_night

__all__ = ["Observers", "get_observer_state"]
104 changes: 104 additions & 0 deletions src/adam_core/observers/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import pyarrow as pa
import pyarrow.compute as pc

from ...time import Timestamp
from ..utils import calculate_observing_night


def test_calculate_observing_night() -> None:
# Rubin Observatory is UTC-7
# Reasonable observating times would be +- 12 hours from local midnight
# or 7:00 to 17:00 UTC

# ZTF is UTC-8
# Reasonable observating times would be +- 12 hours from local midnight
# or 8:00 to 16:00 UTC

# M22 is UTC+2
# Reasonable observating times would be +- 12 hours from local midnight
# or 22:00 to 10:00 UTC

# 000 is UTC
# Reasonable observating times would be +- 12 hours from local midnight
# or 0:00 to 12:00 UTC

codes = pa.array(
[
"X05",
"X05",
"X05",
"X05",
"X05",
"I41",
"I41",
"I41",
"I41",
"I41",
"M22",
"M22",
"M22",
"M22",
"M22",
"000",
"000",
"000",
"000",
"000",
]
)
times_utc = Timestamp.from_mjd(
[
# Rubin Observatory
59000 + 7 / 24 - 12 / 24,
59000 + 7 / 24 - 6 / 24,
59000 + 7 / 24,
59000 + 7 / 24 + 6 / 24,
59000 + 7 / 24 + 12 / 24,
# ZTF
59000 + 8 / 24 - 12 / 24,
59000 + 8 / 24 - 4 / 24,
59000 + 8 / 24,
59000 + 8 / 24 + 4 / 24,
59000 + 8 / 24 + 12 / 24,
# M22
59000 - 2 / 24 - 12 / 24,
59000 - 2 / 24 - 6 / 24,
59000 - 2 / 24,
59000 - 2 / 24 + 6 / 24,
59000 - 2 / 24 + 12 / 24,
# 000
59000 - 12 / 24,
59000 - 6 / 24,
59000,
59000 + 6 / 24,
59000 + 12 / 24,
],
scale="utc",
)

observing_night = calculate_observing_night(codes, times_utc)
desired = pa.array(
[
58999,
58999,
58999,
58999,
59000,
58999,
58999,
58999,
58999,
59000,
58999,
58999,
58999,
58999,
59000,
58999,
58999,
58999,
58999,
59000,
]
)
assert pc.all(pc.equal(observing_night, desired)).as_py()
57 changes: 57 additions & 0 deletions src/adam_core/observers/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from datetime import timedelta
from zoneinfo import ZoneInfo

import numpy as np
import pyarrow as pa
import pyarrow.compute as pc
from astropy.time import Time

from ..time import Timestamp
from .observers import OBSERVATORY_PARALLAX_COEFFICIENTS


def calculate_observing_night(codes: pa.Array, times: Timestamp) -> pa.Array:
"""
Compute the observing night for a given set of observatory codes and times. An observing night is defined as the night
during which the observation is made, in the local time of the observatory +- 12 hours. The observing night is defined
as the integer MJD of the night in the local time of the observatory.

Parameters
----------
codes : pyarrow.Array (N)
An array of observatory codes.
times : Timestamp (N)
An array of times.

Returns
-------
observing_night : pyarrow.Array (N)
An array of observing nights.
"""
half_day = timedelta(hours=12)
observing_night = np.empty(len(times), dtype=np.int64)

for code in pc.unique(codes):

parallax_coefficients = OBSERVATORY_PARALLAX_COEFFICIENTS.select("code", code)
if len(parallax_coefficients) == 0:
raise ValueError(f"Unknown observatory code: {code}")

tz = ZoneInfo(parallax_coefficients.timezone()[0])

mask = pc.equal(codes, code)
times_code = times.apply_mask(mask)

observing_night_code = Time(
[
time + time.astimezone(tz).utcoffset() - half_day
for time in times_code.to_astropy().datetime
],
format="datetime",
scale="utc",
)
observing_night_code = observing_night_code.mjd.astype(int)

observing_night[mask.to_numpy(zero_copy_only=False)] = observing_night_code

return pa.array(observing_night)
Loading