<jupyter_start><jupyter_text>This is the sep tutorial using astropy instead of fitsio.<jupyter_code>import sep
from astropy.io import fits
import numpy as np
import matplotlib.pyplot as plt
from astropy.utils.data import download_file
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Opening the image.Note: The FITS folder must be on the Desktop<jupyter_code># Download the image through the astropy.utils.data.download_file function
image_file = download_file('https://github.com/kbarbary/sep/blob/master/data/image.fits?raw=true', cache=True )

# Open the fits file through the astropy.io.fits class
hdul = fits.open(image_file)

# Set the first entry in hdul equal to data so that
# it can be placed in a standard 2-D numpy array
data = hdul[0].data
# Show the image
m, s = np.mean(data), np.std(data)
plt.figure(figsize=(8,8))
plt.imshow(data, interpolation='nearest', cmap='gray', vmin=m-s, vmax=m+s, origin='lower')
plt.colorbar()

# Save the image
plt.savefig('image_direct.png');<jupyter_output><empty_output><jupyter_text>Background Subtraction<jupyter_code># Measure a spatially varying background on the image
bkg = sep.Background(data)
# Get a "global" mean and noise of the image background
print(bkg.globalback)
print(bkg.globalrms)
# Evaluate the background as a 2-D array of the same
# size as the original
bkg_image = bkg.back()
# bkg_image = np.array(bkg) would be the same as the
# equation above
# Show the background
plt.figure(figsize=(8,8))
plt.imshow(bkg_image, interpolation='nearest', cmap='gray', origin='lower')
plt.colorbar()

# Save the image
plt.savefig('image_back.png');
# Evaluate the background noise as a 2-D array of
# the same size as the original
bkg_rms = bkg.rms()
# Show the background noise
plt.figure(figsize=(8,8))
plt.imshow(bkg_rms, interpolation='nearest', cmap='gray', origin='lower')
plt.colorbar()

# Save the image
plt.savefig('image_rms.png');
# Subtract the background
data_sub = data - bkg
# This is needed in order detect the objects in the image<jupyter_output><empty_output><jupyter_text>Object Detection<jupyter_code># Set the detection threshold to be 1.5 times the
# global background RMS.
objects = sep.extract(data_sub, 1.5, err=bkg.globalrms)
# This will return how many objects were detected
len(objects)<jupyter_output><empty_output><jupyter_text>Note: this output is 1 higher than that of the tutorial. This is because this image has a bkg.globalrms that is 0.0001 higher than that in the tutorial.<jupyter_code>from matplotlib.patches import Ellipse

# Plot the background-subtracted image
fig, ax = plt.subplots(figsize=(8,8))
m, s = np.mean(data_sub), np.std(data_sub)
im = ax.imshow(data_sub, interpolation='nearest', cmap='gray', vmin=m-s, vmax=m+s, origin='lower')

# Plot an ellipse for each object
for i in range(len(objects)):
    e = Ellipse(xy=(objects['x'][i], objects['y'][i]),width=6*objects['a'][i],height=6*objects['b'][i],angle=objects['theta'][i]*180/(2*np.pi))
    e.set_facecolor('none')
    e.set_edgecolor('red')
    ax.add_artist(e)

# Save the image
plt.savefig('image_final.png')<jupyter_output><empty_output><jupyter_text>Note: The objects function has many fields. All of the fields can be seen by typing objects.dtypes.names  Aperature PhotometrySimple circular aperature photometry with a 3 pixel radius at the locations of the objects will be performed.<jupyter_code># Flux, fluxerr, and flag are all 1-D arrays with one entry per object
flux, fluxerr, flag = sep.sum_circle(data_sub,objects['x'],objects['y'],3.0,err=bkg.globalrms,gain=1.0)
# Show the first ten objects' results
for i in range(10):
    print("object {:d}: flux = {:f} +/- {:f}".format(i,flux[i],fluxerr[i]))<jupyter_output>object 0: flux = 2249.159297 +/- 291.027802
object 1: flux = 3092.220430 +/- 291.592204
object 2: flux = 5949.868379 +/- 356.562003
object 3: flux = 1851.426582 +/- 295.028816
object 4: flux = 72736.386914 +/- 440.172206
object 5: flux = 3860.756152 +/- 352.163162
object 6: flux = 6418.913789 +/- 357.458973
object 7: flux = 2210.707656 +/- 350.791223
object 8: flux = 2741.607227 +/- 352.277746
object 9: flux = 20916.875566 +/- 376.966138
{'fraction_non_alphanumeric': 0.12671976828385229, 'fraction_numerical': 0.057446294955346366, 'mean_word_length': 5.485133020344288, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '340733', 'license': 'MIT', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1428, 'n_words': 421, 'path': 'Sep_Tutorial.ipynb', 'repo_name': 'tiseever/ASTR_119_Final_Project', 'size': '4143'}