curryer.spicetime.convert ========================= .. py:module:: curryer.spicetime.convert .. autoapi-nested-parse:: Convert between time formats (ttype). Routine Listings ---------------- adapt Convert from one time format to another. Maintains container type (e.g., scalar input returns a scalar, list input returns a list). SpiceTime Subclass of numpy's ndarray that stores the data's time format `ttype`, and facilitates converting to other time formats. .. rubric:: Examples >>> utc = adapt([0, 1], from_='ugps', to='utc') >>> print('Adapt API: {!r} type={}'.format(utc, type(utc))) Adapt API: ['1980-01-06 00:00:00.000000', '1980-01-06 00:00:00.000001'] type= >>> ugps = SpiceTime([0, 1], ttype='ugps') >>> utc_arr = ugps.to_utc() >>> print('SpiceTime API: {!r}'.format(utc_arr)) SpiceTime API: SpiceTime(['1980-01-06 00:00:00.000000', '1980-01-06 00:00:00.000001'], dtype='>> st = SpiceTime('2016-05-09 14:49:17.371099', 'utc') >>> print(repr(st)) SpiceTime('2016-05-09 14:49:17.371099', dtype='>> print('Ephemeris Time: {}'.format(st.to_et())) Ephemeris Time: 516077425.55644935 >>> print('uGPS: {}'.format(st.to_ugps())) uGPS: 1146840574371099 >>> gps = st.to_gps() >>> print(gps.ttype) gps >>> st = SpiceTime([0, 1], 'gps') >>> print(st, st.ttype) [ 0. 1.] gps >>> print(st.to_utc('%S.%f')) ['00.000000' '01.000000'] .. rubric:: Notes - Requires a leapsecond kernel to be loaded (automated by library). .. py:method:: __array_finalize__(obj) Numpy's replacement for "__init__", supports construction, casting, and templates. .. rubric:: Notes Three possible ways to create this subclass: 1) Explicit construction: arr = SpiceTime(np.arange(3), fmt='et') type(`obj`) is ndarray; `self` is built in `SpiceTime.__new__` 2) View casting: arr = np.arange(3).view(SpiceTime) type(`obj`) can be `SpiceTime` or another 3) New-from-template: arr = np.SpiceTime(arange(3))[1:] type(`obj`) is `SpiceTime` The optional method `__array_prepare__` is similar, but before the values are calculated. .. rubric:: References - https://docs.scipy.org/doc/numpy/user/basics.subclassing.html .. py:method:: __array_prepare__(out_arr, context=None) Numpy's way of supporting subclassing from ufuncs (e.g., `np.min(et_arr)`). Allows viewing (ONLY) the output data structure and context (func & args) before the calculation. .. py:method:: __array_wrap__(out_arr, context=None) Numpy's way of supporting subclassing from ufuncs (e.g., `np.min(et_arr)`). Allows tweaking the output (`out_arr`), before final casting as `SpiceTime`. .. py:method:: __repr__() String representation of the array with the time format `ttype`. .. py:property:: ttype Time format. .. py:method:: adapt(to, **kwargs) Convert from the current time format to another. .. py:method:: to_et() Convert times to Ephemeris Time (SPICE; float64). .. py:method:: to_ugps() Convert times to GPS microseconds (int64). .. py:method:: to_gps() Convert times to GPS seconds (float64). .. py:method:: to_tai() Convert times to International Atomic Time seconds (1958; float64). .. py:method:: to_utc(date_format=None) Convert times to UTC strings (ISO format). .. py:data:: _py_to_spice_format .. py:data:: PY_TO_SPICE_FORMAT .. py:function:: spice_strftime(date_format) Convert a Python datetime format string to an equivalent SPICE string. :param date_format: Python datetime format string (e.g., "%Y-%m-%d %H:%M:%S.%f"). NOTE: Does not support every Python format code. Formats with a non-none return in `PY_TO_SPICE_FORMAT` are supported. :type date_format: str :returns: SPICE UTC format string. :rtype: str .. rubric:: Examples >>> py_format = '%Y-%m-%d %H:%M:%S.%f' >>> print(spice_strftime(py_format)) 'YYYY-MM-DD HR:MN:SC.######::RND ::UTC' .. rubric:: References Python's format string: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior SPICE's format string: https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/timout_c.html#Particulars