Create a MOC from a polygon¶
[1]:
import matplotlib.pyplot as plt
import numpy as np
from astropy import units as u
from mocpy import MOC
[2]:
# Methods for defining random polygons
def generate_rand_polygon(num_points):
"""Generate a random polygon.
Parameters
----------
num_points : int
number of random points to generate
Returns
-------
(astropy.units.quantity.Quantity, astropy.units.quantity.Quantity)
a tuple containing the longitutes and lattitudes of the random points
"""
lon_min, lon_max = (-5, 5)
lat_min, lat_max = (-5, 5)
lon = (np.random.random(num_points) * (lon_max - lon_min) + lon_min) * u.deg
lat = (np.random.random(num_points) * (lat_max - lat_min) + lat_min) * u.deg
return lon, lat
def generate_concave_polygon(num_points, lon_offset, lat_offset):
"""Generate a concave polygon.
Parameters
----------
num_points : int
the number of corners
lon_offset : float
offset in longitudinal direction
lat_offset : float
offset in latitudinal direction
Returns
-------
(astropy.units.quantity.Quantity, astropy.units.quantity.Quantity)
a tuple containing the longitutes and lattitudes of the random points
"""
radius_max = 10
angles = np.linspace(0, 2 * np.pi, num_points)
radius = np.random.random(angles.shape[0]) * radius_max
lon = (np.cos(angles) * radius + lon_offset) * u.deg
lat = (np.sin(angles) * radius + lat_offset) * u.deg
return lon, lat
def generate_convexe_polygon(num_points, lon_offset, lat_offset):
"""Generate the corners on a convexe polygon.
Parameters
----------
num_points : int
the number of corners
lon_offset : float
offset in longitudinal direction
lat_offset : float
offset in latitudinal direction
Returns
-------
(astropy.units.quantity.Quantity, astropy.units.quantity.Quantity)
a tuple containing the longitutes and lattitudes of the random points
"""
radius_max = 10
angles = np.linspace(0, 2 * np.pi, num_points)
radius = np.random.random() * radius_max * np.ones(angles.shape[0])
lon = (np.cos(angles) * radius + lon_offset) * u.deg
lat = (np.sin(angles) * radius + lat_offset) * u.deg
return lon, lat
[3]:
%%time
# Let's generate four MOCs
## Two random ones
lon, lat = generate_rand_polygon(12)
random_moc_1 = MOC.from_polygon(lon=lon, lat=lat, max_depth=12)
lon, lat = generate_rand_polygon(5)
random_moc_2 = MOC.from_polygon(lon=lon, lat=lat, max_depth=12)
## A convexe one
lon, lat = generate_convexe_polygon(15, 20, 7)
convexe_moc = MOC.from_polygon(lon=lon, lat=lat, max_depth=12)
## A concave one
lon, lat = generate_concave_polygon(15, 20, 7)
concave_moc = MOC.from_polygon(lon=lon, lat=lat, max_depth=12)
CPU times: user 18.3 ms, sys: 800 µs, total: 19.1 ms
Wall time: 28 ms
[4]:
# Let's plot the results
fig = plt.figure()
wcs = convexe_moc.wcs(fig) # automatically creates a wcs for the MOC
ax1 = fig.add_subplot(221, projection=wcs) # The first of a 2*2 grig of subplots
convexe_moc.fill(
ax1,
wcs,
color="hotpink",
alpha=0.5,
) # Where the MOC is added to the plot
ax1.set_title("A convexe MOC") # Comments and titles
wcs = random_moc_1.wcs(fig)
ax2 = fig.add_subplot(222, projection=wcs)
random_moc_1.fill(ax2, wcs, color="lightblue", alpha=0.5)
ax2.set_title("A first random MOC")
wcs = random_moc_2.wcs(fig)
ax3 = fig.add_subplot(223, projection=wcs)
random_moc_2.fill(ax3, wcs, color="lightgreen", alpha=0.5)
ax3.set_title("A second random MOC")
wcs = concave_moc.wcs(fig)
ax4 = fig.add_subplot(224, projection=wcs)
concave_moc.fill(ax4, wcs, color="yellow", alpha=0.5)
ax4.set_title("And a concave one")
fig.tight_layout() # prevents overlapping labels