diff --git a/nuztf/ampel_api.py b/nuztf/ampel_api.py index 8be15a6..805f35f 100644 --- a/nuztf/ampel_api.py +++ b/nuztf/ampel_api.py @@ -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, diff --git a/nuztf/skymap.py b/nuztf/skymap.py index 6db0a26..0a5c16d 100644 --- a/nuztf/skymap.py +++ b/nuztf/skymap.py @@ -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)) diff --git a/pyproject.toml b/pyproject.toml index 773b2bd..ee473dc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"