question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

UTC->UT1 conversion fails for zero-length Time object

See original GitHub issue

Description

Converting a zero-length Time object from UTC to UT1 fails because the IERS _interpolate method does not handle the corner case of an empty array.

Example code:

from astropy.time import Time
from astropy import units as u
import numpy as np

time = Time.now() + np.arange(24) * u.hour

indices = np.asarray([0, 1, 2], dtype=np.intp)
print(time[indices].ut1) # This is fine

empty_indices = np.asarray([], dtype=np.intp)
print(time[empty_indices].ut1)  # KABOOM

Expected behavior

Should print:

[datetime.datetime(2021, 3, 31, 18, 17, 6, 727233)
 datetime.datetime(2021, 3, 31, 19, 17, 6, 727213)
 datetime.datetime(2021, 3, 31, 20, 17, 6, 727193)]
[]

Actual behavior

Actually prints:

[datetime.datetime(2021, 3, 31, 18, 17, 6, 727233)
 datetime.datetime(2021, 3, 31, 19, 17, 6, 727213)
 datetime.datetime(2021, 3, 31, 20, 17, 6, 727193)]
Traceback (most recent call last):
  File "./test.py", line 32, in <module>
    print(time[empty_indices].ut1)  # KABOOM
  File "/private/tmp/env/lib/python3.8/site-packages/astropy/time/core.py", line 1327, in __getattr__
    tm._set_scale(attr)
  File "/private/tmp/env/lib/python3.8/site-packages/astropy/time/core.py", line 576, in _set_scale
    args.append(get_dt(jd1, jd2))
  File "/private/tmp/env/lib/python3.8/site-packages/astropy/time/core.py", line 1944, in _get_delta_ut1_utc
    delta = iers_table.ut1_utc(jd1, jd2)
  File "/private/tmp/env/lib/python3.8/site-packages/astropy/utils/iers/iers.py", line 276, in ut1_utc
    return self._interpolate(jd1, jd2, ['UT1_UTC'],
  File "/private/tmp/env/lib/python3.8/site-packages/astropy/utils/iers/iers.py", line 360, in _interpolate
    self._refresh_table_as_needed(mjd)
  File "/private/tmp/env/lib/python3.8/site-packages/astropy/utils/iers/iers.py", line 719, in _refresh_table_as_needed
    max_input_mjd = np.max(mjd)
  File "<__array_function__ internals>", line 5, in amax
  File "/private/tmp/env/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 2733, in amax
    return _wrapreduction(a, np.maximum, 'max', axis, None, out,
  File "/private/tmp/env/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 87, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
ValueError: zero-size array to reduction operation maximum which has no identity

Steps to Reproduce

(See sample code above)

System Details

>>> import platform; print(platform.platform())
macOS-10.15.7-x86_64-i386-64bit
>>> import sys; print("Python", sys.version)
Python 3.8.8 (default, Mar 18 2021, 06:01:57) 
[Clang 11.0.3 (clang-1103.0.32.62)]
>>> import numpy; print("Numpy", numpy.__version__)
Numpy 1.20.2
>>> import astropy; print("astropy", astropy.__version__)
astropy 4.1.dev3458+g7811614f8
>>> import scipy; print("Scipy", scipy.__version__)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'scipy'
>>> import matplotlib; print("Matplotlib", matplotlib.__version__)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'matplotlib'

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lpsingercommented, Apr 1, 2021

I am curious how you managed to install astropy 4.1.dev3458+g7811614f8. It should be 4.3.devXXXXXX by now.

Looks like I hadn’t run git fetch upstream --tags in a while.

0reactions
lpsingercommented, Apr 11, 2021

Go for it, @bhavyakh!

Read more comments on GitHub >

github_iconTop Results From Across the Web

astropy/CHANGES.rst at main - GitHub
An error is thrown whether P format is used but the heap size is bigger ... [#11234]; Fixed converting a zero-length time object...
Read more >
erfa.core — PyERFA v2.0.0.2.dev13+gfb1f520
3) In early eras the conversion is from the "Proleptic Gregorian Calendar"; ... in the usual ERFA manner, which is designed to preserve...
Read more >
ZeroLength Element - OpenSeesWiki
This command is used to construct a zeroLength element object, which is defined by two nodes at the same location.
Read more >
Full Changelog — Astropy v1.0.4
Fixed iteration of scalar Time objects so that iter() correctly raises a TypeError on them (while still allowing Time arrays to be iterated) ......
Read more >
python-astropy-2.0.3-bp151.3.2 - SUSE Package Hub -
Make sure get_pkg_data_fileobj fails if the URL can not be read, ... [#3581] - Add ``Time.to_datetime`` method for converting ``Time`` objects to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found