Python Forum
Trouble on importing function from astropy library - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Trouble on importing function from astropy library (/thread-16931.html)



Trouble on importing function from astropy library - samsonite - Mar-20-2019

Reference is made to online manual, here http://docs.astropy.org/en/stable/api/astropy.utils.iers.IERS.html

I'ld like to run this code:
# -------- tempo5.py ---------------
from astropy.time import Time
#from astropy.utils.iers import pm_xy  # error arises, one more tentative below
from astropy.iers_table import pm_xy   
t = Time('2007-04-05 12:00:0.0', format='iso', scale='utc', precision=6)
#
print (t.utc.iso, t.utc.mjd) 
print (t.utc.jd1, t.utc.jd2)
#
print (pm_xy(t.utc.jd1, t.utc.jd2))

#---------- OUTPUT ----------
# 2007-04-05 12:00:00.000000 54195.5
# 2454196.0 0.0  <--- corrected values of (jd1,jd2)
#
# ------ Error -------- (from astropy.iers_table import pm_xy) --
# C:\Training>python tempo5.py
# Traceback (most recent call last):
#  File "tempo5.py", line 4, in <module>
#    from astropy.iers_table import pm_xy
# ModuleNotFoundError: No module named 'astropy.iers_table'
Help needed on selecting the appropriate class to import the function pm_xy(), thanks in advance and cheers


RE: Trouble on importing function from astropy library - snippsat - Mar-20-2019

Look at doc it say astropy.utils.iers.IERS,so have to have IERS in there.
Quick test test in virtual environment,with ptpython to explore as autocompletion is good to use to see what's is a package.
(del_env) E:\div_code\del_env
λ ptpython

>>> from astropy.utils.iers import IERS
>>> IERS.pm_xy
<function IERS.pm_xy at 0x05FB5A98>

# Just with main import would be
>>> import astropy
>>> astropy.utils.iers.IERS.pm_xy
<function IERS.pm_xy at 0x05FB5A98>


>>> help(IERS.pm_xy)
Help on function pm_xy in module astropy.utils.iers.iers:

pm_xy(self, jd1, jd2=0.0, return_status=False)
    Interpolate polar motions from IERS Table for given dates.

    Parameters
    ----------
    jd1 : float, float array, or Time object
        first part of two-part JD, or Time object
    jd2 : float or float array, optional
        second part of two-part JD.
        Default is 0., ignored if jd1 is `~astropy.time.Time`.
    return_status : bool
        Whether to return status values.  If False (default),
        raise ``IERSRangeError`` if any time is out of the range covered
        by the IERS table.

    Returns
    -------
    PM_x : Quantity with angle units
        x component of polar motion for the requested times
    PM_y : Quantity with angle units
        y component of polar motion for the requested times
    status : int or int array
        Status values (if ``return_status``=``True``)::
        ``iers.FROM_IERS_B``
        ``iers.FROM_IERS_A``
        ``iers.FROM_IERS_A_PREDICTION``
        ``iers.TIME_BEFORE_IERS_RANGE``
        ``iers.TIME_BEYOND_IERS_RANGE``



RE: Trouble on importing function from astropy library - samsonite - Mar-20-2019

Thanks a lot snippsat. Something to add for reaching the target, as shown below. I've no idea about the mentioned interpolate attribute.

# -------- tempo6.py ---------------
from astropy.time import Time
from astropy.utils.iers import IERS
t = Time('2007-04-05 12:00:0.0', format='iso', scale='utc', precision=6)
#
print (t.utc.iso, t.utc.mjd) 
print (t.utc.jd1, t.utc.jd2)
#
j1=float(t.utc.jd1)
j2=float(t.utc.jd2)
print(j1,j2)
#
print (IERS.pm_xy(j1,j2))

#---------- OUTPUT ----------
# C:\Training>python tempo6.py
# 2007-04-05 12:00:00.000000 54195.5
# 2454196.0 0.0
# 2454196.0 0.0
# Traceback (most recent call last):
#  File "tempo6.py", line 13, in <module>
#    print (IERS.pm_xy(j1,j2))
#  File "C:\Users\Alfabeta\AppData\Local\Programs\Python\Python37\lib\site-packages\astropy\utils\iers\iers.py", lin
# e 294, in pm_xy
#    return self._interpolate(jd1, jd2, ['PM_x', 'PM_y'],
#AttributeError: 'float' object has no attribute '_interpolate'
# -----------------------------



RE: Trouble on importing function from astropy library - samsonite - Mar-21-2019

The drawback comes from the file iers.py whose line 294 doesn't work properly, as shown here http://imgbox.com/vWuJoMkN
So, the matter doesn't pertain this thread, which should be marked as solved.
Thank you, forum!

Just marked as solved!


RE: Trouble on importing function from astropy library - samsonite - Mar-21-2019

Completely defined, now!
Output:
[<Quantity 0.034465 arcsec>, <Quantity 0.483674 arcsec>]
# -------- tempo8.py ---------------
from astropy.time import Time
from astropy.utils import iers
t = Time('2007-04-05 12:00:0.0', format='iso', scale='utc', precision=6)
#
iers_b = iers.IERS_B.open()
print (iers_b.ut1_utc(t))
t.delta_ut1_utc = iers_b.ut1_utc(t)
print(t.delta_ut1_utc)
print(t.ut1.iso, t.ut1.mjd)
print(t.utc.iso, t.utc.mjd)
#
print (t.utc.iso, t.utc.mjd) 
print (t.utc.jd1, t.utc.jd2)
#
j1=float(t.utc.jd1)
j2=float(t.utc.jd2)
#
print (iers_b.pm_xy(j1,j2))    # appropriate command ... WOW!

#---------- OUTPUT ----------
# -0.07209225 s
# -0.07209225
# 2007-04-05 11:59:59.927908 54195.4999991656
# 2007-04-05 12:00:00.000000 54195.5
# 2007-04-05 12:00:00.000000 54195.5
# 2454196.0 0.0
# [<Quantity 0.034465 arcsec>, <Quantity 0.483674 arcsec>]
# -----------------------------