<blockquote>
<p>I was wondering why the absorbtion lines of the template are broader
then those of the galaxy, since it actually should be the other way
arround.</p>
</blockquote>
<p>You are correct that it should be the other way around. The reason the plot looks confusing is that you are not actually plotting the galaxy spectrum in the top panel; you are plotting some combination of noise and lack of any signal.</p>
<p>What has happened is that by defining the galaxy spectrum as <code>data_gal[0]</code>, you have extracted the first (bottom) row from the 2D image, which is far away from the actual galaxy light. I would suggest displaying <code>ngc_4697_major_axis.fits</code> in a FITS image display program (such as <a href="https://ds9.si.edu" rel="nofollow noreferrer">SAOimage DS9</a>). The spectrum corresponding to the center of the galaxy will be the brightest line running down the middle of the image. (The axes of the image are wavelength in the one direction and distance along the slit in the perpendicular direction.)</p>
<p>Here's an example from the <a href="http://www.gemini.edu/instrumentation/gmos/capability" rel="nofollow noreferrer">Gemini GMOS instrument webpage</a>. You can see the bright zone running across the middle of the image; this is the center of the galaxy, which gets fainter as you go up or down (i.e., along the slit in either direction):
<a href="https://i.sstatic.net/azhdO.gif" rel="nofollow noreferrer"><img src="https://i.sstatic.net/azhdO.gif" alt="enter image description here" /></a></p>
<p>Figure out what row or rows that corresponds to, and extract them via <code>flux_gal = data_gal[n_row,:]</code> (for a single row) or <code>flux_gal = np.mean(data_gal[n_row1:n_row2,:], 0)</code> to get the mean of rows n_row1 through (n_row2 - 1).</p>
<p>(Remember that Python and Numpy treat image coordinates as [row_number, column_number] = [y, x], where x and y correspond to the normal coordinates when the image is displayed in SAOimage DS9.)</p>
<p>To extract spectra at different radial distances from the galaxy center, do the same thing, but chose rows (or ranges of rows) above or below the row corresponding to the galaxy center. As you get further away from the center, the S/N will get worse, so you will probably want to start summing or averaging over multiple rows.</p>
<blockquote>
<p>Do I have to shift my galaxy spectra for all radii to the
rest-wavelength frame, or can I somehow do it all in advance, since
the redshift for all radii should be the same.</p>
</blockquote>
<p>In general, you do not want to shift the galaxy spectrum (unless perhaps the FCQ algorithm requires it). The redshift of the individual spectra is one of the things you are trying to measure, after all.</p>
<p>The redshift for all radii will almost certainly <em>not</em> be the same, since the redshift at any given radius is the sum of the galaxy's redshift (Hubble flow + peculiar velocity of the galaxy) <em>and</em> the mean rotation velocity of the stars <em>at that radius</em>. Some galaxies (&quot;slow rotators&quot;) may have almost no rotation, but NGC 4697 is a &quot;fast rotator&quot;, and I believe the rotation velocity will reach <span class="math-container">$\sim \pm 100$</span> km/s at a radius of 10 arcsec away from the galaxy nucleus along the major axis.</p>

CC BY-SA 4.0
2021-07-05T05:52:48.663Z
I am  very grateful for the kind support, thank you. So I can take a spectrum at each row from the center. Is it correct that, when I would use the spectral information of row 598, the distance from the center would now respond to 0.2 as in radii ? Since the CRDELT2 keyword has value 0.2 ?  Assume I want to calculate the distance from the center of the galaxy from row 450. Would the radius R correspond to: R=CRVAL2+(450-1)*CDELT2 ?
trynerror
CC BY-SA 4.0
2021-07-05T16:43:25.893Z
@trynerror 1. I *guess* CDELT=0.2 means the spacing along the slit is 0.2 arcsec/pixel, though it would be nice if the CTYPE* keywords had values rather than being blank. I'm not entirely sure you should rely blindly on the CRVAL2 keyword; it's probably safer to ignore it and determine the central (row) coordinate yourself. Then the radius from the center of row Y would be (Y - Y_cen) * CDELT2.
Peter Erwin
CC BY-SA 4.0
2021-07-05T16:44:38.667Z
If you can track down the original paper this data came from, it might tell you what the spacing along the slit in arc seconds was.
Peter Erwin
CC BY-SA 4.0
2021-07-06T06:56:58.363Z
yes, I was told that CDELT=0.2 means 0.2 as/pixel.  I am actually a bit unsure how to determine the exact "central" row, since it is not as easy to distinguish between different brightness levels in the bright central region of the spectra.
trynerror
CC BY-SA 4.0
2021-07-06T06:58:22.097Z
I'd determine the central line by finding the maximum (e.g. fitting a Gaussian) to the intensity profile of the line totals.
planetmaker
CC BY-SA 4.0
2021-07-04T16:36:12.823Z
2021-07-06T13:31:56.757Z
2021-07-06T13:31:56.757Z
Peter Erwin
<p>I am trying to extract the kinematics from the elliptical galaxy NGC 4697 using the Fourier Correlation Quotient (FCQ) algorithm described by Bender (<a href="http://adsabs.harvard.edu/full/1990A%26A...229..441B" rel="nofollow noreferrer">http://adsabs.harvard.edu/full/1990A%26A...229..441B</a>). I am working with the stellar spectrum of the K3-III star hd132345 as template and the galaxy spectra along the major axis of NGC 4697. Both spectra were taken by a long slit spectrograph. I have implemented a first version of the FCQ algorithm, my current problem is that I am not completely certain how to extract  the galaxy spectra for different radii (I am new to working with astrophysical spectra in fits format). Underneath I will show my first lines of code for the data acquisition.</p>
<pre><code>file_temp = dir + '/hd132345.fits'              # template spectra: star hd132345 (K3-IIICN 2 B)
file_gal = dir + '/ngc_4697_major_axis.fits'    # galaxy spectra along major axis from ngc 4697`   
hdu_temp = fits.open(file_temp)
hdu_gal = fits.open(file_gal)

hdr_temp = hdu_temp[0].header
hdr_gal = hdu_gal[0].header
data_gal = hdu_gal[0].data
data_temp = hdu_temp[0].data

# extract wavelength array and flux
flux_gal = data_gal[0]
flux_temp = data_temp
w_gal = WCS(hdr_gal, naxis=1, relax=False, fix=False)
loglam_gal = w_gal.wcs_pix2world(np.arange(len(flux_gal)), 0)[0]
w_temp = WCS(hdr_temp, naxis=1, relax=False, fix=False)
loglam_temp = w_temp.wcs_pix2world(np.arange(len(flux_temp)), 0)[0]`
</code></pre>
<p>Plotted flux_gal over loglam_gal and flux_temp over loglam_temp looks as follows (spectra are rebinned in loglam). The redshift has not been removed yet. I was wondering why the absorbtion lines of the template are broader then those of the galaxy, since it actually should be the other way arround.</p>
<p><a href="https://i.sstatic.net/gyZA2.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/gyZA2.png" alt="enter image description here" /></a></p>
<p>The header of my galaxy fits file:</p>
<p><a href="https://i.sstatic.net/yqHYD.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/yqHYD.png" alt="enter image description here" /></a></p>
<p>I understand that the keywords CRVAL1 and CRVAL2 describe the initial values for ln lambda in A and the radius in arcseconds. While CDELT1 and CDELT2 describe the increments ln lambda and the radius. So I should have a spectrum for my galaxy every 0.2 arcseconds. I am unsure how to extract those from my input fits files and would be very happy about a respond. Do I have to shift my galaxy spectra for all radii to the rest-wavelength frame, or can I somehow do it all in advance, since the redshift for all radii should be the same. I would be pleased about any tips or comments on this issue.</p>
<hr />
<p><strong>Thanks</strong> to Peter Erwin's <a href="https://astronomy.stackexchange.com/questions/44725/how-to-extract-galaxy-spectra-for-different-radii-in-python-for-spectra-taken#comment95208_44725">comment</a>, the results now look as follows.
The entire 2D-image spectral image of NGC-4697 along the major axis (displayed with SAOImage):</p>
<p><a href="https://i.sstatic.net/EboXJ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/EboXJ.png" alt="enter image description here" /></a></p>
<p>The spectrum of the galaxy center taken at row 597. (flux_gal_center = data_gal(597,:)):</p>
<p><a href="https://i.sstatic.net/I6gaJ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/I6gaJ.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2021-07-06T13:29:08.673Z
A quick comment on the (added) spectral image: if you look carefully at it, you can see the galaxy rotation. The vertical dark streaks are the absorption lines, which above the galaxy-center row are shifted to the left (= blueshift relative to galaxy center) and below are shifted to the right (= redshift relative to galaxy center). Which is kind of cool.
Peter Erwin
<observational-astronomy><galaxy><data-analysis><spectra><python>
How to extract galaxy spectra for different radii in Python for spectra taken by long slit spectrograph?
trynerror
# How to extract galaxy spectra for different radii in Python for spectra taken by long slit spectrograph?

(Asked by: username_0 on 2021-07-04)

I am trying to extract the kinematics from the elliptical galaxy NGC 4697 using the Fourier Correlation Quotient (FCQ) algorithm described by Bender (<http://adsabs.harvard.edu/full/1990A%26A...229..441B>). I am working with the stellar spectrum of the K3-III star hd132345 as template and the galaxy spectra along the major axis of NGC 4697. Both spectra were taken by a long slit spectrograph. I have implemented a first version of the FCQ algorithm, my current problem is that I am not completely certain how to extract the galaxy spectra for different radii (I am new to working with astrophysical spectra in fits format). Underneath I will show my first lines of code for the data acquisition.

```
file_temp = dir + '/hd132345.fits'              # template spectra: star hd132345 (K3-IIICN 2 B)
file_gal = dir + '/ngc_4697_major_axis.fits'    # galaxy spectra along major axis from ngc 4697`   
hdu_temp = fits.open(file_temp)
hdu_gal = fits.open(file_gal)

hdr_temp = hdu_temp[0].header
hdr_gal = hdu_gal[0].header
data_gal = hdu_gal[0].data
data_temp = hdu_temp[0].data

# extract wavelength array and flux
flux_gal = data_gal[0]
flux_temp = data_temp
w_gal = WCS(hdr_gal, naxis=1, relax=False, fix=False)
loglam_gal = w_gal.wcs_pix2world(np.arange(len(flux_gal)), 0)[0]
w_temp = WCS(hdr_temp, naxis=1, relax=False, fix=False)
loglam_temp = w_temp.wcs_pix2world(np.arange(len(flux_temp)), 0)[0]`

```

Plotted flux\_gal over loglam\_gal and flux\_temp over loglam\_temp looks as follows (spectra are rebinned in loglam). The redshift has not been removed yet. I was wondering why the absorbtion lines of the template are broader then those of the galaxy, since it actually should be the other way arround.

[![enter image description here](https://i.sstatic.net/gyZA2.png)](https://i.sstatic.net/gyZA2.png)

The header of my galaxy fits file:

[![enter image description here](https://i.sstatic.net/yqHYD.png)](https://i.sstatic.net/yqHYD.png)

I understand that the keywords CRVAL1 and CRVAL2 describe the initial values for ln lambda in A and the radius in arcseconds. While CDELT1 and CDELT2 describe the increments ln lambda and the radius. So I should have a spectrum for my galaxy every 0.2 arcseconds. I am unsure how to extract those from my input fits files and would be very happy about a respond. Do I have to shift my galaxy spectra for all radii to the rest-wavelength frame, or can I somehow do it all in advance, since the redshift for all radii should be the same. I would be pleased about any tips or comments on this issue.

---

**Thanks** to username_1 username_1's [comment](https://astronomy.stackexchange.com/questions/44725/how-to-extract-galaxy-spectra-for-different-radii-in-python-for-spectra-taken#comment95208_44725), the results now look as follows.
The entire 2D-image spectral image of NGC-4697 along the major axis (displayed with SAOImage):

[![enter image description here](https://i.sstatic.net/EboXJ.png)](https://i.sstatic.net/EboXJ.png)

The spectrum of the galaxy center taken at row 597. (flux\_gal\_center = data\_gal(597,:)):

[![enter image description here](https://i.sstatic.net/I6gaJ.png)](https://i.sstatic.net/I6gaJ.png)

**Tags:** observational-astronomy, galaxy, data-analysis, spectra, python

> **Comments:**
> **username_1**: A quick comment on the (added) spectral image: if you look carefully at it, you can see the galaxy rotation. The vertical dark streaks are the absorption lines, which above the galaxy-center row are shifted to the left (= blueshift relative to galaxy center) and below are shifted to the right (= redshift relative to galaxy center). Which is kind of cool.  

---

## 1 answers

### Answer by username_1 on 2021-07-04. Score: 6 | Accepted answer

> I was wondering why the absorbtion lines of the template are broader
> then those of the galaxy, since it actually should be the other way
> arround.

You are correct that it should be the other way around. The reason the plot looks confusing is that you are not actually plotting the galaxy spectrum in the top panel; you are plotting some combination of noise and lack of any signal.

What has happened is that by defining the galaxy spectrum as `data_gal[0]`, you have extracted the first (bottom) row from the 2D image, which is far away from the actual galaxy light. I would suggest displaying `ngc_4697_major_axis.fits` in a FITS image display program (such as [SAOimage DS9](https://ds9.si.edu)). The spectrum corresponding to the center of the galaxy will be the brightest line running down the middle of the image. (The axes of the image are wavelength in the one direction and distance along the slit in the perpendicular direction.)

Here's an example from the [Gemini GMOS instrument webpage](http://www.gemini.edu/instrumentation/gmos/capability). You can see the bright zone running across the middle of the image; this is the center of the galaxy, which gets fainter as you go up or down (i.e., along the slit in either direction):
[![enter image description here](https://i.sstatic.net/azhdO.gif)](https://i.sstatic.net/azhdO.gif)

Figure out what row or rows that corresponds to, and extract them via `flux_gal = data_gal[n_row,:]` (for a single row) or `flux_gal = np.mean(data_gal[n_row1:n_row2,:], 0)` to get the mean of rows n\_row1 through (n\_row2 - 1).

(Remember that Python and Numpy treat image coordinates as [row\_number, column\_number] = [y, x], where x and y correspond to the normal coordinates when the image is displayed in SAOimage DS9.)

To extract spectra at different radial distances from the galaxy center, do the same thing, but chose rows (or ranges of rows) above or below the row corresponding to the galaxy center. As you get further away from the center, the S/N will get worse, so you will probably want to start summing or averaging over multiple rows.

> Do I have to shift my galaxy spectra for all radii to the
> rest-wavelength frame, or can I somehow do it all in advance, since
> the redshift for all radii should be the same.

In general, you do not want to shift the galaxy spectrum (unless perhaps the FCQ algorithm requires it). The redshift of the individual spectra is one of the things you are trying to measure, after all.

The redshift for all radii will almost certainly *not* be the same, since the redshift at any given radius is the sum of the galaxy's redshift (Hubble flow + peculiar velocity of the galaxy) *and* the mean rotation velocity of the stars *at that radius*. Some galaxies ("slow rotators") may have almost no rotation, but NGC 4697 is a "fast rotator", and I believe the rotation velocity will reach $\sim \pm 100$ km/s at a radius of 10 arcsec away from the galaxy nucleus along the major axis.

> **Comments:**
> **username_0**: I am  very grateful for the kind support, thank you. So I can take a spectrum at each row from the center. Is it correct that, when I would use the spectral information of row 598, the distance from the center would now respond to 0.2 as in radii ? Since the CRDELT2 keyword has value 0.2 ?  Assume I want to calculate the distance from the center of the galaxy from row 450. Would the radius R correspond to: R=CRVAL2+(450-1)*CDELT2 ?  
> **username_1**: @username_0 1. I *guess* CDELT=0.2 means the spacing along the slit is 0.2 arcsec/pixel, though it would be nice if the CTYPE* keywords had values rather than being blank. I'm not entirely sure you should rely blindly on the CRVAL2 keyword; it's probably safer to ignore it and determine the central (row) coordinate yourself. Then the radius from the center of row Y would be (Y - Y_cen) * CDELT2.  
> **username_1**: If you can track down the original paper this data came from, it might tell you what the spacing along the slit in arc seconds was.  
> **username_0**: yes, I was told that CDELT=0.2 means 0.2 as/pixel.  I am actually a bit unsure how to determine the exact "central" row, since it is not as easy to distinguish between different brightness levels in the bright central region of the spectra.  
> **username_2**: I'd determine the central line by finding the maximum (e.g. fitting a Gaussian) to the intensity profile of the line totals.  

---

