Skip to content

Commit

Permalink
fix for K2 footprint issue; closes 35
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-rodriguez committed Sep 6, 2020
1 parent d669256 commit 4bc5bf0
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 57 deletions.
157 changes: 112 additions & 45 deletions Moving_Targets_Basic_Workflow.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
"times={'start': '2019-04-01', 'stop': '2019-04-10', 'step': '1d'} # WFC3\n",
"#times = {'start': '1997-07-10', 'stop': '1997-07-15', 'step': '12h'} # WFPC2\n",
"\n",
"# 1143 Odysseus\t2015-08-20 2015-09-01\n",
"obj_name = '1143'\n",
"id_type = 'smallbody'\n",
"times = {'start': '2015-08-20', 'stop': '2015-09-01', 'step': '1d'}\n",
"\n",
"location = None # Geocentric default"
]
},
Expand Down Expand Up @@ -162,9 +167,10 @@
"outputs": [],
"source": [
"from bokeh.plotting import figure, output_file, show, output_notebook\n",
"from bokeh.models import Arrow, VeeHead, Slider, HoverTool\n",
"from bokeh.palettes import Spectral4\n",
"from bokeh.layouts import column"
"from bokeh.models import Arrow, VeeHead, Slider, HoverTool, ColumnDataSource\n",
"from bokeh.palettes import Spectral7 as palette\n",
"from bokeh.layouts import column\n",
"import numpy as np"
]
},
{
Expand All @@ -173,59 +179,120 @@
"metadata": {},
"outputs": [],
"source": [
"mast_results = results.copy()\n",
"stcs = polygon\n",
"def _individual_plots(df):\n",
" # Generate individual patch information for each POLYGON of an observation, currently only for Kepler and K2\n",
"\n",
" plot_data = []\n",
" for i, row in df.iterrows():\n",
" stcs_list = [f'POLYGON {s.strip()}' for s in row['s_region'].split('POLYGON') if s != '']\n",
" for stcs in stcs_list:\n",
" if 'CIRCLE' in stcs:\n",
" # Sometimes circles are in the data and get strings with 'POLYGON CIRCLE'\n",
" stcs = stcs.replace('POLYGON ', '') \n",
"\n",
" # Add patches with the observation footprings\n",
" coords = parse_s_region(stcs)\n",
" patch_xs = [coords['ra']]\n",
" patch_ys = [coords['dec']]\n",
"\n",
" data = ColumnDataSource({'x': patch_xs, 'y': patch_ys, \n",
" 'obs_collection': [row['obs_collection']],\n",
" 'instrument_name': [row['instrument_name']], \n",
" 'obs_id': [row['obs_id']],\n",
" 'target_name': [row['target_name']], \n",
" 'proposal_pi': [row['proposal_pi']],\n",
" 'obs_mid_date': [row['obs_mid_date']], \n",
" 'filters': [row['filters']]})\n",
" plot_data.append(data)\n",
"\n",
" return plot_data\n",
"\n",
"p = figure(plot_width=700, x_axis_label=\"RA (deg)\", y_axis_label=\"Dec (deg)\")\n",
"\n",
"# Target path\n",
"eph_data = {'eph_x': eph['RA'], 'eph_y': eph['DEC'], 'Date': eph['datetime_str']}\n",
"eph_plot1 = p.line(x='eph_x', y='eph_y', source=eph_data, line_width=2, line_color='black', legend=eph['targetname'][0])\n",
"eph_plot2 = p.circle(x='eph_x', y='eph_y', source=eph_data, fill_color=\"black\", size=12, legend=eph['targetname'][0])\n",
"p.add_tools(HoverTool(renderers=[eph_plot1, eph_plot2], tooltips=[('Date', \"@Date\")]))\n",
"def mast_bokeh(eph, mast_results, stcs=None, display=False):\n",
" # Function to produce a Bokeh plot of MAST results with the target path\n",
"\n",
"# Target footprint\n",
"patch_xs = parse_s_region(stcs)['ra']\n",
"patch_ys = parse_s_region(stcs)['dec']\n",
" p = figure(plot_width=700, x_axis_label=\"RA (deg)\", y_axis_label=\"Dec (deg)\")\n",
"\n",
"stcs_data = {'stcs_x': [patch_xs], 'stcs_y': [patch_ys]}\n",
"p.patches('stcs_x', 'stcs_y', source=stcs_data, fill_alpha=0., \n",
" line_color=\"grey\", line_width=0.8, line_dash='dashed', legend='Search Area')\n",
" # Target path\n",
" eph_data = {'eph_x': eph['RA'], 'eph_y': eph['DEC'], 'Date': eph['datetime_str']}\n",
" eph_plot1 = p.line(x='eph_x', y='eph_y', source=eph_data, line_width=2,\n",
" line_color='black', legend=eph['targetname'][0])\n",
" eph_plot2 = p.circle(x='eph_x', y='eph_y', source=eph_data, fill_color=\"black\",\n",
" size=12, legend=eph['targetname'][0])\n",
" p.add_tools(HoverTool(renderers=[eph_plot1, eph_plot2], tooltips=[('Date', \"@Date\")]))\n",
"\n",
"# Prepare MAST footprints\n",
"obsDF = mast_results.to_pandas()\n",
"obsDF['coords'] = obsDF.apply(lambda x: parse_s_region(x['s_region']), axis=1)\n",
"for col in mast_results.colnames:\n",
" if isinstance(obsDF[col][0], bytes):\n",
" obsDF[col] = obsDF[col].str.decode('utf-8')\n",
" # Target footprint\n",
" patch_xs = parse_s_region(stcs)['ra']\n",
" patch_ys = parse_s_region(stcs)['dec']\n",
"\n",
"# Loop over missions, coloring each separately\n",
"mast_plots = []\n",
"for mission, color in zip(obsDF['obs_collection'].unique(), Spectral4):\n",
" ind = obsDF['obs_collection'] == mission\n",
" stcs_data = {'stcs_x': [patch_xs], 'stcs_y': [patch_ys]}\n",
" p.patches('stcs_x', 'stcs_y', source=stcs_data, fill_alpha=0., line_color=\"grey\", line_width=0.8,\n",
" line_dash='dashed', legend='Search Area')\n",
"\n",
" # Add patches with the observation footprings\n",
" patch_xs = [c['ra'] for c in obsDF['coords'][ind]]\n",
" patch_ys = [c['dec'] for c in obsDF['coords'][ind]]\n",
" # Prepare MAST footprints\n",
" obsDF = mast_results.to_pandas()\n",
" obsDF['coords'] = obsDF.apply(lambda x: parse_s_region(x['s_region']), axis=1)\n",
" for col in mast_results.colnames:\n",
" if isinstance(obsDF[col][0], bytes):\n",
" obsDF[col] = obsDF[col].str.decode('utf-8')\n",
"\n",
" data = {'x': patch_xs, 'y': patch_ys, 'obs_collection': obsDF['obs_collection'][ind],\n",
" 'instrument_name': obsDF['instrument_name'][ind], 'obs_id': obsDF['obs_id'][ind],\n",
" 'target_name': obsDF['target_name'][ind], 'proposal_pi': obsDF['proposal_pi'][ind]}\n",
" mast_plots.append(p.patches('x', 'y', source=data, legend=mission,\n",
" fill_color=color, fill_alpha=0.1, line_color=\"white\", line_width=0.5))\n",
" # Loop over missions, coloring each separately\n",
" mast_plots = []\n",
" for mission, color in zip(obsDF['obs_collection'].unique(), palette):\n",
" ind = obsDF['obs_collection'] == mission\n",
"\n",
"# Add hover tooltip for MAST observations\n",
"tooltip = [(\"instrument_name\", \"@instrument_name\"),\n",
" (\"obs_id\", \"@obs_id\"),\n",
" (\"target_name\", \"@target_name\"),\n",
" ('proposal_pi', '@proposal_pi')]\n",
"p.add_tools(HoverTool(renderers=mast_plots, tooltips=tooltip))\n",
" # Some missions have very complex STCS and need to be treated separately\n",
" if mission in ('Kepler', 'K2', 'K2FFI'):\n",
" plot_data = _individual_plots(obsDF[ind])\n",
" for data in plot_data:\n",
" mast_plots.append(p.patches('x', 'y', source=data, legend=mission, \n",
" fill_color=color, fill_alpha=0.3, line_color=\"white\", line_width=0.5))\n",
" else:\n",
" # Add patches with the observation footprings\n",
" patch_xs = [c['ra'] for c in obsDF['coords'][ind]]\n",
" patch_ys = [c['dec'] for c in obsDF['coords'][ind]]\n",
"\n",
"p.x_range.flipped = True\n",
"p.legend.click_policy = \"hide\"\n",
" data = {'x': patch_xs, 'y': patch_ys, 'obs_collection': obsDF['obs_collection'][ind],\n",
" 'instrument_name': obsDF['instrument_name'][ind], 'obs_id': obsDF['obs_id'][ind],\n",
" 'target_name': obsDF['target_name'][ind], 'proposal_pi': obsDF['proposal_pi'][ind],\n",
" 'obs_mid_date': obsDF['obs_mid_date'][ind], 'filters': obsDF['filters'][ind]}\n",
" mast_plots.append(p.patches('x', 'y', source=data, legend=mission,\n",
" fill_color=color, fill_alpha=0.3, line_color=\"white\", line_width=0.5))\n",
"\n",
"output_notebook()\n",
"show(p)"
" # Add hover tooltip for MAST observations\n",
" tooltip = [(\"obs_id\", \"@obs_id\"),\n",
" (\"target_name\", \"@target_name\"),\n",
" (\"instrument_name\", \"@instrument_name\"),\n",
" (\"filters\", \"@filters\"),\n",
" ('obs_mid_date', '@obs_mid_date')]\n",
" p.add_tools(HoverTool(renderers=mast_plots, tooltips=tooltip))\n",
"\n",
" # Additional settings\n",
" p.legend.click_policy = \"hide\"\n",
" p.x_range.flipped = True\n",
"\n",
" # Slider for alpha settings\n",
" slider = Slider(start=0, end=1, step=0.01, value=0.3, title=\"Footprint opacity\")\n",
" for i in range(len(mast_plots)):\n",
" slider.js_link('value', mast_plots[i].glyph, 'fill_alpha')\n",
" final = column(p, slider)\n",
"\n",
" if display:\n",
" output_notebook()\n",
" show(final)\n",
" else:\n",
" return final"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"mast_bokeh(eph, results, stcs=polygon, display=True)"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion misc/scratch.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
times = {'start': '2019-04-01', 'stop': '2019-04-10', 'step': '1d'}
# times = {'start': '1997-07-10', 'stop': '1997-07-15', 'step': '12h'}

#1143 Odysseus 2015-08-20 2015-09-01
# 1143 Odysseus 2015-08-20 2015-09-01
obj_name = '1143'
id_type = 'smallbody'
times = {'start': '2015-08-20', 'stop': '2015-09-01', 'step': '1d'}
Expand Down
58 changes: 47 additions & 11 deletions movingmast/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .polygon import parse_s_region
from bokeh.plotting import figure, output_file, show, output_notebook
from bokeh.layouts import column
from bokeh.models import Arrow, VeeHead, HoverTool, Slider
from bokeh.models import Arrow, VeeHead, HoverTool, Slider, ColumnDataSource
from bokeh.palettes import Spectral7 as palette
import matplotlib.pyplot as plt

Expand Down Expand Up @@ -57,6 +57,35 @@ def quick_plot(stcs):
plt.show()


def _individual_plot_data(df):
# Generate individual patch information for each POLYGON of an observation, currently only for Kepler and K2

plot_data = []
for i, row in df.iterrows():
stcs_list = [f'POLYGON {s.strip()}' for s in row['s_region'].split('POLYGON') if s != '']
for stcs in stcs_list:
if 'CIRCLE' in stcs:
# Sometimes circles are in the data and get strings with 'POLYGON CIRCLE'
stcs = stcs.replace('POLYGON ', '')

# Add patches with the observation footprings
coords = parse_s_region(stcs)
patch_xs = [coords['ra']]
patch_ys = [coords['dec']]

data = ColumnDataSource({'x': patch_xs, 'y': patch_ys,
'obs_collection': [row['obs_collection']],
'instrument_name': [row['instrument_name']],
'obs_id': [row['obs_id']],
'target_name': [row['target_name']],
'proposal_pi': [row['proposal_pi']],
'obs_mid_date': [row['obs_mid_date']],
'filters': [row['filters']]})
plot_data.append(data)

return plot_data


def mast_bokeh(eph, mast_results, stcs=None, display=False):
# Function to produce a Bokeh plot of MAST results with the target path

Expand Down Expand Up @@ -90,16 +119,23 @@ def mast_bokeh(eph, mast_results, stcs=None, display=False):
for mission, color in zip(obsDF['obs_collection'].unique(), palette):
ind = obsDF['obs_collection'] == mission

# Add patches with the observation footprings
patch_xs = [c['ra'] for c in obsDF['coords'][ind]]
patch_ys = [c['dec'] for c in obsDF['coords'][ind]]

data = {'x': patch_xs, 'y': patch_ys, 'obs_collection': obsDF['obs_collection'][ind],
'instrument_name': obsDF['instrument_name'][ind], 'obs_id': obsDF['obs_id'][ind],
'target_name': obsDF['target_name'][ind], 'proposal_pi': obsDF['proposal_pi'][ind],
'obs_mid_date': obsDF['obs_mid_date'][ind], 'filters': obsDF['filters'][ind]}
mast_plots.append(p.patches('x', 'y', source=data, legend=mission,
fill_color=color, fill_alpha=0.3, line_color="white", line_width=0.5))
# Some missions have very complex STCS and need to be treated separately
if mission in ('Kepler', 'K2', 'K2FFI'):
plot_data = _individual_plot_data(obsDF[ind])
for data in plot_data:
mast_plots.append(p.patches('x', 'y', source=data, legend=mission,
fill_color=color, fill_alpha=0.3, line_color="white", line_width=0.5))
else:
# Add patches with the observation footprings
patch_xs = [c['ra'] for c in obsDF['coords'][ind]]
patch_ys = [c['dec'] for c in obsDF['coords'][ind]]

data = {'x': patch_xs, 'y': patch_ys, 'obs_collection': obsDF['obs_collection'][ind],
'instrument_name': obsDF['instrument_name'][ind], 'obs_id': obsDF['obs_id'][ind],
'target_name': obsDF['target_name'][ind], 'proposal_pi': obsDF['proposal_pi'][ind],
'obs_mid_date': obsDF['obs_mid_date'][ind], 'filters': obsDF['filters'][ind]}
mast_plots.append(p.patches('x', 'y', source=data, legend=mission,
fill_color=color, fill_alpha=0.3, line_color="white", line_width=0.5))

# Add hover tooltip for MAST observations
tooltip = [("obs_id", "@obs_id"),
Expand Down

0 comments on commit 4bc5bf0

Please sign in to comment.