{ "cells": [ { "cell_type": "markdown", "id": "e9e26702", "metadata": {}, "source": [ "# Temporal Conversions" ] }, { "cell_type": "code", "execution_count": 33, "id": "e5eb3551", "metadata": {}, "outputs": [], "source": [ "import pystare\n", "import datetime\n", "import numpy\n", "import astropy.time\n", "import erfa" ] }, { "cell_type": "markdown", "id": "d093caaf-8996-4e20-aad9-366ed9daac3f", "metadata": {}, "source": [ "## Full circle" ] }, { "cell_type": "code", "execution_count": 2, "id": "8217b5b0-c91a-4a32-9834-265faf2cc307", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2276059438861267137, 2275939265676325057])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create the TIVs\n", "times = numpy.array(['2021-09-03', '2021-07-17 11:16'], dtype='datetime64')\n", "times = astropy.time.Time(times, scale='utc', format='datetime64')\n", "tivs = pystare.from_julian_date(times.jd1, times.jd2, \n", " scale='utc', \n", " forward_res=48, reverse_res=48)\n", "tivs" ] }, { "cell_type": "code", "execution_count": 3, "id": "7e8e9533-e7e7-4fac-822a-a3b7db946725", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2275469239487829593])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "timestamp = '2021-01-11 19:30:00'\n", "jd = astropy.time.Time(numpy.datetime64(timestamp))\n", "pystare.from_julian_date(jd.jd1, jd.jd2, scale='utc', forward_res=22, reverse_res=22)" ] }, { "cell_type": "code", "execution_count": 4, "id": "e5aa1c99-a290-4240-9a98-9532d1e7cb45", "metadata": {}, "outputs": [], "source": [ "tivs = pystare.from_julian_date(times.jd1, times.jd2, \n", " scale='utc', \n", " forward_res=48, reverse_res=48)" ] }, { "cell_type": "code", "execution_count": 5, "id": "5ebc15b3-351c-4b90-9df9-fd90ad5cb0f2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([datetime.datetime(2021, 9, 3, 0, 0),\n", " datetime.datetime(2021, 7, 17, 11, 16)], dtype=object)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Back via JD\n", "jd = pystare.to_julian_date(tivs, scale='utc')\n", "times = astropy.time.Time(val=jd[0], val2=jd[1], format='jd')\n", "times.to_datetime()" ] }, { "cell_type": "code", "execution_count": 6, "id": "d9701dfd-77a6-4452-a364-7f2eabf7a1d5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['2021-09-03T00:00:00', '2021-07-17T11:16:00']" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Back via MS\n", "mss = pystare.to_ms_since_epoch_utc(tivs)\n", "dts = [datetime.datetime.utcfromtimestamp(ms/1000) for ms in mss]\n", "[dt.isoformat() for dt in dts]" ] }, { "cell_type": "markdown", "id": "1a637f57", "metadata": {}, "source": [ "## Valudations" ] }, { "cell_type": "code", "execution_count": 7, "id": "surgical-estonia", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pystare.validate_stare_timestring('2021-01-09T17:47:56.154 (45 12) (1)')" ] }, { "cell_type": "code", "execution_count": 8, "id": "ad07c1d1-2cba-44e0-9bd7-4cda080b5ebc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pystare.validate_iso8601_string('2021-01-09T17:47:56.2435+05:00')" ] }, { "cell_type": "markdown", "id": "2a19ad57", "metadata": {}, "source": [ "### UTC MS coversions" ] }, { "cell_type": "code", "execution_count": 9, "id": "checked-drive", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1653483732000])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "now = datetime.datetime.now().strftime(\"%Y-%m-%dT%H:%M:%S\")\n", "now = numpy.array([now], dtype='datetime64[ms]')\n", "now = now.astype(numpy.int64)\n", "now" ] }, { "cell_type": "code", "execution_count": 10, "id": "finished-cincinnati", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2275448110396223681])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "timestamps = numpy.array(['2021-01-03'], dtype='datetime64[ms]').astype(numpy.int64)\n", "pystare.from_ms_since_epoch_utc(timestamps, 48, 48)" ] }, { "cell_type": "code", "execution_count": 11, "id": "protective-subdivision", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['2021-01-03T00:00:00.000'], dtype='" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pystare.set_reverse_resolution\n", "pystare.set_forward_resolution\n", "pystare.reverse_resolution\n", "pystare.forward_resolution\n", "pystare.coarsen\n", "pystare.set_temporal_resolutions_from_sorted\n", "pystare.coarsest_resolution_finer_or_equal_ms" ] }, { "cell_type": "markdown", "id": "0892bafe", "metadata": {}, "source": [ "## Comparison" ] }, { "cell_type": "code", "execution_count": 41, "id": "2e907cfc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pystare.cmp_temporal\n", "\n", "pystare.temporal_value_intersection_if_overlap\n", "pystare.temporal_value_union_if_overlap\n", "pystare.temporal_overlap_tai\n", "pystare.temporal_overlap\n", "pystare.temporal_contains_instant" ] }, { "cell_type": "code", "execution_count": null, "id": "05d40f9f-1d29-4beb-81ef-0228283af0e1", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.4" } }, "nbformat": 4, "nbformat_minor": 5 }