Skip to content

Commit

Permalink
some pre commit fixes, still many needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Vebop committed Sep 29, 2024
1 parent 9826835 commit 15aab97
Show file tree
Hide file tree
Showing 10 changed files with 604 additions and 536 deletions.
24 changes: 23 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
repos:
- repo: https://github.com/psf/black
rev: 24.4.2
hooks:
- id: black

- repo: https://github.com/pycqa/flake8
rev: 7.1.0
hooks:
- id: flake8

- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
name: isort (python)

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: check-yaml
exclude: conda/meta.yaml
- id: check-xml

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
Expand All @@ -8,4 +31,3 @@ repos:
rev: 0.7.1
hooks:
- id: nbstripout

2 changes: 1 addition & 1 deletion .ts_pre_commit_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ clang-format: false
flake8: true
format-xmllint: false
isort: true
mypy: true
mypy: false
check-yaml: true
check-xml: true
ruff: false
Expand Down
2 changes: 1 addition & 1 deletion notebooks_tsqr/NightLog.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"limit = 5000 # YAGNI for Auto get more if this isn't enough to get all requested DAYS\n",
"\n",
"date = ut.get_datetime_from_day_obs_str(day_obs)\n",
"# date: is EXLUSIVE (upto, but not including)\n",
"# date: is EXCLUSIVE (upto, but not including)\n",
"days = int(number_of_days)\n",
"\n",
"# Thus: [min_day_obs,max_day_obs)\n",
Expand Down
136 changes: 61 additions & 75 deletions python/lsst/ts/logging_and_reporting/almanac.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import datetime as dt
import math
import warnings

import astropy.coordinates
from astroplan import Observer
from astropy.time import Time
import astropy.coordinates


# Compare to https://www.timeanddate.com/astronomy/chile/santiago
class Almanac:
Expand All @@ -15,120 +17,104 @@ class Almanac:
Times in UTC.
"""


def __init__(self, *, day_obs=None, site='Rubin'):
def __init__(self, *, day_obs=None, site="Rubin"):
if day_obs is None:
astro_day = dt.date.today() - dt.timedelta(days=1)
else:
# Allow formats: int, YYYY-MM-DD, YYYYMMDD
dobs = str(day_obs).replace('-','')
astro_day = dt.datetime.strptime(dobs, '%Y%m%d').date()
dobs = str(day_obs).replace("-", "")
astro_day = dt.datetime.strptime(dobs, "%Y%m%d").date()

with warnings.catch_warnings(action='ignore'):
with warnings.catch_warnings(action="ignore"):
self.loc = astropy.coordinates.EarthLocation.of_site(site)
self.observer = Observer(self.loc, timezone='Chile/Continental')
self.observer = Observer(self.loc, timezone="Chile/Continental")
self.astro_day = astro_day
day1 = dt.timedelta(days=1)
self.astro_midnight = Time(
dt.datetime.combine(self.astro_day+day1, dt.time(0,15)),
format='datetime',
scale='utc',
location=self.loc
dt.datetime.combine(self.astro_day + day1, dt.time(0, 15)),
format="datetime",
scale="utc",
location=self.loc,
)
self.get_moon()
self.get_sun()

def get_moon(self):
self.moon_rise_time = self.observer.moon_rise_time(
self.astro_midnight,
which='nearest'
)
self.astro_midnight, which="nearest"
)
self.moon_set_time = self.observer.moon_set_time(
self.astro_midnight,
which='nearest'
)
self.astro_midnight, which="nearest"
)

# Percent of moon lit
self.moon_illum = self.observer.moon_illumination(
self.astro_midnight
)
self.moon_illum = self.observer.moon_illumination(self.astro_midnight)

def get_sun(self):
# ast(ronoimical) twilight: -18 degrees)
self.ast_twilight_morning = self.observer.twilight_morning_astronomical(
self.astro_midnight,
which='next'
)
self.astro_midnight, which="next"
)
self.ast_twilight_evening = self.observer.twilight_evening_astronomical(
self.astro_midnight,
which='previous'
)

self.astro_midnight, which="previous"
)

# nau(tical) twilight: -12 degrees)
self.nau_twilight_morning = self.observer.twilight_morning_nautical(
self.astro_midnight,
which='next'
)
self.astro_midnight, which="next"
)
self.nau_twilight_evening = self.observer.twilight_evening_nautical(
self.astro_midnight,
which='previous'
)

self.astro_midnight, which="previous"
)

# civ(il) twilight: -6 degrees)
self.civ_twilight_morning = self.observer.twilight_morning_civil(
self.astro_midnight,
which='next'
)
self.astro_midnight, which="next"
)
self.civ_twilight_evening = self.observer.twilight_evening_civil(
self.astro_midnight,
which='previous'
)
self.astro_midnight, which="previous"
)

self.sun_rise_time = self.observer.sun_rise_time(
self.astro_midnight,
which='next'
)
self.astro_midnight, which="next"
)
self.sun_set_time = self.observer.sun_set_time(
self.astro_midnight,
which='previous'
)
self.astro_midnight, which="previous"
)

@property
def night_hours(self):
day_delta = self.ast_twilight_morning - self.ast_twilight_evening
return day_delta.to_value('hr')
return day_delta.to_value("hr")

@property
def as_dict(self):
data_dict = {
'': '(times in UTC)',
'Moon Rise': self.moon_rise_time.iso,
'Moon Set': self.moon_set_time.iso,
'Moon Illumination': f'{self.moon_illum:.0%}',

'Astronomical Twilight (morning)': self.ast_twilight_morning.iso,
'Astronomical Twilight (evening)': self.ast_twilight_evening.iso,
'Nautical Twilight (morning)': self.nau_twilight_morning.iso,
'Nautical Twilight (evening)': self.nau_twilight_evening.iso,
'Civil Twilight (morning)': self.civ_twilight_morning.iso,
'Civil Twilight (evening)': self.civ_twilight_evening.iso,
'Sun Rise': self.sun_rise_time.iso,
'Sun Set': self.sun_set_time.iso,
}
data_dict = {
"": "(times in UTC)",
"Moon Rise": self.moon_rise_time.iso,
"Moon Set": self.moon_set_time.iso,
"Moon Illumination": f"{self.moon_illum:.0%}",
"Astronomical Twilight (morning)": self.ast_twilight_morning.iso,
"Astronomical Twilight (evening)": self.ast_twilight_evening.iso,
"Nautical Twilight (morning)": self.nau_twilight_morning.iso,
"Nautical Twilight (evening)": self.nau_twilight_evening.iso,
"Civil Twilight (morning)": self.civ_twilight_morning.iso,
"Civil Twilight (evening)": self.civ_twilight_evening.iso,
"Sun Rise": self.sun_rise_time.iso,
"Sun Set": self.sun_set_time.iso,
}
help_dict = {
'': '',
'Moon Set': '',
'Moon Rise': '',
'Moon Illumination': '(% illuminated)',
'Astronomical Twilight (evening)': '(-18 degrees)',
'Astronomical Twilight (morning)': '(-18 degrees)',
'Nautical Twilight (evening)': '(-12 degrees)',
'Nautical Twilight (morning)': '(-12 degrees)',
'Civil Twilight (evening)': '(-6 degrees)',
'Civil Twilight (morning)': '(-6 degrees)',
'Sun Set': '',
'Sun Rise': '',
}
"": "",
"Moon Set": "",
"Moon Rise": "",
"Moon Illumination": "(% illuminated)",
"Astronomical Twilight (evening)": "(-18 degrees)",
"Astronomical Twilight (morning)": "(-18 degrees)",
"Nautical Twilight (evening)": "(-12 degrees)",
"Nautical Twilight (morning)": "(-12 degrees)",
"Civil Twilight (evening)": "(-6 degrees)",
"Civil Twilight (morning)": "(-6 degrees)",
"Sun Set": "",
"Sun Rise": "",
}
return data_dict, help_dict
73 changes: 40 additions & 33 deletions python/lsst/ts/logging_and_reporting/dashboard.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Python Standard Library
from collections import defaultdict
from warnings import warn

import lsst.ts.logging_and_reporting.source_adapters as sad

# External Packages
import requests

Expand All @@ -10,32 +12,34 @@ class Dashboard: # TODO Move to its own file (utils.py).
"""Verify that we can get to all the API endpoints and databases we need for
any of our sources.
"""

timeout = 0.8

envs = dict( # key, server
summit = 'https://summit-lsp.lsst.codes',
usdf_dev = 'https://usdf-rsp-dev.slac.stanford.edu',
tucson = 'https://tucson-teststand.lsst.codes',
envs = dict( # key, server
summit="https://summit-lsp.lsst.codes",
usdf_dev="https://usdf-rsp-dev.slac.stanford.edu",
tucson="https://tucson-teststand.lsst.codes",
# Environments not currently used:
# rubin_usdf_dev = '',
# data_lsst_cloud = '',
# usdf = '',
# base_data_facility = '',
# rubin_idf_int = '',
)
adapters = [sad.ExposurelogAdapter,
sad.NarrativelogAdapter,
sad.NightReportAdapter,
]
adapters = [
sad.ExposurelogAdapter,
sad.NarrativelogAdapter,
sad.NightReportAdapter,
]

def get_sample_data(self, server):

samples = defaultdict(dict) # samples[endpoint_url] -> one_record_dict
for adapter in self.adapters:
sa = adapter(server_url=server, limit=1)
for ep in sa.endpoints:
qstr = '?instrument=LSSTComCamSim' if ep == 'exposures' else ''
url = f'{server}/{sa.service}/{ep}{qstr}'
qstr = "?instrument=LSSTComCamSim" if ep == "exposures" else ""
url = f"{server}/{sa.service}/{ep}{qstr}"
try:
res = requests.get(url, timeout=self.timeout)
recs = res.json()
Expand All @@ -44,21 +48,20 @@ def get_sample_data(self, server):
else:
samples[url] = recs[0]
except Exception as err:
warn(f'Could not get data from {url}: {res.content=} {err=}')
warn(f"Could not get data from {url}: {res.content=} {err=}")
samples[url] = None
return dict(samples)


def report(self, timeout=None):
"""Check our ability to connect to every Source on every Environment.
Report a summary.
RETURN: percentage of good connectons.
"""
url_status = dict() # url_status[endpoint_url] = http _status_code
working = set() # Set of servers that work for all our required endpoints.
url_status = dict() # url_status[endpoint_url] = http _status_code
working = set() # Set of servers that work for all our required endpoints.

for env,server in self.envs.items():
for env, server in self.envs.items():
server_all_good = True
for adapter in self.adapters:
service = adapter(server_url=server)
Expand All @@ -71,30 +74,34 @@ def report(self, timeout=None):
total_cnt = good_cnt = 0
good = list()
bad = list()
for url,stat in url_status.items():
for url, stat in url_status.items():
total_cnt += 1
if stat == 200:
good_cnt += 1
good.append(url)
else:
bad.append((url,stat))
bad.append((url, stat))

print(f'\nConnected to {good_cnt} out of {total_cnt} endpoints.'
f'({good_cnt/total_cnt:.0%})'
)
print(
f"\nConnected to {good_cnt} out of {total_cnt} endpoints."
f"({good_cnt/total_cnt:.0%})"
)
goodstr = "\n\t".join(good)
print(f'Successful connects ({good_cnt}): ')
print(f"Successful connects ({good_cnt}): ")
for gurl in good:
print(f'\t{gurl}')

print(f'Failed connects ({total_cnt - good_cnt}): ')
for burl,stat in bad:
print(f'\t{stat}: {burl}')

status = dict(num_good=good_cnt,
num_total=total_cnt,
good_urls=good,
bad_ursl=bad,
)
return good_cnt/total_cnt, working
print(f"\t{gurl}")

print(f"Failed connects ({total_cnt - good_cnt}): ")
for burl, stat in bad:
print(f"\t{stat}: {burl}")

status = dict(
num_good=good_cnt,
num_total=total_cnt,
good_urls=good,
bad_ursl=bad,
)
return good_cnt / total_cnt, working


# END: class Dashboard
Loading

0 comments on commit 15aab97

Please sign in to comment.