Examples

Notebooks examples can be found on:

Here are some code examples manipulating mocpy.moc.MOC objects.

Loading and plotting the MOC of SDSS

This example:

  • Load the coverage of SDSS from a FITS file.
  • Plot the MOC by:
  1. Defining a matplotlib figure.
  2. Defining an astropy WCS representing the field of view of the plot.
  3. Call the mocpy.moc.MOC.fill() and mocpy.moc.MOC.border() so that mocpy plot on a matplotlib axis.
  4. Set the axis labels, a title, enable the grid and plot the final figure.
from mocpy import MOC, WCS
from astropy.coordinates import Angle, SkyCoord
import astropy.units as u
# Load a MOC
filename = './../../resources/P-SDSS9-r.fits'
moc = MOC.from_fits(filename)
# Plot the MOC using matplotlib
import matplotlib.pyplot as plt
fig = plt.figure(111, figsize=(15, 10))
# Define a astropy WCS easily
with WCS(fig, 
        fov=200 * u.deg,
        center=SkyCoord(0, 20, unit='deg', frame='icrs'),
        coordsys="icrs",
        rotation=Angle(0, u.degree),
        projection="AIT") as wcs:
    ax = fig.add_subplot(1, 1, 1, projection=wcs)
    # Call fill with a matplotlib axe and the `~astropy.wcs.WCS` wcs object.
    moc.fill(ax=ax, wcs=wcs, alpha=0.5, fill=True, color="green")
    moc.border(ax=ax, wcs=wcs, alpha=0.5, color="black")
plt.xlabel('ra')
plt.ylabel('dec')
plt.title('Coverage of P-SDSS9-r')
plt.grid(color="black", linestyle="dotted")
plt.show()

(Source code, png, hires.png, pdf)

../_images/plot_SDSS_r.png

Intersection between GALEX and SDSS

This example:

  • Load the coverages of SDSS and GALEX from FITS files.
  • Compute their intersection
  • Compute their union
  • Plot the resulting intersection and union on a same matplotlib axis.
from mocpy import MOC, WCS
from astropy.coordinates import Angle, SkyCoord
import astropy.units as u
# Load Galex and SDSS
sdss = MOC.from_fits('./../../resources/P-SDSS9-r.fits')
galex = MOC.from_fits('./../../resources/P-GALEXGR6-AIS-FUV.fits')
# Compute their intersection
inter = sdss.intersection(galex)
union = sdss.union(galex)
# Plot the MOC using matplotlib
import matplotlib.pyplot as plt
fig = plt.figure(111, figsize=(10, 10))
# Define a astropy WCS easily
with WCS(fig, 
        fov=160 * u.deg,
        center=SkyCoord(0, 0, unit='deg', frame='icrs'),
        coordsys="icrs",
        rotation=Angle(0, u.degree),
        projection="AIT") as wcs:
    ax = fig.add_subplot(1, 1, 1, projection=wcs)
    # Call fill with a matplotlib axe and the `~astropy.wcs.WCS` wcs object.
    union.fill(ax=ax, wcs=wcs, alpha=0.5, fill=True, color="red", linewidth=0, label="Union")
    union.border(ax=ax, wcs=wcs, alpha=1, color="red")

    inter.fill(ax=ax, wcs=wcs, alpha=0.5, fill=True, color="green", linewidth=0, label="Intersection")
    inter.border(ax=ax, wcs=wcs, alpha=1, color="green")
    ax.legend()

plt.xlabel('ra')
plt.ylabel('dec')
plt.title('Logical operations between SDSS and GALEX')
plt.grid(color="black", linestyle="dotted")
plt.show()

(Source code, png, hires.png, pdf)

../_images/logical_operations.png

Create a MOC from a concave polygon

This example shows how to call mocpy.moc.MOC.from_polygon() or mocpy.moc.MOC.from_polygon_skycoord().

from mocpy import MOC, WCS
from astropy.coordinates import Angle, SkyCoord
import astropy.units as u

import numpy as np
# The set of points delimiting the polygon in deg
vertices = np.array([[18.36490956,  5.        ],
                    [15.7446692 ,  6.97214743],
                    [16.80755056, 10.29852928],
                    [13.36215502, 10.14616136],
                    [12.05298691, 13.10706197],
                    [ 9.54793022, 10.4556709 ],
                    [ 8.7677627 ,  7.80921809],
                    [ 9.71595962,  5.30855011],
                    [ 7.32238541,  6.44905255],
                    [ 0.807265  ,  6.53399616],
                    [ 1.08855146,  3.51294225],
                    [ 2.07615384,  0.7118289 ],
                    [ 3.90690158, -1.61886929],
                    [ 9.03727956,  2.80521847],
                    [ 9.22274427, -4.38008174],
                    [10.23563378,  4.06950324],
                    [10.79931601,  3.77655576],
                    [14.16533992,  1.7579858 ],
                    [19.36243491,  1.78587203],
                    [15.31732084,  5.        ]])
skycoord = SkyCoord(vertices, unit="deg", frame="icrs")
# A point that we say it belongs to the inside of the MOC
inside = SkyCoord(ra=10, dec=5, unit="deg", frame="icrs")
moc = MOC.from_polygon_skycoord(skycoord, max_depth=9, inside=inside)
moc.write('polygon_moc.fits', format='fits', overwrite=True)
# Plot the MOC using matplotlib
import matplotlib.pyplot as plt
fig = plt.figure(111, figsize=(10, 10))
# Define a astropy WCS easily
with WCS(fig, 
        fov=20 * u.deg,
        center=SkyCoord(10, 5, unit='deg', frame='icrs'),
        coordsys="icrs",
        rotation=Angle(0, u.degree),
        # The gnomonic projection transforms great circles into straight lines. 
        projection="TAN") as wcs:
    ax = fig.add_subplot(1, 1, 1, projection=wcs)
    # Call fill with a matplotlib axe and the `~astropy.wcs.WCS` wcs object.
    moc.fill(ax=ax, wcs=wcs, alpha=0.5, fill=True, color="red", linewidth=1)
    moc.border(ax=ax, wcs=wcs, alpha=1, color="red")

plt.xlabel('ra')
plt.ylabel('dec')
plt.title('MOC from a polygon')
plt.grid(color="black", linestyle="dotted")
plt.show()

(Source code, png, hires.png, pdf)

../_images/polygon_coverage.png

Get the border(s) of a MOC

This example shows how to call mocpy.moc.MOC.get_boundaries(). The borders are returned as a list of SkyCoord each defining one border. In this example:

  1. The sky coordinates defining the border of the MOC are projected to the pixel image system.
  2. Then, a matplotlib path is defined from the projected vertices.
  3. Finally the path is plot on top of the MOC.
from mocpy import MOC, WCS
from astropy.coordinates import SkyCoord, Angle

from matplotlib.path import Path
from matplotlib.patches import PathPatch

import astropy.units as u

moc = MOC.from_fits('polygon_moc.fits')
skycoords = moc.get_boundaries()

# Plot the MOC using matplotlib
import matplotlib.pyplot as plt
fig = plt.figure(111, figsize=(10, 10))
# Define a astropy WCS easily
with WCS(fig, 
        fov=20 * u.deg,
        center=SkyCoord(10, 5, unit='deg', frame='icrs'),
        coordsys="icrs",
        rotation=Angle(0, u.degree),
        # The gnomonic projection transforms great circles into straight lines.
        projection="TAN") as wcs:
    ax = fig.add_subplot(1, 1, 1, projection=wcs)
    # Call fill with a matplotlib axe and the `~astropy.wcs.WCS` wcs object.
    moc.fill(ax=ax, wcs=wcs, alpha=0.5, fill=True, color="red", linewidth=1)
    moc.border(ax=ax, wcs=wcs, alpha=1, color="red")

    # Plot the border
    from astropy.wcs.utils import skycoord_to_pixel
    x, y = skycoord_to_pixel(skycoords[0], wcs)
    p = Path(np.vstack((x, y)).T)
    patch = PathPatch(p, color="black", fill=False, alpha=0.75, lw=2)
    ax.add_patch(patch)

plt.xlabel('ra')
plt.ylabel('dec')
plt.title('Get the border(s) of a MOC')
plt.grid(color="black", linestyle="dotted")
plt.show()

(Source code, png, hires.png, pdf)

../_images/compute_borders.png