Skip to content

Commit

Permalink
Merge pull request #220 from nyx-space/211-export-epoch-formatters-to…
Browse files Browse the repository at this point in the history
…-python

Export epoch formatters to Python
  • Loading branch information
ChristopherRabotin authored Apr 16, 2023
2 parents b716d60 + f574232 commit dffef5c
Show file tree
Hide file tree
Showing 11 changed files with 338 additions and 88 deletions.
117 changes: 102 additions & 15 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
@@ -1,70 +1,157 @@
name: Python lib
# This file is autogenerated by maturin v0.14.17
# To update, run
#
# maturin generate-ci --pytest -o .github/workflows/python.yml github
#
name: Python CI

on:
push:
branches:
- main
- master
tags:
- "*"
- '*'
pull_request:
workflow_dispatch:

permissions:
contents: read

jobs:
linux:
runs-on: ubuntu-latest
strategy:
matrix:
target: [x86_64, x86, aarch64, armv7, s390x, ppc64le]
steps:
- uses: actions/checkout@v3
- uses: messense/maturin-action@v1
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.target }}
args: --release --out dist --find-interpreter -F python
sccache: 'true'
manylinux: auto
command: build
args: --release --sdist -o dist --find-interpreter -F python

- name: Upload wheels
uses: actions/upload-artifact@v3
with:
name: wheels
path: dist
- name: pytest
if: ${{ startsWith(matrix.target, 'x86_64') }}
shell: bash
run: |
set -e
pip install hifitime --find-links dist --force-reinstall
pip install pytest
pytest
- name: pytest
if: ${{ !startsWith(matrix.target, 'x86') && matrix.target != 'ppc64' }}
uses: uraimo/run-on-arch-action@v2.5.0
with:
arch: ${{ matrix.target }}
distro: ubuntu22.04
githubToken: ${{ github.token }}
install: |
apt-get update
apt-get install -y --no-install-recommends python3 python3-pip
pip3 install -U pip pytest
run: |
set -e
pip3 install hifitime --find-links dist --force-reinstall
pytest
windows:
runs-on: windows-latest
strategy:
matrix:
target: [x64, x86]
steps:
- uses: actions/checkout@v3
- uses: messense/maturin-action@v1
- uses: actions/setup-python@v4
with:
command: build
args: --release -o dist --find-interpreter -F python
python-version: '3.10'
architecture: ${{ matrix.target }}
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.target }}
args: --release --out dist --find-interpreter -F python
sccache: 'true'
- name: Upload wheels
uses: actions/upload-artifact@v3
with:
name: wheels
path: dist
- name: pytest
if: ${{ !startsWith(matrix.target, 'aarch64') }}
shell: bash
run: |
set -e
pip install hifitime --find-links dist --force-reinstall
pip install pytest
pytest
macos:
runs-on: macos-latest
strategy:
matrix:
target: [x86_64, aarch64]
steps:
- uses: actions/checkout@v3
- uses: messense/maturin-action@v1
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
command: build
args: --release -o dist --universal2 --find-interpreter -F python
target: ${{ matrix.target }}
args: --release --out dist --find-interpreter -F python
sccache: 'true'
- name: Upload wheels
uses: actions/upload-artifact@v3
with:
name: wheels
path: dist
- name: pytest
if: ${{ !startsWith(matrix.target, 'aarch64') }}
shell: bash
run: |
set -e
pip install hifitime --find-links dist --force-reinstall
pip install pytest
pytest
sdist:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build sdist
uses: PyO3/maturin-action@v1
with:
command: sdist
args: --out dist
- name: Upload sdist
uses: actions/upload-artifact@v3
with:
name: wheels
path: dist

release:
name: Release
runs-on: ubuntu-latest
if: github.ref_type == 'tag'
needs: [macos, windows, linux]
if: "startsWith(github.ref, 'refs/tags/')"
needs: [linux, windows, macos, sdist]
steps:
- uses: actions/download-artifact@v3
with:
name: wheels
- name: Publish to PyPI
uses: messense/maturin-action@v1
uses: PyO3/maturin-action@v1
env:
MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
with:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ name = "hifitime"
serde = {version = "1.0.155", optional = true}
serde_derive = {version = "1.0.155", optional = true}
der = {version = "0.6.1", features = ["derive", "real"], optional = true}
pyo3 = { version = "0.18.1", features = ["extension-module"], optional = true}
pyo3 = { version = "0.18.1", features = ["extension-module"], optional = true }
num-traits = {version = "0.2.15", default-features = false, features = ["libm"]}
lexical-core = {version = "0.8.5", default-features = false, features = ["parse-integers", "parse-floats"]}
reqwest = { version = "0.11", features = ["blocking", "json"], optional = true}
Expand Down
25 changes: 15 additions & 10 deletions examples/python/basic.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'''
"""
* Hifitime, part of the Nyx Space tools
* Copyright (C) 2022 Christopher Rabotin <christopher.rabotin@gmail.com> et al. (cf. AUTHORS.md)
* This Source Code Form is subject to the terms of the Apache
* v. 2.0. If a copy of the Apache License was not distributed with this
* file, You can obtain one at https://www.apache.org/licenses/LICENSE-2.0.
*
* Documentation: https://nyxspace.com/
'''
"""

from hifitime import Duration, Epoch, TimeSeries, TimeScale, Unit

Expand All @@ -30,7 +30,7 @@
print(f"min positive = {Duration.min_positive()}")

# And more importantly, it does not suffer from rounding issues, even when the duration are very large.
print(f"Max duration: {Duration.max()}") # 1196851200 days
print(f"Max duration: {Duration.max()}") # 1196851200 days
print(f"Nanosecond precision: {Duration.max() - Unit.Nanosecond * 1.0}")
assert f"{Unit.Day * 1.2}" == "1 days 4 h 48 min"
assert f"{Unit.Day * 1.200001598974}" == "1 days 4 h 48 min 138 ms 151 μs 353 ns"
Expand All @@ -40,16 +40,21 @@

# You can also get all of the epochs between two different epochs at a specific step size.
# This is like numpy's `linspace` with high fidelity durations
time_series = TimeSeries(Epoch.system_now(),
Epoch.system_now() + Unit.Day * 0.3,
Unit.Hour * 0.5,
inclusive=True)
time_series = TimeSeries(
Epoch.system_now(),
Epoch.system_now() + Unit.Day * 0.3,
Unit.Hour * 0.5,
inclusive=True,
)
print(time_series)
for (num, epoch) in enumerate(time_series):
for num, epoch in enumerate(time_series):
print(f"#{num}:\t{epoch}")

e1 = Epoch.system_now()
e3 = e1 + Unit.Day * 1.5998
epoch_delta = e3.timedelta(e1)
assert epoch_delta == Unit.Day * 1 + Unit.Hour * 14 + Unit.Minute * 23 + Unit.Second * 42.720
print(epoch_delta)
assert (
epoch_delta
== Unit.Day * 1 + Unit.Hour * 14 + Unit.Minute * 23 + Unit.Second * 42.720
)
print(epoch_delta)
79 changes: 49 additions & 30 deletions examples/python/timescales.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,51 @@
'''
"""
* Hifitime, part of the Nyx Space tools
* Copyright (C) 2022 Christopher Rabotin <christopher.rabotin@gmail.com> et al. (cf. AUTHORS.md)
* This Source Code Form is subject to the terms of the Apache
* v. 2.0. If a copy of the Apache License was not distributed with this
* file, You can obtain one at https://www.apache.org/licenses/LICENSE-2.0.
*
* Documentation: https://nyxspace.com/
'''
"""

try:
import plotly.express as px
except ImportError:
print('\nThis script requires `plotly` (pip install plotly)\n')
print("\nThis script requires `plotly` (pip install plotly)\n")

try:
import pandas as pd
except ImportError:
print('\nThis script requires `pandas` (pip install pandas)\n')
print("\nThis script requires `pandas` (pip install pandas)\n")

from hifitime import Epoch, TimeSeries, Unit

if __name__ == "__main__":
'''
"""
The purpose of this script is to plot the differences between time systems.
It will plot the difference between TAI, UTC, and all of the other timescales supported by hifitime.
Then, as a separate plot, it will remove the UTC line to make the difference between other timescales more evident.
'''
"""
# Start by building a time series from 1970 until 2023 with a step of 30 days.
ts = TimeSeries(Epoch('1970-01-01 00:00:00 UTC'), Epoch('2023-01-01 00:00:00 UTC'),
Unit.Day * 30.0, True)
ts = TimeSeries(
Epoch("1970-01-01 00:00:00 UTC"),
Epoch("2023-01-01 00:00:00 UTC"),
Unit.Day * 30.0,
True,
)

# Define the storage array
data = []
# Define the columns
columns = ["UTC Epoch", "Δ TT (s)", "Δ ET (s)", "Δ TDB (s)", "Δ UTC (s)", "ET-TDB (s)"]
columns = [
"UTC Epoch",
"Δ TT (s)",
"Δ ET (s)",
"Δ TDB (s)",
"Δ UTC (s)",
"ET-TDB (s)",
]

for epoch in ts:
delta_utc = epoch.to_utc_duration() - epoch.to_tai_duration()
Expand All @@ -45,34 +56,42 @@
# Convert the epoch into a pandas datetime
pd_epoch = pd.to_datetime(str(epoch))
# Build the pandas series
data.append([
pd_epoch,
delta_tt.to_seconds(),
delta_et.to_seconds(),
delta_tdb.to_seconds(),
delta_utc.to_seconds(),
delta_et_tdb.to_seconds(),
])
data.append(
[
pd_epoch,
delta_tt.to_seconds(),
delta_et.to_seconds(),
delta_tdb.to_seconds(),
delta_utc.to_seconds(),
delta_et_tdb.to_seconds(),
]
)

df = pd.DataFrame(data, columns=columns)

fig = px.line(df,
x='UTC Epoch',
y=columns[1:-1],
title="Time scale deviation with respect to TAI")
fig = px.line(
df,
x="UTC Epoch",
y=columns[1:-1],
title="Time scale deviation with respect to TAI",
)
fig.write_html("./target/time-scale-deviation.html")
fig.show()

fig = px.line(df,
x='UTC Epoch',
y=columns[1:-2],
title="Time scale deviation with respect to TAI (excl. UTC)")
fig = px.line(
df,
x="UTC Epoch",
y=columns[1:-2],
title="Time scale deviation with respect to TAI (excl. UTC)",
)
fig.write_html("./target/time-scale-deviation-no-utc.html")
fig.show()

fig = px.line(df,
x='UTC Epoch',
y=columns[-1],
title="Time scale deviation of TDB and ET with respect to TAI")
fig = px.line(
df,
x="UTC Epoch",
y=columns[-1],
title="Time scale deviation of TDB and ET with respect to TAI",
)
fig.write_html("./target/time-scale-deviation-tdb-et.html")
fig.show()
fig.show()
Loading

0 comments on commit dffef5c

Please sign in to comment.