# Licensed under a 3-clause BSD style license - see LICENSE.rst"""Other coordinate and distance-related functions."""importnumpyasnpfromastropy.unitsimportQuantity,Unit__all__=["cartesian","D_SUN_TO_GALACTIC_CENTER","galactic","motion_since_birth","polar","velocity_glon_glat",]# TODO: replace this with the default from the Galactocentric frame in astropy.coordinatesD_SUN_TO_GALACTIC_CENTER=Quantity(8.5,"kpc")"""Default assumed distance from the Sun to the Galactic center (`~astropy.units.Quantity`)"""
[docs]defcartesian(r,theta):"""Convert polar coordinates to cartesian coordinates."""x=r*np.cos(theta)y=r*np.sin(theta)returnx,y
[docs]defpolar(x,y):"""Convert cartesian coordinates to polar coordinates."""r=np.sqrt(x**2+y**2)theta=np.arctan2(y,x)returnr,theta
[docs]defgalactic(x,y,z,obs_pos=None):"""Compute galactic coordinates lon, lat and distance. For given position in cartesian coordinates (kpc). """obs_pos=obs_posor[D_SUN_TO_GALACTIC_CENTER,0,0]y_prime=y+D_SUN_TO_GALACTIC_CENTERd=np.sqrt(x**2+y_prime**2+z**2)glon=np.arctan2(x,y_prime).to("deg")glat=np.arcsin(z/d).to("deg")returnd,glon,glat
[docs]defvelocity_glon_glat(x,y,z,vx,vy,vz):""" Compute projected angular velocity in galactic coordinates. Parameters ---------- x, y, z : `~astropy.units.Quantity` Position in x, y, z direction. vx, vy, vz : `~astropy.units.Quantity` Velocity in x, y, z direction. Returns ------- v_glon, v_glat : `~astropy.units.Quantity` Projected velocity in Galactic sky coordinates. """y_prime=y+D_SUN_TO_GALACTIC_CENTERd=np.sqrt(x**2+y_prime**2+z**2)r=np.sqrt(x**2+y_prime**2)v_glon=(-y_prime*vx+x*vy)/r**2v_glat=vz/(np.sqrt(1-(z/d)**2)*d)-np.sqrt(vx**2+vy**2+vz**2)*z/(np.sqrt(1-(z/d)**2)*d**2)returnv_glon*Unit("rad"),v_glat*Unit("rad")
[docs]defmotion_since_birth(v,age,theta,phi):""" Compute motion of an astrophysical object with a given velocity, direction and age. Parameters ---------- v : `~astropy.units.Quantity` Absolute value of the velocity. age : `~astropy.units.Quantity` Age of the source. theta, phi : `~astropy.units.Quantity` Angular direction of the velocity. Returns ------- dx, dy, dz : `~astropy.units.Quantity` Displacement in x, y, z direction. vx, vy, vz : `~astropy.units.Quantity` Velocity in x, y, z direction. """vx=v*np.cos(phi)*np.sin(theta)vy=v*np.sin(phi)*np.sin(theta)vz=v*np.cos(theta)# Compute new positionsdx=vx*agedy=vy*agedz=vz*agereturndx,dy,dz,vx,vy,vz