-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcoverage.py
546 lines (482 loc) · 18.9 KB
/
coverage.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# -*- coding: utf-8 -*-
"""
Methods to perform coverage analysis.
@author: Paul T. Grogan <paul.grogan@asu.edu>
"""
from typing import List, Union
from datetime import datetime, timedelta
import pandas as pd
import numpy as np
import geopandas as gpd
from shapely import geometry as geo
from skyfield.api import wgs84, EarthSatellite
from skyfield.toposlib import GeographicPosition
from ..schemas.point import Point
from ..schemas.satellite import Satellite
from ..schemas.instrument import Instrument
from ..utils import (
compute_min_elevation_angle,
compute_max_access_time,
)
from ..constants import de421, timescale
def _get_visible_interval_series(
point: GeographicPosition,
satellite: EarthSatellite,
min_elevation_angle: float,
start: datetime,
end: datetime,
) -> pd.Series:
"""
Get the series of visible intervals based on altitude angle constraints.
Args:
point (akyfield.toposlib.GeographicPosition): Point to observe.
satellite (skyfield.api.EarthSatellite): Satellite doing the observation.
min_elevation_angle (float): Minimum elevation angle (degrees) for valid observation.
start (datetime.datetime): Start of analysis period.
end (datetime.datetime): End of analysis period.
Returns:
pandas.Series: Series of observation intervals.
"""
# define starting and ending points
t_0 = timescale.from_datetime(start)
t_1 = timescale.from_datetime(end)
# compute the initial satellite altitude
satellite_altitude = wgs84.geographic_position_of(satellite.at(t_0)).elevation.m
# compute the maximum access time to filter bad data
max_access_time = timedelta(
seconds=compute_max_access_time(satellite_altitude, min_elevation_angle)
)
# find the set of observation events
times, events = satellite.find_events(
point, t_0, t_1, altitude_degrees=min_elevation_angle
)
# build the observation periods
obs_periods = []
if len(events) > 0 and np.all(events == 1):
# if all events are type 1 (culminate), create a period from start to end
obs_periods += [pd.Interval(left=pd.Timestamp(start), right=pd.Timestamp(end))]
elif len(events) > 0:
# otherwise, match rise/set events
rises = times[events == 0]
sets = times[events == 2]
if (
len(sets) > 0
and (len(rises) == 0 or sets[0].utc_datetime() < rises[0].utc_datetime())
and start < sets[0].utc_datetime()
):
# if first event is a set, create a period from the start
obs_periods += [
pd.Interval(
left=pd.Timestamp(start), right=pd.Timestamp(sets[0].utc_datetime())
)
]
# create an observation period to match with each rise event if
# there is a following set event within twice the maximum access time
obs_periods += [
pd.Interval(
left=pd.Timestamp(rise.utc_datetime()),
right=pd.Timestamp(
sets[
np.logical_and(
rise.utc_datetime() < sets.utc_datetime(),
sets.utc_datetime()
< rise.utc_datetime() + 2 * max_access_time,
)
][0].utc_datetime()
),
)
for rise in rises
if np.any(
np.logical_and(
rise.utc_datetime() < sets.utc_datetime(),
sets.utc_datetime() < rise.utc_datetime() + 2 * max_access_time,
)
)
]
if (
len(rises) > 0
and (len(sets) == 0 or rises[-1].utc_datetime() > sets[-1].utc_datetime())
and rises[-1].utc_datetime() < end
):
# if last event is a rise, create a period to the end
obs_periods += [
pd.Interval(
left=pd.Timestamp(rises[-1].utc_datetime()), right=pd.Timestamp(end)
)
]
return pd.Series(obs_periods, dtype="interval")
def _get_satellite_altaz_series(
observations: gpd.GeoDataFrame, satellite: EarthSatellite
) -> pd.Series:
"""
Get a series with the satellite altitude/azimuth for each observation.
Args:
observations (geopandas.GeoDataFrame): Data frame of observation records.
satellite (skyfield.api.EarthSatellite): Satellite doing the observation.
Returns:
pandas.Series: Series of altitude-azimuth objects associated with observations.
"""
sat_altaz = observations.apply(
lambda r: (satellite - wgs84.latlon(r.geometry.y, r.geometry.x, r.geometry.z))
.at(timescale.from_datetime(r.epoch))
.altaz(),
axis=1,
)
return pd.Series(sat_altaz, dtype="object")
def _get_satellite_sunlit_series(
observations: gpd.GeoDataFrame, satellite: EarthSatellite
) -> pd.Series:
"""
Get a series with the satellite sunlit condition for each observation.
Args:
observations (geopandas.GeoDataFrame): Data frame of observation records.
satellite (EarthSatellite): Satellite doing the observation.
Returns:
pandas.Series: Series of booleans associated with observations.
"""
sat_sunlit = observations.apply(
lambda r: satellite.at(timescale.from_datetime(r.epoch)).is_sunlit(de421),
axis=1,
)
return pd.Series(sat_sunlit, dtype="bool")
def _get_solar_altaz_series(observations: gpd.GeoDataFrame) -> pd.Series:
"""
Get a series with the solar altitude/azimuth for each observation.
Args:
observations (geopandas.GeoDataFrame): Data frame of observation records.
Returns:
pandas.Series: Series of altitude-azimuth objects associated with observations.
"""
sun_altaz = observations.apply(
lambda r: (
de421["earth"] + wgs84.latlon(r.geometry.y, r.geometry.x, r.geometry.z)
)
.at(timescale.from_datetime(r.epoch))
.observe(de421["sun"])
.apparent()
.altaz(),
axis=1,
)
return pd.Series(sun_altaz, dtype="object")
def _get_solar_time_series(observations: gpd.GeoDataFrame) -> pd.Series:
"""
Get a series with the local solar time for each observation.
Args:
observations (geopandas.GeoDataFrame): Data frame of observation records.
Returns:
pandas.Series: Series of floats (local solar time in hours) associated with observations.
"""
solar_time = observations.apply(
lambda r: (
de421["earth"] + wgs84.latlon(r.geometry.y, r.geometry.x, r.geometry.z)
)
.at(timescale.from_datetime(r.epoch))
.observe(de421["sun"])
.apparent()
.hadec()[0]
.hours
+ 12,
axis=1,
)
return pd.Series(solar_time, dtype="float")
def _get_access_series(observations: gpd.GeoDataFrame) -> pd.Series:
"""
Get a series with the access time for each observation.
Args:
observations (geopandas.GeoDataFrame): Data frame of observation records.
Returns:
pandas.Series: Series of timedeltas measuring access duration of each observation.
"""
# compute the access time for the observation (end - start)
return observations["end"] - observations["start"]
def _get_revisit_series(observations: gpd.GeoDataFrame) -> pd.Series:
"""
Get a series with the revisit times for each observation.
Args:
observations (geopandas.GeoDataFrame): Data frame of observation records.
Returns:
pandas.Series: Series of timedeltas measuring revisit duration for each observation.
"""
# compute the revisit time for each observation (previous end - start)
return observations["start"] - observations["end"].shift()
def _get_empty_coverage_frame(omit_solar: bool) -> gpd.GeoDataFrame:
"""
Gets an empty data frame for coverage analysis results.
Returns:
geopandas.GeoDataFrame: Empty data frame.
"""
columns = {
"point_id": pd.Series([], dtype="int"),
"geometry": pd.Series([], dtype="object"),
"satellite": pd.Series([], dtype="str"),
"instrument": pd.Series([], dtype="str"),
"start": pd.Series([], dtype="datetime64[ns, utc]"),
"epoch": pd.Series([], dtype="datetime64[ns, utc]"),
"end": pd.Series([], dtype="datetime64[ns, utc]"),
"sat_alt": pd.Series(dtype="float"),
"sat_az": pd.Series(dtype="float"),
}
if not omit_solar:
columns = {
**columns,
**{
"sat_sunlit": pd.Series(dtype="bool"),
"solar_alt": pd.Series(dtype="float"),
"solar_az": pd.Series(dtype="float"),
"solar_time": pd.Series(dtype="float"),
},
}
return gpd.GeoDataFrame(columns, crs="EPSG:4326")
def collect_observations(
point: Point,
satellite: Satellite,
instrument: Instrument,
start: datetime,
end: datetime,
omit_solar: bool = True,
) -> gpd.GeoDataFrame:
"""
Collect single satellite observations of a geodetic point of interest.
Args:
point (Point): The ground point of interest.
satellite (Satellite): The observing satellite.
instrument (Instrument): The observing instrument.
start (datetime.datetime): Start of analysis period.
end (datetime.datetime): End of analysis period.
omit_solar (bool): `True`, to omit solar angles to improve
computational efficiency; otherwise `False`.
Returns:
geopandas.GeoDataFrame: The data frame with recorded observations.
"""
# build a topocentric point at the designated geodetic point
topos = wgs84.latlon(point.latitude, point.longitude, point.elevation)
# convert orbit to tle
orbit = satellite.orbit.to_tle()
# construct a satellite for propagation
sat = EarthSatellite(orbit.tle[0], orbit.tle[1], satellite.name)
# compute the initial satellite altitude
satellite_altitude = wgs84.geographic_position_of(
sat.at(timescale.from_datetime(start))
).elevation.m
# compute the minimum altitude angle required for observation
min_elevation_angle = compute_min_elevation_angle(
satellite_altitude,
instrument.field_of_regard,
)
records = [
{
"point_id": point.id,
"geometry": geo.Point(point.longitude, point.latitude, point.elevation),
"satellite": satellite.name,
"instrument": instrument.name,
"start": (
period.left
if not instrument.access_time_fixed
else period.mid - instrument.min_access_time / 2
),
"end": (
period.right
if not instrument.access_time_fixed
else period.mid + instrument.min_access_time / 2
),
"epoch": period.mid,
}
for period in _get_visible_interval_series(
topos, sat, min_elevation_angle, start, end
)
if (
instrument.min_access_time <= period.right - period.left
and instrument.is_valid_observation(
sat, timescale.from_datetime(period.mid)
)
)
]
# build the dataframe
if len(records) > 0:
gdf = gpd.GeoDataFrame(records, crs="EPSG:4326")
# append satellite altitude/azimuth columns
sat_altaz = _get_satellite_altaz_series(gdf, sat)
gdf["sat_alt"] = sat_altaz.apply(lambda r: r[0].degrees)
gdf["sat_az"] = sat_altaz.apply(lambda r: r[1].degrees)
if not omit_solar:
# append satellite sunlit column
gdf["sat_sunlit"] = _get_satellite_sunlit_series(gdf, sat)
# append solar altitude/azimuth columns
sun_altaz = _get_solar_altaz_series(gdf)
gdf["solar_alt"] = sun_altaz.apply(lambda r: r[0].degrees)
gdf["solar_az"] = sun_altaz.apply(lambda r: r[1].degrees)
# append local solar time column
gdf["solar_time"] = _get_solar_time_series(gdf)
else:
gdf = _get_empty_coverage_frame(omit_solar)
return gdf
def collect_multi_observations(
point: Point,
satellites: Union[Satellite, List[Satellite]],
start: datetime,
end: datetime,
omit_solar: bool = True,
) -> gpd.GeoDataFrame:
"""
Collect multiple satellite observations of a geodetic point of interest.
Args:
point (Point): The ground point of interest.
satellites (Satellite or List[Satellite]): The observing satellite(s).
start (datetime.datetime): Start of analysis period.
end (datetime.datetime): End of analysis period.
omit_solar (bool): `True`, to omit solar angles to improve
computational efficiency; otherwise `False`.
Returns:
geopandas.GeoDataFrame: The data frame with all recorded observations.
"""
gdfs = [
collect_observations(point, satellite, instrument, start, end, omit_solar)
for constellation in (
satellites if isinstance(satellites, list) else [satellites]
)
for satellite in (constellation.generate_members())
for instrument in satellite.instruments
]
# concatenate into one data frame, sort by start time, and re-index
return pd.concat(gdfs).sort_values("start").reset_index(drop=True)
def _get_empty_aggregate_frame() -> gpd.GeoDataFrame:
"""
Gets an empty data frame for aggregated coverage analysis results.
Returns:
geopandas.GeoDataFrame: Empty data frame.
"""
columns = {
"point_id": pd.Series([], dtype="int"),
"geometry": pd.Series([], dtype="object"),
"satellite": pd.Series([], dtype="str"),
"instrument": pd.Series([], dtype="str"),
"start": pd.Series([], dtype="datetime64[ns, utc]"),
"epoch": pd.Series([], dtype="datetime64[ns, utc]"),
"end": pd.Series([], dtype="datetime64[ns, utc]"),
}
return gpd.GeoDataFrame(columns, crs="EPSG:4326")
def aggregate_observations(observations: gpd.GeoDataFrame) -> gpd.GeoDataFrame:
"""
Aggregate constellation observations. Interleaves observations by multiple
satellites to compute aggregate performance metrics including access
(observation duration) and revisit (duration between observations).
Args:
observations (geopandas.GeoDataFrame): The collected observations.
Returns:
geopandas.GeoDataFrame: The data frame with aggregated observations.
"""
if observations.empty:
return _get_empty_aggregate_frame()
gdfs = []
# split into constituent data frames based on point_id
for _, gdf in observations.groupby("point_id"):
# sort the values by start datetime
gdf = gdf.sort_values("start")
# assign the observation group number based on overlapping start/end times
gdf["obs"] = (gdf["start"] > gdf["end"].shift().cummax()).cumsum()
# perform the aggregation to group overlapping observations
gdf = gdf.dissolve(
"obs",
aggfunc={
"point_id": "first",
"satellite": ", ".join,
"instrument": ", ".join,
"start": "min",
"epoch": "mean",
"end": "max",
},
)
# compute access and revisit metrics
gdf["access"] = _get_access_series(gdf)
gdf["revisit"] = _get_revisit_series(gdf)
# append to the list of data frames
gdfs.append(gdf)
# return a concatenated data frame and re-index
return pd.concat(gdfs).reset_index(drop=True)
def _get_empty_reduce_frame() -> gpd.GeoDataFrame:
"""
Gets an empty data frame for reduced coverage analysis results.
Returns:
geopandas.GeoDataFrame: Empty data frame.
"""
columns = {
"point_id": pd.Series([], dtype="int"),
"geometry": pd.Series([], dtype="object"),
"access": pd.Series([], dtype="timedelta64[ns]"),
"revisit": pd.Series([], dtype="timedelta64[ns]"),
"samples": pd.Series([], dtype="int"),
}
return gpd.GeoDataFrame(columns, crs="EPSG:4326")
def reduce_observations(aggregated_observations: gpd.GeoDataFrame) -> gpd.GeoDataFrame:
"""
Reduce constellation observations. Computes descriptive statistics for each
geodetic point of interest contained in aggregated observations.
Args:
aggregated_observations (geopandas.GeoDataFrame): The aggregated observations.
Returns:
geopandas.GeoDataFrame: The data frame with reduced observations.
"""
if aggregated_observations.empty:
return _get_empty_reduce_frame()
# operate on a copy of the data frame
gdf = aggregated_observations.copy()
# convert access and revisit to numeric values before aggregation
gdf["access"] = gdf["access"] / timedelta(seconds=1)
gdf["revisit"] = gdf["revisit"] / timedelta(seconds=1)
# assign each record to one observation
gdf["samples"] = 1
# perform the aggregation operation
gdf = gdf.dissolve(
"point_id",
aggfunc={
"access": "mean",
"revisit": "mean",
"samples": "sum",
},
).reset_index()
# convert access and revisit from numeric values after aggregation
gdf["access"] = gdf["access"].apply(lambda t: timedelta(seconds=t))
gdf["revisit"] = gdf["revisit"].apply(
lambda t: pd.NaT if pd.isna(t) else timedelta(seconds=t)
)
return gdf
def grid_observations(
reduced_observations: gpd.GeoDataFrame, cells: gpd.GeoDataFrame
) -> gpd.GeoDataFrame:
"""
Grid reduced observations to cells.
Args:
reduced_observations (geopandas.GeoDataFrame): The reduced observations.
cells (geopandas.GeoDataFrame): The cell specification.
Returns:
geopandas.GeoDataFrame: The data frame with gridded observations.
"""
if reduced_observations.empty:
gdf = cells.copy()
gdf["samples"] = 0
gdf["access"] = None
gdf["revisit"] = None
return gdf
# operate on a copy of the data frame
gdf = reduced_observations.copy()
# convert access and revisit to numeric values before aggregation
gdf["access"] = gdf["access"] / timedelta(seconds=1)
gdf["revisit"] = gdf["revisit"] / timedelta(seconds=1)
gdf = (
cells.sjoin(gdf, how="inner", predicate="contains")
.dissolve(
by="cell_id",
aggfunc={
"samples": "sum",
"access": lambda r: np.average(r, weights=gdf.loc[r.index, "samples"]),
"revisit": lambda r: np.average(r, weights=gdf.loc[r.index, "samples"]),
},
)
.reset_index()
)
# convert access and revisit from numeric values after aggregation
gdf["access"] = gdf["access"].apply(lambda t: timedelta(seconds=t))
gdf["revisit"] = gdf["revisit"].apply(
lambda t: pd.NaT if pd.isna(t) else timedelta(seconds=t)
)
return gdf