<jupyter_start><jupyter_text>open the FITS file<jupyter_code>#read image into a 2-D numpy array
fname = "hlsp_hudf12_hst_wfc3ir_udfmain_f105w_v1.0_drz.fits"
hdu_list = fits.open(fname)
hdu_list.info()
#access image by indexing hdu_list
image_data = hdu_list[0].data
#data is stored as a 2D numpy array. Show the shape of the array
image_data = fits.getdata(fname)
print(type(image_data))
print(image_data.shape)
hdu_list.close()<jupyter_output><empty_output><jupyter_text>show the data<jupyter_code>#show the image
m,s = np.mean(image_data), np.std(image_data)
plt.imshow(image_data, interpolation='nearest',cmap='bone', vmin=m-s, vmax=m+s, origin='lower')
plt.colorbar()
plt.savefig('image_1.png')<jupyter_output><empty_output><jupyter_text>background subtraction<jupyter_code>#measure spatially varying background on image
#change byte data to read image
image_data_new = image_data.byteswap().newbyteorder()
bkg = sep.Background(image_data_new)
bkg = sep.Background(image_data_new, bw=64, bh=64, fw=3, fh=3)
#get global mean and noise of image's background
print(bkg.globalback)
print(bkg.globalrms)
#evaluate background as 2-D array but same size as original image
bkg_image = bkg.back()
#bkg_image = np.array(bkg)
#show background
plt.imshow(bkg_image,interpolation='nearest',cmap='bone',origin='lower')
plt.colorbar();
plt.savefig('image_2.png')
#evaluate background noise as 2-D array, same size as original image
bkg_rms = bkg.rms()
#show background noise
plt.imshow(bkg_rms,interpolation='nearest',cmap='gray',origin='lower')
plt.colorbar();
plt.savefig('image_3.png')
#subtract background
image_data_sub = image_data - bkg<jupyter_output><empty_output><jupyter_text>object detection<jupyter_code>#set detection threshold to be a constant value of 1.5*sigma
#sigma=global background rms
objects = sep.extract(image_data_sub, 1.5, err=bkg.globalrms)
#number of objects detected
len(objects)
#over-plot the object coordinates with some parameters on the image
#this will check where the detected objects are
from matplotlib.patches import Ellipse

#plot background-subtracted image
fig, ax = plt.subplots()
m,s = np.mean(image_data_sub), np.std(image_data_sub)
im = ax.imshow(image_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./np.pi)
    e.set_facecolor('none')
    e.set_edgecolor('red')
    ax.add_artist(e)

plt.savefig('image_4.png')
#see available fields
objects.dtype.names<jupyter_output><empty_output><jupyter_text>aperture photometry<jupyter_code>#perform circular aperture photometry 
#with a 3 pixel radius at locations of the objects
flux, fluxerr, flag = sep.sum_circle(image_data_sub,objects['x'], objects['y'],
                                     3.0, err=bkg.globalrms, gain=1.0)
#show the first 10 objects results:
for i in range(10):
    print("object {:d}: flux = {:f} +/- {:f}".format(i, flux[i], fluxerr[i]))<jupyter_output><empty_output><jupyter_text>histogram their fluxes<jupyter_code>#convert to AB magnitudes
header = fits.getheader(fname)
x = header["PHOTFLAM"]
y = header["PHOTPLAM"]

#calculate zero pt 
zp_ambag = -2.5*np.log10(x) - 5*np.log10(y) - 2.408
abmags = -2.5*np.log10(flux) + zp_ambag

#plot AB magnitudes
plt.hist(abmags);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13375250071448985, 'fraction_numerical': 0.01600457273506716, 'mean_word_length': 5.889763779527559, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '655375', 'license': 'MIT', 'n_tokens_mistral': 1339, 'n_tokens_neox': 1284, 'n_words': 274, 'path': 'final_project_2.ipynb', 'repo_name': 'ashleyparrilla/astro-detection', 'size': '3499'}