Haipeng Li Lab 6 python code (1)

.pdf

School

University of California, Irvine *

*We aren’t endorsed by this school

Course

115A

Subject

Electrical Engineering

Date

Jan 9, 2024

Type

pdf

Pages

9

Uploaded by DeanResolve98810

Report
12/3/23, 1:59 AM Untitled81 - Jupyter Notebook localhost:8888/notebooks/Untitled81.ipynb 1/9 In [50]: import numpy as np import matplotlib.pyplot as plt from astropy.io import fits image_file = 'corrected_s0311.fits' image_data = fits.getdata(image_file) x1, x2, y1, y2 = 100 , 150 , 100 , 150 star_region = image_data[y1:y2, x1:x2] profile_x = np.sum(star_region, axis = 0 ) profile_y = np.sum(star_region, axis = 1 ) peak_x = np.max(profile_x) half_max_x = peak_x / 2 peak_y = np.max(profile_y) half_max_y = peak_y / 2 fwhm_x = np.sum(profile_x > half_max_x) fwhm_y = np.sum(profile_y > half_max_y) plt.figure(figsize = ( 12 , 6 )) plt.subplot( 1 , 2 , 1 ) plt.plot(profile_x) plt.axhline(y = half_max_x, color = 'r' , linestyle = '--' ) plt.title( f'X Profile (FWHM: {fwhm_x} pixels)' ) plt.xlabel( 'Pixel Position' ) plt.ylabel( 'Intensity' ) plt.subplot( 1 , 2 , 2 ) plt.plot(profile_y) plt.axhline(y = half_max_y, color = 'r' , linestyle = '--' ) plt.title( f'Y Profile (FWHM: {fwhm_y} pixels)' ) plt.xlabel( 'Pixel Position' ) plt.ylabel( 'Intensity' ) plt.tight_layout() plt.show()
12/3/23, 1:59 AM Untitled81 - Jupyter Notebook localhost:8888/notebooks/Untitled81.ipynb 2/9 In [29]: Model: CompoundModel Inputs: ('x',) Outputs: ('y',) Model set size: 1 Expression: [0] + [1] Components: [0]: <Gaussian1D(amplitude=3.05394402, mean=1.27426551, stddev=0.81712 989)> [1]: <Const1D(amplitude=0.99037165)> Parameters: amplitude_0 mean_0 stddev_0 amplitude_1 ------------------ ---------------- ------------------ --------------- --- 3.0539440238047133 1.27426550645335 0.8171298855086331 0.9903716523469 903 Computed FWHM: 1.9243408803728308 from astropy.modeling import models, fitting np.random.seed( 0 ) xdata = np.linspace( - 5. , 5. , 200 ) ydata = 3 * np.exp( - 0.5 * (xdata - 1.3 ) ** 2 / 0.8 ** 2 ) + 1.0 ydata += np.random.normal( 0. , 0.2 , xdata.shape) g_init = models.Gaussian1D(amplitude = 1. , mean = 0 , stddev = 1. ) + models.Const1 fit_g = fitting.LevMarLSQFitter() g = fit_g(g_init, xdata, ydata) plt.figure(figsize = ( 8 , 5 )) plt.plot(xdata, ydata, 'ko' , label = 'Data' ) plt.plot(xdata, g(xdata), label = 'Gaussian + Constant' ) plt.xlabel( 'Pixel' ) plt.ylabel( 'Counts' ) plt.legend(loc = 2 ) plt.show() print (g) fwhm_computed = 2.355 * g.stddev_0 print ( "Computed FWHM:" , fwhm_computed)
12/3/23, 1:59 AM Untitled81 - Jupyter Notebook localhost:8888/notebooks/Untitled81.ipynb 3/9 In [30]: x = np.arange( 0 , 50 ) profile_x = np.sum(star_region, axis = 0 ) profile_y = np.sum(star_region, axis = 1 ) fit_g = fitting.LevMarLSQFitter() g_init_x = models.Gaussian1D(amplitude = np.max(profile_x), mean = np.argmax(pro g_x = fit_g(g_init_x, x, profile_x) g_init_y = models.Gaussian1D(amplitude = np.max(profile_y), mean = np.argmax(pro g_y = fit_g(g_init_y, x, profile_y) plt.figure(figsize = ( 12 , 6 )) plt.subplot( 1 , 2 , 1 ) plt.plot(x, profile_x, 'ko' , label = 'X Profile Data' ) plt.plot(x, g_x(x), label = 'Gaussian Fit' , linestyle = '--' ) plt.axhline(y = g_x.amplitude.value / 2 , color = 'r' , linestyle = '--' ) plt.title( f'X Profile and Gaussian Fit\nFWHM: { 2.355 * g_x.stddev.value: .2 f plt.xlabel( 'Pixel Position' ) plt.ylabel( 'Intensity' ) plt.legend() plt.subplot( 1 , 2 , 2 ) plt.plot(x, profile_y, 'ko' , label = 'Y Profile Data' ) plt.plot(x, g_y(x), label = 'Gaussian Fit' , linestyle = '--' ) plt.axhline(y = g_y.amplitude.value / 2 , color = 'r' , linestyle = '--' ) plt.title( f'Y Profile and Gaussian Fit\nFWHM: { 2.355 * g_y.stddev.value: .2 f plt.xlabel( 'Pixel Position' ) plt.ylabel( 'Intensity' ) plt.legend() plt.tight_layout() plt.show()
12/3/23, 1:59 AM Untitled81 - Jupyter Notebook localhost:8888/notebooks/Untitled81.ipynb 4/9 In [60]: another_image_file = 's0576.fits' another_image_data = fits.getdata(another_image_file) another_x1, another_x2, another_y1, another_y2 = 100 , 150 , 100 , 150 another_star_region = another_image_data[another_y1:another_y2, another_x1: another_profile_x = np.sum(another_star_region, axis = 0 ) another_profile_y = np.sum(another_star_region, axis = 1 ) another_peak_x = np.max(another_profile_x) another_half_max_x = another_peak_x / 2 another_peak_y = np.max(another_profile_y) another_half_max_y = another_peak_y / 2 another_fwhm_x = np.sum(another_profile_x > another_half_max_x) another_fwhm_y = np.sum(another_profile_y > another_half_max_y) plt.figure(figsize = ( 12 , 6 )) plt.subplot( 1 , 2 , 1 ) plt.plot(another_profile_x) plt.axhline(y = another_half_max_x, color = 'r' , linestyle = '--' ) plt.title( f'X Profile for {another_image_file} (FWHM: {another_fwhm_x} pixe plt.xlabel( 'Pixel Position' ) plt.ylabel( 'Intensity' ) plt.subplot( 1 , 2 , 2 ) plt.plot(another_profile_y) plt.axhline(y = another_half_max_y, color = 'r' , linestyle = '--' ) plt.title( f'Y Profile for {another_image_file} (FWHM: {another_fwhm_y} pixe plt.xlabel( 'Pixel Position' ) plt.ylabel( 'Intensity' ) plt.tight_layout() plt.show()
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help