Python Forum
Trouble on importing function from astropy library
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble on importing function from astropy library
#1
Reference is made to online manual, here http://docs.astropy.org/en/stable/api/as....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
Reply
#2
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``
Reply
#3
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'
# -----------------------------
Reply
#4
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!
Reply
#5
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>]
# -----------------------------
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling a function (which accesses a library) from another file mouse9095 4 767 Jun-07-2023, 08:55 PM
Last Post: deanhystad
  python standard way of importing library mg24 1 872 Nov-15-2022, 01:41 AM
Last Post: deanhystad
  Problem with importing python-telegram library into the project gandonio 1 1,515 Nov-01-2022, 02:19 AM
Last Post: deanhystad
  Run a Function?: Trouble with Online Python Course webmanoffesto 3 967 Aug-18-2022, 10:14 PM
Last Post: deanhystad
  How to use a function from third party library? rrowhe4d 2 1,812 Aug-31-2021, 04:30 PM
Last Post: Larz60+
  Override a library function that is not in __all__ Weird 7 3,219 Aug-23-2021, 05:03 PM
Last Post: Larz60+
  Importing a function from another file runs the old lines also dedesssse 6 2,478 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  python library not defined in user defined function johnEmScott 2 3,768 May-30-2020, 04:14 AM
Last Post: DT2000
  importing same python library in multiple custom module escape_freedom13 6 3,723 May-10-2020, 07:01 PM
Last Post: escape_freedom13
  Trouble with list function Eggman72 2 1,711 Mar-23-2020, 09:36 PM
Last Post: Eggman72

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020