Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
Merge pull request #6 from SLAC-Gamma-Rays/healpy-fix
Browse files Browse the repository at this point in the history
Healpy fix
  • Loading branch information
youngsm authored Apr 7, 2022
2 parents f6232dd + 2745fb8 commit 08756c2
Show file tree
Hide file tree
Showing 19 changed files with 2,329 additions and 18 deletions.
1 change: 0 additions & 1 deletion .github/CODEOWNERS

This file was deleted.

4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,7 @@ ENV/

# IDE settings
.vscode/
adsgrb/__init__.py
.DS_Store

# grblc settings
grblc/convert/extinction_maps
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ scipy
pandas
glob2
astropy
dustmaps
plotly
ipython
PyPDF2
Expand All @@ -19,3 +18,4 @@ sphinx-math-dollar
numdifftools
emcee
corner
progressbar2
13 changes: 5 additions & 8 deletions grblc/convert/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def ebv2A_b(grb: str, bandpass: str, ra="", dec=""):
"""

from astropy.coordinates import SkyCoord
from dustmaps.sfd import SFDQuery
from .sfd.sfd import SFDQuery

sfd = SFDQuery()

Expand Down Expand Up @@ -161,7 +161,8 @@ def toFlux(
KeyError
If a bandpass is not found in :py:data:`grblc.constants.photometry`.
"""
assert A_b != 0 ^ grb ^ (ra and dec), "Must provide either A_b or grb or ra, dec"
assert bool(A_b != 0) ^ bool(grb) ^ bool(ra and dec), "Must provide either A_b or grb or ra, dec"
_check_dust_maps()

band = re.sub(r"(\'|_|\\|\(.+\))", "", band)
band = re.sub(r"(?<![A-Za-z])([mw]\d{1})", r"uv\1", band)
Expand Down Expand Up @@ -434,12 +435,8 @@ def _check_dust_maps():

data_dir = os.path.join(os.path.dirname(__file__), "extinction_maps")
if not os.path.exists(os.path.join(data_dir, "sfd")):
import dustmaps.sfd
from dustmaps.config import config

config["data_dir"] = data_dir
dustmaps.sfd.fetch()

from .sfd import sfd
sfd.fetch()

# sets directory to the current working directory, or whatever folder you're currently in
directory = os.getcwd()
24 changes: 24 additions & 0 deletions grblc/convert/sfd/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python
#
# __init__.py
# Makes the contents of the package "sfd" discoverable.
#
# Copyright (C) 2016 Gregory M. Green
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# This file has been modified by S. Young to change naming
# to `sfd` instead of `dustmaps`
#
158 changes: 158 additions & 0 deletions grblc/convert/sfd/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#!/usr/bin/env python
#
# config.py
# Allow configuration options to be set.
#
# Copyright (C) 2016 Gregory M. Green
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# This file has been modified by S. Young to change naming
# to `sfd` instead of `dustmaps`
#
import json
import os


class ConfigError(Exception):
pass


class Configuration:
"""
A class that stores the package configuration.
By default, the configuration is loaded from
~/.sfdsrc
This can be overridden by setting the environmental variable
:obj:`SFD_CONFIG_FNAME`.
Paths stored in the configuration file (such as the data
directory, :obj:`data_dir`, can include environmental
variables, which will be expanded.
"""

def __init__(self, fname):
self._success = False
self.fname = fname
self.load()

def load(self):
if os.path.isfile(self.fname):
with open(self.fname) as f:
try:
self._options = json.load(f)
self._success = True
except ValueError as error:
print(('The config file appears to be corrupted:\n\n'
' {fname}\n\n'
'Either fix the config file manually, or overwrite '
'it with a blank configuration as follows:\n\n'
' from grblc.convert.sfd.config import config\n'
' config.reset()\n\n'
'Note that this will delete your configuration! For '
'example, if you have specified a data directory, '
'then sfd will forget about its location.'
).format(fname=self.fname))
self._options = {}
else:
print(('Configuration file not found:\n\n'
' {fname}\n\n'
'To create a new configuration file in the default '
'location, run the following python code:\n\n'
' from grblc.convert.sfd.config import config\n'
' config.reset()\n\n'
'Note that this will delete your configuration! For '
'example, if you have specified a data directory, '
'then sfd will forget about its location.'
).format(fname=self.fname))
self._options = {}
self._success = True

def save(self, force=False):
"""
Saves the configuration to a JSON, in the standard config location.
Args:
force (Optional[:obj:`bool`]): Continue writing, even if the original
config file was not loaded properly. This is dangerous, because
it could cause the previous configuration options to be lost.
Defaults to :obj:`False`.
Raises:
:obj:`ConfigError`: if the configuration file was not successfully
loaded on initialization of the class, and
:obj:`force` is :obj:`False`.
"""
if (not self._success) and (not force):
raise ConfigError((
'The config file appears to be corrupted:\n\n'
' {fname}\n\n'
'Before attempting to save the configuration, please either '
'fix the config file manually, or overwrite it with a blank '
'configuration as follows:\n\n'
' from grblc.convert.sfd.config import config\n'
' config.reset()\n\n'
).format(fname=self.fname))

with open(self.fname, 'w') as f:
json.dump(self._options, f, indent=2)

def __setitem__(self, key, value):
self._options[key] = value
self.save()

def __getitem__(self, key):
return self._options.get(key, None)

def get(self, key, default=None):
"""
Gets a configuration option, returning a default value if the specified
key isn't set.
"""
return self._options.get(key, default)

def remove(self, key):
"""
Deletes a key from the configuration.
"""
self._options.pop(key, None)
self.save()

def reset(self):
"""
Resets the configuration, and overwrites the existing configuration
file.
"""
self._options = {}
self.save(force=True)
self._success = True


# The package configuration filename
default_config_fname = os.path.expanduser('~/.sfdsrc')
config_fname = os.environ.get('SFD_CONFIG_FNAME', default_config_fname)
if default_config_fname != config_fname:
print(f'Overriding default configuration file with {config_fname}')

# The package configuration. By default, this is read from ``~/.sfdsrc``.
# The default location can be overridden by setting the ``SFD_CONFIG_FNAME``
# environment variable.
#
# This is the object that the user should interact with in order to change
# settings. For example, to set the directory where large files (e.g., dust maps)
# will be stored:
#
# .. code-block:: python
#
# from sfd.config import config
# config['data_dir'] = '/path/to/data/directory'
config = Configuration(config_fname)
45 changes: 45 additions & 0 deletions grblc/convert/sfd/dustexceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python
#
# dustexceptions.py
# Defines exceptions for the dustmaps package.
#
# Copyright (C) 2016 Gregory M. Green
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# This file has been modified by S. Young to change naming
# to `sfd` instead of `dustmaps`
#
from . import std_paths

class Error(Exception):
pass

class CoordFrameError(Error):
pass


def data_missing_message(package, name):
return ("The {name} dust map is not in the data directory:\n\n"
" {data_dir}\n\n"
"To change the data directory, call:\n\n"
" from grblc.sfd.config import config\n"
" config['data_dir'] = '/path/to/data/directory'\n\n"
"To download the {name} map to the data directory, call:\n\n"
" import grblc.sfd.{package}\n"
" grblc.sfd.{package}.fetch()\n").format(
data_dir=std_paths.data_dir(),
package=package,
name=name)
Loading

0 comments on commit 08756c2

Please sign in to comment.