Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor(datasets): improve all dataset downloading logic to reuse ex…
…isting high-res models instead of downloading lower-res models.

- Context:
    - Some datasets have multiple resolutions, e.g. `pyshtools.datasets.Mars.MOLA_shape` has degrees 5759, 2879, 1439, and 719.
- Current behavior:
    - When a user loads a dataset, pyshtools chooses the smallest model which satisfies the user's input lmax -- for example, calling `MOLA_shape(2000)` will download the 2879 model. However, if you then call `MOLA_shape(1000)`, pyshtools will download the 1439 model. We would save space/time to reuse the previously downloaded 2879 model.
- Improvement:
    - This commit makes it so in aforementioned scenario, pyshtools will load the existing 2879 model.
    - In terms of code, all datasets with multiple resolutions now call the method `_choose_sh_model(...)` (located in `/pyshtools/datasets/_utils.py`). This takes the user's desired lmax and a pooch object containing available models, and either loads any existing model with sufficient resolution, or downloads the smallest model with enough resolution.
  • Loading branch information
Humboldt-Penguin committed Aug 24, 2025
commit b38cdc963ae0cbc3de679bb1a62956f60789667e
36 changes: 9 additions & 27 deletions pyshtools/datasets/Ceres.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
from pooch import retrieve as _retrieve
from pooch import create as _create
from pooch import HTTPDownloader as _HTTPDownloader
from pooch import DOIDownloader as _DOIDownloader
from ..shclasses import SHGravCoeffs as _SHGravCoeffs
from ..shclasses import SHCoeffs as _SHCoeffs
from ..constants.Ceres import angular_velocity as _omega
from ._utils import _choose_sh_model


def DLR_SPG_shape(lmax=719):
Expand Down Expand Up @@ -59,22 +59,10 @@ def DLR_SPG_shape(lmax=719):
},
)

if lmax < 0:
lmax = 5399

if lmax >= 0 and lmax <= 719:
fname = archive.fetch("Ceres_DLR_SPG_shape_719.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
elif lmax > 719 and lmax <= 1439:
fname = archive.fetch("Ceres_DLR_SPG_shape_1439.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
elif lmax > 1439 and lmax <= 2879:
fname = archive.fetch("Ceres_DLR_SPG_shape_2879.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
else:
fname = archive.fetch("Ceres_DLR_SPG_shape_5399.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
lmax = min(lmax, 5399)
fname, lmax = _choose_sh_model(
archive=archive,
user_lmax=lmax,
)

return _SHCoeffs.from_file(fname, lmax=lmax, name='DLR_SPG_shape (Ceres)',
units='m', format='bshc')
Expand Down Expand Up @@ -120,16 +108,10 @@ def JPL_SPC_shape(lmax=719):
},
)

if lmax < 0:
lmax = 1023

if lmax >= 0 and lmax <= 719:
fname = archive.fetch("Ceres_JPL_SPC_shape_719.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
else:
fname = archive.fetch("Ceres_JPL_SPC_shape_1023.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
lmax = min(lmax, 1023)
fname, lmax = _choose_sh_model(
archive=archive,
user_lmax=lmax,
)

return _SHCoeffs.from_file(fname, lmax=lmax, name='JPL_SPC_shape (Ceres)',
units='m', format='bshc')
Expand Down
15 changes: 5 additions & 10 deletions pyshtools/datasets/Enceladus.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from ..shclasses import SHCoeffs as _SHCoeffs
from ..shclasses import SHGravCoeffs as _SHGravCoeffs
from ..constants.Enceladus import angular_velocity as _omega
from ._utils import _choose_sh_model


def JPL_SPC_shape(lmax=719):
Expand Down Expand Up @@ -57,16 +58,10 @@ def JPL_SPC_shape(lmax=719):
},
)

if lmax < 0:
lmax = 1023

if lmax >= 0 and lmax <= 719:
fname = archive.fetch("Enceladus_JPL_SPC_shape_719.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
else:
fname = archive.fetch("Enceladus_JPL_SPC_shape_1023.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
lmax = min(lmax, 1023)
fname, lmax = _choose_sh_model(
archive=archive,
user_lmax=lmax,
)

return _SHCoeffs.from_file(fname, lmax=lmax,
name='JPL_SPC_shape (Enceladus)',
Expand Down
21 changes: 5 additions & 16 deletions pyshtools/datasets/Mars/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from pooch import Decompress as _Decompress
from ...constants.Mars import angular_velocity as _omega
from . import historical # noqa: F401
from .._utils import _choose_sh_model


def MOLA_shape(lmax=719):
Expand Down Expand Up @@ -66,22 +67,10 @@ def MOLA_shape(lmax=719):
},
)

if lmax < 0:
lmax = 5759

if lmax >= 0 and lmax <= 719:
fname = archive.fetch("Mars_MOLA_shape_719.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
elif lmax > 719 and lmax <= 1439:
fname = archive.fetch("Mars_MOLA_shape_1439.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
elif lmax > 1439 and lmax <= 2879:
fname = archive.fetch("Mars_MOLA_shape_2879.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
else:
fname = archive.fetch("Mars_MOLA_shape_5759.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
lmax = min(lmax, 5759)
fname, lmax = _choose_sh_model(
archive=archive,
user_lmax=lmax,
)

return _SHCoeffs.from_file(fname, lmax=lmax, name='MOLA_shape (Mars)',
units='m', format='bshc')
Expand Down
22 changes: 5 additions & 17 deletions pyshtools/datasets/Mercury.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
from pooch import retrieve as _retrieve
from pooch import create as _create
from pooch import HTTPDownloader as _HTTPDownloader
from pooch import DOIDownloader as _DOIDownloader
from ..shclasses import SHCoeffs as _SHCoeffs
from ..shclasses import SHGravCoeffs as _SHGravCoeffs
from ..constants.Mercury import angular_velocity as _omega
from ._utils import _choose_sh_model


def USGS_SPG_shape(lmax=719):
Expand Down Expand Up @@ -59,22 +59,10 @@ def USGS_SPG_shape(lmax=719):
},
)

if lmax < 0:
lmax = 5759

if lmax >= 0 and lmax <= 719:
fname = archive.fetch("Mercury_shape_719.sh.gz",
downloader=_DOIDownloader(progressbar=True))
elif lmax > 719 and lmax <= 1439:
fname = archive.fetch("Mercury_shape_1439.sh.gz",
downloader=_DOIDownloader(progressbar=True))
elif lmax > 1439 and lmax <= 2879:
fname = archive.fetch("Mercury_shape_2879.sh.gz",
downloader=_DOIDownloader(progressbar=True))
else:
fname = archive.fetch("Mercury_shape_5759.sh.gz",
downloader=_DOIDownloader(progressbar=True))
lmax = min(lmax, 5759)
fname, lmax = _choose_sh_model(
archive=archive,
user_lmax=lmax,
)

return _SHCoeffs.from_file(fname, lmax=lmax,
name='USGS SPG_shape (Mercury)', units='m',
Expand Down
44 changes: 9 additions & 35 deletions pyshtools/datasets/Moon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from ...shclasses import SHMagCoeffs as _SHMagCoeffs
from ...constants.Moon import angular_velocity as _omega
from . import historical # noqa: F401
from .._utils import _choose_sh_model


def LDEM_shape_pa(lmax=719):
Expand Down Expand Up @@ -75,25 +76,10 @@ def LDEM_shape_pa(lmax=719):
},
)

if lmax < 0:
lmax = 11519

if lmax >= 0 and lmax <= 719:
fname = archive.fetch("Moon_LDEM128_shape_pa_719.sh.gz",
downloader=_DOIDownloader(progressbar=True))
elif lmax > 719 and lmax <= 1439:
fname = archive.fetch("Moon_LDEM128_shape_pa_1439.sh.gz",
downloader=_DOIDownloader(progressbar=True))
elif lmax > 1439 and lmax <= 2879:
fname = archive.fetch("Moon_LDEM128_shape_pa_2879.sh.gz",
downloader=_DOIDownloader(progressbar=True))
elif lmax > 2879 and lmax <= 5759:
fname = archive.fetch("Moon_LDEM128_shape_pa_5759.sh.gz",
downloader=_DOIDownloader(progressbar=True))
else:
fname = archive.fetch("Moon_LDEM128_shape_pa_11519.sh.gz",
downloader=_DOIDownloader(progressbar=True))
lmax = min(lmax, 11519)
fname, lmax = _choose_sh_model(
archive=archive,
user_lmax=lmax,
)

return _SHCoeffs.from_file(fname, lmax=lmax, name='LDEM_shape_pa (Moon)',
units='m', format='bshc')
Expand Down Expand Up @@ -139,22 +125,10 @@ def LOLA_shape(lmax=719):
},
)

if lmax < 0:
lmax = 5759

if lmax >= 0 and lmax <= 719:
fname = archive.fetch("Moon_LOLA_shape_719.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
elif lmax > 719 and lmax <= 1439:
fname = archive.fetch("Moon_LOLA_shape_1439.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
elif lmax > 1439 and lmax <= 2879:
fname = archive.fetch("Moon_LOLA_shape_2879.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
else:
fname = archive.fetch("Moon_LOLA_shape_5759.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
lmax = min(lmax, 5759)
fname, lmax = _choose_sh_model(
archive=archive,
user_lmax=lmax,
)

return _SHCoeffs.from_file(fname, lmax=lmax, name='LOLA_shape (Moon)',
units='m', format='bshc')
Expand Down
21 changes: 5 additions & 16 deletions pyshtools/datasets/Moon/historical/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from pooch import HTTPDownloader as _HTTPDownloader
from pooch import DOIDownloader as _DOIDownloader
from ....shclasses import SHCoeffs as _SHCoeffs
from ..._utils import _choose_sh_model


def LOLA_shape_pa(lmax=719):
Expand Down Expand Up @@ -54,22 +55,10 @@ def LOLA_shape_pa(lmax=719):
},
)

if lmax < 0:
lmax = 5759

if lmax >= 0 and lmax <= 719:
fname = archive.fetch("Moon_LOLA_shape_pa_719.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
elif lmax > 719 and lmax <= 1439:
fname = archive.fetch("Moon_LOLA_shape_pa_1439.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
elif lmax > 1439 and lmax <= 2879:
fname = archive.fetch("Moon_LOLA_shape_pa_2879.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
else:
fname = archive.fetch("Moon_LOLA_shape_pa_5759.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
lmax = min(lmax, 5759)
fname, lmax = _choose_sh_model(
archive=archive,
user_lmax=lmax,
)

return _SHCoeffs.from_file(fname, lmax=lmax, name='LOLA_shape_pa (Moon)',
units='m', format='bshc')
Expand Down
22 changes: 5 additions & 17 deletions pyshtools/datasets/Vesta.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
from pooch import retrieve as _retrieve
from pooch import create as _create
from pooch import HTTPDownloader as _HTTPDownloader
from pooch import DOIDownloader as _DOIDownloader
from ..shclasses import SHGravCoeffs as _SHGravCoeffs
from ..shclasses import SHCoeffs as _SHCoeffs
from ..constants.Vesta import angular_velocity as _omega
from ._utils import _choose_sh_model


def DLR_SPG_shape(lmax=719):
Expand Down Expand Up @@ -57,22 +57,10 @@ def DLR_SPG_shape(lmax=719):
},
)

if lmax < 0:
lmax = 5759

if lmax >= 0 and lmax <= 719:
fname = archive.fetch("Vesta_DLR_SPG_shape_719.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
elif lmax > 719 and lmax <= 1439:
fname = archive.fetch("Vesta_DLR_SPG_shape_1439.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
elif lmax > 1439 and lmax <= 2879:
fname = archive.fetch("Vesta_DLR_SPG_shape_2879.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
else:
fname = archive.fetch("Vesta_DLR_SPG_shape_5759.bshc.gz",
downloader=_DOIDownloader(progressbar=True))
lmax = min(lmax, 5759)
fname, lmax = _choose_sh_model(
archive=archive,
user_lmax=lmax,
)

return _SHCoeffs.from_file(fname, lmax=lmax, name='DLR_SPG_shape (Vesta)',
units='m', format='bshc')
Expand Down
Loading