<jupyter_start><jupyter_text>Planning observations with `astroplan` - Constraints<jupyter_code>import numpy as np
import pandas as pd

import pytz

import astropy.units as u
from astropy.time import Time
from astropy.coordinates import SkyCoord, EarthLocation
from astroplan import Observer, FixedTarget

import warnings
warnings.filterwarnings('ignore', category=Warning)<jupyter_output><empty_output><jupyter_text>--- List ComprehensionsList comprehensions provide a concise way to create lists (arrays). Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence. For example: Create the list: `[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]`<jupyter_code>squares = []                    # create a blank list

for x in range(10):             # foor loop 0 -> 9
    squares.append(x**2)        # calculate x**2 for each x, add to end of list

squares<jupyter_output><empty_output><jupyter_text>You can do the same thing with:<jupyter_code>squares = [x**2 for x in range(10)]

squares<jupyter_output><empty_output><jupyter_text>You can include `if` statements:<jupyter_code>even_squares = []

for x in range(10):
    
    if (x % 2 == 0):
        even_squares.append(x**2)

even_squares<jupyter_output><empty_output><jupyter_text>You can do the same thing with:<jupyter_code>even_squares = [x**2 for x in range(10) if (x % 2 == 0)]

even_squares<jupyter_output><empty_output><jupyter_text>--- Now to observations  Let us start with a external list of target objects:<jupyter_code>target_table = pd.read_csv('./Data/ObjectList.csv')
target_table[0:3]<jupyter_output><empty_output><jupyter_text>Shortcut to pull all values from a `pandas` table:<jupyter_code>table_values = target_table.as_matrix()
table_values[0:3]
targets = [FixedTarget(coord=SkyCoord(ra = RA*u.hourangle, dec = DEC*u.deg), name=Name)
           for Name, RA, DEC in table_values]
targets[0:3]<jupyter_output><empty_output><jupyter_text>Observing Night You are the most junior member of the team, so you get stuck observing on Thanksgiving Observing Window: * Start: `Nov 22, 2018 12:00:00 UTC` * End: `Nov 23, 2018 12:00:00 UTC`<jupyter_code>window_start = Time("2018-11-22 12:00:00")
window_end = Time("2018-11-23 12:00:00")<jupyter_output><empty_output><jupyter_text>But, you get a trip to the [Canary Islands](https://en.wikipedia.org/wiki/Roque_de_los_Muchachos_Observatory)<jupyter_code>#Gran Telescopio Canarias
#Observatorio Roque de los Muchachos

my_location = Observer(longitude = -17.892059 * u.deg,
                       latitude = 28.756581 * u.deg,
                       elevation = 2396 * u.m,
                       timezone = 'UTC',
                       name = "Gran Telescopio Canarias"
                      )<jupyter_output><empty_output><jupyter_text>What time does the window open locally?<jupyter_code>print(window_start.to_datetime(my_location.timezone))<jupyter_output><empty_output><jupyter_text>It is 12 pm local, so my observations start at the `nearest` astromonical twilight.<jupyter_code>observe_start = my_location.twilight_evening_astronomical(window_start, which='nearest')
observe_end = my_location.twilight_morning_astronomical(window_start, which='next')
observing_length = (observe_end - observe_start)

print("You can observe for {0:.1f} tonight".format(observing_length.to(u.h)))<jupyter_output><empty_output><jupyter_text>Plot the objects - Over the observing window<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt

from astroplan import time_grid_from_range
from astroplan.plots import plot_sky, plot_airmass
window_range = [window_start, window_end]
window_time_grid = time_grid_from_range(window_range)
fig,ax = plt.subplots(1,1)
fig.set_size_inches(10,5)

fig.tight_layout()

for my_object in targets:
    ax = plot_airmass(my_object, my_location, window_time_grid)

ax.vlines(observe_start.datetime, 1,3, color='r', linewidth=5)
ax.vlines(observe_end.datetime, 1,3, color='r', linewidth=5)

ax.legend(loc=0,shadow=True);<jupyter_output><empty_output><jupyter_text>Observing Constraints<jupyter_code>from astroplan import AltitudeConstraint, AirmassConstraint, AtNightConstraint
from astroplan import observability_table
observing_range = [observe_start, observe_end]
constraints = [AirmassConstraint(2)]
observing_table = observability_table(constraints, my_location, targets, time_range=observing_range)

print(observing_table)<jupyter_output><empty_output><jupyter_text>Let us add another constraint<jupyter_code>constraints.append(AltitudeConstraint(min=None, max=50*u.deg))
observing_table = observability_table(constraints, my_location, targets, time_range=observing_range)

print(observing_table)<jupyter_output><empty_output><jupyter_text>Additional Constraints`from astroplan import CONSTRAINT`* `AtNightConstraint()` - Constrain the Sun to be below horizon.* `MoonIlluminationConstraint(min, max)` - Constrain the fractional illumination of the Moon.* `MoonSeparationConstraint(min, max)` - Constrain the separation between the Moon and some targets.* `SunSeparationConstraint(min, max)` - Constrain the separation between the Sun and some targets.<jupyter_code>from astroplan import moon_illumination
moon_illumination(observe_start)
from astroplan import MoonSeparationConstraint
constraints.append(MoonSeparationConstraint(50*u.deg))
observing_table = observability_table(constraints, my_location, targets, time_range=observing_range)

print(observing_table)
fig,ax = plt.subplots(1,1)
fig.set_size_inches(10,5)

fig.tight_layout()

for i, my_object in enumerate(targets):

    if observing_table['ever observable'][i]:
        ax = plot_airmass(my_object, my_location, window_time_grid, max_airmass=2.0)

ax.vlines(observe_start.datetime, 1,3, color='r', linewidth=5)
ax.vlines(observe_end.datetime, 1,3, color='r', linewidth=5)

ax.legend(loc=0,shadow=True);<jupyter_output><empty_output><jupyter_text>Print out observing lengths for each object<jupyter_code>for i, my_object in enumerate(targets):

    if observing_table['ever observable'][i]:
        name = observing_table['target name'][i]
        obj_frac = observing_table['fraction of time observable'][i]
        obj_time = obj_frac * observing_length
        output = "You can observe {0:s} for {1:.2f}".format(name, obj_time.to(u.h))
        print(output)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10322073841319718, 'fraction_numerical': 0.023723487824037708, 'mean_word_length': 6.081201334816463, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 66, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '292635', 'license': 'MIT', 'n_tokens_mistral': 2275, 'n_tokens_neox': 2111, 'n_words': 485, 'path': 'Python_Astroplan_Constraints.ipynb', 'repo_name': 'UWashington-Astro300/Astro300-A18', 'size': '6365'}