Skip to content

Commit

Permalink
Merge pull request #204 from astrophpeter/djones-host-fixes
Browse files Browse the repository at this point in the history
Djones host fixes
  • Loading branch information
djones1040 committed Apr 23, 2024
2 parents c9f7985 + 616fcbc commit 739a0aa
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 23 deletions.
19 changes: 12 additions & 7 deletions app/host/cutouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,24 @@ def download_and_save_cutouts(

if not cutout_object.exists():
cutout_object = Cutout(name=cutout_name, filter=filter, transient=transient)
cutout_exists = False
else:
cutout_object = cutout_object[0]
cutout_exists = True

fits = None
if (
not file_exists and cutout_object.message != "No image found"
(not file_exists or not cutout_exists)
and cutout_object.message != "No image found"
) or not overwrite == "False":
fits, status, err = cutout(transient.sky_coord, filter, fov=fov)
if fits:
save_dir = f"{media_root}/{transient.name}/{filter.survey.name}/"
os.makedirs(save_dir, exist_ok=True)
path_to_fits = save_dir + f"{filter.name}.fits"
fits.writeto(path_to_fits, overwrite=True)
if not file_exists and cutout_object.message != "No image found":
fits, status, err = cutout(transient.sky_coord, filter, fov=fov)

if fits:
save_dir = f"{media_root}/{transient.name}/{filter.survey.name}/"
os.makedirs(save_dir, exist_ok=True)
path_to_fits = save_dir + f"{filter.name}.fits"
fits.writeto(path_to_fits, overwrite=True)

# if there is data, save path to the file
# otherwise record that we searched and couldn't find anything
Expand Down
21 changes: 12 additions & 9 deletions app/host/ghost.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,18 @@ def run_ghost(transient, output_dir=settings.GHOST_OUTPUT_ROOT):
ascentMatch=False,
)

# sometimes photo-zs randomly fail
try:
host_data = calc_photoz(
host_data,
dust_path=settings.GHOST_DUST_PATH,
model_path=settings.GHOST_PHOTOZ_PATH,
)
except Exception as err:
print(f"""Error running calc_photoz(): {err}""")
# photo-z only implemented for dec > -30
if transient_position.dec.deg > -30:
# still getting random photo-z bugs
# but this shouldn't be a show-stopper
try:
host_data = calc_photoz(
host_data,
dust_path=settings.GHOST_DUST_PATH,
model_path=settings.GHOST_PHOTOZ_PATH,
)
except Exception as e:
print("warning : photo-z step failed")

# clean up after GHOST...
# dir_list = glob.glob('transients_*/*/*')
Expand Down
19 changes: 17 additions & 2 deletions app/host/host_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time
import warnings
from collections import namedtuple
from xml.parsers.expat import ExpatError

import astropy.units as u
import extinction
Expand All @@ -23,6 +24,7 @@
cosmo = FlatLambdaCDM(H0=70, Om0=0.315)

from django.conf import settings
from django.db.models import Q
from dustmaps.sfd import SFDQuery
from photutils.aperture import aperture_photometry
from photutils.aperture import EllipticalAperture
Expand Down Expand Up @@ -396,7 +398,7 @@ def select_cutout_aperture(cutouts, choice=0):
### edited to allow initial offset
filter_choice = filter_names[choice]

while not cutouts.filter(filter__name=filter_choice).exists():
while not cutouts.filter(filter__name=filter_choice).filter(~Q(fits="")).exists():
choice += 1
filter_choice = filter_names[choice]

Expand Down Expand Up @@ -533,7 +535,20 @@ def get_source_data(threshhold_sigma):
def query_ned(position):
"""Get a Galaxy's redshift from ned if it is available."""

result_table = Ned.query_region(position, radius=1.0 * u.arcsec)
too_many_requests_count = 0
too_many_requests = True
while too_many_requests and too_many_requests_count < 5:
try:
result_table = Ned.query_region(position, radius=1.0 * u.arcsec)
too_many_requests = False
except ExpatError as e:
too_many_requests = True
print("too many requests! going to sleep 60s...")
time.sleep(60)
too_many_requests_count += 1
if too_many_requests:
raise RuntimeError("too many requests to NED")

result_table = result_table[result_table["Redshift"].mask == False]

redshift = result_table["Redshift"].value
Expand Down
27 changes: 24 additions & 3 deletions app/host/system_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import glob
import shutil

from dateutil import parser
from django.conf import settings
from django.db.models import Q
from django.utils import timezone
Expand All @@ -15,6 +16,7 @@
from .transient_name_server import get_daily_tns_staging_csv
from .transient_name_server import get_tns_credentials
from .transient_name_server import get_transients_from_tns
from .transient_name_server import get_transients_from_tns_by_name
from .transient_name_server import tns_staging_blast_transient
from .transient_name_server import tns_staging_file_date_name
from .transient_name_server import update_blast_transient
Expand All @@ -29,12 +31,18 @@ def run_process(self, interval_minutes=200):
recent_transients = get_transients_from_tns(
now - time_delta, tns_credentials=tns_credentials
)

print("TNS DONE")
saved_transients = Transient.objects.all()
count = 0
for transient in recent_transients:
print(transient.name)
try:
saved_transient = saved_transients.get(name__exact=transient.name)
if saved_transient.public_timestamp.replace(tzinfo=None) - parser.parse(
transient.public_timestamp
) == datetime.timedelta(0):
continue

# if there was *not* a redshift before and there *is* one now
# then it would be safest to reprocess everything
Expand All @@ -47,9 +55,22 @@ def run_process(self, interval_minutes=200):
### update info
new_transient_dict = transient.__dict__
if "host_id" in new_transient_dict.keys():
del new_transient_dict["host_id"]
del new_transient_dict["_state"]
del new_transient_dict["id"]
if saved_transient.host_id is not None:
new_transient_dict["host_id"] = saved_transient.host_id
else:
del new_transient_dict["host_id"]

keys_to_del = [
"_state",
"id",
"progress",
"tasks_initialized",
"photometric_class",
"milkyway_dust_reddening",
"processing_status",
]
for k in keys_to_del:
del new_transient_dict[k]
saved_transients.filter(name__exact=transient.name).update(
**new_transient_dict
)
Expand Down
6 changes: 4 additions & 2 deletions app/host/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,15 @@ def results(request, slug):
else:
form = ImageGetForm(filter_choices=filters)

cutouts = Cutout.objects.filter(transient__name__exact=slug)
cutouts = Cutout.objects.filter(transient__name__exact=slug).filter(~Q(fits=""))
## choose a cutout, if possible
cutout = None
choice = 0
try:
while cutout is None and choice <= 8:
cutout = select_cutout_aperture(cutouts, choice=choice)
cutout = select_cutout_aperture(cutouts, choice=choice).filter(
~Q(fits="")
)
if not len(cutout):
cutout = None
else:
Expand Down

0 comments on commit 739a0aa

Please sign in to comment.