Skip to content

Commit

Permalink
use SNCOSMO_DATA_DIR environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
kbarbary committed Jan 8, 2017
1 parent 7c39062 commit 4e1af31
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 30 deletions.
53 changes: 32 additions & 21 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,35 @@ Configuring the Directories
What if you would rather use a different directory to store downloaded
data? Perhaps you'd rather the data not be in a hidden directory, or
perhaps there are multiple users who wish to use a shared data
directory. This is where the configuration file comes in. This file is
found in the astropy configuration directory, e.g.,
``$HOME/.astropy/config/sncosmo.cfg``. When you ``import sncosmo`` it
checks for this file and creates a default one if it doesn't
exist. The default one looks like this::

$ cat ~/.astropy/config/sncosmo.cfg

## Directory containing SFD (1998) dust maps, with names:
## 'SFD_dust_4096_ngp.fits' and 'SFD_dust_4096_sgp.fits'
## Example: sfd98_dir = /home/user/data/sfd98
# sfd98_dir = None

## Directory where sncosmo will store and read downloaded data resources.
## If None, ASTROPY_CACHE_DIR/sncosmo will be used.
## Example: data_dir = /home/user/data/sncosmo
# data_dir = None

To change the data directory, simply uncomment the last line and set it to the
desired directory. You can even move the data directory around, as long as you
update this configuration parameter accordingly.
directory. There are two options:

1. Set the environment variable ``SNCOSMO_DATA_DIR`` to the directory you
wish to use. For example, in bash::

export SNCOSMO_DATA_DIR=/home/user/data/sncosmo

If this environment variable is set, it takes precedence over the
second option (below).

2. Set the ``data_dir`` variable in the sncosmo configuartion file.
This file is found in the astropy configuration directory, e.g.,
``$HOME/.astropy/config/sncosmo.cfg``. When you ``import sncosmo`` it
checks for this file and creates a default one if it doesn't
exist. The default one looks like this::

$ cat ~/.astropy/config/sncosmo.cfg

## Directory containing SFD (1998) dust maps, with names:
## 'SFD_dust_4096_ngp.fits' and 'SFD_dust_4096_sgp.fits'
## Example: sfd98_dir = /home/user/data/sfd98
# sfd98_dir = None

## Directory where sncosmo will store and read downloaded data resources.
## If None, ASTROPY_CACHE_DIR/sncosmo will be used.
## Example: data_dir = /home/user/data/sncosmo
# data_dir = None

To change the data directory, simply uncomment the last line and set
it to the desired directory. You can even move the data directory
around, as long as you update this configuration parameter
accordingly.
5 changes: 5 additions & 0 deletions docs/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ v1.5.0 (unreleased)
>>> data['Fluxcov'].shape == (len(data), len(data))
True

- The environment variable ``SNCOSMO_DATA_DIR`` can be used to set the
path to the data directory. If set, it takes precedence over the
``data_dir`` variable in the configuration file
(``$HOME/.astropy/config/sncosmo.cfg``).

v1.4.0 (2016-11-16)
===================

Expand Down
36 changes: 28 additions & 8 deletions sncosmo/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ def get_url(name, version=None):
return urls[key]


CHECKED_DATA_DIR = None


def get_data_dir():
"""Return the full path to the data directory, ensuring that it exists.
Expand All @@ -64,21 +67,38 @@ def get_data_dir():
Otherwise, uses `(astropy cache dir)/sncosmo` (automatically created if
it doesn't exist)."""
if conf.data_dir is not None:
if os.path.isdir(conf.data_dir):
return conf.data_dir
else:

global CHECKED_DATA_DIR

# use a global to avoid checking if the directory exists every time
if CHECKED_DATA_DIR is not None:
return CHECKED_DATA_DIR

# use the environment variable if set
data_dir = os.environ.get('SNCOSMO_DATA_DIR')

# otherwise, use config file value if set.
if data_dir is None:
data_dir = conf.data_dir

# if either of the above, check existance
if data_dir is not None:
if not os.path.isdir(data_dir):
raise RuntimeError(
"data directory {0!r} not an existing directory"
.format(conf.data_dir))
else:

# if still None, use astropy cache dir and create if necessary
if data_dir is None:
data_dir = join(get_cache_dir(), "sncosmo")
if not os.path.isdir(data_dir):
if os.path.exists(data_dir):
raise RuntimeError("{0} not a directory".format(data_dir))
else:
os.mkdir(data_dir)
return data_dir
os.mkdir(data_dir)

CHECKED_DATA_DIR = data_dir

return data_dir


def get_abspath(relpath, name, version=None):
Expand Down
2 changes: 1 addition & 1 deletion sncosmo/tests/test_salt2source.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@ def test_salt2source_rcov_vs_snfit():
ref = np.loadtxt(fname, skiprows=1)

rcov = model._bandflux_rcov(band, times)
assert_allclose(ref, rcov, rtol=1.e-5)
assert_allclose(ref, rcov, rtol=5.e-5)

0 comments on commit 4e1af31

Please sign in to comment.