Skip to content

Commit

Permalink
Fix logging (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
simeonreusch authored Jun 1, 2023
1 parent 4e51c72 commit 19c6a82
Showing 1 changed file with 67 additions and 55 deletions.
122 changes: 67 additions & 55 deletions nuztf/ampel_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,79 +468,91 @@ def ampel_api_skymap(
logger=None,
) -> tuple:
"""
Function to query ampel based on a list of healpix pixels (nside is the pixelization degree)
Function to query ampel based on lists of healpix pixels of different resolution (nside is the respective resolution)
"""

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

if with_history:
hist = "true"
else:
hist = "false"

if with_cutouts:
cutouts = "true"
else:
cutouts = "false"
queryurl_skymap = API_ZTF_ARCHIVE_URL + f"/alerts/healpix/skymap"

# First, we create a json body to post
headers = {
"accept": "application/json",
"Content-Type": "application/json",
"Authorization": f"Bearer {ampel_api_archive_token}",
}
if "v2" in API_ZTF_ARCHIVE_URL:
lt = "lt"
gt = "gt"
else:
lt = "$lt"
gt = "$gt"

# Now we reduce the query size
regions = utils.deres(nside=nside, ipix=pixels)

n_pix = 0
for reg in regions:
n_pix += len(reg["pixels"])

logger.debug(f"This comprises {n_pix} individual pixels")

if n_pix > MAX_N_PIX:
logger.warning(
f"Total number of pixels exceeds threshold ({MAX_N_PIX} pixels). Issuing a query for the full sky instead."
)
regions = [{"nside": 1, "pixels": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]}]

query = {
"regions": regions,
"jd": {
lt: t_max_jd,
gt: t_min_jd,
},
"latest": "false",
"candidate": {
"rb": {"$gt": 0.3},
"magpsf": {"$gt": 15},
"ndethist": {"$gt": 0, "$lte": max_n_detections},
},
"with_history": hist,
"with_cutouts": cutouts,
"chunk_size": chunk_size,
}
# if we have a resume_token to proceed to the next chunk, that's all we need
if resume_token is not None:
# fake query (the only thing that counts is the resume token)
regions = [{"nside": 1, "pixels": [0]}]
query = {
"regions": regions,
"jd": {
"$lt": t_max_jd,
"$gt": t_min_jd,
},
"resume_token": resume_token,
}
response = requests.post(url=queryurl_skymap, json=query, headers=headers)

# if we don't have a resume_token, we first need to create the full query
else:
if with_history:
hist = "true"
else:
hist = "false"

if resume_token:
query["resume_token"] = resume_token
if with_cutouts:
cutouts = "true"
else:
cutouts = "false"

if program_id is not None:
query["programid"] = program_id
# Now we reduce the query size
regions = utils.deres(nside=nside, ipix=pixels)

queryurl_skymap = API_ZTF_ARCHIVE_URL + f"/alerts/healpix/skymap"
n_pix = 0
for reg in regions:
n_pix += len(reg["pixels"])

logger.debug(f"Query url:\n{queryurl_skymap}")
logger.debug(f"Query:\n{query}")
logger.debug(f"This comprises {n_pix} individual pixels")

response = requests.post(url=queryurl_skymap, json=query, headers=headers)
if n_pix > MAX_N_PIX:
logger.warning(
f"Total number of pixels exceeds threshold ({MAX_N_PIX} pixels). Issuing a query for the full sky instead."
)
regions = [
{"nside": 1, "pixels": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]}
]

query = {
"regions": regions,
"jd": {
"$lt": t_max_jd,
"$gt": t_min_jd,
},
"latest": "false",
"candidate": {
"rb": {"$gt": 0.3},
"magpsf": {"$gt": 15},
"ndethist": {"$gt": 0, "$lte": max_n_detections},
},
"with_history": hist,
"with_cutouts": cutouts,
"chunk_size": chunk_size,
}

if resume_token:
query["resume_token"] = resume_token

if program_id is not None:
query["programid"] = program_id

logger.debug(f"Query url:\n{queryurl_skymap}")
logger.debug(f"Query:\n{query}")

response = requests.post(url=queryurl_skymap, json=query, headers=headers)

logger.debug(response)
logger.debug(response.status_code)
Expand Down

0 comments on commit 19c6a82

Please sign in to comment.