Skip to content

Commit

Permalink
make cutouts v3 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
simeonreusch committed Dec 18, 2022
1 parent 4ac149b commit 2d80731
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
12 changes: 6 additions & 6 deletions nuztf/ampel_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@
from base64 import b64encode
from json import JSONDecodeError
import numpy as np
from astropy.time import Time
from astropy.io import fits
from astropy.time import Time # type: ignore
from astropy.io import fits # type: ignore
from requests.auth import HTTPBasicAuth
from nuztf.credentials import load_credentials

# AMPEL API URLs

API_BASEURL = "https://ampel.zeuthen.desy.de"
API_ZTF_ARCHIVE_URL = API_BASEURL + "/api/ztf/archive/v2"
API_CATALOGMATCH_URL = API_BASEURL + "/api/catalogmatch"
API_CUTOUT_URL = API_BASEURL + "/api/ztf/archive/v2/cutouts"

_, ampel_api_archive_token = load_credentials("ampel_api_archive_token")

Expand Down Expand Up @@ -516,7 +513,10 @@ def ampel_api_cutout(candid: int, logger=None):
if logger is None:
logger = logging.getLogger(__name__)

queryurl_cutouts = API_CUTOUT_URL + f"/{candid}"
if "v2" in API_ZTF_ARCHIVE_URL:
queryurl_cutouts = API_ZTF_ARCHIVE_URL + f"/cutouts/{candid}"
else:
queryurl_cutouts = API_ZTF_ARCHIVE_URL + f"/alert/{candid}/cutouts"

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

Expand Down
19 changes: 14 additions & 5 deletions nuztf/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def lightcurve_from_alert(
[cutoutsci, cutouttemp, cutoutdiff],
["Science", "Template", "Difference"],
):
create_stamp_plot(alert=cutout_, ax=ax_, type=type_)
create_stamp_plot(alert=cutout_, ax=ax_, cutout_type=type_)

img = get_ps_stamp(
candidate["ra"], candidate["dec"], size=240, color=["y", "g", "i"]
Expand Down Expand Up @@ -297,12 +297,21 @@ def absmag_to_mag(absmag):
return fig, axes


def create_stamp_plot(alert: dict, ax, type: str):
def create_stamp_plot(alert: dict, ax, cutout_type: str):
"""Helper function to create cutout subplot"""
v3_cutout_names = {
"Science": "Cutoutscience",
"Template": "Cutouttemplate",
"Difference": "Cutoutdifference",
}

if alert.get(f"cutout{cutout_type}") is None:
cutout_type = v3_cutout_names[cutout_type]
data = alert[f"cutout{cutout_type}"]["stampData"]["stampData"]
else:
data = alert[f"cutout{cutout_type}"]["stampData"]

with gzip.open(
io.BytesIO(b64decode(alert[f"cutout{type}"]["stampData"])), "rb"
) as f:
with gzip.open(io.BytesIO(b64decode(data)), "rb") as f:
data = fits.open(io.BytesIO(f.read()), ignore_missing_simple=True)[0].data
vmin, vmax = np.percentile(data[data == data], [0, 100])
data_ = visualization.AsinhStretch()((data - vmin) / (vmax - vmin))
Expand Down

0 comments on commit 2d80731

Please sign in to comment.