Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shiny #48

Merged
merged 4 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions Chandra/Time/Time.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,24 @@
day of year (1-366), and day of week (0-6, where 0 is Monday).
These are all referenced to UTC time.
Date when hour, minutes, seconds not provided
---------------------------------------------
A date like ``2020:001`` will be taken as ``2020:001:00:00:00`` since version 4.0.
Before 4.0, ``2020:001`` was ``2020:001:12:00:00``. To get the pre-4.0 behavior
use the following code::
from Chandra.Time import use_noon_day_start
# Set to use 12:00:00 globally from now on.
use_noon_day_start()
.. note::
You should do this globally once in your code at the beginning. There
is no way to revert to using 00:00:00 after calling ``use_noon_day_start``.
This impacts all code using ``DateTime``, not just the calls from your script.
"""
import re
from functools import wraps
Expand All @@ -190,6 +208,31 @@
import six
import numpy as np

# Time for dates specified without HMS. This was changed from '12:00:00' to
# '00:00:00' in version 4.0 of Chandra.Time. Call use_noon_day_start(True)
# for compatibility with the pre-4.0 behavior.
_DAY_START = '00:00:00'


def use_noon_day_start():
"""Set global default so date with no hours, min, sec uses 12:00:00.
A date like 2020:001 will be taken as 2020:001:00:00:00 since version 4.0.
Before 4.0, 2020:001 was 2020:001:12:00:00. To get the pre-4.0 behavior
use the following code.
NOTE: you should do this globally once in your code at the beginning. There
is no way to revert to using 00:00:00 after calling ``use_noon_day_start``.
::
from Chandra.Time import use_noon_day_start
# Set to use 12:00:00 globally from now on.
use_noon_day_start()
"""
global _DAY_START
_DAY_START = '12:00:00'


def override__dir__(f):
"""
Expand Down Expand Up @@ -337,7 +380,7 @@ def mx_DateTime_ISO_ParseDateTime(t):
match_expr=RE['year_mon_day'],
ax3_fmt='f3',
ax3_sys='u',
preprocess=lambda t: t + 'T12:00:00',
preprocess=lambda t: t + 'T' + _DAY_START,
postprocess=lambda t: re.sub(r'T\d{2}:\d{2}:\d{2}\.\d+$', '', t),
),
TimeStyle(name='relday',
Expand Down Expand Up @@ -408,7 +451,7 @@ def mx_DateTime_ISO_ParseDateTime(t):
match_expr=RE['year_doy'],
ax3_fmt='d3',
ax3_sys='u',
preprocess=lambda t: t + ':12:00:00',
preprocess=lambda t: t + ':' + _DAY_START,
postprocess=lambda t: re.sub(r':\d{2}:\d{2}:\d{2}\.\d+$', '', t),
),
TimeStyle(name='jd',
Expand Down Expand Up @@ -705,6 +748,10 @@ def __init__(self, time_in=None, format=None):
# then just convert to cxcsec (secs).
try:
secs = time_in.cxcsec
# For working in Chandra operations, possibly with no network access, we cannot
# allow auto downloads.
from astropy.utils import iers
iers.conf.auto_download = False
import astropy.time
assert isinstance(time_in, astropy.time.Time)
time_in = secs
Expand Down
22 changes: 17 additions & 5 deletions Chandra/Time/tests/test_Time.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
import pytest

from ..Time import DateTime, convert, convert_vals, date2secs, secs2date
from ..Time import DateTime, convert, convert_vals, date2secs, secs2date, use_noon_day_start
from cxotime import CxoTime
from astropy.time import Time

Expand Down Expand Up @@ -50,6 +50,18 @@ def test_mxDateTime_in():
assert convert('1998-01-01 00:00:30') == 93.184


def test_use_noon_day_start():
from .. import Time
assert Time._DAY_START == '00:00:00'
use_noon_day_start()
assert Time._DAY_START == '12:00:00'
tm = DateTime('2020:001')
assert tm.date == '2020:001:12:00:00.000'

# Set it back for rest of testing
Time._DAY_START = '00:00:00'


def test_iso():
assert convert(93.184, fmt_out='iso') == '1998-01-01 00:00:30.000'
assert convert(93.184, fmt_out='iso') == '1998-01-01 00:00:30.000'
Expand Down Expand Up @@ -150,8 +162,8 @@ def test_plotdate():
array([ 733773.5])
"""
pd = DateTime('2010:001').plotdate
assert pd == 733773.5
assert DateTime(pd, format='plotdate').date == '2010:001:12:00:00.000'
assert pd == 733773.0
assert DateTime(pd, format='plotdate').date == '2010:001:00:00:00.000'


def test_greta():
Expand All @@ -176,12 +188,12 @@ def test_start_day():

def test_year_doy():
assert DateTime(20483020.0).year_doy == '1998:238'
assert DateTime('2004:121').date == '2004:121:12:00:00.000'
assert DateTime('2004:121').date == '2004:121:00:00:00.000'


def test_year_mon_day():
assert DateTime('2004:121').year_mon_day == '2004-04-30'
assert DateTime('2007-01-01').date == '2007:001:12:00:00.000'
assert DateTime('2007-01-01').date == '2007:001:00:00:00.000'


def test_add():
Expand Down