n the following code a projection of a cone onto a plane is created. The problem is that actual position of the cone base is affected by inclination. Fix it.
Define cone parameters
cone_top = SkyCoord('7h28m43.5s 69d11m24s', frame='icrs') # Cone top (apex) as SkyCoord
cone_base_center = SkyCoord('7h28m38s 69d12m28s', frame='icrs') # Cone base center as SkyCoord
cone_opening_angle = 20 * u.deg # Opening angle of the cone
cone_orientation = 10 * u.deg # In-plane orientation angle
cone_inclination = 50 * u.deg # Tilt relative to the plane of the sky
Load WCS for the image (assumed to be already defined)
wcs = WCS(HD(h_ha))
def radec_to_cartesian(ra_deg, dec_deg):
"""Convert RA and Dec in degrees to Cartesian coordinates."""
ra_rad = np.deg2rad(ra_deg)
dec_rad = np.deg2rad(dec_deg)
x = np.cos(dec_rad) * np.cos(ra_rad)
y = np.cos(dec_rad) * np.sin(ra_rad)
z = np.sin(dec_rad)
return np.array([x, y, z])
def cartesian_to_radec(x, y, z):
"""Convert Cartesian coordinates to RA and Dec in degrees."""
hyp = np.hypot(x, y)
ra_rad = np.arctan2(y, x)
dec_rad = np.arctan2(z, hyp)
ra_deg = np.rad2deg(ra_rad) % 360.0
dec_deg = np.rad2deg(dec_rad)
return ra_deg, dec_deg
def cone_base_vertices(cone_top, cone_base_center, cone_opening_angle, cone_inclination, num_points=100):
"""
Generate vertices for the cone's base, projected onto the sky,
taking into account the cone_inclination.
"""
# Convert apex and base center to Cartesian coordinates
apex_vector = radec_to_cartesian(cone_top.ra.deg, cone_top.dec.deg)
base_center_vector = radec_to_cartesian(cone_base_center.ra.deg, cone_base_center.dec.deg)
code
Assistant
# Compute the axis vector from apex to base center
axis_vector = base_center_vector - apex_vector
distance = np.linalg.norm(axis_vector)
axis_vector = axis_vector / distance
# Compute current inclination angle between apex_vector and axis_vector
current_inclination = np.arccos(np.dot(apex_vector, axis_vector))
# Convert cone_inclination to radians
cone_inclination_rad = cone_inclination.to(u.radian).value
# Compute the angle difference
delta_inclination = cone_inclination_rad - current_inclination
# If there is a difference, rotate axis_vector around rotation_axis by delta_inclination
if not np.isclose(delta_inclination, 0):
# Compute rotation axis (perpendicular to both apex_vector and axis_vector)
rotation_axis = np.cross(apex_vector, axis_vector)
if np.linalg.norm(rotation_axis) == 0:
# apex_vector and axis_vector are parallel, choose an arbitrary perpendicular axis
rotation_axis = np.cross(apex_vector, np.array([1, 0, 0]))
rotation_axis /= np.linalg.norm(rotation_axis)
# Rotate axis_vector by delta_inclination using Rodrigues' rotation formula
axis_vector_rotated = (axis_vector * np.cos(delta_inclination) +
np.sin(delta_inclination) * np.cross(rotation_axis, axis_vector) +
(1 - np.cos(delta_inclination)) * np.dot(rotation_axis, axis_vector) * rotation_axis)
axis_vector = axis_vector_rotated / np.linalg.norm(axis_vector_rotated)
# Now axis_vector has the correct inclination
# Recompute base_center_vector using adjusted axis_vector
base_center_vector = apex_vector + axis_vector * distance
base_center_vector /= np.linalg.norm(base_center_vector)
# Compute base radius
cone_opening_angle_rad = cone_opening_angle.to(u.radian).value
base_radius = np.tan(cone_opening_angle_rad) * distance
# At base center, compute orthonormal vectors perpendicular to axis_vector
# Find a vector not parallel to axis_vector
if np.allclose(axis_vector, [0, 0, 1]) or np.allclose(axis_vector, [0, 0, -1]):
v1 = np.array([1, 0, 0])
else:
v1 = np.array([0, 0, 1])
# Compute perpendicular vectors
v_east = np.cross(axis_vector, v1)
v_east /= np.linalg.norm(v_east)
v_north = np.cross(axis_vector, v_east)
v_north /= np.linalg.norm(v_north)
# Generate circle points around the base
theta = np.linspace(0, 2 * np.pi, num_points)
circle_points = base_center_vector[:, np.newaxis] + base_radius * (
v_east[:, np.newaxis] * np.cos(theta) + v_north[:, np.newaxis] * np.sin(theta)
)
circle_points /= np.linalg.norm(circle_points, axis=0) # Normalize to unit sphere
# Convert circle points to RA and Dec
ra_base_vertices, dec_base_vertices = cartesian_to_radec(
circle_points[0], circle_points[1], circle_points[2]
)
# Return base vertices as SkyCoord
base_vertices = SkyCoord(ra=ra_base_vertices * u.deg, dec=dec_base_vertices * u.deg, frame='icrs')
return base_vertices
The issue you're encountering stems from the way the inclination is being applied in your cone_base_vertices function. Specifically, adjusting the cone's inclination modifies the axis_vector, which in turn alters the base_center_vector. This unintended modification shifts the actual position of the cone's base, affecting its RA/Dec coordinates.
To fix this problem, you need to ensure that the base_center_vector remains fixed based on the input cone_base_center. Instead of rotating the axis_vector (which affects the base_center_vector), apply the inclination by rotating the base vertices around the fixed axis_vector. This approach preserves the position of the base center while correctly applying the desired inclination to the cone's shape.
Here's how you can modify your code to achieve this:
Remove the Rotation of the axis_vector:
Do not adjust the axis_vector based on the desired inclination. This ensures that the base_center_vector remains fixed.
Apply Inclination by Rotating Base Vertices:
Instead of rotating the entire axis, apply a rotation to the base vertices to achieve the desired inclination.
Ensure Proper Orientation:
Use the cone_orientation to define the in-plane rotation of the base vertices.
Below is the revised version of your cone_base_vertices function with these adjustments:
python
import numpy as np
from astropy.coordinates import SkyCoord
import astropy.units as u
from astropy.wcs import WCS
# Define cone parameters
cone_top = SkyCoord('7h28m43.5s 69d11m24s', frame='icrs') # Cone top (apex) as SkyCoord
cone_base_center = SkyCoord('7h28m38s 69d12m28s', frame='icrs') # Cone base center as SkyCoord
cone_opening_angle = 20 * u.deg # Opening angle of the cone
cone_orientation = 10 * u.deg # In-plane orientation angle
cone_inclination = 50 * u.deg # Tilt relative to the plane of the sky
# Load WCS for the image (assumed to be already defined)
wcs = WCS(HD(h_ha)) # Ensure HD(h_ha) is correctly defined
def radec_to_cartesian(ra_deg, dec_deg):
"""Convert RA and Dec in degrees to Cartesian coordinates."""
ra_rad = np.deg2rad(ra_deg)
dec_rad = np.deg2rad(dec_deg)
x = np.cos(dec_rad) * np.cos(ra_rad)
y = np.cos(dec_rad) * np.sin(ra_rad)
z = np.sin(dec_rad)
return np.array([x, y, z])
def cartesian_to_radec(x, y, z):
"""Convert Cartesian coordinates to RA and Dec in degrees."""
hyp = np.hypot(x, y)
ra_rad = np.arctan2(y, x)
dec_rad = np.arctan2(z, hyp)
ra_deg = np.rad2deg(ra_rad) % 360.0
dec_deg = np.rad2deg(dec_rad)
return ra_deg, dec_deg
def rotate_vectors(vectors, axis, angle_rad):
"""
Rotate a set of vectors around a given axis by angle_rad using Rodrigues' rotation formula.
vectors: 3xN array of vectors
axis: 3-element array
angle_rad: rotation angle in radians
"""
axis = axis / np.linalg.norm(axis)
cos_angle = np.cos(angle_rad)
sin_angle = np.sin(angle_rad)
rotated = (vectors * cos_angle +
np.cross(axis, vectors) * sin_angle +
axis * np.dot(axis, vectors) * (1 - cos_angle))
return rotated
def cone_base_vertices(cone_top, cone_base_center, cone_opening_angle, cone_orientation, cone_inclination, num_points=100):
"""
Generate vertices for the cone's base, projected onto the sky,
taking into account the cone_orientation and cone_inclination.
The base_center position remains fixed.
"""
# Convert apex and base center to Cartesian coordinates
apex_vector = radec_to_cartesian(cone_top.ra.deg, cone_top.dec.deg)
base_center_vector = radec_to_cartesian(cone_base_center.ra.deg, cone_base_center.dec.deg)
# Compute the axis vector from apex to base center
axis_vector = base_center_vector - apex_vector
distance = np.linalg.norm(axis_vector)
axis_vector = axis_vector / distance # Normalize
# Compute base radius
cone_opening_angle_rad = cone_opening_angle.to(u.radian).value
base_radius = np.tan(cone_opening_angle_rad) * distance
# Define in-plane orientation by rotating the orthonormal basis
# Find an arbitrary vector not parallel to axis_vector for constructing basis
if not np.allclose(axis_vector, [0, 0, 1]) and not np.allclose(axis_vector, [0, 0, -1]):
arbitrary = np.array([0, 0, 1])
else:
arbitrary = np.array([1, 0, 0])
# Compute orthonormal basis vectors
v_east = np.cross(axis_vector, arbitrary)
v_east /= np.linalg.norm(v_east)
v_north = np.cross(axis_vector, v_east)
v_north /= np.linalg.norm(v_north)
# Apply in-plane orientation by rotating v_east and v_north
orientation_rad = cone_orientation.to(u.radian).value
rotation_matrix = np.array([
[np.cos(orientation_rad), -np.sin(orientation_rad), 0],
[np.sin(orientation_rad), np.cos(orientation_rad), 0],
[0, 0, 1]
])
# Rotate the basis vectors around the axis_vector
v_east = rotate_vectors(v_east, axis_vector, orientation_rad)
v_north = np.cross(axis_vector, v_east)
v_north /= np.linalg.norm(v_north)
# Generate circle points around the base
theta = np.linspace(0, 2 * np.pi, num_points)
circle_local = (v_east[:, np.newaxis] * np.cos(theta) +
v_north[:, np.newaxis] * np.sin(theta))
circle_world = base_center_vector[:, np.newaxis] + base_radius * circle_local
# Apply inclination by rotating the circle around the east axis
inclination_rad = cone_inclination.to(u.radian).value
# Define rotation axis for inclination (assuming inclination is tilt from plane of sky)
# Here, we define 'east' as one of the basis vectors
rotation_axis = v_east # Rotate around the east direction to apply inclination
circle_world = rotate_vectors(circle_world, rotation_axis, inclination_rad)
# Normalize to unit sphere (assuming projection on celestial sphere)
circle_world /= np.linalg.norm(circle_world, axis=0)
# Convert circle points to RA and Dec
ra_base_vertices, dec_base_vertices = cartesian_to_radec(
circle_world[0], circle_world[1], circle_world[2]
)
# Return base vertices as SkyCoord
base_vertices = SkyCoord(ra=ra_base_vertices * u.deg, dec=dec_base_vertices * u.deg, frame='icrs')
return base_vertices
# Generate the base vertices with the corrected function
cone_base = cone_base_vertices(
cone_top=cone_top,
cone_base_center=cone_base_center,
cone_opening_angle=cone_opening_angle,
cone_orientation=cone_orientation,
cone_inclination=cone_inclination,
num_points=100
)
Explanation of Changes
Fixed base_center_vector:
The base_center_vector is now kept constant based on the input cone_base_center. We no longer modify it based on the inclination.
Applied Inclination to Base Vertices:
The inclination is applied by rotating the generated base circle around the v_east (in-plane east) axis. This ensures that the base maintains its central position while the cone tilts appropriately.
Handled In-Plane Orientation:
The cone_orientation parameter is used to rotate the orthonormal basis (v_east and v_north) to achieve the desired in-plane orientation of the cone.
Rotation Helper Function:
A helper function rotate_vectors is introduced to handle the rotation of vectors using Rodrigues' rotation formula. This makes the code cleaner and more modular.
Normalization:
After applying rotations, the vectors are normalized to ensure they lie on the unit sphere, which is essential for accurate RA/Dec conversions.
Additional Considerations
Projection Method:
Ensure that the normalization step (circle_world /= np.linalg.norm(circle_world, axis=0)) accurately represents the projection onto the celestial sphere. Depending on your specific use case, you might need a different projection method.
Error Handling:
If axis_vector is parallel to the arbitrary vector chosen for constructing the orthonormal basis, the code selects a different arbitrary vector to avoid computational errors.
WCS Definition:
Make sure that the WCS object is correctly defined with all necessary parameters. The placeholder HD(h_ha) should be replaced with your actual header information.
Final Remarks
By keeping the base_center_vector fixed and only applying rotations to the base vertices, the base position remains unaffected by the cone's inclination. This ensures that your cone projection maintains the correct base location while accurately representing the desired tilt and orientation.
If you encounter further issues or have specific requirements for the projection, feel free to provide more details, and I can assist you in refining the solution.