def calculate_local_north_and_up_in_celestial_coords(self, force_update): current_time = self.get_time_in_millis() diff = math.fabs(current_time - self.celestial_coords_last_updated) if (not force_update) and diff < self.MINIMUM_TIME_BETWEEN_CELESTIAL_COORD_UPDATES_MILLIS: return self.celestial_coords_last_updated = current_time self.update_magnetic_correction() up_ra, up_dec = Geometry.calculate_RADec_of_zenith(self.get_time(), self.location) self.up_celestial = get_instance(up_ra, up_dec) z = self.AXIS_OF_EARTHS_ROTATION z_dotu = Geometry.scalar_product(self.up_celestial, z) self.true_north_celestial = \ Geometry.add_vectors(z, Geometry.scale_vector(self.up_celestial, -z_dotu)) self.true_north_celestial.normalize() self.true_east_celestial = Geometry.vector_product(self.true_north_celestial, \ self.up_celestial) # Apply magnetic correction. Rather than correct the phone's axes for # the magnetic declination, it's more efficient to rotate the # celestial axes by the same amount in the opposite direction. declination = self.magnetic_declination_calc.get_declination() rotation_matrix = Geometry.calculate_rotation_matrix(declination, self.up_celestial) magnetic_north_celestial = Geometry.matrix_vector_multiply(rotation_matrix, \ self.true_north_celestial) magnetic_east_celestial = Geometry.vector_product(magnetic_north_celestial, \ self.up_celestial) self.axes_magnetic_celestial_matrix = get_colmatrix_from_vectors(magnetic_north_celestial, \ self.up_celestial, \ magnetic_east_celestial)
def rotate(self, degrees): ''' rotate astronomers view (clockwise/anti-clockwise) ''' if not self.enabled: return pointing = self.model.pointing pointing_xyz = pointing.get_line_of_sight() top_xyz = pointing.get_perpendicular() rotation = Geometry.calculate_rotation_matrix(degrees, pointing_xyz) new_up_xyz = Geometry.matrix_vector_multiply(rotation, top_xyz) new_up_xyz.normalize() self.model.set_pointing(pointing_xyz, new_up_xyz)
def get_west(self): ''' In celestial coordinates ''' self.calculate_local_north_and_up_in_celestial_coords(False) v = Geometry.scale_vector(self.true_east_celestial, -1) return get_instance_from_vector3(v)
def calculate_percent_illuminated(self, t_struct): """ Calculate the percent of the body that is illuminated. The value returned is a fraction in the range from 0.0 to 100.0. """ phase_angle = self.calculate_phase_angle(t_struct) return 50.0 * (1.0 + math.cos(Geometry.degrees_to_radians(phase_angle)))
def calculate_percent_illuminated(self, t_struct): ''' Calculate the percent of the body that is illuminated. The value returned is a fraction in the range from 0.0 to 100.0. ''' phase_angle = self.calculate_phase_angle(t_struct) return 50.0 * (1.0 + math.cos(Geometry.degrees_to_radians(phase_angle)))
def calculate_phase_angle(self, t_struct): """ Calculates the phase angle of the planet, in degrees. """ # For the moon, we will approximate phase angle by calculating the # elongation of the moon relative to the sun. This is accurate to within # about 1%. if self.id == planet_enum.MOON: moon_ra_dec = self.calculate_lunar_geocentric_location(t_struct) moon = gc_get_instance(moon_ra_dec.ra, moon_ra_dec.dec) sun_coords = hc_get_instance( t_struct=t_struct, planet=Planet( planet_enum.SUN, res[planet_enum.SUN][0], res[planet_enum.SUN][1], res[planet_enum.SUN][2] ), ) sun_ra_dec = calculate_ra_dec_dist(sun_coords) sun = gc_get_instance(sun_ra_dec.ra, sun_ra_dec.dec) return 180.0 - Geometry.radians_to_degrees(math.acos(sun.x * moon.x + sun.y * moon.y + sun.z * moon.z)) # First, determine position in the solar system. planet_coords = hc_get_instance(planet=self, t_struct=t_struct) # Second, determine position relative to Earth earth_coords = hc_get_instance( t_struct=t_struct, planet=Planet(planet_enum.SUN, res[planet_enum.SUN][0], res[planet_enum.SUN][1], res[planet_enum.SUN][2]), ) earth_distance = planet_coords.distance_from(earth_coords) # Finally, calculate the phase of the body. phase = Geometry.radians_to_degrees( math.acos( ( earth_distance * earth_distance + planet_coords.radius * planet_coords.radius - earth_coords.radius * earth_coords.radius ) / (2.0 * earth_distance * planet_coords.radius) ) ) return phase
def calculate_hour_angle(self, altitude, latitude, declination): ''' Calculates the hour angle of a given declination for the given location. This is a helper application for the rise and set calculations. Its probably not worth using as a general purpose method. All values are in degrees. This method calculates the hour angle from the meridian using the following equation from the Astronomical Almanac (p487): cos ha = (sin alt - sin lat * sin dec) / (cos lat * cos dec) ''' alt_rads = Geometry.degrees_to_radians(altitude) lat_rads = Geometry.degrees_to_radians(latitude) dec_rads = Geometry.degrees_to_radians(declination) cos_ha = (math.sin(alt_rads) - math.sin(lat_rads) * math.sin(dec_rads)) / \ (math.cos(lat_rads) * math.cos(dec_rads)) return Geometry.radians_to_degrees(math.acos(cos_ha))
def calculate_pointing(self): ''' update the direction that the phone is pointing and the up vector perpendicular to the phone (in celestial coords). ''' if not (self.auto_update_pointing): return transform = Geometry.matrix_multiply(self.axes_magnetic_celestial_matrix, \ self.axes_phone_inverse_matrix) view_in_space_space = \ Geometry.matrix_vector_multiply(transform, self.POINTING_DIR_IN_PHONE_COORDS) screen_up_in_space_space = \ Geometry.matrix_vector_multiply(transform, self.SCREEN_UP_IN_PHONE_COORDS) self.pointing.update_line_of_sight(view_in_space_space) self.pointing.update_perpendicular(screen_up_in_space_space)
def calculate_hour_angle(self, altitude, latitude, declination): """ Calculates the hour angle of a given declination for the given location. This is a helper application for the rise and set calculations. Its probably not worth using as a general purpose method. All values are in degrees. This method calculates the hour angle from the meridian using the following equation from the Astronomical Almanac (p487): cos ha = (sin alt - sin lat * sin dec) / (cos lat * cos dec) """ alt_rads = Geometry.degrees_to_radians(altitude) lat_rads = Geometry.degrees_to_radians(latitude) dec_rads = Geometry.degrees_to_radians(declination) cos_ha = (math.sin(alt_rads) - math.sin(lat_rads) * math.sin(dec_rads)) / ( math.cos(lat_rads) * math.cos(dec_rads) ) return Geometry.radians_to_degrees(math.acos(cos_ha))
def change_right_left(self, radians): ''' Moves the astronomer's pointing right or left @param radians the angular change in the pointing in radians (only accurate in the limit as radians tends to 0.) ''' if not self.enabled: return pointing = self.model.pointing pointing_xyz = pointing.get_line_of_sight() top_xyz = pointing.get_perpendicular() horizontal_xyz = Geometry.vector_product(pointing_xyz, top_xyz) delta_xyz = Geometry.scale_vector(horizontal_xyz, radians) new_pointing_xyz = Geometry.add_vectors(pointing_xyz, delta_xyz) new_pointing_xyz.normalize() self.model.set_pointing(new_pointing_xyz, top_xyz)
def assertVectorEquals(v1, v2, tol_angle, tol_length): import src.utils.Geometry as Geometry import math normv1 = v1.length() normv2 = v2.length() if abs(normv1 - normv2) > tol_length: return False cosineSim = Geometry.cosine_similarity(v1, v2) cosTol = math.cos(tol_angle) return cosineSim >= cosTol
def get_magnitude(self, t_struct): """ Calculates the planet's magnitude for the given date. """ if self.id == planet_enum.SUN: return -27.0 if self.id == planet_enum.MOON: return -10.0 # First, determine position in the solar system. planet_coords = hc_get_instance(planet=self, t_struct=t_struct) # Second, determine position relative to Earth earth_coords = hc_get_instance( t_struct=t_struct, planet=Planet(planet_enum.SUN, res[planet_enum.SUN][0], res[planet_enum.SUN][1], res[planet_enum.SUN][2]), ) earth_distance = planet_coords.distance_from(earth_coords) # Third, calculate the phase of the body. phase = Geometry.radians_to_degrees( math.acos( ( earth_distance * earth_distance + planet_coords.radius * planet_coords.radius - earth_coords.radius * earth_coords.radius ) / (2.0 * earth_distance * planet_coords.radius) ) ) p = phase / 100.0 # Normalized phase angle # Finally, calculate the magnitude of the body. mag = -100.0 # Apparent visual magnitude if self.id == planet_enum.MERCURY: mag = -0.42 + (3.80 - (2.73 - 2.00 * p) * p) * p elif self.id == planet_enum.VENUS: mag = -4.40 + (0.09 + (2.39 - 0.65 * p) * p) * p elif self.id == planet_enum.MARS: mag = -1.52 + 1.6 * p elif self.id == planet_enum.JUPITER: mag = -9.40 + 0.5 * p elif self.id == planet_enum.SATURN: mag = -8.75 elif self.id == planet_enum.URANUS: mag = -7.19 elif self.id == planet_enum.NEPTUNE: mag = -6.87 elif self.id == planet_enum.PLUTO: mag = -1.0 else: mag = 100 return mag + 5.0 * math.log10(planet_coords.radius * earth_distance)
def calculate_phase_angle(self, t_struct): ''' Calculates the phase angle of the planet, in degrees. ''' # For the moon, we will approximate phase angle by calculating the # elongation of the moon relative to the sun. This is accurate to within # about 1%. if self.id == planet_enum.MOON: moon_ra_dec = self.calculate_lunar_geocentric_location(t_struct) moon = gc_get_instance(moon_ra_dec.ra, moon_ra_dec.dec) sun_coords = hc_get_instance(t_struct=t_struct, planet=Planet(planet_enum.SUN, res[planet_enum.SUN][0], res[planet_enum.SUN][1], res[planet_enum.SUN][2])) sun_ra_dec = calculate_ra_dec_dist(sun_coords) sun = gc_get_instance(sun_ra_dec.ra, sun_ra_dec.dec) return 180.0 - \ Geometry.radians_to_degrees(math.acos(sun.x * moon.x + sun.y * moon.y + sun.z * moon.z)) # First, determine position in the solar system. planet_coords = hc_get_instance(planet=self, t_struct=t_struct) # Second, determine position relative to Earth earth_coords = hc_get_instance(t_struct=t_struct, planet=Planet(planet_enum.SUN, res[planet_enum.SUN][0], res[planet_enum.SUN][1], res[planet_enum.SUN][2])) earth_distance = planet_coords.distance_from(earth_coords) # Finally, calculate the phase of the body. phase = Geometry.radians_to_degrees(\ math.acos((earth_distance * earth_distance + \ planet_coords.radius * planet_coords.radius - \ earth_coords.radius * earth_coords.radius) / \ (2.0 * earth_distance * planet_coords.radius))) return phase
def change_up_down(self, radians): ''' Moves the astronomer's pointing up or down. @param radians the angular change in the pointing in radians (only accurate in the limit as radians tends to 0.) ''' if not self.enabled: return pointing = self.model.pointing pointing_xyz = pointing.get_line_of_sight() top_xyz = pointing.get_perpendicular() delta_xyz = Geometry.scale_vector(top_xyz, -radians) new_pointing_xyz = Geometry.add_vectors(pointing_xyz, delta_xyz) new_pointing_xyz.normalize() delta_up_xyz = Geometry.scale_vector(pointing_xyz, radians) new_up_xyz = Geometry.add_vectors(top_xyz, delta_up_xyz) new_up_xyz.normalize() self.model.set_pointing(new_pointing_xyz, new_up_xyz)
def calculate_local_north_and_up_in_phone_coords(self): down = self.acceleration.copy() down.normalize() # Magnetic field goes *from* North to South, so reverse it. magnetic_field_to_north = self.magnetic_field.copy() magnetic_field_to_north.scale(-1) magnetic_field_to_north.normalize() # This is the vector to magnetic North *along the ground*. v2 = Geometry.scale_vector(down, -Geometry.scalar_product(magnetic_field_to_north, \ down)) magnetic_north_phone = Geometry.add_vectors(magnetic_field_to_north, v2) magnetic_north_phone.normalize() up_phone = Geometry.scale_vector(down, -1) magnetic_east_phone = Geometry.vector_product(magnetic_north_phone, up_phone) # The matrix is orthogonal, so transpose it to find its inverse. # Easiest way to do that is to construct it from row vectors instead # of column vectors. self.axes_phone_inverse_matrix = \ get_rowmatrix_from_vectors(magnetic_north_phone, up_phone, magnetic_east_phone)
def get_magnitude(self, t_struct): ''' Calculates the planet's magnitude for the given date. ''' if self.id == planet_enum.SUN: return -27.0 if self.id == planet_enum.MOON: return -10.0 # First, determine position in the solar system. planet_coords = hc_get_instance(planet=self, t_struct=t_struct) # Second, determine position relative to Earth earth_coords = hc_get_instance(t_struct=t_struct, planet=Planet(planet_enum.SUN, res[planet_enum.SUN][0], res[planet_enum.SUN][1], res[planet_enum.SUN][2])) earth_distance = planet_coords.distance_from(earth_coords) # Third, calculate the phase of the body. phase = Geometry.radians_to_degrees(\ math.acos((earth_distance * earth_distance + \ planet_coords.radius * planet_coords.radius - \ earth_coords.radius * earth_coords.radius) / \ (2.0 * earth_distance * planet_coords.radius))) p = phase/100.0 # Normalized phase angle # Finally, calculate the magnitude of the body. mag = -100.0 # Apparent visual magnitude if self.id == planet_enum.MERCURY: mag = -0.42 + (3.80 - (2.73 - 2.00 * p) * p) * p elif self.id == planet_enum.VENUS: mag = -4.40 + (0.09 + (2.39 - 0.65 * p) * p) * p elif self.id == planet_enum.MARS: mag = -1.52 + 1.6 * p elif self.id == planet_enum.JUPITER: mag = -9.40 + 0.5 * p elif self.id == planet_enum.SATURN: mag = -8.75 elif self.id == planet_enum.URANUS: mag = -7.19 elif self.id == planet_enum.NEPTUNE: mag = -6.87 elif self.id == planet_enum.PLUTO: mag = -1.0 else: mag = 100 return (mag + 5.0 * math.log10(planet_coords.radius * earth_distance))
def calculate_lunar_geocentric_location(self, t_struct): """ Calculate the geocentric right ascension and declination of the moon using an approximation as described on page D22 of the 2008 Astronomical Almanac All of the variables in this method use the same names as those described in the text: lambda = Ecliptic longitude (degrees) beta = Ecliptic latitude (degrees) pi = horizontal parallax (degrees) r = distance (Earth radii) NOTE: The text does not give a specific time period where the approximation is valid, but it should be valid through at least 2009. """ # First, calculate the number of Julian centuries from J2000.0. t = (calculate_julian_day(t_struct) - 2451545.0) / 36525.0 # Second, calculate the approximate geocentric orbital elements. lambda_val = ( 218.32 + 481267.881 * t + 6.29 * math.sin(Geometry.degrees_to_radians(135.0 + 477198.87 * t)) - 1.27 * math.sin(Geometry.degrees_to_radians(259.3 - 413335.36 * t)) + 0.66 * math.sin(Geometry.degrees_to_radians(235.7 + 890534.22 * t)) + 0.21 * math.sin(Geometry.degrees_to_radians(269.9 + 954397.74 * t)) - 0.19 * math.sin(Geometry.degrees_to_radians(357.5 + 35999.05 * t)) - 0.11 * math.sin(Geometry.degrees_to_radians(186.5 + 966404.03 * t)) ) beta = ( 5.13 * math.sin(Geometry.degrees_to_radians(93.3 + 483202.02 * t)) + 0.28 * math.sin(Geometry.degrees_to_radians(228.2 + 960400.89 * t)) - 0.28 * math.sin(Geometry.degrees_to_radians(318.3 + 6003.15 * t)) - 0.17 * math.sin(Geometry.degrees_to_radians(217.6 - 407332.21 * t)) ) # Third, convert to RA and Dec. l = math.cos(Geometry.degrees_to_radians(beta)) * math.cos(Geometry.degrees_to_radians(lambda_val)) m = 0.9175 * math.cos(Geometry.degrees_to_radians(beta)) * math.sin( Geometry.degrees_to_radians(lambda_val) ) - 0.3978 * math.sin(Geometry.degrees_to_radians(beta)) n = 0.3978 * math.cos(Geometry.degrees_to_radians(beta)) * math.sin( Geometry.degrees_to_radians(lambda_val) ) + 0.9175 * math.sin(Geometry.degrees_to_radians(beta)) ra = Geometry.radians_to_degrees(Geometry.mod_2_pi(math.atan2(m, l))) dec = Geometry.radians_to_degrees(math.asin(n)) return RaDec(ra, dec)
def calculate_lunar_geocentric_location(self, t_struct): ''' Calculate the geocentric right ascension and declination of the moon using an approximation as described on page D22 of the 2008 Astronomical Almanac All of the variables in this method use the same names as those described in the text: lambda = Ecliptic longitude (degrees) beta = Ecliptic latitude (degrees) pi = horizontal parallax (degrees) r = distance (Earth radii) NOTE: The text does not give a specific time period where the approximation is valid, but it should be valid through at least 2009. ''' # First, calculate the number of Julian centuries from J2000.0. t = ((calculate_julian_day(t_struct) - 2451545.0) / 36525.0) # Second, calculate the approximate geocentric orbital elements. lambda_val = 218.32 + 481267.881 * t + 6.29 \ * math.sin(Geometry.degrees_to_radians(135.0 + 477198.87 * t)) - 1.27 \ * math.sin(Geometry.degrees_to_radians(259.3 - 413335.36 * t)) + 0.66 \ * math.sin(Geometry.degrees_to_radians(235.7 + 890534.22 * t)) + 0.21 \ * math.sin(Geometry.degrees_to_radians(269.9 + 954397.74 * t)) - 0.19 \ * math.sin(Geometry.degrees_to_radians(357.5 + 35999.05 * t)) - 0.11 \ * math.sin(Geometry.degrees_to_radians(186.5 + 966404.03 * t)) beta = 5.13 \ * math.sin(Geometry.degrees_to_radians(93.3 + 483202.02 * t)) + 0.28 \ * math.sin(Geometry.degrees_to_radians(228.2 + 960400.89 * t)) - 0.28 \ * math.sin(Geometry.degrees_to_radians(318.3 + 6003.15 * t)) - 0.17 \ * math.sin(Geometry.degrees_to_radians(217.6 - 407332.21 * t)) # Third, convert to RA and Dec. l = math.cos(Geometry.degrees_to_radians(beta)) \ * math.cos(Geometry.degrees_to_radians(lambda_val)) m = 0.9175 * math.cos(Geometry.degrees_to_radians(beta)) \ * math.sin(Geometry.degrees_to_radians(lambda_val)) - 0.3978 \ * math.sin(Geometry.degrees_to_radians(beta)) n = 0.3978 * math.cos(Geometry.degrees_to_radians(beta)) \ * math.sin(Geometry.degrees_to_radians(lambda_val)) + 0.9175 \ * math.sin(Geometry.degrees_to_radians(beta)) ra = Geometry.radians_to_degrees(Geometry.mod_2_pi(math.atan2(m, l))) dec = Geometry.radians_to_degrees(math.asin(n)) return RaDec(ra, dec)
def get_orbital_elements(self, t_struct): # Centuries since J2000 jc = julian_centuries(t_struct) if self.id == planet_enum.MERCURY: a = 0.38709927 + 0.00000037 * jc e = 0.20563593 + 0.00001906 * jc i = Geometry.degrees_to_radians(7.00497902 - 0.00594749 * jc) l = Geometry.mod_2_pi(Geometry.degrees_to_radians(252.25032350 + 149472.67411175 * jc)) w = Geometry.degrees_to_radians(77.45779628 + 0.16047689 * jc) o = Geometry.degrees_to_radians(48.33076593 - 0.12534081 * jc) return OrbitalElements(a, e, i, o, w, l) elif self.id == planet_enum.VENUS: a = 0.72333566 + 0.00000390 * jc e = 0.00677672 - 0.00004107 * jc i = Geometry.degrees_to_radians(3.39467605 - 0.00078890 * jc) l = Geometry.mod_2_pi(Geometry.degrees_to_radians(181.97909950 + 58517.81538729 * jc)) w = Geometry.degrees_to_radians(131.60246718 + 0.00268329 * jc) o = Geometry.degrees_to_radians(76.67984255 - 0.27769418 * jc) return OrbitalElements(a, e, i, o, w, l) elif self.id == planet_enum.SUN: # Note that this is the orbital data for Earth. a = 1.00000261 + 0.00000562 * jc e = 0.01671123 - 0.00004392 * jc i = Geometry.degrees_to_radians(-0.00001531 - 0.01294668 * jc) l = Geometry.mod_2_pi(Geometry.degrees_to_radians(100.46457166 + 35999.37244981 * jc)) w = Geometry.degrees_to_radians(102.93768193 + 0.32327364 * jc) o = 0.0 return OrbitalElements(a, e, i, o, w, l) elif self.id == planet_enum.MARS: a = 1.52371034 + 0.00001847 * jc e = 0.09339410 + 0.00007882 * jc i = Geometry.degrees_to_radians(1.84969142 - 0.00813131 * jc) l = Geometry.mod_2_pi(Geometry.degrees_to_radians(-4.55343205 + 19140.30268499 * jc)) w = Geometry.degrees_to_radians(-23.94362959 + 0.44441088 * jc) o = Geometry.degrees_to_radians(49.55953891 - 0.29257343 * jc) return OrbitalElements(a, e, i, o, w, l) elif self.id == planet_enum.JUPITER: a = 5.20288700 - 0.00011607 * jc e = 0.04838624 - 0.00013253 * jc i = Geometry.degrees_to_radians(1.30439695 - 0.00183714 * jc) l = Geometry.mod_2_pi(Geometry.degrees_to_radians(34.39644051 + 3034.74612775 * jc)) w = Geometry.degrees_to_radians(14.72847983 + 0.21252668 * jc) o = Geometry.degrees_to_radians(100.47390909 + 0.20469106 * jc) return OrbitalElements(a, e, i, o, w, l) elif self.id == planet_enum.SATURN: a = 9.53667594 - 0.00125060 * jc e = 0.05386179 - 0.00050991 * jc i = Geometry.degrees_to_radians(2.48599187 + 0.00193609 * jc) l = Geometry.mod_2_pi(Geometry.degrees_to_radians(49.95424423 + 1222.49362201 * jc)) w = Geometry.degrees_to_radians(92.59887831 - 0.41897216 * jc) o = Geometry.degrees_to_radians(113.66242448 - 0.28867794 * jc) return OrbitalElements(a, e, i, o, w, l) elif self.id == planet_enum.URANUS: a = 19.18916464 - 0.00196176 * jc e = 0.04725744 - 0.00004397 * jc i = Geometry.degrees_to_radians(0.77263783 - 0.00242939 * jc) l = Geometry.mod_2_pi(Geometry.degrees_to_radians(313.23810451 + 428.48202785 * jc)) w = Geometry.degrees_to_radians(170.95427630 + 0.40805281 * jc) o = Geometry.degrees_to_radians(74.01692503 + 0.04240589 * jc) return OrbitalElements(a, e, i, o, w, l) elif self.id == planet_enum.NEPTUNE: a = 30.06992276 + 0.00026291 * jc e = 0.00859048 + 0.00005105 * jc i = Geometry.degrees_to_radians(1.77004347 + 0.00035372 * jc) l = Geometry.mod_2_pi(Geometry.degrees_to_radians(-55.12002969 + 218.45945325 * jc)) w = Geometry.degrees_to_radians(44.96476227 - 0.32241464 * jc) o = Geometry.degrees_to_radians(131.78422574 - 0.00508664 * jc) return OrbitalElements(a, e, i, o, w, l) elif self.id == planet_enum.PLUTO: a = 39.48211675 - 0.00031596 * jc e = 0.24882730 + 0.00005170 * jc i = Geometry.degrees_to_radians(17.14001206 + 0.00004818 * jc) l = Geometry.mod_2_pi(Geometry.degrees_to_radians(238.92903833 + 145.20780515 * jc)) w = Geometry.degrees_to_radians(224.06891629 - 0.04062942 * jc) o = Geometry.degrees_to_radians(110.30393684 - 0.01183482 * jc) return OrbitalElements(a, e, i, o, w, l) else: raise RuntimeError("Unknown Planet:" + str(self.id))