Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ dynamic = ["version"]
requires-python = ">=3.10.0,<3.14"
dependencies = [
"tomtoolkit >=3.0.0,<4.0",
"across-client"
"across-client",
"across-tools @ git+https://github.com/NASA-ACROSS/across-tools@170-add-functionality-to-link-observations-to-archival-data-pages"
]

[tool.poetry]
Expand Down
70 changes: 70 additions & 0 deletions tom_across/tables.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import django_tables2 as tables
from tom_common.htmx_table import HTMXTable
from django.urls import reverse
from django.utils.html import format_html
import logging

from tom_across.utils import get_across_instrument_ids, get_across_observatory_telescope_name_map

from across.tools.archive_resolver import HEASARCArchiveResolver, MASTArchiveResolver

logger = logging.getLogger(__name__)
class ObservationTable(HTMXTable):
telescope = tables.Column()
Expand All @@ -12,6 +17,7 @@ class ObservationTable(HTMXTable):
type = tables.Column()
filter_name = tables.Column()
wavelength_range = tables.Column()
external_observation_id = tables.Column()

selection = None

Expand Down Expand Up @@ -43,3 +49,67 @@ def render_wavelength_range(self, value):
return f"{rendered_min} - {rendered_max}"
except (ValueError, TypeError):
return value

def render_external_observation_id(self, value):
"""
Render the external observation ID as a hyperlink to the corresponding archive page.

Args:
value (tuple): A tuple containing the instrument ID and the external observation ID.

Returns:
str: The external observation ID, as an href link to the corresponding archive page if resolvable,
or just the ID if not.
"""
if not value:
return ""
try:
inst_id_dict = get_across_instrument_ids()
inst_tele_name = inst_id_dict[value[0]][1]

obs_tele_name_dict = get_across_observatory_telescope_name_map()
name_to_try = None
for obs_name, tele_names in obs_tele_name_dict.items():
if inst_tele_name in tele_names:
name_to_try = obs_name
break

if not name_to_try:
return value[1]

if name_to_try in ["HST", "JWST"]:
resolver = MASTArchiveResolver(
external_observation_id=value[1].split(":")[0],
mission=name_to_try
)

elif name_to_try in [
"Chandra", "IXPE", "NICER", "NuSTAR", "XMM-Newton", "Swift", "XRISM"
]:
name_table_dict = {
"Chandra": "chanmastr",
"IXPE": "ixmaster",
"NICER": "nicermastr",
"NuSTAR": "numaster",
"Swift": "swiftmastr",
"XMM-Newton": "xmmmaster",
"XRISM": "xrismmastr",
}
resolver = HEASARCArchiveResolver(
external_observation_id=value[1],
heasarc_table=name_table_dict[name_to_try]
)
else:
return value[1]

resolver.construct_archive_url()
if resolver.archive_url:
return format_html(
f'<a href="{resolver.archive_url}" target="_blank">{value[1]}</a>'
)
else:
logger.warning(f"Could not resolve archive URL for observation ID: {value}")
return value[1]
except Exception as e:
logger.error(f"Error resolving archive URL for observation ID {value}: {e}")
return value[1]
22 changes: 21 additions & 1 deletion tom_across/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ def observation_rows(target, start_date=None, end_date=None, wavelength_type=Non
'date': obs.date_range.end,
'type': getattr(obs.type, 'value', obs.type),
'filter_name': band,
'wavelength_range': (min_band, max_band)
'wavelength_range': (min_band, max_band),
'external_observation_id': (obs.instrument_id, obs.external_observation_id)
})

except ServiceException as e:
Expand All @@ -219,4 +220,23 @@ def get_across_instrument_ids():

cache.set(cache_key, data, timeout=24 * 60 * 60) # Cache for 24 hours

return data

def get_across_observatory_telescope_name_map():
"""
Build a dictionary of ACROSS observatory names and their corresponding telescope names.
Cached for 24 hours, refreshed on cache miss.
"""
cache_key = "across_observatory_telescope_name_map"
data = cache.get(cache_key)

if data is None:
print('GETTING OBSERVATORY TELESCOPE NAMES FROM ACROSS')
observatories = client.observatory.get_many()
data = {}
for obs in observatories:
data[obs.short_name] = [tele.name for tele in obs.telescopes]

cache.set(cache_key, data, timeout=24 * 60 * 60) # Cache for 24 hours

return data
Loading