# library of small functions
import datetime
import numpy as np
[docs]
def isostr2date(isostr):
"""
Convert strings representing dates into datetime.datetime objects aimed ad Django REST API
ISO 8601: '2018-05-22T14:35:22.99585' or '2018-05-22T14:35:22'
:param isostr: a string, list of strings or panda Series / numpy arrays containing strings
:return: a scalar, list of
"""
# NB this is intended for scalars or small list. See the ciso8601 pypi module instead for
# a performance implementation
if not isinstance(isostr, str):
return [isostr2date(el) for el in isostr]
format = '%Y-%m-%dT%H:%M:%S'
if '.' in isostr:
format += '.%f'
if '+' in isostr:
format += '.%f'
return datetime.datetime.strptime(isostr, format)
[docs]
def date2isostr(adate):
# NB this is intended for scalars or small list. See the ciso8601 pypi module instead for
# a performance implementation
if type(adate) is datetime.date:
adate = datetime.datetime.fromordinal(adate.toordinal())
return datetime.datetime.isoformat(adate)
[docs]
def convert_pgts(time):
"""Convert PointGray cameras timestamps to seconds.
Use convert then uncycle"""
# offset = time & 0xFFF
cycle1 = (time >> 12) & 0x1FFF
cycle2 = (time >> 25) & 0x7F
seconds = cycle2 + cycle1 / 8000.
return seconds
[docs]
def uncycle_pgts(time):
"""Unwrap the converted seconds of a PointGray camera timestamp series."""
cycles = np.insert(np.diff(time) < 0, 0, False)
cycleindex = np.cumsum(cycles)
return time + cycleindex * 128