Skip to content

Commit

Permalink
Add option to use Ampel API alerts endpoint (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertdstein authored Jul 31, 2024
1 parent 7dd120a commit e70fba9
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
57 changes: 57 additions & 0 deletions nuztf/ampel_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,63 @@ def ampel_api_lightcurve(
return query_res


@backoff.on_exception(
backoff.expo,
requests.exceptions.RequestException,
max_time=600,
)
def ampel_api_alerts(
ztf_name: str,
t_min_jd=Time("2017-01-01T00:00:00.0", format="isot", scale="utc").jd,
t_max_jd=Time.now().jd,
program_id: int = None,
logger=None,
) -> list:
"""
Function to query ampel via name, returns a virtual alert
constructed by AMPEL containing ALL photopoints and upper limits
"""

if logger is None:
logger = logging.getLogger(__name__)

if program_id is None:
queryurl_lightcurve = (
API_ZTF_ARCHIVE_URL + f"/object/{ztf_name}/alerts?jd_start={t_min_jd}&"
f"jd_end={t_max_jd}&with_history=true"
)
else:
queryurl_lightcurve = (
API_ZTF_ARCHIVE_URL + f"/object/{ztf_name}/alerts?jd_start={t_min_jd}&"
f"jd_end={t_max_jd}&programid={program_id}"
)

logger.debug(queryurl_lightcurve)

headers = {"Authorization": f"Bearer {ampel_api_archive_token}"}

response = requests.get(
queryurl_lightcurve,
headers=headers,
)

if response.status_code == 503:
if response.headers:
logger.debug(response.headers)
raise requests.exceptions.RequestException

try:
query_res = [response.json()]

except JSONDecodeError:
if response.headers:
logger.debug(response.headers)
raise requests.exceptions.RequestException

return query_res


@backoff.on_exception(
backoff.expo,
requests.exceptions.RequestException,
Expand Down
2 changes: 1 addition & 1 deletion nuztf/skymap.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(
self.skymap_path = SKYMAP_DIR.joinpath(basename)
self.rev = None

if event[:8] == "https://":
if (event[:8] == "https://") | (event[:7] == "http://"):
if not self.skymap_path.exists():
self.logger.info(f"Downloading from: {event}")
wget.download(event, str(self.skymap_path))
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ gunicorn = {version = ">=20.1,<23.0", optional = true}
slackeventsapi = {version = "^3.0.1", optional = true}



[tool.poetry.dev-dependencies]
black = "^24.4.2"
coverage = "^6.0"
Expand Down

0 comments on commit e70fba9

Please sign in to comment.