sot / chandra_time

Convert between various time formats relevant to Chandra.
https://sot.github.io/Chandra.Time
BSD 3-Clause "New" or "Revised" License
4 stars 1 forks source link

Initializing Chandra.Time.DateTime from chandra_time.DateTime fails #57

Closed taldcroft closed 1 year ago

taldcroft commented 1 year ago

The problem is below where the ChandraTimeError that is being caught here is the wrong one https://github.com/sot/chandra_time/blob/2c12c8d3f11752b97ae067c979b786f24837e3ce/chandra_time/Time.py#L775

There are two ChandraTimeError classes defined (in Chandra.Time and chandra_time). This highlights a subtle problem in the de-namespacing strategy.

One fix could be to catch both versions.

In [1]: import chandra_time, Chandra.Time

In [2]: Chandra.Time.DateTime(chandra_time.DateTime())
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File ~/miniconda3/envs/ska3/lib/python3.10/site-packages/chandra_time/Time.py:644, in convert(time_in, sys_in, fmt_in, sys_out, fmt_out)
    643         raise TypeError
--> 644     return [_convert(x, sys_in, fmt_in, sys_out, fmt_out) for x in time_in]
    645 except TypeError:

TypeError: 'float' object is not iterable

During handling of the above exception, another exception occurred:

ChandraTimeError                          Traceback (most recent call last)
Cell In[2], line 1
----> 1 Chandra.Time.DateTime(chandra_time.DateTime())

File ~/miniconda3/envs/ska3/lib/python3.10/site-packages/Chandra/Time/Time.py:766, in DateTime.__init__(self, time_in, format)
    761 # Handle the case of astropy Time or CxoTime input by first checking if
    762 # it is likely one of those (without importing astropy), and if so then
    763 # make sure by checking the object type explicitly.  Once there is a match
    764 # then just convert to cxcsec (secs).
    765 try:
--> 766     secs = time_in.cxcsec
    767     # For working in Chandra operations, possibly with no network access, we cannot
    768     # allow auto downloads.
    769     from astropy.utils import iers

File ~/miniconda3/envs/ska3/lib/python3.10/site-packages/chandra_time/Time.py:798, in DateTime.__getattr__(self, fmt_out)
    795 if fmt_out.startswith('_'):
    796     return self.__getattribute__(fmt_out)
--> 798 return convert(self.time_in,
    799                fmt_in=self.format,
    800                fmt_out=fmt_out,
    801                )

File ~/miniconda3/envs/ska3/lib/python3.10/site-packages/chandra_time/Time.py:646, in convert(time_in, sys_in, fmt_in, sys_out, fmt_out)
    644     return [_convert(x, sys_in, fmt_in, sys_out, fmt_out) for x in time_in]
    645 except TypeError:
--> 646     return _convert(time_in, sys_in, fmt_in, sys_out, fmt_out)

File ~/miniconda3/envs/ska3/lib/python3.10/site-packages/chandra_time/Time.py:688, in _convert(time_in, sys_in, fmt_in, sys_out, fmt_out)
    686         break
    687 else:
--> 688     raise ChandraTimeError("Invalid output format '%s'" % fmt_out)
    690 if sys_out:
    691     if sys_out in time_system:

ChandraTimeError: Invalid output format 'cxcsec'