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
  Importing a function from another file runs the old lines also dedesssse 6 226 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  python library not defined in user defined function johnEmScott 2 853 May-30-2020, 04:14 AM
Last Post: DT2000
  importing same python library in multiple custom module escape_freedom13 6 1,305 May-10-2020, 07:01 PM
Last Post: escape_freedom13
  Trouble with list function Eggman72 2 636 Mar-23-2020, 09:36 PM
Last Post: Eggman72
  Problem importing resource library ChrisM 8 1,347 Oct-23-2019, 01:40 PM
Last Post: ChrisM
  Importing/loading a library file (.so) tomasby 0 914 Aug-24-2019, 08:13 PM
Last Post: tomasby
  importing datetime trouble Dixon 4 2,527 Aug-24-2019, 11:47 AM
Last Post: satishm
  Trouble importing modules on a new python version snackman_barry 2 1,068 Jul-12-2019, 11:15 AM
Last Post: snackman_barry
  Conditional importing inside a function Roger 1 2,907 Jul-03-2019, 10:02 PM
Last Post: Gribouillis
  Help Importing Protocol Library Into Scapy joedirgy 0 994 May-02-2019, 07:31 PM
Last Post: joedirgy

Forum Jump:

User Panel Messages

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