From 619024e7e1cbdbb83fc6dd2b52414089d7e30b21 Mon Sep 17 00:00:00 2001 From: Tom Aldcroft Date: Sun, 25 Dec 2022 09:44:16 -0500 Subject: [PATCH 1/4] Add config files --- .github/workflows/black.yml | 10 ++++++++++ .github/workflows/flake8.yml | 6 +++--- .pre-commit-config.yaml | 12 ++++++++++++ pyproject.toml | 20 ++++++++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/black.yml create mode 100644 .pre-commit-config.yaml create mode 100644 pyproject.toml diff --git a/.github/workflows/black.yml b/.github/workflows/black.yml new file mode 100644 index 0000000..6f47e98 --- /dev/null +++ b/.github/workflows/black.yml @@ -0,0 +1,10 @@ +name: Check black formatting + +on: [push] + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: psf/black@stable diff --git a/.github/workflows/flake8.yml b/.github/workflows/flake8.yml index d65419e..680f4b4 100644 --- a/.github/workflows/flake8.yml +++ b/.github/workflows/flake8.yml @@ -8,12 +8,12 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set up Python 3.8 - uses: actions/setup-python@v1 + uses: actions/setup-python@v4 with: python-version: 3.8 - name: Lint with flake8 run: | pip install flake8 - flake8 . --count --exclude=docs --ignore=E402,W503,F541 --max-line-length=100 --show-source --statistics + flake8 . --exclude=docs --count --ignore=W503,W504,F541,E203 --max-line-length=100 --show-source --statistics diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..e0034c7 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,12 @@ +repos: +- repo: https://github.com/psf/black + rev: 22.12.0 + hooks: + - id: black + language_version: python3.11 + +- repo: https://github.com/pycqa/isort + rev: 5.11.4 + hooks: + - id: isort + name: isort (python) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..9d60528 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,20 @@ +[tool.black] +include = '\.pyi?$' +exclude = ''' +/( + \.git + | \.mypy_cache + | \.tox + | \.venv + | \.vscode + | \.eggs + | _build + | buck-out + | build + | dist + | docs +)/ +''' + +[tool.isort] +profile = "black" From 59456895f187e0434c7dd4b32c5c197c7cea4a0e Mon Sep 17 00:00:00 2001 From: Tom Aldcroft Date: Sun, 25 Dec 2022 09:46:24 -0500 Subject: [PATCH 2/4] Apply isort --- docs/conf.py | 1 + make_rolldev_file.py | 1 + setup.py | 2 +- ska_sun/__init__.py | 1 + ska_sun/sun.py | 12 ++++++------ ska_sun/tests/test_sun.py | 13 ++++++++++--- 6 files changed, 20 insertions(+), 10 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 2f6dcff..878401f 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -19,6 +19,7 @@ # import os import sys + sys.path.insert(0, os.path.abspath('..')) from Ska.Sun import __version__ diff --git a/make_rolldev_file.py b/make_rolldev_file.py index f523ef9..61598d8 100644 --- a/make_rolldev_file.py +++ b/make_rolldev_file.py @@ -11,6 +11,7 @@ import numpy as np from astropy.table import Table + dat = Table.read('pitch_roll_2022.csv') assert np.allclose(np.abs(dat['negrolldev']), dat['rolldev'], rtol=0, atol=1e-08) diff --git a/setup.py b/setup.py index 2e129e8..0070739 100755 --- a/setup.py +++ b/setup.py @@ -1,8 +1,8 @@ # Licensed under a 3-clause BSD style license - see LICENSE.rst from setuptools import setup -from testr.setup_helper import cmdclass from ska_helpers.setup_helper import duplicate_package_info +from testr.setup_helper import cmdclass name = "ska_sun" namespace = "Ska.Sun" diff --git a/ska_sun/__init__.py b/ska_sun/__init__.py index c93f5e7..5931496 100644 --- a/ska_sun/__init__.py +++ b/ska_sun/__init__.py @@ -1,6 +1,7 @@ # Licensed under a 3-clause BSD style license - see LICENSE.rst import ska_helpers + from .sun import * # noqa __version__ = ska_helpers.get_version('ska_sun') diff --git a/ska_sun/sun.py b/ska_sun/sun.py index 1ec05a4..e6083a1 100755 --- a/ska_sun/sun.py +++ b/ska_sun/sun.py @@ -2,16 +2,16 @@ """ Utility for calculating sun position, pitch angle and values related to roll. """ +from math import acos, asin, atan2, cos, degrees, pi, radians, sin from pathlib import Path + import numpy as np -from math import cos, sin, acos, atan2, asin, pi, radians, degrees +import Ska.quatutil from astropy.table import Table - -from ska_helpers.utils import LazyVal -from Quaternion import Quat -from chandra_aca.transform import radec_to_eci from Chandra.Time import DateTime -import Ska.quatutil +from chandra_aca.transform import radec_to_eci +from Quaternion import Quat +from ska_helpers.utils import LazyVal def load_roll_table(): diff --git a/ska_sun/tests/test_sun.py b/ska_sun/tests/test_sun.py index 4c420fe..c0c1fea 100644 --- a/ska_sun/tests/test_sun.py +++ b/ska_sun/tests/test_sun.py @@ -2,9 +2,16 @@ import numpy as np from Quaternion import Quat -from ..sun import (apply_sun_pitch_yaw, get_sun_pitch_yaw, nominal_roll, - off_nominal_roll, position, allowed_rolldev, - pitch as sun_pitch) + +from ..sun import ( + allowed_rolldev, + apply_sun_pitch_yaw, + get_sun_pitch_yaw, + nominal_roll, + off_nominal_roll, +) +from ..sun import pitch as sun_pitch +from ..sun import position def test_allowed_rolldev(): From c7fe3ae618cbd833edd81ab30dcbb87947d2b1d5 Mon Sep 17 00:00:00 2001 From: Tom Aldcroft Date: Sun, 25 Dec 2022 09:47:43 -0500 Subject: [PATCH 3/4] Apply black --skip-string-normalization --preview --- ska_sun/__init__.py | 1 + ska_sun/sun.py | 46 ++++++++++++++++++++--------------- ska_sun/tests/test_sun.py | 51 +++++++++++++++++++++------------------ 3 files changed, 55 insertions(+), 43 deletions(-) diff --git a/ska_sun/__init__.py b/ska_sun/__init__.py index 5931496..519b354 100644 --- a/ska_sun/__init__.py +++ b/ska_sun/__init__.py @@ -12,4 +12,5 @@ def test(*args, **kwargs): Run py.test unit tests. ''' import testr + return testr.test(*args, **kwargs) diff --git a/ska_sun/sun.py b/ska_sun/sun.py index e6083a1..7cf43fe 100755 --- a/ska_sun/sun.py +++ b/ska_sun/sun.py @@ -102,25 +102,30 @@ def position(time): # allow for the Venus perturbations using the mean anomaly of Venus MV mv = 212.603219 + (58517.803875 * t) % 360.0 - vencorr = 4.8 * cos((299.1017 + mv - me) * dtor) + \ - 5.5 * cos((148.3133 + 2.0 * mv - 2.0 * me) * dtor) + \ - 2.5 * cos((315.9433 + 2.0 * mv - 3.0 * me) * dtor) + \ - 1.6 * cos((345.2533 + 3.0 * mv - 4.0 * me) * dtor) + \ - 1.0 * cos((318.15 + 3.0 * mv - 5.0 * me) * dtor) + vencorr = ( + 4.8 * cos((299.1017 + mv - me) * dtor) + + 5.5 * cos((148.3133 + 2.0 * mv - 2.0 * me) * dtor) + + 2.5 * cos((315.9433 + 2.0 * mv - 3.0 * me) * dtor) + + 1.6 * cos((345.2533 + 3.0 * mv - 4.0 * me) * dtor) + + 1.0 * cos((318.15 + 3.0 * mv - 5.0 * me) * dtor) + ) lon = lon + vencorr # Allow for the Mars perturbations using the mean anomaly of Mars MM mm = 319.529425 + (19139.858500 * t) % 360.0 - marscorr = 2.0 * cos((343.8883 - 2.0 * mm + 2.0 * me) * dtor) + \ - 1.8 * cos((200.4017 - 2.0 * mm + me) * dtor) + marscorr = 2.0 * cos((343.8883 - 2.0 * mm + 2.0 * me) * dtor) + 1.8 * cos( + (200.4017 - 2.0 * mm + me) * dtor + ) lon = lon + marscorr # Allow for the Jupiter perturbations using the mean anomaly of Jupiter MJ mj = 225.328328 + (3034.6920239 * t) % 360.0 - jupcorr = 7.2 * cos((179.5317 - mj + me) * dtor) + \ - 2.6 * cos((263.2167 - mj) * dtor) + \ - 2.7 * cos((87.1450 - 2.0 * mj + 2.0 * me) * dtor) + \ - 1.6 * cos((109.4933 - 2.0 * mj + me) * dtor) + jupcorr = ( + 7.2 * cos((179.5317 - mj + me) * dtor) + + 2.6 * cos((263.2167 - mj) * dtor) + + 2.7 * cos((87.1450 - 2.0 * mj + 2.0 * me) * dtor) + + 1.6 * cos((109.4933 - 2.0 * mj + me) * dtor) + ) lon = lon + jupcorr # Allow for the Moons perturbations using the mean elongation of the Moon @@ -148,11 +153,11 @@ def position(time): lon = lon / 3600.0 ra = atan2(sin(lon * dtor) * cos(oblt * dtor), cos(lon * dtor)) - while ((ra < 0) or (ra > (2 * pi))): - if (ra < 0): - ra += (2 * pi) - if (ra > (2 * pi)): - ra -= (2 * pi) + while (ra < 0) or (ra > (2 * pi)): + if ra < 0: + ra += 2 * pi + if ra > (2 * pi): + ra -= 2 * pi dec = asin(sin(lon * dtor) * sin(oblt * dtor)) @@ -230,12 +235,14 @@ def nominal_roll(ra, dec, time=None, sun_ra=None, sun_dec=None): sun_ra, sun_dec = position(time) sun_eci = Ska.quatutil.radec2eci(sun_ra, sun_dec) body_x = Ska.quatutil.radec2eci(ra, dec) - if np.sum((sun_eci - body_x)**2) < 1e-10: + if np.sum((sun_eci - body_x) ** 2) < 1e-10: raise ValueError('No nominal roll for ra, dec == sun_ra, sun_dec') body_y = np.cross(body_x, sun_eci) body_y = body_y / np.sqrt(np.sum(body_y**2)) body_z = np.cross(body_x, body_y) - body_z = body_z / np.sqrt(np.sum(body_z**2)) # shouldn't be needed but do it anyway + body_z = body_z / np.sqrt( + np.sum(body_z**2) + ) # shouldn't be needed but do it anyway q_att = Quat(np.array([body_x, body_y, body_z]).transpose()) return q_att.roll @@ -314,8 +321,7 @@ def get_sun_pitch_yaw(ra, dec, time=None, sun_ra=None, sun_dec=None): return np.rad2deg(pitch), np.rad2deg(yaw) -def apply_sun_pitch_yaw(att, pitch=0, yaw=0, - time=None, sun_ra=None, sun_dec=None): +def apply_sun_pitch_yaw(att, pitch=0, yaw=0, time=None, sun_ra=None, sun_dec=None): """Apply pitch(es) and yaw(s) about Sun line to an attitude. :param att: Quaternion-like diff --git a/ska_sun/tests/test_sun.py b/ska_sun/tests/test_sun.py index c0c1fea..ff91329 100644 --- a/ska_sun/tests/test_sun.py +++ b/ska_sun/tests/test_sun.py @@ -17,24 +17,27 @@ def test_allowed_rolldev(): # Test array of pitchs and allowed roll dev - testarr = [[135, 13.979], - [138, 14.516], - [0, 0], - [40, 0], - [179.9, 18.748772], - [179.997, 0], - [180, 0], - [181, 0], - [85.49229, 13.677669], - [85.52, 18.756727], - [124.99, 18.748772], - [125, 17.0]] + testarr = [ + [135, 13.979], + [138, 14.516], + [0, 0], + [40, 0], + [179.9, 18.748772], + [179.997, 0], + [180, 0], + [181, 0], + [85.49229, 13.677669], + [85.52, 18.756727], + [124.99, 18.748772], + [125, 17.0], + ] for pitch, rolldev in testarr: assert np.isclose(allowed_rolldev(pitch), rolldev) # Also test with pitch as vector - assert np.allclose(allowed_rolldev(np.array(testarr)[:, 0]), - np.array(testarr)[:, 1]) + assert np.allclose( + allowed_rolldev(np.array(testarr)[:, 0]), np.array(testarr)[:, 1] + ) def test_position(): @@ -62,8 +65,9 @@ def test_off_nominal_roll_and_pitch(): def test_apply_get_sun_pitch_yaw(): """Test apply and get sun_pitch_yaw with multiple components""" - att = apply_sun_pitch_yaw([0, 45, 0], pitch=[0, 10, 20], yaw=[0, 5, 10], - sun_ra=0, sun_dec=90) + att = apply_sun_pitch_yaw( + [0, 45, 0], pitch=[0, 10, 20], yaw=[0, 5, 10], sun_ra=0, sun_dec=90 + ) pitch, yaw = get_sun_pitch_yaw(att.ra, att.dec, sun_ra=0, sun_dec=90) assert np.allclose(pitch, 45 + np.array([0, 10, 20])) assert np.allclose(yaw, 180 + np.array([0, 5, 10])) @@ -82,15 +86,16 @@ def test_apply_sun_pitch_yaw(): def test_apply_sun_pitch_yaw_with_grid(): """Use np.ogrid to make a grid of RA/Dec values (via dpitches and dyaws)""" dpitches, dyaws = np.ogrid[0:-3:2j, -5:5:3j] - atts = apply_sun_pitch_yaw(att=[0, 45, 10], pitch=dpitches, yaw=dyaws, sun_ra=0, sun_dec=90) + atts = apply_sun_pitch_yaw( + att=[0, 45, 10], pitch=dpitches, yaw=dyaws, sun_ra=0, sun_dec=90 + ) assert atts.shape == (2, 3) exp = np.array( - [[[355., 45., 10.], - [360., 45., 10.], - [5., 45., 10.]], - [[355., 48., 10.], - [0., 48., 10.], - [5., 48., 10.]]]) + [ + [[355.0, 45.0, 10.0], [360.0, 45.0, 10.0], [5.0, 45.0, 10.0]], + [[355.0, 48.0, 10.0], [0.0, 48.0, 10.0], [5.0, 48.0, 10.0]], + ] + ) assert np.allclose(atts.equatorial, exp) From a341ceeeb4dde3f19fd0e3b52dc7d217174880e3 Mon Sep 17 00:00:00 2001 From: Tom Aldcroft Date: Sun, 25 Dec 2022 09:48:01 -0500 Subject: [PATCH 4/4] Apply black --- make_rolldev_file.py | 10 +++++----- ska_sun/__init__.py | 6 +++--- ska_sun/sun.py | 14 +++++++------- ska_sun/tests/test_sun.py | 14 +++++++------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/make_rolldev_file.py b/make_rolldev_file.py index 61598d8..c68b7f0 100644 --- a/make_rolldev_file.py +++ b/make_rolldev_file.py @@ -12,10 +12,10 @@ import numpy as np from astropy.table import Table -dat = Table.read('pitch_roll_2022.csv') +dat = Table.read("pitch_roll_2022.csv") -assert np.allclose(np.abs(dat['negrolldev']), dat['rolldev'], rtol=0, atol=1e-08) +assert np.allclose(np.abs(dat["negrolldev"]), dat["rolldev"], rtol=0, atol=1e-08) -dat['pitch'] = dat['pitch'].astype(np.float32) -dat['rolldev'] = dat['rolldev'].astype(np.float32) -dat['pitch', 'rolldev'].write('pitch_roll.fits.gz', overwrite=True) +dat["pitch"] = dat["pitch"].astype(np.float32) +dat["rolldev"] = dat["rolldev"].astype(np.float32) +dat["pitch", "rolldev"].write("pitch_roll.fits.gz", overwrite=True) diff --git a/ska_sun/__init__.py b/ska_sun/__init__.py index 519b354..39027d6 100644 --- a/ska_sun/__init__.py +++ b/ska_sun/__init__.py @@ -4,13 +4,13 @@ from .sun import * # noqa -__version__ = ska_helpers.get_version('ska_sun') +__version__ = ska_helpers.get_version("ska_sun") def test(*args, **kwargs): - ''' + """ Run py.test unit tests. - ''' + """ import testr return testr.test(*args, **kwargs) diff --git a/ska_sun/sun.py b/ska_sun/sun.py index 7cf43fe..1cd68b9 100755 --- a/ska_sun/sun.py +++ b/ska_sun/sun.py @@ -15,11 +15,11 @@ def load_roll_table(): - dat = Table.read(Path(__file__).parent / 'data' / 'pitch_roll.fits.gz') + dat = Table.read(Path(__file__).parent / "data" / "pitch_roll.fits.gz") # Add a terminating row to the data such that for pitch at or greater # than 180 the allowed roll deviation is defined as 0. - dat.add_row({'pitch': 180, 'rolldev': 0}) + dat.add_row({"pitch": 180, "rolldev": 0}) return dat @@ -31,13 +31,13 @@ def allowed_rolldev(pitch): :param pitch: Sun pitch angle (deg) :returns: Roll deviation (deg) """ - idx1 = np.searchsorted(ROLL_TABLE.val['pitch'], pitch, side='right') + idx1 = np.searchsorted(ROLL_TABLE.val["pitch"], pitch, side="right") idx0 = idx1 - 1 idx_max = len(ROLL_TABLE.val) - 1 idx0 = np.clip(idx0, 0, idx_max) idx1 = np.clip(idx1, 0, idx_max) - val0 = ROLL_TABLE.val['rolldev'][idx0] - val1 = ROLL_TABLE.val['rolldev'][idx1] + val0 = ROLL_TABLE.val["rolldev"][idx0] + val1 = ROLL_TABLE.val["rolldev"][idx1] out = np.minimum(val0, val1) # works even for a vector input for `pitch` return out @@ -236,7 +236,7 @@ def nominal_roll(ra, dec, time=None, sun_ra=None, sun_dec=None): sun_eci = Ska.quatutil.radec2eci(sun_ra, sun_dec) body_x = Ska.quatutil.radec2eci(ra, dec) if np.sum((sun_eci - body_x) ** 2) < 1e-10: - raise ValueError('No nominal roll for ra, dec == sun_ra, sun_dec') + raise ValueError("No nominal roll for ra, dec == sun_ra, sun_dec") body_y = np.cross(body_x, sun_eci) body_y = body_y / np.sqrt(np.sum(body_y**2)) body_z = np.cross(body_x, body_y) @@ -309,7 +309,7 @@ def get_sun_pitch_yaw(ra, dec, time=None, sun_ra=None, sun_dec=None): sun_frame_rot = sun_frame.transform.T # Compute attitude vector in Sun frame. - att_sun = np.einsum('...jk,...k->...j', sun_frame_rot, att_eci) + att_sun = np.einsum("...jk,...k->...j", sun_frame_rot, att_eci) # Usual for pitch and yaw. The yaw is set to match ORviewer: # get_sun_pitch_yaw(109, 55.3, time='2021:242') ~ (60, 30) diff --git a/ska_sun/tests/test_sun.py b/ska_sun/tests/test_sun.py index ff91329..54d13f2 100644 --- a/ska_sun/tests/test_sun.py +++ b/ska_sun/tests/test_sun.py @@ -41,21 +41,21 @@ def test_allowed_rolldev(): def test_position(): - ra, dec = position('2008:002:00:01:02') + ra, dec = position("2008:002:00:01:02") assert np.allclose((ra, dec), (281.903448, -22.989273)) def test_nominal_roll(): - roll = nominal_roll(205.3105, -14.6925, time='2011:019:20:51:13') + roll = nominal_roll(205.3105, -14.6925, time="2011:019:20:51:13") assert np.allclose(roll, 68.83020) # vs. 68.80 for obsid 12393 in JAN1711A def test_off_nominal_roll_and_pitch(): att = (198.392135, 36.594359, 33.983322) # RA, Dec, Roll of obsid 16354 - oroll = off_nominal_roll(att, '2015:335:00:00:00') # NOT the 16354 time + oroll = off_nominal_roll(att, "2015:335:00:00:00") # NOT the 16354 time assert np.isclose(oroll, -12.224010) - date = '2015:077:01:07:04' + date = "2015:077:01:07:04" pitch = sun_pitch(att[0], att[1], time=date) assert np.isclose(pitch, 139.5651) # vs. 139.59 in SOT MP page @@ -104,9 +104,9 @@ def test_get_sun_pitch_yaw(): See slack discussion "ORviewer sun / anti-sun plots azimuthal Sun yaw angle" """ - pitch, yaw = get_sun_pitch_yaw(109, 55.3, time='2021:242') + pitch, yaw = get_sun_pitch_yaw(109, 55.3, time="2021:242") assert np.allclose((pitch, yaw), (60.453385, 29.880125)) - pitch, yaw = get_sun_pitch_yaw(238.2, -58.9, time='2021:242') + pitch, yaw = get_sun_pitch_yaw(238.2, -58.9, time="2021:242") assert np.allclose((pitch, yaw), (92.405603, 210.56582)) - pitch, yaw = get_sun_pitch_yaw(338, -9.1, time='2021:242') + pitch, yaw = get_sun_pitch_yaw(338, -9.1, time="2021:242") assert np.allclose((pitch, yaw), (179.417797, 259.703451))