<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Investigating Gas and Stellar Kinematics using Marvin
We're going to study a galaxy (8711-6102) with an inner gas disk that is rotating faster than the stellar disk.
Step1:  1. Get the Halpha velocity and stellar velocity maps
Step2:  2. Create a map of the difference between the Halpha and stellar velocities
Hint
Step3:  3. Plot the difference map using the Marvin Maps plot function
Step4:  Bonus
Step5:  4. Stack spectra in inner gas disk
Step6:  5. Plot stacked spectra from red and blue sides using Marvin Spectrum plot functionality
Hint
<ASSISTANT_TASK:>
Python Code:
import copy
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-darkgrid')
%matplotlib inline
Explanation: Investigating Gas and Stellar Kinematics using Marvin
We're going to study a galaxy (8711-6102) with an inner gas disk that is rotating faster than the stellar disk.
End of explanation
# For Marvin team
# from marvin import config
# config.forceDbOff()
from marvin.tools.maps import Maps
maps = Maps(plateifu='8711-6102')
havel = maps['emline_gvel_ha_6564']
stvel = maps['stellar_vel']
Explanation: 1. Get the Halpha velocity and stellar velocity maps
End of explanation
diff = copy.deepcopy(stvel)
diff.value = havel.value - stvel.value
diff.ivar = 1. / ((1. / havel.ivar) + (1. / stvel.ivar))
diff.mask = havel.mask | stvel.mask
diff.property_name = 'Ha vel - stellar vel'
Explanation: 2. Create a map of the difference between the Halpha and stellar velocities
Hint: copy one of the maps and modify the value, mask, and property_name attributes.
End of explanation
diff.plot()
Explanation: 3. Plot the difference map using the Marvin Maps plot function
End of explanation
import marvin.utils.plot.map as mapplot
fig, axes = plt.subplots(1, 3, figsize=(15, 4))
for ax, map_ in zip(axes, [havel, stvel, diff]):
    mapplot.plot(dapmap=map_, fig=fig, ax=ax)
Explanation: Bonus: plot Halpha velocity, stellar velocity, and difference maps
End of explanation
def stack_spectra(ind):
    # get a list of spaxels by passing a list of x-indices and a list of y-indices
    spaxels = maps.getSpaxel(x=ind[0], y=ind[1], xyorig='lower')
    # copy the first spaxel so that the stack is a Marvin Spaxel object
    stack = copy.deepcopy(spaxels[0])
    
    # overwrite the flux with the mean flux of the spectra in the stack (but keep the other meta-data)
    stack.spectrum.flux = np.mean([sp.spectrum.flux for sp in spaxels], axis=0)
    return stack
# Stack spectra with (Halpha vel - stellar vel) between -70 and -40
ind_neg = np.where((diff.value > -70) & (diff.value < -40))
stack_neg = stack_spectra(ind_neg)
# Stack spectra with (Halpha vel - stellar vel) between 40 and 70
ind_pos = np.where((diff.value > 40) & (diff.value < 70))
stack_pos = stack_spectra(ind_pos)
Explanation: 4. Stack spectra in inner gas disk
End of explanation
# adjust continuum normalization for ease of by-eye comparison
stack_pos_adjusted = copy.deepcopy(stack_pos)
stack_pos_adjusted.spectrum.flux /= 2
fig = plt.figure(figsize=(15, 4))
ax, fig = stack_pos_adjusted.spectrum.plot(figure=fig, return_figure=True)
ax, fig = stack_neg.spectrum.plot(figure=fig, return_figure=True)
# zoom in on calcium H and K lines
fig = plt.figure(figsize=(15, 4))
ax, fig = stack_pos_adjusted.spectrum.plot(figure=fig, return_figure=True)
ax, fig = stack_neg.spectrum.plot(figure=fig, return_figure=True)
ax.set_xlim([4050, 4400])
ax.set_ylim([0., 0.12])
# zoom in on Halpha line
fig = plt.figure(figsize=(15, 4))
ax, fig = stack_pos_adjusted.spectrum.plot(figure=fig, return_figure=True)
ax, fig = stack_neg.spectrum.plot(figure=fig, return_figure=True)
ax.set_xlim([6850, 7200])
ax.set_ylim([0.13, 0.27])
Explanation: 5. Plot stacked spectra from red and blue sides using Marvin Spectrum plot functionality
Hint: divide the Halpha spectrum flux by 2 to get similar normalizations
End of explanation
<END_TASK>