Skip to content

Commit

Permalink
Merge pull request #178 from mwcraig/update-notebooks-2.0
Browse files Browse the repository at this point in the history
Update notebooks
  • Loading branch information
mwcraig authored Nov 26, 2023
2 parents b31f464 + 18e1a00 commit c8b99b3
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 163 deletions.
22 changes: 11 additions & 11 deletions stellarphot/differential_photometry/aij_rel_fluxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

__all__ = ['add_in_quadrature', 'calc_aij_relative_flux']


def add_in_quadrature(array):
"""
Add an array of numbers in quadrature.
Expand All @@ -16,19 +17,19 @@ def add_in_quadrature(array):
def calc_aij_relative_flux(star_data, comp_stars,
in_place=True, coord_column=None,
star_id_column='star_id',
counts_column_name='aperture_net_counts'):
counts_column_name='aperture_net_cnts'):
"""
Calculate AstroImageJ-style flux ratios.
Parameters
----------
star_data : '~astropy.table.Table'
Table of star data from one or more images.
star_data : 'stellarphot.PhotometryData'
Photometry data from one or more images.
comp_stars : '~astropy.table.Table'
Table of comparison stars in the field. Must contain a column
called ``RA`` and a column called ``Dec``.
called ``ra`` and a column called ``dec``.
NOTE that not all
of the comparison stars will necessarily be used. Stars in
this table are excluded from the comparison set if, in any
Expand All @@ -47,7 +48,6 @@ def calc_aij_relative_flux(star_data, comp_stars,
counts_column_name : str, optional
If provided, use this column to find counts.
If not provided, the column 'aperture_net_counts' is used.
star_id_column : str, optional
Name of the column that provides a unique identifier for each
Expand All @@ -56,33 +56,33 @@ def calc_aij_relative_flux(star_data, comp_stars,
Returns
-------
`astropy.table.Table` or None
`stellarphot.PhotometryData` or None
The return type depends on the value of ``in_place``. If it is
``False``, then the new columns are returned as a separate table,
otherwise the columns are simply added to the input table.
"""

# Match comparison star list to instrumental magnitude information
if star_data['RA'].unit is None:
if star_data['ra'].unit is None:
unit = 'degree'
else:
# Pulled this from the source code -- None is ok but need
# to match the number of coordinates.
unit = [None, None]

star_data_coords = SkyCoord(ra=star_data['RA'], dec=star_data['Dec'],
star_data_coords = SkyCoord(ra=star_data['ra'], dec=star_data['dec'],
unit=unit)

if coord_column is not None:
comp_coords = comp_stars[coord_column]
else:
if comp_stars['RA'].unit is None:
if comp_stars['ra'].unit is None:
unit = 'degree'
else:
# Pulled this from the source code -- None is ok but need
# to match the number of coordinates.
unit = [None, None]
comp_coords = SkyCoord(ra=comp_stars['RA'], dec=comp_stars['Dec'],
comp_coords = SkyCoord(ra=comp_stars['ra'], dec=comp_stars['dec'],
unit=unit)

# Check for matches of stars in star data to the stars in comp_stars
Expand Down Expand Up @@ -115,7 +115,7 @@ def calc_aij_relative_flux(star_data, comp_stars,
this_comp = star_data[star_id_column] == comp
good[this_comp] = False

error_column_name = 'noise-aij'
error_column_name = 'noise_electrons'
# Calculate comp star counts for each time

# Make a small table with just counts, errors and time for all of the comparison
Expand Down
32 changes: 17 additions & 15 deletions stellarphot/differential_photometry/tests/test_aij_rel_fluxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from astropy.time import Time
import astropy.units as u

from stellarphot import PhotometryData
from stellarphot.differential_photometry.aij_rel_fluxes import calc_aij_relative_flux


Expand Down Expand Up @@ -51,9 +52,10 @@ def _raw_photometry_table():
_repeat(star_dec, n_times), _repeat(fluxes, n_times),
_repeat(errors, n_times),
_repeat(star_ids, n_times)],
names=['date-obs', 'RA', 'Dec', 'aperture_net_counts',
'noise-aij', 'star_id'])
names=['date-obs', 'ra', 'dec', 'aperture_net_cnts',
'noise_electrons', 'star_id'])

photom = PhotometryData(raw_table)
return expected_flux_ratios, expected_flux_error, raw_table, raw_table[1:4]


Expand All @@ -71,12 +73,12 @@ def test_relative_flux_calculation(in_place,
all_expected_error = _repeat(expected_error, n_times)

if not star_ra_dec_have_units:
input_table['RA'] = input_table['RA'].data
input_table['Dec'] = input_table['Dec'].data
input_table['ra'] = input_table['ra'].data
input_table['dec'] = input_table['dec'].data

if not comp_ra_dec_have_units:
comp_star['RA'] = comp_star['RA'].data
comp_star['Dec'] = comp_star['Dec'].data
comp_star['ra'] = comp_star['ra'].data
comp_star['dec'] = comp_star['dec'].data

output_table = calc_aij_relative_flux(input_table, comp_star,
in_place=in_place)
Expand Down Expand Up @@ -106,27 +108,27 @@ def test_bad_comp_star(bad_thing):

if bad_thing == 'RA':
# "Jiggle" one of the stars by moving it by a few arcsec in one image.
coord_inp = SkyCoord(ra=last_one['RA'], dec=last_one['Dec'],
coord_inp = SkyCoord(ra=last_one['ra'], dec=last_one['dec'],
unit=u.degree)
coord_bad_ra = coord_inp.ra + 3 * u.arcsecond
input_table['RA'][-1] = coord_bad_ra.degree
input_table['ra'][-1] = coord_bad_ra.degree
elif bad_thing == 'NaN':
input_table['aperture_net_counts'][-1] = np.nan
input_table['aperture_net_cnts'][-1] = np.nan

output_table = calc_aij_relative_flux(input_table, comp_star,
in_place=False)

old_total_flux = comp_star['aperture_net_counts'].sum()
new_flux = old_total_flux - last_one['aperture_net_counts']
old_total_flux = comp_star['aperture_net_cnts'].sum()
new_flux = old_total_flux - last_one['aperture_net_cnts']
# This works for target stars, i.e. those never in comparison set
new_expected_flux = old_total_flux / new_flux * expected_flux

# Oh wow, this is terrible....
# Need to manually calculate for the only two that are still in comparison
new_expected_flux[1] = (comp_star['aperture_net_counts'][0] /
comp_star['aperture_net_counts'][1])
new_expected_flux[2] = (comp_star['aperture_net_counts'][1] /
comp_star['aperture_net_counts'][0])
new_expected_flux[1] = (comp_star['aperture_net_cnts'][0] /
comp_star['aperture_net_cnts'][1])
new_expected_flux[2] = (comp_star['aperture_net_cnts'][1] /
comp_star['aperture_net_cnts'][0])

new_expected_flux[3] = expected_flux[3]
if bad_thing == 'NaN':
Expand Down
38 changes: 27 additions & 11 deletions stellarphot/notebooks/photometry/03-photometry-template.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from pathlib import Path\n",
Expand Down Expand Up @@ -31,7 +33,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"##\n",
Expand All @@ -41,7 +45,7 @@
" read_noise = 10.0 * u.electron,\n",
" dark_current=0.01 * u.electron / u.second,\n",
" pixel_scale = 0.563 * u.arcsec / u.pix)\n",
"feder_filters = {'up':'SU', 'gp':'SG', 'rp':'SR', 'zp':'SZ', 'ip':'SI'}\n",
"feder_filters = {'up':'SU', 'gp':'SG', 'rp':'SR', 'zp':'SZ', 'ip':'SI', 'V': 'V', 'B': 'B'}\n",
"feder_obs = EarthLocation(lat = 46.86678, lon=-96.45328, height=311)\n",
"\n",
"# Maxmimum ADU for the images before we should consider pixels saturated\n",
Expand All @@ -60,7 +64,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"ps = PhotometrySettings()\n",
Expand All @@ -77,7 +83,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"##\n",
Expand Down Expand Up @@ -109,7 +117,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# The maximum allowed shift in an object position (in pixels) when comparing\n",
Expand Down Expand Up @@ -138,7 +148,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"photometry_table = multi_image_photometry(image_directory,\n",
Expand All @@ -151,13 +163,15 @@
" reject_too_close=True,\n",
" reject_background_outliers=True,\n",
" passband_map=feder_filters,\n",
" fwhm_by_fit=True)"
" fwhm_by_fit=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"photometry_table.write(photometry_file)"
Expand All @@ -166,7 +180,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"photometry_table\n"
Expand Down Expand Up @@ -196,7 +212,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.6"
}
},
"nbformat": 4,
Expand Down
39 changes: 25 additions & 14 deletions stellarphot/notebooks/photometry/04-transform-pared-back.ipynb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from pathlib import Path\n",
Expand Down Expand Up @@ -67,14 +69,15 @@
"c_min = -0.5\n",
"d_min = -1e-6\n",
"\n",
"our_filters = ['B', 'V', 'rp']\n",
"our_filters = ['SI']\n",
"\n",
"aavso_band_names = dict(\n",
" B='B', \n",
" V='V',\n",
" gp='SG',\n",
" rp='SR',\n",
" ip='SI'\n",
" ip='SI',\n",
" SI='SI'\n",
")\n",
"\n",
"cat_color_colums = dict(\n",
Expand All @@ -83,30 +86,34 @@
" gp=('g_mag', 'r_mag'),\n",
" rp=('r_mag', 'i_mag'),\n",
" ip=('r_mag', 'i_mag'),\n",
" \n",
" SI=('r_mag', 'i_mag'),\n",
")\n",
"\n",
"cat_filter = dict(\n",
" B='Bmag', \n",
" V='Vmag',\n",
" gp='g_mag',\n",
" rp='r_mag',\n",
" ip='i_mag')\n",
" ip='i_mag',\n",
" SI='i_mag',\n",
")\n",
"\n",
"input_photometry_file = 'm13-2021-09-28.csv'\n",
"output_photometry_file = 'some_name.csv'"
"input_photometry_file = 'TIC-402828941-2021-09-28.ecsv'\n",
"output_photometry_file = 'some_name.ecsv'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"all_mags = Table.read(input_photometry_file)\n",
"\n",
"# Ensure we have the right table ordering later\n",
"all_mags.sort(['filter', 'BJD'])"
"all_mags.sort(['passband', 'bjd'])"
]
},
{
Expand All @@ -119,10 +126,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"filter_groups = all_mags.group_by('filter')\n",
"filter_groups = all_mags.group_by('passband')\n",
"\n",
"\n",
"# Check: do we have any unexpected filters?\n",
Expand All @@ -140,14 +149,16 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"output_table = [] \n",
"\n",
"for k, group in zip(our_filters, filter_groups.groups):\n",
" print(f'Transforming band {k}')\n",
" by_bjd = group.group_by('BJD')\n",
" by_bjd = group.group_by('bjd')\n",
" \n",
" transform_to_catalog(by_bjd, f'mag_inst', aavso_band_names[k], \n",
" obs_error_column='mag_error', \n",
Expand Down Expand Up @@ -213,7 +224,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit c8b99b3

Please sign in to comment.