<jupyter_start><jupyter_text>3. Contoh fitting garis emisi, mengukur equivalent width dan fluks  Fitting spektrum terhadap kontinum Cara fitting ini hanya dapat digunakan untuk rentang spektrum yang tidak lebar atau untuk spektrum degnan kontinum yang lemah (misal: spektrum planetary nebula).Sebagai contoh, kita akan gunakan spektrum IC 418.<jupyter_code>import astropy.units as u
from specutils import Spectrum1D
import numpy as np
import matplotlib.pyplot as plt

from specutils import Spectrum1D, SpectralRegion #spectrum1D (specutils)
from astropy.modeling import models, fitting
from specutils.fitting import fit_generic_continuum

fluxunit = u.erg / (u.cm ** 2 * u.s)

file = np.loadtxt("ic418.csv", delimiter = ",").T  # no reason to convert to a list
wave = file[0] * u.AA
flux = file[1] * fluxunit

spec = Spectrum1D(spectral_axis=wave, flux=flux)

#Visualize continuum fitting
x = spec.spectral_axis
y = spec.flux

#fitting continuum (with exclude region between 4800 and 5100A)
g1_fit = fit_generic_continuum(spec, exclude_regions=[SpectralRegion(4800 * u.AA, 5100 * u.AA),SpectralRegion(6500 * u.AA, 6600 * u.AA)])
y_continuum_fitted = g1_fit(x)


#plot spectrum
fig, ax = plt.subplots() 
ax.plot(spec.spectral_axis, spec.flux)  
ax.set_xlabel("Wavelength (Angstrom)")  
ax.set_ylabel("Flux (erg/cm2/s/A)") 
ax.grid(color='grey', alpha=0.8, linestyle='-.', linewidth=0.2, axis='both') 
ax.set_title("IC 418", loc='center', fontsize=12, fontweight=0.5)

fig10, ax10 = plt.subplots(figsize=(8,5))
#spectrum
ax10.plot(x, y)
#continuum
ax10.plot(x, y_continuum_fitted)
ax10.grid(True)
#plt.show()

#Show Plot
plt.show()<jupyter_output>WARNING: Model is linear in parameters; consider using linear fitting methods. [astropy.modeling.fitting]<jupyter_text>Mendeteksi garis emisi<jupyter_code>import astropy.units as u
from specutils import Spectrum1D
import numpy as np

from specutils import Spectrum1D, SpectralRegion #spectrum1D (specutils)
from astropy.modeling import models, fitting
from specutils.fitting import fit_generic_continuum

from specutils.fitting import find_lines_derivative
from specutils.fitting import fit_lines

fluxunit = u.erg / (u.cm ** 2 * u.s)

file = np.loadtxt("ic418.csv", delimiter = ",").T  # no reason to convert to a list
wave = file[0] * u.AA
flux = file[1] * fluxunit

spec = Spectrum1D(spectral_axis=wave, flux=flux)

#Visualize continuum fitting
x = spec.spectral_axis
y = spec.flux

#fitting continuum (with exclude region between 4800 and 5100A)
g1_fit = fit_generic_continuum(spec, exclude_regions=[SpectralRegion(4800 * u.AA, 5100 * u.AA),SpectralRegion(6500 * u.AA, 6600 * u.AA)])
y_continuum_fitted = g1_fit(x)

#divide spectrum by his continuum
y_cont_fitted = g1_fit(x)
spec_normalized = spec / y_cont_fitted 

#emission line detections
lines = find_lines_derivative(spec_normalized, flux_threshold=10)
print('emission: ', lines[lines['line_type'] == 'emission'])<jupyter_output>WARNING: Model is linear in parameters; consider using linear fitting methods. [astropy.modeling.fitting]
WARNING: Spectrum is not below the threshold signal-to-noise 0.01. This may indicate you have not continuum subtracted this spectrum (or that you have but it has high SNR features).

If you want to suppress this warning either type 'specutils.conf.do_continuum_function_check = False' or see http://docs.astropy.org/en/stable/config/#adding-new-configuration-items for other ways to configure the warning. [specutils.analysis.flux]<jupyter_text>Menghitung FWHM dan fluks<jupyter_code>import astropy.units as u
from specutils import Spectrum1D
import numpy as np
import matplotlib.pyplot as plt

from specutils import Spectrum1D, SpectralRegion #spectrum1D (specutils)
from astropy.modeling import models, fitting
from specutils.fitting import fit_generic_continuum

from specutils.fitting import find_lines_derivative
from specutils.fitting import fit_lines

from specutils.manipulation import extract_region
from specutils.fitting import estimate_line_parameters
from specutils.analysis import centroid, fwhm
from specutils.analysis import line_flux
from specutils.analysis import equivalent_width

fluxunit = u.erg / (u.cm ** 2 * u.s)

file = np.loadtxt("ic418.csv", delimiter = ",").T  # no reason to convert to a list
wave = file[0] * u.AA
flux = file[1] * fluxunit

spec = Spectrum1D(spectral_axis=wave, flux=flux)

#Visualize continuum fitting
x = spec.spectral_axis
y = spec.flux

#fitting continuum (with exclude region between 4800 and 5100A)
g1_fit = fit_generic_continuum(spec, exclude_regions=[SpectralRegion(4800 * u.AA, 5100 * u.AA),SpectralRegion(6500 * u.AA, 6600 * u.AA)])
y_continuum_fitted = g1_fit(x)

#divide spectrum by his continuum
y_cont_fitted = g1_fit(x)
spec_normalized = spec / y_cont_fitted 

#emission line detections
lines = find_lines_derivative(spec_normalized, flux_threshold=10)
#print('emission: ', lines[lines['line_type'] == 'emission']) 

#spectral region - line analysis: line center & FWHM
sr =  SpectralRegion((6563-20)*u.AA, (6563+20)*u.AA)

#print centroid - need a spectrum and a spectral region in parameters
center = centroid(spec_normalized, sr)  
print("center : ", center)

#print fwhm - need a spectrum and a spectral region in parameters
fwhm_spec = fwhm(spec_normalized, regions=sr)
print("fwhm : ", fwhm_spec)

#line flux
fluks = line_flux(spec, sr)
print("line flux : ", fluks)

#equivalent width
ew = equivalent_width(spec_normalized, regions=sr) 
print("equivalent width : ", ew)


#create a new spectrum of the selected region for plot
sub_spectrum = extract_region(spec_normalized, sr)
Ha_line = Spectrum1D(flux=sub_spectrum.flux,spectral_axis=sub_spectrum.spectral_axis)


#plot spectrum
fig, ax = plt.subplots() 
ax.plot(Ha_line.spectral_axis, Ha_line.flux)
ax.set_xlabel("Wavelength (Angstrom)")  
ax.set_ylabel("Flux (erg/(cm^2 s A)") 
ax.grid(True)
plt.show()
#Show Plot
plt.show()<jupyter_output>WARNING: Model is linear in parameters; consider using linear fitting methods. [astropy.modeling.fitting]
WARNING: Spectrum is not below the threshold signal-to-noise 0.01. This may indicate you have not continuum subtracted this spectrum (or that you have but it has high SNR features).

If you want to suppress this warning either type 'specutils.conf.do_continuum_function_check = False' or see http://docs.astropy.org/en/stable/config/#adding-new-configuration-items for other ways to configure the warning. [specutils.analysis.flux]
{'fraction_non_alphanumeric': 0.09830719055753999, 'fraction_numerical': 0.025159186209038672, 'mean_word_length': 5.807610993657505, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '76016', 'license': 'MIT', 'n_tokens_mistral': 2325, 'n_tokens_neox': 2130, 'n_words': 630, 'path': '3_Mengukur_EW_dan_fluks.ipynb', 'repo_name': 'evaniakbar/pn', 'size': '6439'}