.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated/gallery/coordinates_demo.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_generated_gallery_coordinates_demo.py: ================================== Astropy Coordinates and SunPy Demo ================================== Written by Steven Christe and presented at the Heliopython meeting on November 13-15, 2018. The purpose of this demo is to show off the AstroPy coordinates framework as well as show how SunPy extends it to add solar coordinate systems. .. GENERATED FROM PYTHON SOURCE LINES 13-16 The astropy coordinates package provides classes for representing a variety of celestial/spatial coordinates and their velocity components, as well as tools for converting between common coordinate systems in a uniform way. .. GENERATED FROM PYTHON SOURCE LINES 16-20 .. code-block:: default from astropy import units as u from astropy.coordinates import SkyCoord, AltAz from astropy.time import Time .. GENERATED FROM PYTHON SOURCE LINES 21-24 SkyCoord As an example of creating a SkyCoord to represent an ICRS (Right ascension [RA], Declination [Dec]) sky position: .. GENERATED FROM PYTHON SOURCE LINES 24-25 .. code-block:: default c = SkyCoord(ra=10.625*u.degree, dec=41.2*u.degree, frame='icrs') .. GENERATED FROM PYTHON SOURCE LINES 26-27 It can also handle arrays (many ways to instantiate a SkyCoord) .. GENERATED FROM PYTHON SOURCE LINES 27-29 .. code-block:: default c = SkyCoord(ra=[10, 11, 12, 13]*u.degree, dec=[41, -5, 42, 0]*u.degree) .. GENERATED FROM PYTHON SOURCE LINES 30-31 SkyCoord can also handle 3D positions, just add a distance .. GENERATED FROM PYTHON SOURCE LINES 31-33 .. code-block:: default c = SkyCoord(ra=10.68458*u.degree, dec=41.26917*u.degree, distance=770*u.kpc) .. GENERATED FROM PYTHON SOURCE LINES 34-35 So now cartesian coordinates are available .. GENERATED FROM PYTHON SOURCE LINES 35-37 .. code-block:: default print('r = ({0}, {1}, {2})'.format(c.cartesian.x, c.cartesian.y, c.cartesian.z)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none r = (568.7128654235231 kpc, 107.3008974042025 kpc, 507.88994291875713 kpc) .. GENERATED FROM PYTHON SOURCE LINES 38-40 Positions of objects Can also register positions of objects or do object lookups .. GENERATED FROM PYTHON SOURCE LINES 40-43 .. code-block:: default crab = SkyCoord.from_name("Crab") print(crab) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 44-46 let's consider now consider a position in the sky from a specific location on the Earth. .. GENERATED FROM PYTHON SOURCE LINES 46-48 .. code-block:: default from astropy.coordinates import EarthLocation .. GENERATED FROM PYTHON SOURCE LINES 49-50 Many positions are already availabe such as that of the VLA. .. GENERATED FROM PYTHON SOURCE LINES 50-53 .. code-block:: default vla_coord = EarthLocation.of_site('vla') print(vla_coord) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none (-1601184.40191992, -5041989.95569235, 3554875.07685646) m .. GENERATED FROM PYTHON SOURCE LINES 54-56 Using a position on the Earth we can calculate Alt/Az, since dkist is missing from the library, let's add it as a position .. GENERATED FROM PYTHON SOURCE LINES 56-60 .. code-block:: default dkist = EarthLocation(lat=20.70818*u.deg, lon=-156.2569*u.deg, height=3084*u.m) utcoffset = -10 * u.hour midnight = Time('2018-11-14 00:00:00') - utcoffset .. GENERATED FROM PYTHON SOURCE LINES 61-62 We can now get the position of the Crab in the sky as observed from DKIST .. GENERATED FROM PYTHON SOURCE LINES 62-66 .. code-block:: default crab_altaz = crab.transform_to(AltAz(obstime=midnight,location=dkist)) print(crab_altaz) print("Crab's Altitude = {0.alt:}".format(crab_altaz)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Crab's Altitude = 55.861343085319625 deg .. GENERATED FROM PYTHON SOURCE LINES 67-69 Let's now move on to showing how SunPy extends AstroPy coordinates by adding solar coordinate systems. .. GENERATED FROM PYTHON SOURCE LINES 69-72 .. code-block:: default from sunpy.coordinates import frames from sunpy.coordinates.sun import earth_distance .. GENERATED FROM PYTHON SOURCE LINES 73-75 SunPy defines HeliographicStonyhurst, HeliographicCarrington, Heliocentric, and Helioprojective. Let's define the center of the Sun .. GENERATED FROM PYTHON SOURCE LINES 75-77 .. code-block:: default sun = SkyCoord(0 * u.arcsec, 0 * u.arcsec, obstime=midnight, frame=frames.Helioprojective, observer='Earth') .. GENERATED FROM PYTHON SOURCE LINES 78-79 The position in the sky from the DKIST site is .. GENERATED FROM PYTHON SOURCE LINES 79-82 .. code-block:: default sun_altaz = sun.transform_to(AltAz(obstime=midnight,location=dkist)) print('Altitude is {0} and Azimuth is {1}'.format(sun_altaz.T.alt, sun_altaz.T.az)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Altitude is -86.69112108195809 deg and Azimuth is 317.3715108577912 deg .. GENERATED FROM PYTHON SOURCE LINES 83-84 As expected the Sun is below the horizon! Let's consider noon now. .. GENERATED FROM PYTHON SOURCE LINES 84-88 .. code-block:: default noon = Time('2018-11-14 12:00:00') - utcoffset sun_altaz = sun.transform_to(AltAz(obstime=noon,location=dkist)) print('Altitude is {0} and Azimuth is {1}'.format(sun_altaz.T.alt, sun_altaz.T.az)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Altitude is 50.83292620533457 deg and Azimuth is 176.42009890469524 deg .. GENERATED FROM PYTHON SOURCE LINES 89-93 Next let’s check this calculation by converting it back to helioprojective. We should get our original input which was the center of the Sun. To go from Altitude/Azimuth to Helioprojective, you will need the distance to the Sun. solar distance. Define distance with SunPy’s almanac. .. GENERATED FROM PYTHON SOURCE LINES 93-98 .. code-block:: default distance = earth_distance(noon) b = SkyCoord(az=sun_altaz.T.az, alt=sun_altaz.T.alt, distance=distance, frame=AltAz(obstime=noon,location=dkist), observer='Earth') sun_helioproj = b.transform_to(frames.Helioprojective) print('The helioprojective point is {0}, {1}'.format(sun_helioproj.T.Tx, sun_helioproj.T.Ty)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none The helioprojective point is -9.287273101974279 arcsec, 1.0377010641434536 arcsec .. GENERATED FROM PYTHON SOURCE LINES 99-101 Let's now show off how we can convert between Solar coordinates Coordinates. Transform to HeliographicStonyhurst .. GENERATED FROM PYTHON SOURCE LINES 101-103 .. code-block:: default sun.transform_to(frames.HeliographicStonyhurst) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 104-105 Transform to Heliocentric .. GENERATED FROM PYTHON SOURCE LINES 105-107 .. code-block:: default sun.transform_to(frames.Heliocentric) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none ): (x, y, z) in AU (0., 0., 0.00465047)> .. GENERATED FROM PYTHON SOURCE LINES 108-109 Transform to HeliographicCarrington .. GENERATED FROM PYTHON SOURCE LINES 109-110 .. code-block:: default sun.transform_to(frames.HeliographicCarrington) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none ): (lon, lat, radius) in (deg, deg, AU) (115.41667055, 2.94605581, 0.00465047)> .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 2.177 seconds) .. _sphx_glr_download_generated_gallery_coordinates_demo.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/HeliophysicsPy/gallery/gh-pages?urlpath=lab/tree/notebooks/generated/gallery/coordinates_demo.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: coordinates_demo.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: coordinates_demo.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_