In [2]:
import ephem
from IPython.display import HTML

In [3]:
# Set observation location - select key from list below
location = 'hilo'

# Add more locations as needed
observer_locations = {
    'hilo': {
        'lon': '-155:05:23:99',
        'lat': '19:42:00:00',
        'elevation': 88 , # meters
        'temp': 23, # celsius
        'pressure': 1010 # This is default but also a guess
     },
    'vis': {
        'lon': '-155:27:52:35', 
        'lat': '19:45:42:96',
        'elevation': 2848.5, # meters
        'temp': 15, # celsius - this is a guess
        'pressure': 700 #this is a guess            
    },
}

In [10]:
# Building an Observer to watch the moon. Shouldn't need to change anything here. Move along.

# Phases to watch
min_phase = 0.08
max_phase = 0.58

# Time to watch (UTC)
min_hour = 5   # 7pm HST
max_hour = 15  # 5am HST
midnight = 10

# Get the settings for our location
location_settings = observer_locations[location]

# Setup our observer from our given location
observer = ephem.Observer()

observer.lat = location_settings['lat']
observer.lon = location_settings['lon']
observer.elevation = location_settings['elevation']
observer.temp = location_settings['temp']
observer.pressure = location_settings['pressure']


print(observer.next_rising(ephem.Moon()))
print(observer.next_setting(ephem.Moon()))


2013/9/28 10:47:05
2013/9/27 23:17:01

In [6]:
# Dates to check, starting from today
observer.date = ephem.Date('2013/09/27 10:00:00')
stop_date = ephem.Date('2014/01/01')

# Create our Moon and look at it from our observer location
moon = ephem.Moon(observer)

# Get next rise time
next_rise_time = observer.next_rising(ephem.Moon())
next_set_time = observer.next_setting(ephem.Moon())

while next_rise_time < stop_date:
    # Get info about moon for the night
    phase = moon.moon_phase
    hour = next_rise_time.tuple()[3]
        
    # Check for good conditions
    #if (phase > min_phase) & (phase < max_phase) & (hour > min_hour) & (hour < max_hour):
        # Get local time
    #local_rise_time = ephem.localtime(next_rise_time).strftime('%a %m/%d %H:%M')
    local_rise_time = ephem.localtime(next_rise_time).strftime('%H:%M')
    local_set_time = ephem.localtime(next_set_time).strftime('%H:%M')
    print(local_rise_time, local_set_time)
    
    # Get next rising time
    observer.date += 1
    
    next_rise_time = observer.next_rising(ephem.Moon()) 
    next_set_time = observer.next_setting(ephem.Moon()) 
    
    moon.compute(observer)


00:47 13:17
00:47 14:00
01:37 14:41
02:27 15:21
03:18 16:00
04:09 16:39
05:02 17:19
05:56 18:00
06:52 18:45
07:50 19:32
08:49 20:23
09:49 21:18
10:49 22:16
11:46 23:16
12:41 00:17
13:32 00:17
14:20 01:17
15:05 02:16
15:48 03:14
16:30 04:11
17:12 05:07
17:55 06:02
18:39 06:57
19:25 07:51
20:12 08:44
21:00 09:35
21:49 10:24
22:38 11:10
23:28 11:54
00:18 12:36
00:18 13:16
01:07 13:54
01:58 14:33
02:49 15:12
03:42 15:53
04:38 16:36
05:35 17:23
06:35 18:13
07:37 19:09
08:39 20:07
09:39 21:09
10:36 22:11
11:29 23:12
12:18 00:11
13:04 00:11
13:47 01:09
14:29 02:05
15:10 03:00
15:51 03:54
16:34 04:48
17:19 05:42
18:05 06:35
18:53 07:27
19:41 08:17
20:31 09:04
21:21 09:49
22:10 10:32
22:59 11:12
23:49 11:51
00:38 12:28
00:38 13:06
01:29 13:45
02:22 14:26
03:18 15:10
04:16 15:59
05:17 16:52
06:20 17:50
07:23 18:52
08:24 19:57
09:21 21:01
10:14 22:03
11:02 23:03
11:47 00:01
12:29 00:01
13:10 00:56
13:51 01:51
14:33 02:44
15:16 03:37
16:01 04:30
16:48 05:21
17:36 06:12
18:25 07:00
19:15 07:46
20:05 08:29
20:54 09:10
21:43 09:50
22:32 10:27
23:21 11:04
00:12 11:42
00:12 12:21
01:04 13:02
01:59 13:46
02:57 14:35
03:58 15:30
05:01 16:30
06:03 17:34

In [4]:
# Here we are getting a list of all the known magnitude stars and then creating
# a function which will return the nearest star to the moon for our given observer.

from ephem import stars

# First we go through the stars and compute their position for our observer
# There are not that many stars in the list (122) so this is not a big deal
for name, star in stars.stars.items():
    star.compute(observer)

def find_nearest_star(obj):
    """
        Finds the star that appears closest to the moon for the observer. The star list
        comes from ephems list of stars of known magnitudes.
    """
    # Get the dict of all separations for object
    star_sep = {name:ephem.separation(obj,star) for name,star in stars.stars.items()}
    
    # Get the star with minimum separation
    min_star_name = min(star_sep, key = lambda k: star_sep[k])
    return stars.star(min_star_name)

In [8]:
# Create our Moon and look at it from our observer location
moon = ephem.Moon(observer)

observer.date = ephem.now()

show_break = False

header = "|{4:s}\n| {0:s}| {1:s}| {2:s}| {3:}\n|{4:s}".format('Date'.ljust(18),'Phase'.ljust(10),'Moon'.ljust(30),'Mag Star','-' * 80)
print(header)
while observer.date.triple()[0] < 2014:
    p = moon.moon_phase
    
    hour = observer.date.tuple()[3]
    
    near_star = find_nearest_star(moon)
    display_date = ephem.localtime(observer.date).strftime('%a %m/%d %H:%M')
    
    
    if (p > 0.08) & (p < 0.58) & (moon.alt > ephem.degrees('20:00:00')) & (hour > 5) &(hour < 15):
        show_break = True
        print("| {}".format(display_date.ljust(18)), end='')
        print("| {:.2f}\t".format(p), end='')
        print("| {} x {}\t".format(moon.az,moon.alt), end='')
        print("| {} {}".format(near_star.name,ephem.separation(near_star,moon)))
    else:
        if show_break:
            print("{}\n".format('-'*71))
            print(header)
        show_break = False
        
    observer.date += ephem.hour
    moon.compute(observer)


|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Thu 09/12 21:52   | 0.57	| 232:19:24.7 x 28:46:49.9	| Nunki 16:00:33.4
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Thu 09/26 00:52   | 0.57	| 76:25:12.7 x 21:57:28.9	| Elnath 10:11:41.9
| Thu 09/26 01:52   | 0.56	| 79:45:17.6 x 35:24:42.1	| Elnath 10:19:03.7
| Thu 09/26 02:52   | 0.56	| 82:45:24.7 x 49:03:07.3	| Elnath 10:26:29.9
| Thu 09/26 03:52   | 0.56	| 85:36:41.0 x 62:49:09.5	| Elnath 10:34:10.9
| Thu 09/26 04:52   | 0.55	| 88:43:00.5 x 76:39:58.4	| Elnath 10:42:24.2
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Fri 09/27 01:52   | 0.47	| 78:02:31.9 x 24:30:40.2	| Alhena 2:07:05.1
| Fri 09/27 02:52   | 0.47	| 81:29:47.8 x 38:03:07.1	| Alhena 2:07:31.7
| Fri 09/27 03:52   | 0.46	| 84:49:35.1 x 51:45:45.4	| Alhena 2:10:51.8
| Fri 09/27 04:52   | 0.46	| 88:31:11.6 x 65:35:02.3	| Alhena 2:15:37.2
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Sat 09/28 02:52   | 0.37	| 80:42:52.7 x 27:04:33.4	| Procyon 11:53:54.7
| Sat 09/28 03:52   | 0.37	| 84:31:13.1 x 40:43:50.2	| Procyon 11:47:02.2
| Sat 09/28 04:52   | 0.37	| 88:39:25.6 x 54:31:17.8	| Procyon 11:40:39.0
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Sun 09/29 03:52   | 0.28	| 84:25:53.3 x 29:38:53.3	| Procyon 13:14:14.0
| Sun 09/29 04:52   | 0.28	| 88:53:24.4 x 43:24:00.2	| Procyon 13:27:39.0
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Mon 09/30 04:52   | 0.19	| 89:09:47.4 x 32:08:52.7	| Regulus 14:44:43.7
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Tue 10/01 04:52   | 0.12	| 89:27:19.6 x 20:42:34.9	| Regulus 5:28:38.2
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Thu 10/10 20:52   | 0.42	| 234:15:01.6 x 28:11:07.5	| Nunki 8:38:00.5
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Fri 10/11 20:52   | 0.54	| 225:50:04.8 x 39:34:47.1	| Nunki 13:02:32.3
| Fri 10/11 21:52   | 0.54	| 236:48:52.4 x 28:54:51.4	| Nunki 13:25:59.4
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Sat 10/26 01:52   | 0.55	| 83:43:26.6 x 31:29:36.2	| Procyon 11:05:10.8
| Sat 10/26 02:52   | 0.54	| 87:58:18.6 x 45:14:38.8	| Procyon 11:11:18.3
| Sat 10/26 03:52   | 0.54	| 93:15:37.7 x 59:04:18.2	| Procyon 11:16:18.2
| Sat 10/26 04:52   | 0.54	| 102:49:25.5 x 72:48:00.3	| Procyon 11:20:12.3
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Sun 10/27 01:52   | 0.45	| 83:37:20.9 x 20:27:58.4	| Procyon 17:57:11.2
| Sun 10/27 02:52   | 0.45	| 88:09:56.8 x 34:10:09.2	| Procyon 18:16:54.4
| Sun 10/27 03:52   | 0.45	| 93:26:00.2 x 47:57:36.3	| Procyon 18:33:57.8
| Sun 10/27 04:52   | 0.44	| 101:05:53.1 x 61:40:50.0	| Procyon 18:48:46.5
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Mon 10/28 02:52   | 0.35	| 88:24:20.0 x 22:58:16.4	| Regulus 8:45:32.0
| Mon 10/28 03:52   | 0.35	| 93:38:53.3 x 36:42:41.8	| Regulus 8:27:01.3
| Mon 10/28 04:52   | 0.35	| 100:24:58.5 x 50:23:57.5	| Regulus 8:11:38.3
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Tue 10/29 03:52   | 0.26	| 93:52:24.1 x 25:15:08.6	| Regulus 7:56:16.2
| Tue 10/29 04:52   | 0.26	| 100:03:58.1 x 38:53:23.9	| Regulus 8:15:13.0
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Wed 10/30 04:52   | 0.17	| 99:50:28.2 x 27:05:37.8	| Denebola 16:27:59.2
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Fri 11/08 20:52   | 0.39	| 240:14:50.2 x 28:27:43.0	| Alshain 21:41:25.2
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Sat 11/09 20:52   | 0.51	| 234:55:39.7 x 41:01:02.2	| Sadalmelik 16:34:26.4
| Sat 11/09 21:52   | 0.51	| 244:57:44.2 x 29:11:17.9	| Sadalmelik 16:08:26.0
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Mon 11/25 01:52   | 0.53	| 92:35:25.0 x 27:23:19.6	| Regulus 5:19:16.4
| Mon 11/25 02:52   | 0.53	| 98:38:51.7 x 41:05:03.9	| Regulus 5:23:11.5
| Mon 11/25 03:52   | 0.52	| 107:16:55.4 x 54:32:17.7	| Regulus 5:29:14.4
| Mon 11/25 04:52   | 0.52	| 123:13:11.2 x 67:05:09.0	| Regulus 5:36:55.4
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Tue 11/26 02:52   | 0.43	| 98:33:37.8 x 29:40:40.9	| Regulus 14:11:46.2
| Tue 11/26 03:52   | 0.43	| 105:53:40.6 x 43:07:17.7	| Regulus 14:34:03.1
| Tue 11/26 04:52   | 0.42	| 117:05:59.0 x 55:57:41.6	| Regulus 14:54:40.0
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Wed 11/27 03:52   | 0.33	| 104:56:34.8 x 31:24:21.7	| Denebola 16:26:29.3
| Wed 11/27 04:52   | 0.33	| 113:42:13.7 x 44:22:22.0	| Denebola 16:35:07.8
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Thu 11/28 04:52   | 0.23	| 111:21:51.6 x 32:21:34.2	| Gienah Corvi 11:59:08.6
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Sat 12/07 20:52   | 0.35	| 249:28:29.0 x 27:54:56.6	| Sadalmelik 8:01:15.3
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Sun 12/08 20:52   | 0.46	| 247:10:42.5 x 40:54:35.0	| Sadalmelik 10:48:58.4
| Sun 12/08 21:52   | 0.47	| 255:24:06.3 x 27:56:43.1	| Sadalmelik 11:08:50.5
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Mon 12/09 20:52   | 0.57	| 243:53:19.7 x 53:31:44.2	| Algenib 16:11:04.4
| Mon 12/09 21:52   | 0.58	| 254:25:34.3 x 40:40:38.9	| Algenib 15:50:57.2
| Mon 12/09 22:52   | 0.58	| 261:32:03.1 x 27:16:13.6	| Algenib 15:29:51.6
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Tue 12/24 04:52   | 0.60	| 152:47:35.2 x 67:37:40.3	| Denebola 16:02:24.4
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Wed 12/25 01:52   | 0.51	| 102:57:00.2 x 21:51:08.1	| Gienah Corvi 13:40:32.1
| Wed 12/25 02:52   | 0.51	| 110:15:09.6 x 34:59:18.8	| Gienah Corvi 13:26:50.7
| Wed 12/25 03:52   | 0.50	| 120:40:50.5 x 47:24:18.9	| Gienah Corvi 13:13:59.8
| Wed 12/25 04:52   | 0.50	| 137:28:36.7 x 58:07:22.2	| Gienah Corvi 13:01:52.2
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Thu 12/26 02:52   | 0.40	| 108:39:18.7 x 23:05:58.5	| Spica 6:56:30.4
| Thu 12/26 03:52   | 0.40	| 116:50:00.0 x 35:43:25.6	| Spica 6:30:41.2
| Thu 12/26 04:52   | 0.39	| 128:40:00.7 x 47:16:19.5	| Spica 6:07:18.8
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Fri 12/27 03:52   | 0.30	| 113:49:58.6 x 23:35:01.5	| Spica 6:56:27.5
| Fri 12/27 04:52   | 0.29	| 122:41:47.6 x 35:37:08.7	| Spica 7:21:52.4
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------
| Sat 12/28 04:52   | 0.20	| 118:02:21.5 x 23:21:25.7	| Spica 21:11:19.6
-----------------------------------------------------------------------

|--------------------------------------------------------------------------------
| Date              | Phase     | Moon                          | Mag Star
|--------------------------------------------------------------------------------

In [8]:
def get_good_obs_dates(start_date=ephem.now(),end_date=ephem.Date('2014-01-01')):
    """ 
        Given a start_date and an end_date, loop
        through and find every (night) hour when
        moon is above 20°. Defaults to remainder
        of year.
    """