导入astroplan模块时如何处理OldEarthOrientationDataWarning更新IERS表(Python3)

2024-10-01 15:33:11 发布

您现在位置:Python中文网/ 问答频道 /正文

对于我当前使用的模块之一(astroplan),它必须下载一个IERS表,以便它的Observer对象中的计时是准确的。(我用它锻炼日出/日落时间等)。问题是在14天左右之后,这个表就过时了,需要下载一个新的表。一旦表过期,则在导入模块时会引发OldEarthOrientationDataWarning警告。你知道吗

我想做的是,检测是否引发了警告,并在出现警告时更新表。你知道吗

通常使用以下方法导入模块:

>>> from astroplan import Observer

我知道如何使用以下方法手动更新表:

>>> from astroplan import download_IERS_A
>>> download_IERS_A()
>>> from astroplan import Observer

我还知道Python有一个warnings模块,我认为catch\u warnings函数就是我需要使用的。关于如何让代码忽略弃用警告,有很多例子,但我不想隐藏警告。我只是不知道该怎么安排才能让它做我想做的事。你知道吗

我使用的是python3,需要在UNIX机器上运行的代码。你知道吗

任何帮助都将不胜感激,谢谢。你知道吗


Tags: 模块对象方法代码fromimport警告download
1条回答
网友
1楼 · 发布于 2024-10-01 15:33:11

我想我自己已经整理好了。我会把代码贴在下面,以防有人想做同样的事情。你知道吗

import warnings

# Set up the thing to catch the warning (and potentially others)
with warnings.catch_warnings(record=True) as w:
    # import the modules
    from astroplan import Observer
    from astroplan import OldEarthOrientationDataWarning
    #One want to know aout the first time a warning is thrown
    warnings.simplefilter("once")

#Look through all the warnings to see if one is OldEarthOrientationDataWarning,
# update the table if it is.
for i in w:
    if i.category == OldEarthOrientationDataWarning:
        # This new_mess statement isn't really needed I just didn't want to print
        #  all the information that is produce in the warning.
        new_mess = '.'.join(str(i.message).split('.')[:3])
        print('WARNING:',new_mess)
        print('Updating IERS bulletin table...')
        from astroplan import download_IERS_A
        download_IERS_A()

相关问题 更多 >

    热门问题