Skip to content

Commit

Permalink
added astropy dependency, made search for 2mass dir more robust and f…
Browse files Browse the repository at this point in the history
…ail more gracefully, and output dataframes now have a 'radec' field that is of type astropy.coordinates.SkyCoord
  • Loading branch information
henryroe committed May 23, 2015
1 parent a30301e commit 05d0329
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 9 deletions.
8 changes: 8 additions & 0 deletions CHANGES.txt
Expand Up @@ -2,6 +2,14 @@
Py2MASS Change Log
~~~~~~~~~~~~~~~~~~

------------------
v0.1.4, 2015-05-23
------------------

- output DataFrame now has 'radec' field that is of type astropy.coordinates.SkyCoord
- added astropy>=1.0.0 dependency
- made search for 2MASS dir more robust & exception more informative when search fails

------------------
v0.1.3, 2015-05-21
------------------
Expand Down
1 change: 1 addition & 0 deletions py2mass/.gitignore
@@ -0,0 +1 @@
*.pyc
2 changes: 1 addition & 1 deletion py2mass/__about__.py
Expand Up @@ -10,7 +10,7 @@
__uri__ = "https://github.com/henryroe/Py2MASS"

# VERSION should be PEP386 compatible (http://www.python.org/dev/peps/pep-0386)
__version__ = "0.1.3"
__version__ = "0.1.4"

# Indicates if this version is a release version
RELEASE = 'dev' not in __version__
Expand Down
51 changes: 44 additions & 7 deletions py2mass/py2mass.py
Expand Up @@ -10,6 +10,9 @@
import pickle
import pdb
import io
import glob
from astropy.coordinates import SkyCoord, ICRS
from astropy import units

base_dir = os.path.abspath(os.path.dirname(__file__))

Expand All @@ -24,6 +27,9 @@ class Error(Exception):
pass


class PathTo2MASSNotFound(Exception):
pass

class FileNotFound(Exception):
pass

Expand All @@ -39,15 +45,16 @@ def _find_2mass_dir():
of the directory.
"""
paths_to_search = [os.environ.get('2MASS_DIR')]
paths_to_search.append('~/2MASS/')
paths_to_search.append('~/2mass/')
paths_to_search.append('~/2[mM][aA][sS][sS]/')
for cur_path in glob.glob('/Volumes/*/2[mM][aA][sS][sS]'):
paths_to_search.append(cur_path)
for cur_path_to_test in paths_to_search:
if cur_path_to_test is not None:
if os.path.isdir(os.path.expanduser(cur_path_to_test)):
return os.path.expanduser(cur_path_to_test)
raise Error("py2mass.py: No 2MASS installation found.\n" +
" User needs to specifiy location with, e.g.:\n" +
" py2mass.set_2mass_path('~/my_2mass_dir')")
raise PathTo2MASSNotFound("py2mass.py: No 2MASS installation found.\n" +
" User needs to specifiy location with, e.g.:\n" +
" py2mass.set_2mass_path('~/my_2mass_dir')")
return None

try:
Expand Down Expand Up @@ -79,6 +86,10 @@ def _get_file_object(file_basename):
Open the file (with gunzip if necessary) and return the file object.
"""
if _2mass_dir is None:
raise PathTo2MASSNotFound("_2mass_dir is None\n" +
" User needs to specifiy location with, e.g.:\n" +
" py2mass.set_2mass_path('~/my_2mass_dir')")
if os.path.exists(_2mass_dir + '/' + file_basename + '.gz'):
return gzip.open(_2mass_dir + '/' + file_basename + '.gz', 'r')
elif os.path.exists(_2mass_dir + '/' + file_basename):
Expand Down Expand Up @@ -116,15 +127,41 @@ def _convert_xsc_text_to_dataframe(sources_txt):
Takes raw lines from extended source catalog (XSC) file and converts to a pandas DataFrame
"""
xsc_format_descriptor = _get_xsc_format_descriptor()
return read_csv(io.StringIO(unicode(sources_txt)), sep='|', names=xsc_format_descriptor['Parameter Name'].values)
return _add_skycoord_radec_field_xsc(read_csv(io.StringIO(unicode(sources_txt)), sep='|',
names=xsc_format_descriptor['Parameter Name'].values))


def _convert_psc_text_to_dataframe(sources_txt):
"""
Takes raw lines from point source catalog (PSC) file and converts to a pandas DataFrame
"""
psc_format_descriptor = _get_psc_format_descriptor()
return read_csv(io.StringIO(unicode(sources_txt)), sep='|', names=psc_format_descriptor['Column Name'].values)
return _add_skycoord_radec_field_psc(read_csv(io.StringIO(unicode(sources_txt)), sep='|',
names=psc_format_descriptor['Column Name'].values))


def _add_skycoord_radec_field_psc(df):
"""
for PSC objects:
Add the ICRS coordinates as 'radec' column in SkyCoord type object
(ra & dec/decl fields in decimal deg)
"""
df['radec'] = [SkyCoord(df.loc[ix, 'ra'], df.loc[ix, 'dec/decl'],
frame=ICRS, unit=(units.degree, units.degree))
for ix in df.index]
return df


def _add_skycoord_radec_field_xsc(df):
"""
for XSC objects:
Add the Super-coadd centroid as 'radec' column in SkyCoord type object
(sup_ra & sup_dec fields in decimal deg)
"""
df['radec'] = [SkyCoord(df.loc[ix, 'sup_ra'], df.loc[ix, 'sup_dec'],
frame=ICRS, unit=(units.degree, units.degree))
for ix in df.index]
return df


def fetch_2mass_xsc_box(ra_range, dec_range):
Expand Down
Binary file removed py2mass/py2mass.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -81,7 +81,7 @@
# project is installed. For an analysis of "install_requires" vs pip's
# requirements files see:
# https://packaging.python.org/en/latest/requirements.html
install_requires=['pandas>=0.10.1'],
install_requires=['pandas>=0.10.1', 'astropy>=1.0.0'],

# List additional groups of dependencies here (e.g. development dependencies).
# You can install these using the following syntax, for example:
Expand Down

0 comments on commit 05d0329

Please sign in to comment.