Skip to content

Instantly share code, notes, and snippets.

@taldcroft
Created November 26, 2014 14:18
Show Gist options
  • Save taldcroft/19aac546eeeba29e47ed to your computer and use it in GitHub Desktop.
Save taldcroft/19aac546eeeba29e47ed to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": "",
"signature": "sha256:00712bbf2a64d56e5d07362ce693e74ed5bc8e4b44e998b88877b025aa4234d7"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"from astropy.coordinates import SkyCoord\n",
"from astropy.coordinates.sky_coordinate import _get_frame\n",
"from astropy.table import Table\n",
"import astropy.units as u"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"ra = [1] * u.deg\n",
"dec = [2] * u.deg\n",
"x = [1] * u.m\n",
"y = [2] * u.m\n",
"z = [3] * u.m"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"tsph = Table([ra, dec], names=('ra', 'dec'))\n",
"tcart = Table([x, y, z], names=('x', 'y', 'z'))\n",
"tgal = Table([dec, ra], names=('b', 'l'))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 23
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def from_table(table, get_coord_columns_func=None, **kwargs):\n",
"\n",
" def default_get_coord_columns(frame, table):\n",
" \"\"\"\n",
" Return a dict of ``frame`` components from ``table``\n",
" \"\"\"\n",
" comp_kwargs = {}\n",
" for comp_name in frame.representation_component_names:\n",
" if comp_name in table.colnames:\n",
" comp_kwargs[comp_name] = table[comp_name]\n",
" return comp_kwargs\n",
" \n",
" kwargs_frame = kwargs.get('frame')\n",
" frame = _get_frame([], kwargs) \n",
" kwargs['frame'] = kwargs_frame\n",
"\n",
" if get_coord_columns_func is None:\n",
" get_coord_columns_func = default_get_coord_columns\n",
" kwargs.update(get_coord_columns_func(frame, table))\n",
"\n",
" return SkyCoord(**kwargs)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 40
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from_table(tcart, representation='cartesian')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 41,
"text": [
"<SkyCoord (ICRS): (x, y, z) in m\n",
" (1.0, 2.0, 3.0)>"
]
}
],
"prompt_number": 41
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from_table(tsph)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 42,
"text": [
"<SkyCoord (ICRS): (ra, dec) in deg\n",
" (1.0, 2.0)>"
]
}
],
"prompt_number": 42
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from_table(tgal, frame='galactic')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 43,
"text": [
"<SkyCoord (Galactic): (l, b) in deg\n",
" (1.0, 2.0)>"
]
}
],
"prompt_number": 43
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"SkyCoord.guess_from_table(tsph)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 44,
"text": [
"<SkyCoord (ICRS): (ra, dec) in deg\n",
" (1.0, 2.0)>"
]
}
],
"prompt_number": 44
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"SkyCoord.guess_from_table(tgal)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "Cannot create a SkyCoord without data",
"output_type": "pyerr",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-45-e16e591dff82>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mSkyCoord\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mguess_from_table\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtgal\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m/Users/aldcroft/git/astropy/astropy/coordinates/sky_coordinate.pyc\u001b[0m in \u001b[0;36mguess_from_table\u001b[0;34m(cls, table, **coord_kwargs)\u001b[0m\n\u001b[1;32m 937\u001b[0m \u001b[0;32mbreak\u001b[0m \u001b[0;31m# moves on to the next `nm`\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 938\u001b[0m \u001b[0mcoord_kwargs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'frame'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minitframe\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 939\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mcls\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0mcoord_kwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 940\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 941\u001b[0m \u001b[0;31m# Name resolve\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/Users/aldcroft/git/astropy/astropy/coordinates/sky_coordinate.pyc\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 152\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 153\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_sky_coord_frame\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhas_data\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 154\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Cannot create a SkyCoord without data'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 155\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 156\u001b[0m \u001b[0;34m@\u001b[0m\u001b[0mproperty\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: Cannot create a SkyCoord without data"
]
}
],
"prompt_number": 45
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"SkyCoord.guess_from_table(tcart, representation='cartesian')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "Unrecognized keyword argument(s) 'y', 'x', 'z'",
"output_type": "pyerr",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-46-f0aa8e6b9eb8>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mSkyCoord\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mguess_from_table\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtcart\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrepresentation\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'cartesian'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m/Users/aldcroft/git/astropy/astropy/coordinates/sky_coordinate.pyc\u001b[0m in \u001b[0;36mguess_from_table\u001b[0;34m(cls, table, **coord_kwargs)\u001b[0m\n\u001b[1;32m 937\u001b[0m \u001b[0;32mbreak\u001b[0m \u001b[0;31m# moves on to the next `nm`\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 938\u001b[0m \u001b[0mcoord_kwargs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'frame'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minitframe\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 939\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mcls\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0mcoord_kwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 940\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 941\u001b[0m \u001b[0;31m# Name resolve\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/Users/aldcroft/git/astropy/astropy/coordinates/sky_coordinate.pyc\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 133\u001b[0m \u001b[0;31m# creating the internal self._sky_coord_frame object\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 134\u001b[0m \u001b[0margs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# Make it mutable\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 135\u001b[0;31m \u001b[0mkwargs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parse_inputs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 136\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 137\u001b[0m \u001b[0;31m# Set internal versions of object state attributes\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/Users/aldcroft/git/astropy/astropy/coordinates/sky_coordinate.pyc\u001b[0m in \u001b[0;36m_parse_inputs\u001b[0;34m(self, args, kwargs)\u001b[0m\n\u001b[1;32m 215\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 216\u001b[0m raise ValueError('Unrecognized keyword argument(s) {0}'\n\u001b[0;32m--> 217\u001b[0;31m .format(', '.join(\"'{0}'\".format(key) for key in kwargs)))\n\u001b[0m\u001b[1;32m 218\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 219\u001b[0m \u001b[0;31m# Finally deal with the unnamed args. This figures out what the arg[0] is\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: Unrecognized keyword argument(s) 'y', 'x', 'z'"
]
}
],
"prompt_number": 46
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 46
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment