From 0a1d8f4cd17bf8ae2a76291aca02b40ebe5b72b3 Mon Sep 17 00:00:00 2001 From: Marshall Perrin Date: Wed, 18 Dec 2024 14:38:50 -0500 Subject: [PATCH 1/3] adjust/enhance auto data install: if data was already installed, no need to emit warning. --- stpsf/utils.py | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/stpsf/utils.py b/stpsf/utils.py index 1f83c25..06f5f97 100644 --- a/stpsf/utils.py +++ b/stpsf/utils.py @@ -3,6 +3,7 @@ import sys import warnings from collections import OrderedDict +from pathlib import Path import astropy.io.fits as fits import astropy.units as u @@ -20,6 +21,13 @@ _Strehl_perfect_cache = {} # dict for caching perfect images used in Strehl calcs. +# import deprecation decorator. This is in the standard library as of python 3.13, +# or available from typing_extensions backported to earlier versions +if sys.version_info >= (3, 13): + from warnings import deprecated +else: + from typing_extensions import deprecated + # Helper routines for logging: ### @@ -179,16 +187,18 @@ def setup_logging(level='INFO', filename=None): **************************************************************************** """ +def get_default_data_path(): + return Path.home() / "data" / "stpsf-data" + def auto_download_stpsf_data(): - import os import tarfile from pathlib import Path from tempfile import TemporaryDirectory from urllib.request import urlretrieve # Create a default directory for the data files - default_path = Path.home() / "data" / "stpsf-data" + default_path = get_default_data_path() default_path.mkdir(parents=True, exist_ok=True) os.environ["STPSF_PATH"] = str(default_path) @@ -206,6 +216,7 @@ def auto_download_stpsf_data(): # Extract the tarball with tarfile.open(filename, "r:gz") as tar: tar.extractall(default_path.parent, filter="fully_trusted") + warnings.warn(f"STPSF data files downloaded and installed to {default_path}.") if not any(default_path.iterdir()): raise IOError(f"Failed to get and extract STPSF data files to {default_path}") @@ -216,30 +227,32 @@ def auto_download_stpsf_data(): def get_stpsf_data_path(data_version_min=None, return_version=False): """Get the STPSF data path - Simply checking an environment variable is not always enough, since - for packaging this code as a Mac .app bundle, environment variables are - not available since .apps run outside the Terminal or X11 environments. - - Therefore, check first the environment variable STPSF_PATH, and secondly + Check first the environment variable STPSF_PATH, and secondly check the configuration file in the user's home directory. If data_version_min is supplied (as a 3-tuple of integers), it will be compared with the version number from version.txt in the STPSF data package. """ - import os - from pathlib import Path path_from_config = conf.STPSF_PATH # read from astropy configuration if path_from_config == 'from_environment_variable': path = os.getenv('STPSF_PATH') if path is None: - message = ( - 'Environment variable $STPSF_PATH is not set!\n' - f'{MISSING_STPSF_DATA_MESSAGE} searching default location..s' - ) - warnings.warn(message) - path = auto_download_stpsf_data() + + # Check if the data were already downloaded to the default path + default_path = get_default_data_path() + if os.path.exists(default_path): + # If so, let's look there + path = default_path + else: + # If not, let's try to download the data + message = ( + 'Environment variable $STPSF_PATH is not set!\n' + f'{MISSING_STPSF_DATA_MESSAGE} searching default location.' + ) + warnings.warn(message) + path = auto_download_stpsf_data() else: path = path_from_config @@ -281,6 +294,11 @@ def get_stpsf_data_path(data_version_min=None, return_version=False): return str(path) +@deprecated("Use get_stpsf_data_path instead.") +def get_webbpsf_data_path(**kwargs): + """ Back compatibility alias, to support existing code that calls get_webbpsf_data_path""" + return get_stpsf_data_path() + DIAGNOSTIC_REPORT = """ OS: {os} CPU: {cpu} @@ -1069,4 +1087,4 @@ def get_target_phase_map_filename(apername): raise ValueError("File wss_target_phase_{}.fits, \ not found under {}.".format(apername.split('_')[1].lower(),path)) - return was_targ_file \ No newline at end of file + return was_targ_file From eff743f83dfc7ba9359d07512931bd1ab5fe226f Mon Sep 17 00:00:00 2001 From: Marshall Perrin Date: Wed, 18 Dec 2024 15:45:22 -0500 Subject: [PATCH 2/3] update install docs for data auto install --- docs/installation.rst | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/docs/installation.rst b/docs/installation.rst index 94a968f..d9d2d70 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -40,14 +40,6 @@ STPSF and its underlying optical library POPPY may be installed from the `Python Successfully installed stpsf -Note that ``pip install stpsf`` only installs the program code. **If you install via pip, you must manually download and install the data files, as** :ref:`described ` **below.** -To obtain source spectra for calculations, you should also follow :ref:`installation instructions for synphot `. - -.. note:: - Installation through conda is not available as of STPSF version 1.1.0. Conda - users should instead follow the insructions in the preceding section to - install via pip. - .. _synphot_install: @@ -61,13 +53,14 @@ Stsynphot is an optional dependency, but is highly recommended. Its installation Installing the Required Data Files ---------------------------------- -*If you install via pip or manually*, you must install the data files yourself. +.. note:: + The necessary data files will be installed automatically in a default path, ``$HOME/data/stpsf-data``, and there is no user action required unless you want to install them somewhere else. -.. (If you install via Conda, the data files are automatically installed, in - which case you can skip this section.) [uncomment once conda installation is - available again] +Files containing such information as the JWST pupil shape, instrument throughputs, and aperture positions are distributed separately from STPSF. These sum to about 100 MB in size. -Files containing such information as the JWST pupil shape, instrument throughputs, and aperture positions are distributed separately from STPSF. To run STPSF, you must download these files and tell STPSF where to find them using the ``STPSF_PATH`` environment variable. +STPSF will now **automatically download and install these files** the first time it is run. By default these will be downloaded and installed into a folder ``$HOME/data/stpsf-data`` within the user's home directory. This directory will be created automatically if it doesn't exist already. If this path is acceptable to you, then no further action is needed on your part, and it'll all work automatically. + +*If you would like to place the data files at some other location*, you can manually install the data files yourself. 1. Download the following file: `stpsf-data-LATEST.tar.gz `_ [approx. 70 MB] 2. Untar ``stpsf-data-LATEST.tar.gz`` into a directory of your choosing. @@ -75,7 +68,7 @@ Files containing such information as the JWST pupil shape, instrument throughput export STPSF_PATH=$HOME/data/stpsf-data -for bash. (You will probably want to add this to your ``.bashrc``.) +for bash. (You will probably want to add this to your ``.bashrc``, or equivalent for the shell of your choice.) You should now be able to successfully ``import stpsf`` in a Python session. @@ -87,13 +80,13 @@ You should now be able to successfully ``import stpsf`` in a Python session. .. Note:: **For STScI Users Only:** Users at STScI may access the required data files from the Central Storage network. Set the following environment variables in your ``bash`` shell. (You will probably want to add this to your ``.bashrc``.) :: + export STPSF_PATH="/grp/stpsf/stpsf-data" export PYSYN_CDBS="/grp/hst/cdbs" Software Requirements --------------------- - See `the requirements.txt specification file `_ for the required package dependencies. **Required Python version**: STPSF 1.1 and above require Python 3.10 or higher. From df4a7c9f0ec67c4ee89754e3829fbf8d30dbb529 Mon Sep 17 00:00:00 2001 From: Marshall Perrin Date: Wed, 18 Dec 2024 16:19:56 -0500 Subject: [PATCH 3/3] remove get_webbpsf_data_path compatibility layer --- stpsf/utils.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/stpsf/utils.py b/stpsf/utils.py index 06f5f97..2b6df8c 100644 --- a/stpsf/utils.py +++ b/stpsf/utils.py @@ -21,13 +21,6 @@ _Strehl_perfect_cache = {} # dict for caching perfect images used in Strehl calcs. -# import deprecation decorator. This is in the standard library as of python 3.13, -# or available from typing_extensions backported to earlier versions -if sys.version_info >= (3, 13): - from warnings import deprecated -else: - from typing_extensions import deprecated - # Helper routines for logging: ### @@ -294,11 +287,6 @@ def get_stpsf_data_path(data_version_min=None, return_version=False): return str(path) -@deprecated("Use get_stpsf_data_path instead.") -def get_webbpsf_data_path(**kwargs): - """ Back compatibility alias, to support existing code that calls get_webbpsf_data_path""" - return get_stpsf_data_path() - DIAGNOSTIC_REPORT = """ OS: {os} CPU: {cpu}