11from datetime import datetime, timedelta
2- from typing import Union
2+ from typing import Iterable, Union
33
44from dateutil.parser import parse as parse_date
55import numpy
6+ from pandas import DataFrame
67from pyproj import CRS
78
89from .model import SECONDS_TO_GROUND
@@ -36,6 +37,13 @@ def append(self, packet: LocationPacket):
3637 packet.transform_to(self.crs)
3738 self.packets.append(packet)
3839
40+ def extend(self, packets: [LocationPacket]):
41+ for packet in packets:
42+ self.append(packet)
43+
44+ def sort(self):
45+ self.packets.sort()
46+
3947 @property
4048 def times(self) -> numpy.ndarray:
4149 return numpy.array([packet.time for packet in self.packets], dtype=numpy.datetime64)
@@ -137,8 +145,13 @@ def length(self) -> float:
137145 """ total length of the packet track over the ground """
138146 return sum([distance.overground for distance in self.packets.difference])
139147
140- def __getitem__(self, index: Union[int, slice]) -> LocationPacket:
141- return self.packets[index]
148+ def __getitem__(self, index: Union[int, Iterable[int], slice]) -> Union[LocationPacket, 'LocationPacketTrack']:
149+ if isinstance(index, int):
150+ return self.packets[index]
151+ elif isinstance(index, Iterable) or isinstance(index, slice):
152+ return self.__class__(self.name, self.packets[index], self.crs)
153+ else:
154+ raise ValueError(f'unrecognized index: {index}')
142155
143156 def __iter__(self):
144157 return iter(self.packets)
@@ -158,6 +171,22 @@ def __eq__(self, other) -> bool:
158171 def __str__(self) -> str:
159172 return str(list(self))
160173
174+ @property
175+ def dataframe(self) -> DataFrame:
176+ return DataFrame({
177+ 'name': [self.name for _ in range(len(self))],
178+ 'times': self.times,
179+ 'x': self.coordinates[:, 0],
180+ 'y': self.coordinates[:, 1],
181+ 'z': self.coordinates[:, 2],
182+ 'intervals': self.intervals,
183+ 'overground_distances': self.overground_distances,
184+ 'ascents': self.ascents,
185+ 'ascent_rates': self.ascent_rates,
186+ 'ground_speeds': self.ground_speeds,
187+ 'cumulative_overground_distances': self.cumulative_overground_distances,
188+ })
189+
161190
162191class BalloonTrack(LocationPacketTrack):
163192 def __init__(self, name: str, packets: [LocationPacket] = None, crs: CRS = None):
0 commit comments