In this code I create the cone using given parameters. However the cone base center…
- User
In this code I create the cone using given parameters. However the cone base center shifts from its expected position due to inclination. Correct the cone axis length to ensure cone base center and cone apex are fixed, but ensure the cone base itself is still correctly projected. Your changes should affect cone base center and its size, but not its shape.
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)
# 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_verticesGenerate the base vertices
cone_base = cone_base_vertices(cone_top, cone_base_center, cone_opening_angle, cone_inclination)
- Assistant
To ensure that the cone's apex and base center remain fixed while applying the desired inclination, you need to adjust the axis length appropriately. In your current implementation, after rotating the
axis_vectorto account for the inclination, you recompute thebase_center_vector, which causes the base center to shift from its expected position.To fix this, you should keep the
base_center_vectorfixed and adjust theaxis_vector's length based on the desired inclination. This ensures that the apex and base center remain in their original positions, and only the size of the base is altered accordingly.Below is the corrected version of your
cone_base_verticesfunction with detailed explanations of the changes: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)) # Uncomment and define 'HD' and 'h_ha' as needed 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. Parameters: - cone_top: SkyCoord object for the cone apex. - cone_base_center: SkyCoord object for the cone base center. - cone_opening_angle: Opening angle of the cone (quantity with angular units). - cone_inclination: Inclination angle of the cone (quantity with angular units). - num_points: Number of points to generate around the base circle. Returns: - base_vertices: SkyCoord object containing the vertices of the cone base. """ # 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_original = np.linalg.norm(axis_vector) axis_unit_vector = axis_vector / distance_original # Desired inclination in radians cone_inclination_rad = cone_inclination.to(u.radian).value # Current inclination of the axis vector relative to the plane of the sky (z-axis) # Assuming the plane of the sky is the XY-plane, so inclination is the angle with respect to Z-axis current_inclination = np.arccos(axis_unit_vector[2]) # angle with Z-axis # Compute the angle difference needed to achieve the desired inclination delta_inclination = cone_inclination_rad - current_inclination # If there is a difference, rotate the axis_vector around an appropriate rotation axis if not np.isclose(delta_inclination, 0): # Define an arbitrary rotation axis perpendicular to the current axis vector # To avoid numerical issues, choose a rotation axis that is not parallel to the current axis if not np.allclose(axis_unit_vector, [0, 0, 1]) and not np.allclose(axis_unit_vector, [0, 0, -1]): rotation_axis = np.cross(axis_unit_vector, [0, 0, 1]) else: rotation_axis = np.cross(axis_unit_vector, [1, 0, 0]) rotation_axis /= np.linalg.norm(rotation_axis) # Rodrigues' rotation formula to rotate the axis_vector axis_vector_rotated = (axis_unit_vector * np.cos(delta_inclination) + np.sin(delta_inclination) * np.cross(rotation_axis, axis_unit_vector) + (1 - np.cos(delta_inclination)) * np.dot(rotation_axis, axis_unit_vector) * rotation_axis) axis_vector_rotated /= np.linalg.norm(axis_vector_rotated) else: axis_vector_rotated = axis_unit_vector.copy() # To keep apex and base_center fixed, compute the new distance along the rotated axis # Project the original axis_vector onto the rotated axis_vector_rotated # distance_new = projection of (base_center_vector - apex_vector) onto axis_vector_rotated base_vector = base_center_vector - apex_vector distance_new = np.dot(base_vector, axis_vector_rotated) # Update the axis vector with the new rotated direction and adjusted length axis_vector_new = axis_vector_rotated * distance_new # Calculate the base radius using the opening angle cone_opening_angle_rad = cone_opening_angle.to(u.radian).value base_radius = np.tan(cone_opening_angle_rad) * np.abs(distance_new) # At the base center, compute orthonormal vectors perpendicular to the new axis_vector_new # Choose a reference vector that is not parallel to axis_vector_new if not np.allclose(axis_vector_new, [0, 0, 1]) and not np.allclose(axis_vector_new, [0, 0, -1]): v_ref = np.array([0, 0, 1]) else: v_ref = np.array([1, 0, 0]) # Compute perpendicular vectors v_east = np.cross(axis_vector_new, v_ref) v_east /= np.linalg.norm(v_east) v_north = np.cross(axis_vector_new, v_east) v_north /= np.linalg.norm(v_north) # Generate circle points around the base theta = np.linspace(0, 2 * np.pi, num_points) base_center_vector_fixed = base_center_vector # Keep the base center fixed circle_points = base_center_vector_fixed[:, np.newaxis] + base_radius * ( v_east[:, np.newaxis] * np.cos(theta) + v_north[:, np.newaxis] * np.sin(theta) ) # Normalize the circle points to lie on the unit sphere (assuming unit distance) circle_points /= np.linalg.norm(circle_points, axis=0) # Convert circle points back 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 # Generate the base vertices cone_base = cone_base_vertices(cone_top, cone_base_center, cone_opening_angle, cone_inclination) # (Optional) Plotting the cone base to verify correctness # import matplotlib.pyplot as plt # fig = plt.figure(figsize=(8, 6)) # ax = fig.add_subplot(111, projection='aitoff') # ax.scatter(cone_base.ra.wrap_at(180*u.deg).radian, cone_base.dec.radian, s=1, label='Cone Base') # ax.scatter(cone_top.ra.wrap_at(180*u.deg).radian, cone_top.dec.radian, color='red', label='Apex') # ax.scatter(cone_base_center.ra.wrap_at(180*u.deg).radian, cone_base_center.dec.radian, color='green', label='Base Center') # ax.legend() # plt.show()Explanation of Changes
-
Maintaining Fixed Apex and Base Center:
- Original Issue: The original code recalculates
base_center_vectorafter rotating theaxis_vector, causing the base center to shift. - Solution: Keep
base_center_vectorfixed by not recalculating it after applying the inclination. Instead, adjust theaxis_vector's length (distance_new) to ensure that the apex and base center remain in their original positions.
- Original Issue: The original code recalculates
-
Adjusting Axis Length Based on Inclination:
- After rotating the
axis_vectorto achieve the desired inclination (cone_inclination), calculate the new distance (distance_new) by projecting the original base vector (base_vector) onto the rotatedaxis_vector_rotated. - This ensures that the rotated axis aligns correctly with the fixed apex and base center.
- After rotating the
-
Calculating Base Radius:
- The base radius is recalculated using the adjusted
distance_newto ensure that the base retains its correct projection on the sky. base_radius = tan(cone_opening_angle_rad) * |distance_new|
- The base radius is recalculated using the adjusted
-
Generating Base Vertices Without Shifting the Base Center:
- Circle points around the base are generated around the fixed
base_center_vector_fixed, ensuring that the base center does not shift. - These points are normalized to lie on the unit sphere (assuming a unit distance for simplicity).
- Circle points around the base are generated around the fixed
-
Preserving the Shape of the Base:
- The shape of the base remains a circle in 3D space, projected correctly onto the sky without distortion, since only the base radius and position are adjusted, not the intrinsic geometry.
-
Optional Plotting:
- An optional plotting section (commented out) is provided to visualize the cone base, apex, and base center for verification purposes. You can uncomment and customize this section as needed.
Key Takeaways
-
Fixed Points: To keep specific points (apex and base center) fixed while applying transformations like inclination, it's crucial to adjust related parameters (like axis length) rather than repositioning the fixed points.
-
Vector Projections: Utilizing vector projections ensures that directional changes (like inclination) are accommodated without altering the positions of fixed points.
-
Normalization: Always normalize vectors when dealing with directional data to avoid unintended scaling effects.
This corrected implementation ensures that the cone's apex and base center remain fixed in their expected positions on the sky, while the base's size is adjusted to account for the inclination, preserving the base's shape.
-