예제 #1
0
def get_next_setting(location: ephem.Observer,
                     sun: ephem._sun,
                     transit_datetime: Datetime,
                     use_center: bool = False) -> Optional[Datetime]:
    try:
        d = ephem.localtime(location.next_setting(
            sun, use_center=use_center)).astimezone(tz=None)
        return remove_odd(transit_datetime, d)
    except (ephem.NeverUpError, ephem.AlwaysUpError):
        return None
예제 #2
0
def rise_and_set(lat, lon, horizon=0):
    sun = Sun()
    date = None
    while True:
        loc = Observer()
        loc.horizon = str(horizon)
        loc.lat = str(lat)
        loc.lon = str(lon)
        loc.date = clock.now()
        #if date:
        #    loc.date = date
        #loc.date = loc.date.datetime().date() # strip time
        t_rise = loc.next_rising(sun)
        t_set = loc.next_setting(sun)
        #date = yield localtime(t_rise), localtime(t_set)
        yield localtime(t_rise), localtime(t_set)
예제 #3
0
def main():
    # arguments
    from argparse import ArgumentParser
    args = ArgumentParser(
        epilog=
        """This script will compute the local coordinates (altitude and azimuth) for Jupiter
and Saturn for the given date. Altitude is degrees above the horizon and azimuth is degrees eastwards from North. Locate North
by finding the Polaris, the pole star.""")
    args.add_argument(
        "-x",
        "--longitude",
        type=float,
        default=-74.151494,
        help=
        "East longitude of the observer in decimal degrees. West is negative.")
    args.add_argument(
        "-y",
        "--latitude",
        type=float,
        default=40.373545,
        help="North latitude of the observer. South is negative.")
    args.add_argument("-t",
                      "--timezone",
                      type=str,
                      default='US/Eastern',
                      help="The local time-zone of the observer.")
    args.add_argument("-e",
                      "--elevation",
                      type=float,
                      default=20e0,
                      help="Elevation in metres above sea level.")
    args.add_argument(
        "time",
        nargs='?',
        default=datetime.now().strftime("%H:%M:%S"),
        help="Local time for calculation, default is current time, as HH:MM:SS."
    )
    args.add_argument(
        "date",
        nargs='?',
        default=datetime.now().strftime("%Y-%m-%d"),
        help=
        "Local date for calculation, default is current date, as YYYY-MM-DD.")
    args = args.parse_args()

    # time
    localtime = timezone(args.timezone)
    utc = timezone('UTC')
    timestamp = localtime.localize(
        datetime.strptime(args.date + ' ' + args.time, '%Y-%m-%d %H:%M:%S'))
    observer = Observer()
    observer.lat = args.latitude * pi / 180.0
    observer.lon = args.longitude * pi / 180.0
    observer.date = timestamp.astimezone(utc)
    observer.elevation = args.elevation

    print(
        "Calculation of the location of Jupiter and Saturn for an observer at %.4f° %s, %.4f° %s, %.4f m above sea level.\n"
        % (abs(args.longitude), "E" if args.longitude > 0 else "W",
           abs(args.latitude), "N" if args.latitude > 0 else "S",
           args.elevation))

    print("Computed for local time %s." %
          timestamp.astimezone(localtime).strftime('%Y-%m-%d %H:%M:%S'))

    # Sun
    sun = Sun(observer)
    sunlight = max(0e0, cos(pi / 2 - sun.alt)) * 1e2

    if sunlight > 0:
        print(
            "The Sun is currently above the horizon, with light at %.2f %%, at %.2f° %s."
            % (sunlight, (sun.az - pi if sun.az > pi else sun.az) * 180e0 / pi,
               "E" if sun.az < pi else "W" if sun.az > pi else "S"))

        sunset = utc.localize(
            datetime.strptime(str(
                observer.next_setting(sun)), "%Y/%m/%d %H:%M:%S")).astimezone(
                    localtime) if observer.next_setting(sun) != None else None

        if sunset != None:
            print("The Sun will set at %s." % sunset.strftime("%H:%M:%S"))

    else:
        print("The Sun has set.")

        sunrise = utc.localize(
            datetime.strptime(str(
                observer.next_rising(sun)), "%Y/%m/%d %H:%M:%S")).astimezone(
                    localtime) if observer.next_rising(sun) != None else None
        print("The Sun will rise at %s." % sunrise.strftime("%H:%M:%S"))

    # Moon
    moon = Moon(observer)
    moonlight = max(0e0, cos(pi / 2 - moon.alt)) * 1e2

    if moonlight > 0:
        print(
            "The Moon is currently above the horizon, with light at %.2f %%." %
            moonlight)

    # Jupiter
    jupiter = Jupiter(observer)

    if jupiter.alt > 0e0:
        print(
            "Jupiter is %.2f° above the horizon, %.2f° %s." %
            (jupiter.alt * 180e0 / pi,
             (2 * pi - jupiter.az if jupiter.az > pi else jupiter.az) * 180e0 /
             pi, "W" if jupiter.az > pi else "E" if jupiter.az < pi else "S"))

    else:
        print("Jupiter is not above the horizon.")

    # Jupiter
    saturn = Saturn(observer)

    if saturn.alt > 0e0:
        print("Saturn is %.2f° above the horizon, %.2f° %s." %
              (saturn.alt * 180e0 / pi,
               (2 * pi - saturn.az if saturn.az > pi else saturn.az) * 180e0 /
               pi, "W" if saturn.az > pi else "E" if saturn.az < pi else "S"))

    else:
        print("Saturn is not above the horizon.")

    # done
    print("Done.")
예제 #4
0
def whats_up(
        start: datetime.datetime,
        end: datetime.datetime,
        location: ephem.Observer,
        magnitude: float = 6.) -> dict:
    """
    Find all objects that will be "up" between start and end time.

    Takes a location, start and end time, and limiting magnitude.
    """
    body_list = []
    start_e, end_e = ephem.Date(start), ephem.Date(end)
    location.date = start_e
    for o,body_type in filter(lambda x: x[1] != 'planetary_moon', OBJECT_DICT.values()):
        o.compute(location)
        circumpolar = False
        rising, setting = None, None
        if body_type != 'satellite':
            try:
                rising = location.next_rising(o, start = location.previous_antitransit(o))
                setting = location.next_setting(o, start = rising)
            except ephem.AlwaysUpError:
                circumpolar = True
            except ephem.NeverUpError:
                pass
        elif body_type == 'satellite':
            info = list(location.next_pass(o))
            rising, setting = info[0], info[4]
        # the logic here is as follows:
        # 1. Is object bright enough?
        # 2. Is it in the sky now?
        #    a. Is it circumpolar (does it never set)?
        #    b. Does it rise during the given timeframe?
        #    c. Does it set during the given timeframe?
        #    d. Does it rise before the given timeframe and set after the given timeframe?
        if o.mag < -30:
            # something's wrong with the data--skip it.
            continue
        if o.mag < magnitude and (circumpolar or \
                                  (rising and start_e < rising < end_e) or \
                                  (setting and start_e < setting < end_e) or \
                                  (rising and setting and (rising < start_e) and \
                                      (setting > end_e))):
            # If it's in the sky and bright enough, add the entry to the list
            if rising or setting or circumpolar:
                body_list.append({
                    'name': o.name.split('|') if o.name.find('|') >= 0 else o.name,
                    'magnitude': o.mag
                })
            # If it has a rise time, add that to the most recently added item
            if rising:
                body_list[-1]['rise_time'] = rising.datetime()
            # If it has a set time, add that to the most recently added item
            if setting:
                body_list[-1]['set_time'] = setting.datetime()
            if o.name == 'Moon':
                body_list[-1]['phase'] = o.moon_phase * 100
    body_list.sort(key = lambda x: x['magnitude'])
    return {
        'start_time': start, # start of timeframe
        'end_time': end, # end of timeframe
        'objects': body_list # all items visible during timeframe, as computed above
    }