Skip to content

Commit

Permalink
WIP: negative fluxes when importing ZTF alert photometry (#555)
Browse files Browse the repository at this point in the history
* convert ZTF alert photometry (from Kowalski) to flux before saving to the database, so we can use the `isdiffpos` to save negative fluxes as such.
  • Loading branch information
Theodlz authored Jul 10, 2024
1 parent e25faf4 commit 55cddab
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions extensions/skyportal/skyportal/handlers/api/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ def post_alert(
"_id": 1,
"prv_candidates.magpsf": 1,
"prv_candidates.sigmapsf": 1,
"prv_candidates.isdiffpos": 1,
"prv_candidates.diffmaglim": 1,
"prv_candidates.programid": 1,
"prv_candidates.fid": 1,
Expand Down Expand Up @@ -430,13 +431,14 @@ def post_alert(

# post photometry
ztf_filters = {1: "ztfg", 2: "ztfr", 3: "ztfi"}
df["ztf_filter"] = df["fid"].apply(lambda x: ztf_filters[x])
df["magsys"] = "ab"
df["filter"] = df["fid"].apply(lambda x: ztf_filters[x])
df["mjd"] = df["jd"] - 2400000.5

df["mjd"] = df["mjd"].apply(lambda x: np.float64(x))
df["magpsf"] = df["magpsf"].apply(lambda x: np.float32(x))
df["sigmapsf"] = df["sigmapsf"].apply(lambda x: np.float32(x))
df["zp"] = 23.9
df["magsys"] = "ab"

# deduplicate
df = (
Expand Down Expand Up @@ -475,6 +477,39 @@ def post_alert(
)
df = df.loc[mask_good_stream_id]

# filter out bad data:
mask_good_diffmaglim = df["diffmaglim"] > 0
df = df.loc[mask_good_diffmaglim]

# convert from mag to flux

# step 1: calculate the coefficient that determines whether the
# flux should be negative or positive
coeff = df["isdiffpos"].apply(
lambda x: 1.0 if x in [True, 1, "y", "Y", "t", "1", "T"] else -1.0
)

# step 2: calculate the flux normalized to an arbitrary AB zeropoint of
# 23.9 (results in flux in uJy)
df["flux"] = coeff * 10 ** (-0.4 * (df["magpsf"] - 23.9))

# step 3: separate detections from non detections
detected = np.isfinite(df["magpsf"])
undetected = ~detected

# step 4: calculate the flux error
df["fluxerr"] = None # initialize the column

# step 4a: calculate fluxerr for detections using sigmapsf
df.loc[detected, "fluxerr"] = np.abs(
df.loc[detected, "sigmapsf"] * df.loc[detected, "flux"] * np.log(10) / 2.5
)

# step 4b: calculate fluxerr for non detections using diffmaglim
df.loc[undetected, "fluxerr"] = (
10 ** (-0.4 * (df.loc[undetected, "diffmaglim"] - 23.9)) / 5.0
) # as diffmaglim is the 5-sigma depth

# post the photometry in a loop for each different stream_id
# as the add_external_photometry function uses the same stream_ids for all the datapoints
for stream_id in df["stream_ids"].unique():
Expand All @@ -486,11 +521,11 @@ def post_alert(
"obj_id": obj_id_to_save,
"instrument_id": instrument.id,
"mjd": df_stream["mjd"].tolist(),
"mag": df_stream["magpsf"].tolist(),
"magerr": df_stream["sigmapsf"].tolist(),
"limiting_mag": df_stream["diffmaglim"].tolist(),
"flux": df_stream["flux"].tolist(),
"fluxerr": df_stream["fluxerr"].tolist(),
"filter": df_stream["filter"].tolist(),
"zp": df_stream["zp"].tolist(),
"magsys": df_stream["magsys"].tolist(),
"filter": df_stream["ztf_filter"].tolist(),
"ra": df_stream["ra"].tolist(),
"dec": df_stream["dec"].tolist(),
"stream_ids": df_stream["stream_ids"].tolist(),
Expand Down

0 comments on commit 55cddab

Please sign in to comment.