Skip to content

Instantly share code, notes, and snippets.

@PritishC
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PritishC/828d49931d470bc03045 to your computer and use it in GitHub Desktop.
Save PritishC/828d49931d470bc03045 to your computer and use it in GitHub Desktop.
SunPy_PPT
{
"metadata": {
"name": "",
"signature": "sha256:b63eeaad1baa3086408d88e67a43bff6c163dfff9ef9e24603a49983b3bdcfb0"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##Presentation Stuff\n",
"\n",
"Here we go!\n",
"\n",
"###Introduction \n",
"\n",
"Hello folks. This notebook was prepared with the aim of enlightening you all with what's been done so far in my GSoC project: _Re-implementation of sunpy.wcs as sunpy.coordinates_. A lot has changed in the past few months until Astropy hit a somewhat stable period. We will be going through how to use the coordinates framework in its present form. \n",
"Quick Recap: Astropy released their [APE5](https://github.com/astropy/astropy-APEs/blob/master/APE5.rst) model for desigining coordinate frames, based on which the SunPy coordinates framework was envisaged. We have four frame classes so far -:\n",
"\n",
"1. `HelioGraphicStonyhurst`\n",
"2. `HelioGraphicCarrington`\n",
"3. `HelioCentric`\n",
"4. `HelioProjective`\n",
"\n",
"Each one of them subclasses `~astropy.coordinates.BaseCoordinateFrame`, except the second, which subclasses from the first (to re-use some details in the constructor of the first). APE5 also defines how to implement a transformation framework for your frame classes, and we have that too. We shall be using the `~astropy.coordinates.SkyCoord` class as the high-level interface to our frames.\n",
"\n",
"###Code Examples\n",
"\n",
"* `SkyCoord` provides an easy-to-use interface to pass in data to our frames. Note that once you have defined a `SkyCoord` or frame object, they are immutable. Frames can have two kinds of attributes mainly - attributes which are relevant to the representation of the frame in coordinate space, and FrameAttribute instances."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from sunpy.coordinates import frames\n",
"from astropy.coordinates import * # Sorry for * import, heh\n",
"from astropy import units as u\n",
"\n",
"sc = SkyCoord(1*u.deg, 1*u.deg, 3*u.km, frame='heliographicstonyhurst', dateobs='2011/01/01T00:00:45')\n",
"print(sc)\n",
"print(sc.frame)\n",
"print(sc.hlon) # An attribute of the representation of frame.\n",
"print(sc.dateobs) # A FrameAttribute derived attribute of the frame."
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<SkyCoord (HelioGraphicStonyhurst): B0=0.0 deg, dateobs=2011-01-01 00:00:45, L0=0.0 deg, hlon=1.0 deg, hlat=1.0 deg, rad=3.0 km>\n",
"<HelioGraphicStonyhurst Coordinate: B0=0.0 deg, dateobs=2011-01-01 00:00:45, L0=0.0 deg, hlon=1.0 deg, hlat=1.0 deg, rad=3.0 km>\n",
"1d00m00s\n",
"2011-01-01 00:00:45\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* A frame can be represented in several ways, and within code, it has a particular default representation in which it expects parameters to be given to it at the time of instantiation. If parameters are to be given in a different representation, a `~astropy.coordinates.BaseRepresentation` object must be fed to it. For example, `HelioGraphicStonyhurst` expects parameters to be in `~sunpy.coordinates.SphericalWrap180Representation`, which is an offshoot of `SphericalRepresentation` that allows the longitude parameter to have a range of -180 to 180 degrees. Frame instances can, of course, be represented in different forms."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"sc2 = SkyCoord(CartesianRepresentation(1*u.km, 1*u.km, 1*u.km), frame='heliographicstonyhurst', dateobs='2011/01/01T00:00:45')\n",
"print(sc2) # note how it prints in default representation.\n",
"print(sc2.represent_as(CartesianRepresentation)) # close to our original parameters.\n",
"print(sc2.represent_as(CylindricalRepresentation)) # why? because I can.\n",
"sc_setrepr = SkyCoord(1*u.deg, 1*u.deg, frame='heliographicstonyhurst', dateobs='2011/01/01T00:00:45')\n",
"sc_setrepr.representation = 'cartesian' # this changes the representation in which the SkyCoord object is printed, but does *not* change the underlying default rep.\n",
"# Doing sc.x/sc.y/sc.z would yield an error.\n",
"print(sc_setrepr)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<SkyCoord (HelioGraphicStonyhurst): B0=0.0 deg, dateobs=2011-01-01 00:00:45, L0=0.0 deg, hlon=45.0 deg, hlat=35.2643896828 deg, rad=1.73205080757 km>\n",
"(1.0000000000000002, 1.0, 1.0) km\n",
"(1.41421356237 km, 0.785398163397 rad, 1.0 km)\n",
"<SkyCoord (HelioGraphicStonyhurst): B0=0.0 deg, dateobs=2011-01-01 00:00:45, L0=0.0 deg, x=695296.157659 km, y=12136.4395763 km, z=12138.2882964 km>\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Frames can be transformed into one-another. This is done by use of Astropy's frame transformation graph, or as it is called, the 'bidirectional registry'. The transformations are defined in the form of functions preceded by the relevant transform decorators. The decorators are of the form (SourceFrame, DestFrame, TransformType), where TransformType may be `DynamicMatrixTransform`, `StaticMatrixTransform` or `FunctionTransform`. One may go through the [Astropy Docs](http://astropy.readthedocs.org/en/latest/coordinates/frames.html) for more information."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(sc.transform_to('heliographiccarrington'))\n",
"print(sc.transform_to('heliocentric'))\n",
"print(sc.transform_to('helioprojective'))\n",
"print(sc.transform_to('heliographicstonyhurst'))\n",
"\n",
"import numpy.testing as npt\n",
"sc3 = sc.transform_to('heliocentric').transform_to('helioprojective').transform_to('heliographicstonyhurst')\n",
"print('\\n')\n",
"print((sc.hlon, sc3.hlon))\n",
"print((sc.hlat, sc3.hlat))\n",
"print((sc.rad, sc3.rad))\n",
"# Values are close."
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<SkyCoord (HelioGraphicCarrington): B0=0.0 deg, dateobs=2011-01-01 00:00:45, L0=0.0 deg, hlon=-104.698696917 deg, hlat=1.0 deg, rad=3.0 km>\n",
"<SkyCoord (HelioCentric): D0=149597870.7 km, B0=0.0 deg, dateobs=2011-01-01 00:00:45, L0=0.0 deg, x=0.0523492450538 km, y=0.0523572193119 km, z=2.99908624053 km>\n",
"<SkyCoord (HelioProjective): L0=0.0 deg, B0=0.0 deg, dateobs=2011-01-01 00:00:45, D0=149597870.7 km, Tx=7.21788823206e-05 arcsec, Ty=7.21898771885e-05 arcsec, distance=149597867.701 km>"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"<SkyCoord (HelioGraphicStonyhurst): B0=0.0 deg, dateobs=2011-01-01 00:00:45, L0=0.0 deg, hlon=1.0 deg, hlat=1.0 deg, rad=3.0 km>\n",
"\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"(<Longitude180 1.0 deg>, <Longitude180 0.9999999992842148 deg>)\n",
"(<Latitude 1.0 deg>, <Latitude 0.9999999992844331 deg>)\n",
"(<Distance 3.0 km>, <Distance 3.000000002146484 km>)\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Frame specifics: We have added some extra features to our frames by modifying their constructors. Specifically, we have made some frames default their representation attributes and FrameAttribute instances. For example, `HelioGraphicStonyhurst` defaults its `rad` representation attribute to the solar radius if no value is specified. Similarly, its `L0` and `B0` FrameAttributes are defaulted to zero degrees. `HelioProjective` frames also make use of this defaulting mechanism for their `distance` attributes. Specifically, `HelioProjective` frames can also carry a `zeta` property by which `distance` is calculated. Only one of them is specified at a time."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"sc = SkyCoord(1*u.deg, 1*u.deg, frame='heliographicstonyhurst', dateobs='2011/01/01T00:00:45')\n",
"print(sc) # note how rad and L0,B0 magically appear.\n",
"\n",
"from sunpy.coordinates.frames import HelioProjective\n",
"frame = SkyCoord(HelioProjective(1*u.deg, 1*u.deg, zeta=1*u.km, dateobs='2012/02/02T00:00:50'))\n",
"print(frame) # unfortunately, as zeta is a frame property object, SkyCoord cannot recognize it firsthand."
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<SkyCoord (HelioGraphicStonyhurst): B0=0.0 deg, dateobs=2011-01-01 00:00:45, L0=0.0 deg, hlon=1.0 deg, hlat=1.0 deg, rad=695508.0 km>\n",
"<SkyCoord (HelioProjective): L0=0.0 deg, B0=0.0 deg, dateobs=2012-02-02 00:00:50, D0=149597870.7 km, Tx=3600.0 arcsec, Ty=3600.0 arcsec, distance=149597869.7 km>\n"
]
}
],
"prompt_number": 30
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Miscellaneous: `SkyCoord` has some other interesting methods. They are on display here. Also, see the [documentation](http://astropy.readthedocs.org/en/latest/coordinates/skycoord.html?highlight=skycoord) for more information."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"sc = SkyCoord([1,2,3]*u.deg, [3,2,1]*u.deg, [1,1,1]*u.km, frame='heliographicstonyhurst', dateobs='2011/01/01T00:00:45') # cool array type inputs\n",
"print(sc)\n",
"print(sc2.position_angle(sc3)) # calculates the position angle between this SkyCoord and another.\n",
"print(sc.separation(sc3)) # calculates on-sky separation. Extra keyword args such as 'dateobs' must be the same in both SkyCoords.\n",
"print(sc.separation_3d(sc3)) # calculates the 3D separation. Can not be used on 2D frames. Keywordargs condition applies."
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<SkyCoord (HelioGraphicStonyhurst): B0=0.0 deg, dateobs=2011-01-01 00:00:45, L0=0.0 deg, (hlon, hlat, rad) in (deg, deg, km)\n",
" [(1.0, 3.0, 1.0), (2.0, 2.0, 1.0), (3.0, 1.0, 1.0)]>\n",
"4.18879rad\n",
"[u'2d00m00s' u'1d24m50.2642s' u'1d59m58.9033s']\n",
"[ 2.00091355 2.00045669 2.00091327] km"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 41
},
{
"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