-
Notifications
You must be signed in to change notification settings - Fork 348
/
Copy pathiss.py
169 lines (146 loc) · 4.64 KB
/
iss.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "pandas==2.2.3",
# "vega-datasets==0.9.0",
# "requests==2.32.3",
# "altair==5.4.1",
# "marimo",
# ]
# ///
import marimo
__generated_with = "0.10.6"
app = marimo.App(width="full")
@app.cell
def _(
chart,
iss_df,
mo,
n_points_slider,
refresh_interval_slider,
refresher,
):
mo.hstack(
[
mo.vstack(
[
mo.md(
"## Settings | [`marimo.ui.slider`](https://docs.marimo.io/api/inputs/slider.html), [`marimo.ui.refresh`](https://docs.marimo.io/recipes.html#run-a-cell-on-a-timer)\n---"
),
refresh_interval_slider,
n_points_slider,
refresher,
mo.md(
"## ISS Positions | [`marimo.ui.altair_chart`](https://docs.marimo.io/api/plotting.html#marimo.ui.altair_chart)\n---"
),
mo.as_html(chart).style({"width": "700px"}),
],
align="center",
),
mo.vstack(
[
mo.md(
"## Data | [`marimo.as_html`](https://docs.marimo.io/api/html.html)`(pd.DataFrame)`\n---"
),
mo.as_html(iss_df),
]
),
],
justify="center",
wrap=True,
gap=3,
)
return
@app.cell
def _(alt, get_iss_positions, sphere, world):
hover = alt.selection_point(on="mouseover", clear="mouseout")
# iss positions
iss_df = get_iss_positions()
iss = (
alt.Chart(iss_df[["longitude", "latitude", "timestamp"]])
.mark_circle(
stroke="black",
size=100,
)
.encode(
longitude=alt.Longitude("longitude:Q"),
latitude="latitude:Q",
fill=alt.Fill(
"timestamp:Q", scale=alt.Scale(scheme="purples"), legend=None
),
strokeWidth=alt.condition(
hover, alt.value(3, empty=False), alt.value(0)
),
tooltip=[
alt.Tooltip("longitude:Q", title="Longitude", format=".4f"),
alt.Tooltip("latitude:Q", title="Latitude", format=".4f"),
alt.Tooltip(
"timestamp:T", title="Timestamp", format="%Y-%m-%d %H:%M:%S"
),
],
)
.add_params(hover)
)
chart = (
alt.layer(sphere, world, iss)
.project(type="naturalEarth1")
.properties(width=640, title="")
)
return chart, hover, iss, iss_df
@app.cell
def _(alt, data):
# load geo data from Vega Datasets
countries = alt.topo_feature(data.world_110m.url, 'countries')
# world base
sphere = alt.Chart(alt.sphere()).mark_geoshape(
fill="aliceblue", stroke="black", strokeWidth=1.5
)
# world map
world = alt.Chart(countries).mark_geoshape(
fill="mintcream", stroke="black", strokeWidth=0.35
)
return countries, sphere, world
@app.cell
def _(
n_points_slider,
pd,
refresh_interval_slider,
refresher,
requests,
time,
):
def get_iss_positions(refresher=refresher):
refresher
timepoints = [int(time())]
while len(timepoints) <= n_points_slider.value:
timepoints.append(timepoints[-1] - refresh_interval_slider.value)
else:
timepoints.pop(0)
timepoints_str = str(timepoints)[1:-1].replace(" ", "")
iss_url = f"https://api.wheretheiss.at/v1/satellites/25544/positions?timestamps={timepoints_str}"
response = requests.get(iss_url)
df = pd.DataFrame(response.json())
df['timestamp'] = pd.to_datetime(df.timestamp, unit='s')
return df[['timestamp','latitude','longitude','altitude','velocity','visibility']]
return (get_iss_positions,)
@app.cell
def _(mo, refresh_interval_slider):
refresher = mo.ui.refresh(default_interval=f"{refresh_interval_slider.value}s")
return (refresher,)
@app.cell
def _(mo):
refresh_interval_slider = mo.ui.slider(start=5, stop=60, step=1, value=10, label="refresh interval (default = 10 sec)")
n_points_slider = mo.ui.slider(start=5, stop=30, step=1, value=15, label="number of points (default = 15)")
return n_points_slider, refresh_interval_slider
@app.cell
def _():
import altair as alt
import marimo as mo
import pandas as pd
import requests
from time import time
from vega_datasets import data
pd.options.display.max_rows = 30
return alt, data, mo, pd, requests, time
if __name__ == "__main__":
app.run()