def get_asteroid(name, timestamp): # https://rhodesmill.org/skyfield/example-plots.html#drawing-a-finder-chart-for-comet-neowise # https://astroquery.readthedocs.io/en/latest/mpc/mpc.html designation = name try: asteroids = Asteroid.objects.filter(designation__icontains=name) designation = asteroids[0].designation except: pass with load.open(settings.MY_ASTEROIDS_URL) as f: minor_planets = mpc.load_mpcorb_dataframe(f) bad_orbits = minor_planets.semimajor_axis_au.isnull() minor_planets = minor_planets[~bad_orbits] # Index by designation for fast lookup. minor_planets = minor_planets.set_index('designation', drop=False) row = minor_planets.loc[designation] ts = load.timescale() eph = load('de421.bsp') sun, earth = eph['sun'], eph['earth'] asteroid = sun + mpc.mpcorb_orbit(row, ts, GM_SUN) t = ts.utc(timestamp.year, timestamp.month, timestamp.day, timestamp.hour, timestamp.minute) ra, dec, distance_from_sun = sun.at(t).observe(asteroid).radec() ra, dec, distance_from_earth = earth.at(t).observe(asteroid).radec() # https://towardsdatascience.com/space-science-with-python-a-very-bright-opposition-62e248abfe62 # how do I calculate the current phase_angle between sun and earth as seen from the asteroid ra_sun, dec_sun, d = asteroid.at(t).observe(sun).radec() ra_earth,dec_earth, d = asteroid.at(t).observe(earth).radec() phase_angle_in_degrees = abs(ra_sun.hours - ra_earth.hours) phase_angle = phase_angle_in_degrees * math.pi / 180 visual_magnitude = app_mag(abs_mag=row['magnitude_H'], \ phase_angle=phase_angle, \ slope_g=row['magnitude_G'], \ d_ast_sun=distance_from_sun.au, \ d_ast_earth=distance_from_earth.au) result = {} result['name'] = name result['designation'] = designation result['timestamp'] = str(timestamp) result['ra'] = str(ra) result['dec'] = str(dec) result['ra_decimal'] = str(ra.hours * 15) result['dec_decimal'] = str(dec.degrees) result['distance_from_earth'] = str(distance_from_earth.au) result['distance_from_sun'] = str(distance_from_sun.au) result['magnitude_h'] = row['magnitude_H'] result['magnitude_g'] = row['magnitude_G'] result['visual_magnitude'] = visual_magnitude result['last_observation_date'] = row['last_observation_date'] # result['row'] = row return result,asteroid
def minorPlanet(self, designation: str, symbol: Optional[str] = None) -> CelestialObject: """Look up a minor planet by its designation, e.g. (2060) Chiron""" data = self._minorPlanets.loc[designation] shortName = designation[designation.find(") ") + 2:] return makeObject( type=ObjectType.MINOR_PLANET, name=shortName, position=self.sun.position + mpc.mpcorb_orbit(data, self.timescale, GM_SUN), dataFrame=data, symbol=symbol, )
def test_minor_planet(): text = (b'00001 3.4 0.15 K205V 162.68631 73.73161 80.28698' b' 10.58862 0.0775571 0.21406009 2.7676569 0 MPO492748' b' 6751 115 1801-2019 0.60 M-v 30h Williams 0000 ' b'(1) Ceres 20190915\n') ts = load.timescale() t = ts.utc(2020, 6, 17) eph = load('de421.bsp') df = mpc.load_mpcorb_dataframe(BytesIO(text)) row = df.iloc[0] assert row.designation_packed == '00001' assert row.designation == '(1) Ceres' ceres = mpc.mpcorb_orbit(row, ts, GM_SUN) ra, dec, distance = eph['earth'].at(t).observe(eph['sun'] + ceres).radec() assert ceres.target == '(1) Ceres' assert abs(ra.hours - 23.1437) < 0.00005 assert abs(dec.degrees - -17.323) < 0.0005
def minor_planets(): """View minor_planets.""" search_form = SearchMinorPlanetForm() ret, page = process_paginated_session_search('minor_planet_search_page', [ ('minor_planet_search', search_form.q), ]) per_page = get_items_per_page(search_form.items_per_page) if not ret: return redirect(url_for('main_minor_planet.minor_planets')) offset = (page - 1) * per_page minor_planet_query = MinorPlanet.query if search_form.q.data: search_expr = search_form.q.data.replace('"', '') minor_planet_query = minor_planet_query.filter( MinorPlanet.designation.like('%' + search_expr + '%')) magnitudes = {} minor_planets_for_render = minor_planet_query.order_by( MinorPlanet.int_designation).limit(per_page).offset(offset).all() ts = load.timescale(builtin=True) eph = load('de421.bsp') sun, earth = eph['sun'], eph['earth'] t = ts.now() ra, dec, earth_sun_distance = earth.at(t).observe(sun).apparent().radec() mpc_minor_planets = _get_mpc_minor_planets() for minor_planet in minor_planets_for_render: mpc_minor_planet = mpc_minor_planets.iloc[minor_planet.int_designation] body = sun + mpc.mpcorb_orbit(mpc_minor_planet, ts, GM_SUN) ra, dec, sun_body_distance = sun.at(t).observe(body).radec() ra, dec, earth_body_distance = earth.at(t).observe( body).apparent().radec() apparent_magnitude = _get_apparent_magnitude_hg( minor_planet.magnitude_H, minor_planet.magnitude_G, earth_body_distance.au, sun_body_distance.au, earth_sun_distance.au) if apparent_magnitude: magnitudes[minor_planet.int_designation] = '{:.2f}'.format( apparent_magnitude) pagination = Pagination(page=page, per_page=per_page, total=minor_planet_query.count(), search=False, record_name='minor_planets', css_framework='semantic', not_passed_args='back') return render_template('main/solarsystem/minor_planets.html', minor_planets=minor_planets_for_render, pagination=pagination, search_form=search_form, magnitudes=magnitudes)
def minor_planet_info(minor_planet_id): """View a minor_planet info.""" minor_planet = MinorPlanet.query.filter_by(id=minor_planet_id).first() if minor_planet is None: abort(404) form = MinorPlanetFindChartForm() ts = load.timescale(builtin=True) eph = load('de421.bsp') sun, earth = eph['sun'], eph['earth'] mpc_minor_planet = _get_mpc_minor_planets().iloc[ minor_planet.int_designation - 1] c = sun + mpc.mpcorb_orbit(mpc_minor_planet, ts, GM_SUN) if not form.date_from.data or not form.date_to.data: today = datetime.today() form.date_from.data = today form.date_to.data = today + timedelta(days=7) if (form.date_from.data is None) or (form.date_to.data is None) or form.date_from.data >= form.date_to.data: t = ts.now() minor_planet_ra_ang, minor_planet_dec_ang, distance = earth.at( t).observe(c).radec() trajectory_b64 = None else: d1 = date(form.date_from.data.year, form.date_from.data.month, form.date_from.data.day) d2 = date(form.date_to.data.year, form.date_to.data.month, form.date_to.data.day) t = ts.now() minor_planet_ra_ang, minor_planet_dec_ang, distance = earth.at( t).observe(c).radec() if d1 != d2: time_delta = d2 - d1 if time_delta.days > 365: d2 = d1 + timedelta(days=365) dt = get_trajectory_time_delta(d1, d2) trajectory = [] while d1 <= d2: t = ts.utc(d1.year, d1.month, d1.day) ra, dec, distance = earth.at(t).observe(c).radec() trajectory.append( (ra.radians, dec.radians, d1.strftime('%d.%m.'))) if d1 == d2: break d1 += dt # timedelta(days=1) if d1 > d2: d1 = d2 t = ts.utc(d1.year, d1.month, d1.day) trajectory_json = json.dumps(trajectory) trajectory_b64 = base64.b64encode(trajectory_json.encode('utf-8')) else: trajectory_b64 = None minor_planet_ra = minor_planet_ra_ang.radians minor_planet_dec = minor_planet_dec_ang.radians if not common_ra_dec_fsz_from_request(form): form.ra.data = minor_planet_ra form.dec.data = minor_planet_dec chart_control = common_prepare_chart_data(form) return render_template('main/solarsystem/minor_planet_info.html', fchart_form=form, type='info', minor_planet=minor_planet, minor_planet_ra=minor_planet_ra, minor_planet_dec=minor_planet_dec, chart_control=chart_control, trajectory=trajectory_b64)