From c7575299cdce37107ef89eddfe3ac93c20b8f799 Mon Sep 17 00:00:00 2001 From: Anne Archibald Date: Fri, 12 Aug 2022 13:02:41 +0100 Subject: [PATCH] Test preloading works --- src/pint/utils.py | 39 ++++++++-- tests/test_utils.py | 168 +++++++++++++++++++++++--------------------- 2 files changed, 119 insertions(+), 88 deletions(-) diff --git a/src/pint/utils.py b/src/pint/utils.py index 2775d7253..9427f9a7b 100644 --- a/src/pint/utils.py +++ b/src/pint/utils.py @@ -38,7 +38,7 @@ import sys import textwrap from collections import OrderedDict -from contextlib import contextmanager +from contextlib import contextmanager, ExitStack from copy import deepcopy from io import StringIO from pathlib import Path @@ -1673,10 +1673,12 @@ def preload_cache(extra_ephemerides=None): update_all() IERS_Auto.open() ephemerides = [ - "de430", + "de200", "de405", "de421", + "de430", "de430t", + "de432s", "de434", "de436", "de436t", @@ -1703,11 +1705,34 @@ def set_no_internet(mode="warn"): mode : 'warn' or 'ignore' What to do when files appear to be out of date """ - from astropy.utils.data import conf + # We use ExitStack here to "cheat" and make the effect of the context + # manager permanent + with ExitStack() as stack: + stack.enter_context(no_internet) + # Exit without finalizing the with statements + stack.pop_all() + + +@contextmanager +def no_internet(mode="warn"): + """Run Astropy and PINT in a context without Internet access. + + The sets up a number of Astropy configuration options. If you want to achieve + this effect without having to add this line to your scripts, you can create + an Astropy config file and edit it to contain these same options. See + https://docs.astropy.org/en/stable/config/index.html#astropy-config + for details of how to do this. - conf.auto_download = False - conf.allow_internet = False + Parameters + ---------- + mode : 'warn' or 'ignore' + What to do when files appear to be out of date + """ + from astropy.utils.data import conf import astropy.utils.iers - astropy.ustils.iers.conf.auto_download = False - astropy.ustils.iers.conf.iers_degraded_accuracy = mode + with conf.set_temp("auto_download", False): + with conf.set_temp("allow_internet", False): + with astropy.ustils.iers.conf.set_temp("auto_download", False): + with astropy.ustils.iers.conf.set_temp("iers_degraded_accuracy", warn): + yield diff --git a/tests/test_utils.py b/tests/test_utils.py index 9fcdf76ca..c82f32fec 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -11,6 +11,7 @@ import pytest import scipy.stats from astropy.time import Time +from astropy.config import set_temp_cache from hypothesis import assume, example, given from hypothesis.extra.numpy import array_shapes, arrays, scalar_dtypes from hypothesis.strategies import ( @@ -769,84 +770,89 @@ def test_compute_hash_accepts_no_change(a): assert h_a == h_b -def test_preload_cache_gives_all_expected_files(): - # FIXME: set up temporary cache - preload_cache() - expected = [ - "ftp://anonymous:mail%40astropy.org@gdc.cddis.eosdis.nasa.gov/pub/products/iers/finals2000A.all", - "ftp://ssd.jpl.nasa.gov/pub/eph/planets/bsp/de200.bsp", - "http://data.astropy.org/coordinates/sites.json", - "http://hpiers.obspm.fr/iers/eop/eopc04/eopc04_IAU2000.62-now", - "https://data.nanograv.org/static/data/ephem/de405.bsp", - "https://data.nanograv.org/static/data/ephem/de421.bsp", - "https://data.nanograv.org/static/data/ephem/de430t.bsp", - "https://data.nanograv.org/static/data/ephem/de434.bsp", - "https://data.nanograv.org/static/data/ephem/de436.bsp", - "https://data.nanograv.org/static/data/ephem/de436t.bsp", - "https://datacenter.iers.org/data/9/finals2000A.all", - "https://hpiers.obspm.fr/iers/bul/bulc/Leap_Second.dat", - "https://naif.jpl.nasa.gov/pub/naif/generic_kernels/spk/planets/de430.bsp", - "https://naif.jpl.nasa.gov/pub/naif/generic_kernels/spk/planets/de432s.bsp", - "https://naif.jpl.nasa.gov/pub/naif/generic_kernels/spk/planets/de440.bsp", - "https://naif.jpl.nasa.gov/pub/naif/generic_kernels/spk/planets/de440s.bsp", - # "https://data.nanograv.org/static/data/ephem/de440.bsp", - # "https://maia.usno.navy.mil/ser7/finals2000A.all", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/ao2gps.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/ao2gps_tempo2.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/ao2nist.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/chime2gps.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/eff2gps.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/effix2gps.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/gbt2gps.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/gbt2gps_tempo2.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/gmrt2gps.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/gps2utc.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/gps2utc_c0p.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/gps2utc_cc.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/gps2utc_tempo2.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/jb2gps.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/jbdfb2jb.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/jbroach2jb.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/mk2utc.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/mk2utc_observatory.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/mo2gps.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/ncyobs2obspm.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/obspm2gps.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/pks2gps.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/srt2gps.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm01.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm05.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm06.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2003.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2004.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2010.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2012.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2013.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2014.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2015.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2016.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2017.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2018.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2019.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2020.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2021.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm92.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/vla2gps.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/vla2nist.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/wsrt2gps.clk", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/index.txt", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/leap.sec", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/time_ao.dat", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/time_fast.dat", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/time_gb140.dat", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/time_gb853.dat", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/time_gbt.dat", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/time_jb.dat", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/time_nuppi.dat", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/time_pks.dat", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/time_vla.dat", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/time_wsrt.dat", - "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/ut1.dat", - ] - for k in expected: - assert k in astropy.utils.data.cache_contents() +def test_preload_cache_gives_all_expected_files(tmp_path): + with set_temp_cache(): + preload_cache() + expected = [ + # "ftp://ssd.jpl.nasa.gov/pub/eph/planets/bsp/de200.bsp", + "http://data.astropy.org/coordinates/sites.json", + "http://hpiers.obspm.fr/iers/eop/eopc04/eopc04_IAU2000.62-now", + # Ephemerides are sometimes pulled in via Astropy and sometimes via PINT + # Astropy + "https://naif.jpl.nasa.gov/pub/naif/generic_kernels/spk/planets/de430.bsp", + "https://naif.jpl.nasa.gov/pub/naif/generic_kernels/spk/planets/de432s.bsp", + "https://naif.jpl.nasa.gov/pub/naif/generic_kernels/spk/planets/de440.bsp", + "https://naif.jpl.nasa.gov/pub/naif/generic_kernels/spk/planets/de440s.bsp", + # PINT + "https://data.nanograv.org/static/data/ephem/de200.bsp", + "https://data.nanograv.org/static/data/ephem/de405.bsp", + "https://data.nanograv.org/static/data/ephem/de421.bsp", + "https://data.nanograv.org/static/data/ephem/de430t.bsp", + "https://data.nanograv.org/static/data/ephem/de434.bsp", + "https://data.nanograv.org/static/data/ephem/de436.bsp", + "https://data.nanograv.org/static/data/ephem/de436t.bsp", + # IERS A table + "https://datacenter.iers.org/data/9/finals2000A.all", + # "https://maia.usno.navy.mil/ser7/finals2000A.all", # This is the mirror address, should never be needed I think? + # "ftp://anonymous:mail%40astropy.org@gdc.cddis.eosdis.nasa.gov/pub/products/iers/finals2000A.all", # Another mirror that shouldn't be needed + "https://hpiers.obspm.fr/iers/bul/bulc/Leap_Second.dat", + # Clock corrections + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/ao2gps.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/ao2gps_tempo2.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/ao2nist.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/chime2gps.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/eff2gps.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/effix2gps.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/gbt2gps.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/gbt2gps_tempo2.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/gmrt2gps.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/gps2utc.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/gps2utc_c0p.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/gps2utc_cc.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/gps2utc_tempo2.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/jb2gps.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/jbdfb2jb.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/jbroach2jb.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/mk2utc.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/mk2utc_observatory.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/mo2gps.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/ncyobs2obspm.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/obspm2gps.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/pks2gps.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/srt2gps.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm01.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm05.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm06.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2003.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2004.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2010.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2012.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2013.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2014.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2015.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2016.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2017.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2018.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2019.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2020.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm2021.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/tai2tt_bipm92.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/vla2gps.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/vla2nist.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/T2runtime/clock/wsrt2gps.clk", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/index.txt", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/leap.sec", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/time_ao.dat", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/time_fast.dat", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/time_gb140.dat", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/time_gb853.dat", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/time_gbt.dat", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/time_jb.dat", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/time_nuppi.dat", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/time_pks.dat", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/time_vla.dat", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/time_wsrt.dat", + "https://raw.githubusercontent.com/ipta/pulsar-clock-corrections/main/tempo/clock/ut1.dat", + ] + for k in expected: + assert k in astropy.utils.data.cache_contents()