diff --git a/.buildinfo b/.buildinfo index 28c6e3df..b50313cb 100644 --- a/.buildinfo +++ b/.buildinfo @@ -1,4 +1,4 @@ # Sphinx build info version 1 -# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done. -config: c2890eadd3f6df9eefeba2536d630529 +# This file records the configuration used when building these files. When it is not found, a full rebuild will be done. +config: 7bf1cc2146bfcf907aa130f64618d5fe tags: 645f666f9bcd5a90fca523b33c5a78b7 diff --git a/_collections/notebooks/00-MOCpy_introduction.html b/_collections/notebooks/00-MOCpy_introduction.html index 9529aece..e49c7001 100644 --- a/_collections/notebooks/00-MOCpy_introduction.html +++ b/_collections/notebooks/00-MOCpy_introduction.html @@ -5,16 +5,16 @@ -
[1]:
import astropy.units as u
-import matplotlib.pyplot as plt
-import mocpy
-from astropy.coordinates import Angle, SkyCoord
-from astropy.visualization.wcsaxes.frame import EllipticalFrame
-from astropy.wcs import WCS
-from astroquery.mocserver import MOCServer
-from astroquery.vizier import Vizier
-from ipyaladin import Aladin
-from matplotlib import patheffects
-from regions import CircleSkyRegion
+import astropy.units as u
+import matplotlib.pyplot as plt
+import mocpy
+from astropy.coordinates import Angle, SkyCoord
+from astropy.visualization.wcsaxes.frame import EllipticalFrame
+from astropy.wcs import WCS
+from astroquery.mocserver import MOCServer
+from astroquery.vizier import Vizier
+from ipyaladin import Aladin
+from matplotlib import patheffects
+from regions import CircleSkyRegion
-mocpy.__version__
+mocpy.__version__
[2]:
# HST MOC footprint
-# -----------------
-# We want to retrieve all the HST surveys i.e. the HST surveys covering any region of the sky.
-allsky = CircleSkyRegion(SkyCoord(0, 0, unit="deg"), Angle(180, unit="deg"))
-hst_moc = MOCServer.query_region(
- region=allsky,
- # We want a MOCpy object instead of an astropy table
- return_moc=True,
- # The order of the MOC
- max_norder=11,
- # Expression on the ID meta-data
- meta_data="ID=*HST*",
-)
+# HST MOC footprint
+# -----------------
+# We want to retrieve all the HST surveys i.e. the HST surveys covering any region of the sky.
+allsky = CircleSkyRegion(SkyCoord(0, 0, unit="deg"), Angle(180, unit="deg"))
+hst_moc = MOCServer.query_region(
+ region=allsky,
+ # We want a MOCpy object instead of an astropy table
+ return_moc=True,
+ # The order of the MOC
+ max_norder=11,
+ # Expression on the ID meta-data
+ meta_data="ID=*HST*",
+)
-# SDSS9
-# -----
-sdss_moc = MOCServer.find_datasets(
- meta_data="ID=CDS/P/SDSS9/color",
- return_moc=True,
- max_norder=11,
-)
+# SDSS9
+# -----
+sdss_moc = MOCServer.find_datasets(
+ meta_data="ID=CDS/P/SDSS9/color",
+ return_moc=True,
+ max_norder=11,
+)
[3]:
type(sdss_moc)
+type(sdss_moc)
[4]:
%%time
-sdss_and_hst_moc = sdss_moc.intersection(hst_moc)
-sdss_moc.serialize(format="fits")
-sdss_moc.save("sdss_moc.fits", format="fits", overwrite=True)
+%%time
+sdss_and_hst_moc = sdss_moc.intersection(hst_moc)
+sdss_moc.serialize(format="fits")
+sdss_moc.save("sdss_moc.fits", format="fits", overwrite=True)
[5]:
fig = plt.figure(figsize=(20, 10))
+fig = plt.figure(figsize=(20, 10))
-wcs = WCS(
- {
- "naxis": 2,
- "naxis1": 1620,
- "naxis2": 810,
- "crpix1": 810.5,
- "crpix2": 405.5,
- "cdelt1": -0.2,
- "cdelt2": 0.2,
- "ctype1": "RA---AIT",
- "ctype2": "DEC--AIT",
- },
-)
+wcs = WCS(
+ {
+ "naxis": 2,
+ "naxis1": 1620,
+ "naxis2": 810,
+ "crpix1": 810.5,
+ "crpix2": 405.5,
+ "cdelt1": -0.2,
+ "cdelt2": 0.2,
+ "ctype1": "RA---AIT",
+ "ctype2": "DEC--AIT",
+ },
+)
-# Create a matplotlib axe and give it a astropy.wcs.WCS object
-ax = fig.add_subplot(1, 1, 1, projection=wcs, frame_class=EllipticalFrame)
+# Create a matplotlib axe and give it a astropy.wcs.WCS object
+ax = fig.add_subplot(1, 1, 1, projection=wcs, frame_class=EllipticalFrame)
-# Fill the SDSS MOC in red with an opacity of 70%
-sdss_moc.fill(
- ax=ax,
- wcs=wcs,
- edgecolor="r",
- facecolor="r",
- linewidth=0,
- fill=True,
- alpha=0.7,
- label="sdss9 footprint",
-)
-# Draw its perimeter in black
-sdss_moc.border(ax=ax, wcs=wcs, color="black", alpha=0.5)
+# Fill the SDSS MOC in red with an opacity of 70%
+sdss_moc.fill(
+ ax=ax,
+ wcs=wcs,
+ edgecolor="r",
+ facecolor="r",
+ linewidth=0,
+ fill=True,
+ alpha=0.7,
+ label="sdss9 footprint",
+)
+# Draw its perimeter in black
+sdss_moc.border(ax=ax, wcs=wcs, color="black", alpha=0.5)
-# Fill the HST surveys MOC in green with an opacity of 70%
-hst_moc.fill(
- ax=ax,
- wcs=wcs,
- edgecolor="g",
- facecolor="g",
- linewidth=0,
- fill=True,
- alpha=0.7,
- label="hst all surveys footprint",
-)
-# Draw its perimeter in black
-hst_moc.border(ax=ax, wcs=wcs, color="black", alpha=0.5)
+# Fill the HST surveys MOC in green with an opacity of 70%
+hst_moc.fill(
+ ax=ax,
+ wcs=wcs,
+ edgecolor="g",
+ facecolor="g",
+ linewidth=0,
+ fill=True,
+ alpha=0.7,
+ label="hst all surveys footprint",
+)
+# Draw its perimeter in black
+hst_moc.border(ax=ax, wcs=wcs, color="black", alpha=0.5)
-# Fill the intersection MOC in blue
-sdss_and_hst_moc.fill(
- ax=ax,
- wcs=wcs,
- edgecolor="b",
- facecolor="b",
- linewidth=0,
- fill=True,
- alpha=0.7,
- label="intersection",
-)
-# Draw its perimeter in black
-sdss_and_hst_moc.border(ax=ax, wcs=wcs, color="black", alpha=0.5)
+# Fill the intersection MOC in blue
+sdss_and_hst_moc.fill(
+ ax=ax,
+ wcs=wcs,
+ edgecolor="b",
+ facecolor="b",
+ linewidth=0,
+ fill=True,
+ alpha=0.7,
+ label="intersection",
+)
+# Draw its perimeter in black
+sdss_and_hst_moc.border(ax=ax, wcs=wcs, color="black", alpha=0.5)
-# we set the axis off by half a pixel otherwise they are slightly outside the sphere
-# see astropy issue for the half-pixel trick https://github.com/astropy/astropy/issues/10201
-ax.set_xlim(-0.5, 1620 - 0.5)
-ax.set_ylim(-0.5, 810 - 0.5)
-ax.set_aspect(1.0)
+# we set the axis off by half a pixel otherwise they are slightly outside the sphere
+# see astropy issue for the half-pixel trick https://github.com/astropy/astropy/issues/10201
+ax.set_xlim(-0.5, 1620 - 0.5)
+ax.set_ylim(-0.5, 810 - 0.5)
+ax.set_aspect(1.0)
-# Usual matplotlib calls
-plt.title("Using matplotlib to vizualize MOCs")
-plt.xlabel("ra")
-plt.ylabel("dec")
-plt.legend()
-plt.grid(color="black", linestyle="dotted")
-path_effects = [patheffects.withStroke(linewidth=3, foreground="black")]
-ax.coords[0].set_ticklabel(color="white", path_effects=path_effects)
-plt.show()
-plt.close()
+# Usual matplotlib calls
+plt.title("Using matplotlib to vizualize MOCs")
+plt.xlabel("ra")
+plt.ylabel("dec")
+plt.legend()
+plt.grid(color="black", linestyle="dotted")
+path_effects = [patheffects.withStroke(linewidth=3, foreground="black")]
+ax.coords[0].set_ticklabel(color="white", path_effects=path_effects)
+plt.show()
+plt.close()
[ ]:
viz = Vizier(columns=["*", "_RAJ2000", "_DEJ2000"])
-viz.ROW_LIMIT = -1
-# Photometric standard stars (tables II and IV of paper)
-tables = viz.get_catalogs("II/50")
-tables
+viz = Vizier(columns=["*", "_RAJ2000", "_DEJ2000"])
+viz.ROW_LIMIT = -1
+# Photometric standard stars (tables II and IV of paper)
+tables = viz.get_catalogs("II/50")
+tables
[ ]:
table_II50 = tables[0]
-table_II50
+table_II50 = tables[0]
+table_II50
[ ]:
idx_inside = sdss_and_hst_moc.contains_lonlat(
- table_II50["_RAJ2000"].T * u.deg,
- table_II50["_DEJ2000"].T * u.deg,
-)
-sources_inside = table_II50[idx_inside]
-sources_outside = table_II50[~idx_inside]
-sources_inside
+idx_inside = sdss_and_hst_moc.contains_lonlat(
+ table_II50["_RAJ2000"].T * u.deg,
+ table_II50["_DEJ2000"].T * u.deg,
+)
+sources_inside = table_II50[idx_inside]
+sources_outside = table_II50[~idx_inside]
+sources_inside
[ ]:
aladin = Aladin()
-aladin
+aladin = Aladin()
+aladin
[ ]:
aladin.target = "messier 51"
-aladin.fov = 1
-aladin.height = 600
+aladin.target = "messier 51"
+aladin.fov = 1
+aladin.height = 600
[ ]:
aladin.coo_frame = "icrs"
+aladin.coo_frame = "icrs"
[ ]:
aladin.survey = "P/SDSS9/color"
+aladin.survey = "P/SDSS9/color"
[ ]:
aladin.add_moc(
- sdss_and_hst_moc,
- color="blue",
- opacity=0.3,
- fill=True,
- edge=False,
- perimeter=True,
-)
+aladin.add_moc(
+ sdss_and_hst_moc,
+ color="blue",
+ opacity=0.3,
+ fill=True,
+ edge=False,
+ perimeter=True,
+)
[ ]:
# Add astropy source tables to the aladin lite viewer
-aladin.add_table(sources_inside, color="green", shape="rhomb")
-aladin.add_table(sources_outside, color="red", shape="cross")
+# Add astropy source tables to the aladin lite viewer
+aladin.add_table(sources_inside, color="green", shape="rhomb")
+aladin.add_table(sources_outside, color="red", shape="cross")
[ ]:
# change the fov and target
-aladin.target = "13 04 4.193 -03 34 13.54"
-aladin.fov = 11
+# change the fov and target
+aladin.target = "13 04 4.193 -03 34 13.54"
+aladin.fov = 11
© Copyright 2024, Matthieu Baumann <matthieu.baumann@astro.unistra.fr> Thomas Boch <thomas.boch@astro.unistra.fr> Manon Marchand <manon.marchand@astro.unistra.fr> F.-X. Pineau <francois-xavier.pineau@astro.unistra.fr>.
- Last built 26 Jul 2024.
+ Last built 17 Oct 2024.
[1]:
import matplotlib.pyplot as plt
-import numpy as np
-from astropy import units as u
-from astropy.coordinates import SkyCoord
-from mocpy import MOC
+import matplotlib.pyplot as plt
+import numpy as np
+from astropy import units as u
+from astropy.coordinates import SkyCoord
+from mocpy import MOC
[2]:
cone = MOC.from_cone(10 * u.deg, 15 * u.deg, radius=2 * u.arcmin, max_depth=14)
+cone = MOC.from_cone(10 * u.deg, 15 * u.deg, radius=2 * u.arcmin, max_depth=14)
-fig = plt.figure()
-wcs = cone.wcs(fig) # automatically creates a wcs for the MOC
-ax = fig.add_subplot(projection=wcs)
-cone.fill(ax, wcs, color="teal", alpha=0.5)
+fig = plt.figure()
+wcs = cone.wcs(fig) # automatically creates a wcs for the MOC
+ax = fig.add_subplot(projection=wcs)
+cone.fill(ax, wcs, color="teal", alpha=0.5)
[3]:
cones = MOC.from_cones(
- [10, 4, 2] * u.arcmin,
- [11, 3, 10] * u.arcmin,
- radius=2 * u.arcmin,
- max_depth=14,
-)
-
-fig = plt.figure()
-wcs = sum(cones).wcs(fig) # automatically creates a wcs for the union of the MOC
-ax = fig.add_subplot(projection=wcs)
-for cone, color in zip(cones, ["red", "blue", "purple"]):
- cone.fill(ax, wcs, color=color, alpha=0.5)
+cones = MOC.from_cones(
+ [10, 4, 2] * u.arcmin,
+ [11, 3, 10] * u.arcmin,
+ radius=2 * u.arcmin,
+ max_depth=14,
+)
+
+fig = plt.figure()
+wcs = sum(cones).wcs(fig) # automatically creates a wcs for the union of the MOC
+ax = fig.add_subplot(projection=wcs)
+for cone, color in zip(cones, ["red", "blue", "purple"]):
+ cone.fill(ax, wcs, color=color, alpha=0.5)
[4]:
cones = MOC.from_cones(
- [10, 4, 2] * u.arcmin,
- [11, 3, 10] * u.arcmin,
- radius=[2, 10, 20] * u.arcmin,
- max_depth=14,
-)
-fig = plt.figure()
-wcs = sum(cones).wcs(fig) # automatically creates a wcs for the union of the MOC
-ax = fig.add_subplot(projection=wcs)
-for cone, color in zip(cones, ["red", "blue", "purple"]):
- cone.fill(ax, wcs, color=color, alpha=0.5)
+cones = MOC.from_cones(
+ [20, 4, 2] * u.arcmin,
+ [1, -3, 10] * u.arcmin,
+ radius=[2, 10, 20] * u.arcmin,
+ max_depth=14,
+)
+fig = plt.figure()
+wcs = sum(cones).wcs(fig) # automatically creates a wcs for the union of the MOC
+ax = fig.add_subplot(projection=wcs)
+for cone, color in zip(cones, ["red", "blue", "purple"]):
+ cone.fill(ax, wcs, color=color, alpha=0.5)
If you don’t need the list of MOCs corresponding to each cone, but you only need the sum of all MOCs, then from_cones
with the option union_strategy
will be faster.
For cones that don’t overlap, the small_cones
will be faster
[5]:
+
moc = MOC.from_cones(
+ [10, 4, 2] * u.arcmin,
+ [11, 3, 10] * u.arcmin,
+ radius=2 * u.arcmin,
+ max_depth=14,
+ union_strategy="small_cones",
+)
+
+fig = plt.figure()
+wcs = moc.wcs(fig)
+ax = fig.add_subplot(projection=wcs)
+moc.fill(ax, wcs, color="hotpink", alpha=0.5)
+
[6]:
+
cone = MOC.from_cones(
+ [20, 4, 2] * u.arcmin,
+ [1, -3, 10] * u.arcmin,
+ radius=[2, 10, 20] * u.arcmin,
+ max_depth=14,
+ union_strategy="large_cones",
+)
+fig = plt.figure()
+wcs = cone.wcs(fig)
+ax = fig.add_subplot(projection=wcs)
+cone.fill(ax, wcs, color="purple", alpha=0.5)
+
A single box
+A single box. A box is defined by a center and is the intersection of two spherical wedges (thus the borders and inside cross defined by the angles follow great circles).
[5]:
+[7]:
-moc = MOC.from_box(
- lon=0 * u.deg,
- lat=0 * u.deg,
- a=5 * u.deg,
- b=2 * u.deg,
- angle=45 * u.deg,
- max_depth=10,
-)
-
-fig = plt.figure()
-wcs = moc.wcs(fig) # automatically creates a wcs for the MOC
-ax = fig.add_subplot(projection=wcs)
-moc.fill(ax, wcs, color="teal", alpha=0.5)
+moc = MOC.from_box(
+ lon=0 * u.deg,
+ lat=0 * u.deg,
+ a=5 * u.deg,
+ b=2 * u.deg,
+ angle=45 * u.deg,
+ max_depth=10,
+)
+
+fig = plt.figure()
+wcs = moc.wcs(fig) # automatically creates a wcs for the MOC
+ax = fig.add_subplot(projection=wcs)
+moc.fill(ax, wcs, color="teal", alpha=0.5)
@@ -199,187 +255,231 @@ Create MOCs from box(es)
-
+
A set of identical boxes (this is more efficient than calling from_box
a lot of times. This method is multi-threaded)
[6]:
+[8]:
-boxes = MOC.from_boxes(
- lon=[1, 5] * u.deg,
- lat=[1, 5] * u.deg,
- a=10 * u.deg,
- b=5 * u.deg,
- angle=30 * u.deg,
- max_depth=10,
-)
-fig = plt.figure()
-wcs = sum(boxes).wcs(fig) # automatically creates a wcs for the union of the MOC
-ax = fig.add_subplot(projection=wcs)
-for box, color in zip(boxes, ["yellow", "orange"]):
- box.fill(ax, wcs, color=color, alpha=0.5)
+boxes = MOC.from_boxes(
+ lon=[1, 5] * u.deg,
+ lat=[1, 5] * u.deg,
+ a=10 * u.deg,
+ b=5 * u.deg,
+ angle=30 * u.deg,
+ max_depth=10,
+)
+fig = plt.figure()
+wcs = sum(boxes).wcs(fig) # automatically creates a wcs for the union of the MOC
+ax = fig.add_subplot(projection=wcs)
+for box, color in zip(boxes, ["yellow", "orange"]):
+ box.fill(ax, wcs, color=color, alpha=0.5)
-
+
-
-same [1, 2]
-
+
+
+
+
+[9]:
+
+
+boxes = MOC.from_boxes(
+ lon=[1, 7, 2] * u.deg,
+ lat=[2, 6, 10] * u.deg,
+ a=[10, 20, 5] * u.deg,
+ b=[5, 5, 5] * u.deg,
+ angle=[30, 10, 5] * u.deg,
+ max_depth=10,
+)
+fig = plt.figure()
+wcs = sum(boxes).wcs(fig) # automatically creates a wcs for the union of the MOC
+ax = fig.add_subplot(projection=wcs)
+for box, color in zip(boxes, ["blue", "teal", "green"]):
+ box.fill(ax, wcs, color=color, alpha=0.5)
+
+
-
+
+
+Single MOC from a list of boxes¶
+Just as for cones, you can also get the union of MOCs from boxes by chosing either small_boxes
or large_boxes
for the union_strategy
parameter.
-[7]:
+[10]:
-boxes = MOC.from_boxes(
- lon=[1, 7, 2] * u.deg,
- lat=[2, 6, 10] * u.deg,
- a=[10, 20, 5] * u.deg,
- b=[5, 5, 5] * u.deg,
- angle=[30, 10, 5] * u.deg,
- max_depth=10,
-)
-fig = plt.figure()
-wcs = sum(boxes).wcs(fig) # automatically creates a wcs for the union of the MOC
-ax = fig.add_subplot(projection=wcs)
-for box, color in zip(boxes, ["blue", "teal", "green"]):
- box.fill(ax, wcs, color=color, alpha=0.5)
+moc = MOC.from_boxes(
+ lon=[1, 7, 2] * u.deg,
+ lat=[2, 6, 10] * u.deg,
+ a=[10, 20, 5] * u.deg,
+ b=[5, 5, 5] * u.deg,
+ angle=[30, 10, 5] * u.deg,
+ max_depth=10,
+ union_strategy="large_boxes",
+)
+fig = plt.figure()
+wcs = moc.wcs(fig) # automatically creates a wcs for the union of the MOC
+ax = fig.add_subplot(projection=wcs)
+moc.fill(ax, wcs, color="green", alpha=0.5)
-
+
-
-different [7, 8, 6]
-
+
+
+
+
+
+
+Create a MOC from a zone¶
+A zone is defined by a range of longitudes and latitudes. It follows great circles on the longitudinal axis and small circles on the latitudinal axis.
+
+[11]:
+
+
+moc = MOC.from_zone(
+ SkyCoord([[-50, -50], [50, 50]], unit="deg"),
+ max_depth=10,
+)
+
+fig = plt.figure()
+wcs = moc.wcs(fig) # automatically creates a wcs for the MOC
+ax = fig.add_subplot(projection=wcs)
+moc.fill(ax, wcs, color="teal", alpha=0.5)
+ax.grid(visible=True)
+
+
-
+
Create MOCs from polygon(s)¶
-[8]:
+[12]:
-# 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
+# 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 longitudes and latitudes of the random points
+ """
+ lon_min, lon_max = (-5, 5)
+ lat_min, lat_max = (-5, 5)
+ rng = np.random.default_rng(0)
+ lon = (rng.random(num_points) * (lon_max - lon_min) + lon_min) * u.deg
+ lat = (rng.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 longitudes and latitudes of the random points
+ """
+ radius_max = 10
+ rng = np.random.default_rng(1)
+ angles = np.linspace(0, 2 * np.pi, num_points)
+ radius = rng.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 longitudes and latitudes of the random points
+ """
+ radius_max = 10
+ rng = np.random.default_rng(2)
+ angles = np.linspace(0, 2 * np.pi, num_points)
+ radius = rng.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
-[9]:
+[13]:
-%%time
-# Let's generate four MOCs
+%%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)
+## 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)
+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 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)
+## A concave one
+lon, lat = generate_concave_polygon(15, 20, 7)
+concave_moc = MOC.from_polygon(lon=lon, lat=lat, max_depth=12)
@@ -388,44 +488,44 @@ Create MOCs from polygon(s)
-CPU times: user 29.3 ms, sys: 191 µs, total: 29.5 ms
-Wall time: 29.3 ms
+CPU times: user 47.2 ms, sys: 2.2 ms, total: 49.4 ms
+Wall time: 52 ms
-[10]:
+[14]:
-# Let's plot the results
+# Let's plot the results
-fig = plt.figure()
+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 = 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_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 = 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")
+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
+fig.tight_layout() # prevents overlapping labels
@@ -433,32 +533,32 @@ Create MOCs from polygon(s)
-
+
To generate a lot of MOCs from a list of polygons, use the from_polygons
method.
-[11]:
+[15]:
-list_vertices = [
- SkyCoord([-4, 4, 4, -4], [4, 4, -4, -4], unit="deg"),
- SkyCoord([0, 6, 0, -6], [6, 0, -6, 0], unit="deg"),
-]
-list_mocs = MOC.from_polygons(list_vertices)
+list_vertices = [
+ SkyCoord([-4, 4, 4, -4], [4, 4, -4, -4], unit="deg"),
+ SkyCoord([0, 6, 0, -6], [6, 0, -6, 0], unit="deg"),
+]
+list_mocs = MOC.from_polygons(list_vertices)
-[12]:
+[16]:
-# Let's plot them
-fig = plt.figure()
-wcs = sum(list_mocs).wcs(fig) # gets the WCS for the union of the mocs
-ax = fig.add_subplot(projection=wcs)
-for moc in list_mocs:
- moc.fill(ax, wcs, alpha=0.5)
+# Let's plot them
+fig = plt.figure()
+wcs = sum(list_mocs).wcs(fig) # gets the WCS for the union of the mocs
+ax = fig.add_subplot(projection=wcs)
+for moc in list_mocs:
+ moc.fill(ax, wcs, alpha=0.5)
@@ -466,7 +566,7 @@ Create MOCs from polygon(s)
-
+
-
+
+
@@ -26,7 +26,7 @@
-
+
@@ -55,7 +55,7 @@
Navigation
-
-
+
next »
@@ -66,7 +66,7 @@ Navigation
|
-
- MOCPy 0.16.2 documentation
+ MOCPy 0.17.0 documentation
»
- User documentation »
@@ -85,11 +85,11 @@ Navigation
[1]:
-import matplotlib.pyplot as plt
-import regions
-from astropy import units as u
-from astropy.coordinates import SkyCoord
-from mocpy import MOC
+import matplotlib.pyplot as plt
+import regions
+from astropy import units as u
+from astropy.coordinates import SkyCoord
+from mocpy import MOC
@@ -103,9 +103,9 @@ Circles¶
[2]:
-center = SkyCoord(42, 43, unit="deg", frame="fk5")
-circle = regions.CircleSkyRegion(center, radius=3 * u.deg)
-moc_circle = MOC.from_astropy_regions(circle, max_depth=8)
+center = SkyCoord(42, 43, unit="deg", frame="fk5")
+circle = regions.CircleSkyRegion(center, radius=3 * u.deg)
+moc_circle = MOC.from_astropy_regions(circle, max_depth=8)
@@ -113,11 +113,11 @@ Circles¶
[3]:
-fig = plt.figure(figsize=(5, 5))
-wcs = moc_circle.wcs(fig)
-ax = fig.add_subplot(projection=wcs)
-moc_circle.border(ax, wcs)
-circle.to_pixel(wcs).plot();
+fig = plt.figure(figsize=(5, 5))
+wcs = moc_circle.wcs(fig)
+ax = fig.add_subplot(projection=wcs)
+moc_circle.border(ax, wcs)
+circle.to_pixel(wcs).plot();
@@ -135,14 +135,14 @@ Rings¶
[4]:
-ring = regions.CircleAnnulusSkyRegion(center, 3 * u.deg, 4 * u.deg)
-moc_ring = MOC.from_astropy_regions(ring, max_depth=9)
+ring = regions.CircleAnnulusSkyRegion(center, 3 * u.deg, 4 * u.deg)
+moc_ring = MOC.from_astropy_regions(ring, max_depth=9)
-fig = plt.figure(figsize=(5, 5))
-wcs = moc_ring.wcs(fig)
-ax = fig.add_subplot(projection=wcs)
-moc_ring.border(ax, wcs)
-ring.to_pixel(wcs).plot();
+fig = plt.figure(figsize=(5, 5))
+wcs = moc_ring.wcs(fig)
+ax = fig.add_subplot(projection=wcs)
+moc_ring.border(ax, wcs)
+ring.to_pixel(wcs).plot();
@@ -160,20 +160,20 @@ Ellipses
[5]:
-angle = 50 * u.deg
+angle = 50 * u.deg
-ellipse = regions.EllipseSkyRegion(center, 3 * u.deg, 6 * u.deg, angle)
-ellipse_swapped = regions.EllipseSkyRegion(center, 6 * u.deg, 3 * u.deg, angle)
-moc_ellipse = MOC.from_astropy_regions(ellipse, max_depth=9)
-moc_ellipse_swapped = MOC.from_astropy_regions(ellipse_swapped, max_depth=9)
+ellipse = regions.EllipseSkyRegion(center, 3 * u.deg, 6 * u.deg, angle)
+ellipse_swapped = regions.EllipseSkyRegion(center, 6 * u.deg, 3 * u.deg, angle)
+moc_ellipse = MOC.from_astropy_regions(ellipse, max_depth=9)
+moc_ellipse_swapped = MOC.from_astropy_regions(ellipse_swapped, max_depth=9)
-fig = plt.figure(figsize=(5, 5))
-wcs = moc_ellipse.wcs(fig)
-ax = fig.add_subplot(projection=wcs)
-moc_ellipse.border(ax, wcs)
-moc_ellipse_swapped.border(ax, wcs, color="red")
-ellipse.to_pixel(wcs).plot()
-ellipse_swapped.to_pixel(wcs).plot(color="r");
+fig = plt.figure(figsize=(5, 5))
+wcs = moc_ellipse.wcs(fig)
+ax = fig.add_subplot(projection=wcs)
+moc_ellipse.border(ax, wcs)
+moc_ellipse_swapped.border(ax, wcs, color="red")
+ellipse.to_pixel(wcs).plot()
+ellipse_swapped.to_pixel(wcs).plot(color="r");
@@ -200,21 +200,21 @@ Rectangles/Box[6]:
-angle = 60 * u.deg
+angle = 60 * u.deg
-box = regions.RectangleSkyRegion(center, 12 * u.deg, 6 * u.deg, angle)
-box_swapped = regions.RectangleSkyRegion(center, 6 * u.deg, 12 * u.deg, angle)
-moc_box = MOC.from_astropy_regions(box, max_depth=8)
-moc_box_swapped = MOC.from_astropy_regions(box_swapped, max_depth=8)
+box = regions.RectangleSkyRegion(center, 12 * u.deg, 6 * u.deg, angle)
+box_swapped = regions.RectangleSkyRegion(center, 6 * u.deg, 12 * u.deg, angle)
+moc_box = MOC.from_astropy_regions(box, max_depth=8)
+moc_box_swapped = MOC.from_astropy_regions(box_swapped, max_depth=8)
-fig = plt.figure(figsize=(5, 5))
-wcs = moc_box.wcs(fig)
-ax = fig.add_subplot(projection=wcs)
-moc_box.border(ax, wcs)
-moc_box_swapped.border(ax, wcs, color="teal")
-box.to_pixel(wcs).plot()
-box_swapped.to_pixel(wcs).plot(color="teal");
+fig = plt.figure(figsize=(5, 5))
+wcs = moc_box.wcs(fig)
+ax = fig.add_subplot(projection=wcs)
+moc_box.border(ax, wcs)
+moc_box_swapped.border(ax, wcs, color="teal")
+box.to_pixel(wcs).plot()
+box_swapped.to_pixel(wcs).plot(color="teal");
@@ -232,16 +232,16 @@ Polygons
[7]:
-vertices = SkyCoord([1, 2, 2], [1, 1, 2], unit="deg", frame="fk5")
+vertices = SkyCoord([1, 2, 2], [1, 1, 2], unit="deg", frame="fk5")
-polygon = regions.PolygonSkyRegion(vertices)
-moc_polygon = MOC.from_astropy_regions(polygon, max_depth=10)
+polygon = regions.PolygonSkyRegion(vertices)
+moc_polygon = MOC.from_astropy_regions(polygon, max_depth=10)
-fig = plt.figure(figsize=(5, 5))
-wcs = moc_polygon.wcs(fig)
-ax = fig.add_subplot(projection=wcs)
-moc_polygon.fill(ax, wcs, alpha=0.2)
-polygon.to_pixel(wcs).plot();
+fig = plt.figure(figsize=(5, 5))
+wcs = moc_polygon.wcs(fig)
+ax = fig.add_subplot(projection=wcs)
+moc_polygon.fill(ax, wcs, alpha=0.2)
+polygon.to_pixel(wcs).plot();
@@ -260,16 +260,16 @@ Points¶
[8]:
-point = SkyCoord("+23h20m48.3s +61d12m06s")
+point = SkyCoord("+23h20m48.3s +61d12m06s")
-point = regions.PointSkyRegion(point)
-moc_point = MOC.from_astropy_regions(point, max_depth=10)
+point = regions.PointSkyRegion(point)
+moc_point = MOC.from_astropy_regions(point, max_depth=10)
-fig = plt.figure(figsize=(5, 5))
-wcs = moc_point.wcs(fig)
-ax = fig.add_subplot(projection=wcs)
-moc_point.fill(ax, wcs, alpha=0.2)
-point.to_pixel(wcs).plot();
+fig = plt.figure(figsize=(5, 5))
+wcs = moc_point.wcs(fig)
+ax = fig.add_subplot(projection=wcs)
+moc_point.fill(ax, wcs, alpha=0.2)
+point.to_pixel(wcs).plot();
@@ -284,7 +284,7 @@ Points¶
[9]:
-moc_point
+moc_point
@@ -331,7 +331,7 @@ Points¶
Back to Top
© Copyright 2024, Matthieu Baumann <matthieu.baumann@astro.unistra.fr> Thomas Boch <thomas.boch@astro.unistra.fr> Manon Marchand <manon.marchand@astro.unistra.fr> F.-X. Pineau <francois-xavier.pineau@astro.unistra.fr>.
- Last built 26 Jul 2024.
+ Last built 17 Oct 2024.