<jupyter_start><jupyter_text>This is an examples of how to perform a CGM analysis using the rb_spec.py pipeline Written By: <NAME>----------------The goal of this notebook is to show a running example analysing absorption line spectra. We will read in a fits file of a HST/COS quasar, pick one absorption line, continuum normalize it locally and then compute the EW and AOD column density of that line. This pipeline allows one to do a batch mode continuum normalization or an interactive local continuum normalization-----------------Example Created: Feb 11, 2021Tested on Python 3.7https://github.com/rongmon/rbcodes------------------- Requirements: astropy, numpy, scipy, matplotlib,linetoolslinetools for input output of fits files, and for COS LSF<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook
from GUIs.rb_spec import rb_spec as r
from IGM import rb_setline as setline
from linetools.spectra.xspectrum1d import XSpectrum1D<jupyter_output><empty_output><jupyter_text>Read in the data that you want to measure<jupyter_code>filename='example-data/test.fits'
s=r.from_file(filename,filetype='xfits')<jupyter_output>Normalizing spectrum using given continuum...<jupyter_text>The transition of the absorber and it's redshift<jupyter_code>#Redshift of the absorber you are trying to fit
zabs=0.348
transition=1025.6 # HI 1025<jupyter_output><empty_output><jupyter_text>Shift spectrum to the rest frame of the absorber<jupyter_code>s.shift_spec(zabs);<jupyter_output><empty_output><jupyter_text>Create a small slice of the spectrum that you want to analyze<jupyter_code>xlim=[-600,600]
s.slice_spec(transition,xlim[0],xlim[1],use_vel=True);
# Showing the atomic tranistion and its line properties
str1=setline.rb_setline(transition,'closest',linelist='LLS')
str1<jupyter_output><empty_output><jupyter_text>Fit continuum  First we will fit in a non-interactive mode, a Legendre polinomial fit with part of the spectrum masked offNote: Use only the continuum fitting routine you want. You don't need to use both methods. I am commenting out the non-interactive mode below. But it is presented for psterity. We mask the part of spectrum that we don't want to fit. Mask is created to have multiple low vel, high vel ranges.e.g. mask = [-300.,-250.,100.,120.] will exclude -300,-250 and 100,120 km/s parts of the spectrum in the fit<jupyter_code>#s.fit_continuum(mask=[-170,120.,140.,290.],domain=xlim,Legendre=3)



# Show the fits
#plt.subplot(2,1,1)
#plt.step(s.velo,s.flux_slice)
#plt.step(s.velo,s.flux_slice/s.fnorm)
#plt.xlim(xlim)
#plt.subplot(2,1,2)
#plt.step(s.velo,s.fnorm)
#plt.plot([-600,600],[1,1],'--')
#plt.xlim(xlim)<jupyter_output><empty_output><jupyter_text>An alternate method to fit local continuum.  Interactive Continuum Fitting. ***********************************The required keystrikes are:         This calls an interactive continuum fitter for 1D spectrum.            We will create a spline continuum fit from selected points.                Program sequence is important. First you select the continuum positions using either left mouse button or keystroke b. Right clock will deselect any continuum point.         Then press 'enter' to fit the continuum. Then press 'n' to show the normalized spectrum.                    Useful Keystrokes:                    Mouse Clicks:                                    Left Click  : Select the median flux value within +/- 5 pixel from the x-coordinate.                                  These points are used for the continuum fit.                    Right Click : Delete the nearest continuum point.                    Keystrokes:                                    b     :    Select a point for continuum fit at that exact (x,y) coordinate.                  enter :    Perform a spline fit to data to create a continuum.                  n     :    Show the normalized spectrum.                  w     :    Only after pressing n: This will ourput the continuum.                   h     :    This Help screen.                  r     :    Reset fit.                  q     :    Quit Program.             ---------------------------------------------------------------------------            Written By:  <NAME>                                   July 13 2017.<jupyter_code>from GUIs import interactive_cont_jn as ft
reload(ft)
cont=ft.interactive_cont(s.wave_slice,s.flux_slice,error=s.error_slice)<jupyter_output><empty_output><jupyter_text>Add the handfitted continuum into the rb_spec object<jupyter_code>s.fit_continuum(prefit_cont=cont.cont);<jupyter_output>/Users/bordoloi/WORK/python/rbcodes/GUIs/rb_spec.py:284: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  if prefit_cont == 'False':<jupyter_text>Third option for automatic continuum fitting using RANSAC fitting<jupyter_code>#s.fit_continuum(mask=[-100,300],domain=xlim,Legendre=3)
s.fit_continuum_ransac(window=199,mednorm=False)
plt.subplot(2,1,1)
plt.step(s.velo,s.flux_slice)
plt.step(s.velo,s.flux_slice/s.fnorm)
plt.xlim(xlim)
plt.subplot(2,1,2)
plt.step(s.velo,s.fnorm)
plt.plot([-600,600],[1,1],'--')
plt.xlim(xlim)

plt.show()<jupyter_output>Initializing RANSAC continuum fitter<jupyter_text>Compute Equivalent Width and column density for the line profile within a velocity range<jupyter_code># Setting plot False will hide the plot below
s.compute_EW(transition,vmin=-200.,vmax=305.,plot=True);

print(s.vel_centroid,s.vel_disp)<jupyter_output>-19.92095659901945<jupyter_text>Save all the information produced above. See rb_spec.py file for details of what is saved.All entries are saved as a python dictionary in a pickle file. The entries for the saved dictionary are:zabs= Absorber redshiftwave_slice= sliced observed wavelength vectorflux_slice= sliced observed flux vectorerror_slice= sliced velocity spectra linelist=. LineList usedvelo=  sliced velocity vectorcont = Fitted continuumfnorm= Normalized fluxenorm= Normalized errortrans=  Name of the Transitionfval= fvalue of transitiontrans_wave= rest frame wavelength of transitionvmin=     velocity minimum used for equivalent width calculationvmax=    velocity maximum used for equivalent width calculationW=    Rest Frame equivalenth widthW_e=  uncertainty on rest frame equivalent widthlogN=  AOD column densitylogN_e= AOD column density uncertaintyTau= Apparant optical depth as a function of velocityvel_centroid= EW weighted velocity centroidvel_disp= 1 sigma velocity dispersion<jupyter_code>s.save_slice('outfile.p')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0994365768235115, 'fraction_numerical': 0.021623267854423634, 'mean_word_length': 3.7663280116110305, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '91615', 'license': 'MIT', 'n_tokens_mistral': 2082, 'n_tokens_neox': 1886, 'n_words': 638, 'path': 'example-analysis-cgm.ipynb', 'repo_name': 'lblogan14/rbcodes', 'size': '6587'}