from __future__ import print_function, division, absolute_import
import matplotlib.pyplot as plt
%matplotlib inline

"""
Explanation: First Steps
Now that you have installed Marvin, it's time to take your first steps.  If you want to learn more about how Marvin works, then go see General Info to learn about Marvin Modes, Versions, or Downloading.  If you just want to play, then read on.
First let's run some boilerplate code for Python 2/3 compatibility and plotting in the notebook:
End of explanation
"""


import marvin

"""
Explanation: Now, let’s import Marvin:
End of explanation
"""


marvin.config.release

"""
Explanation: Let's see what release we're using.  Releases can be either MPLs (e.g. MPL-5) or DRs (e.g. DR13), however DRs are currently disabled in Marvin.
End of explanation
"""


from marvin import config
config.setRelease('MPL-3')

print('MPL:', config.release)

"""
Explanation: On intial import, Marvin will set the default data release to use the latest MPL available, currently MPL-5. You can change the version of MaNGA data using the Marvin Config.
End of explanation
"""


config.setMPL('MPL-5')
print('MPL:', config.release)

"""
Explanation: You can also specifically change releases using the setMPL method as well:
End of explanation
"""


config.setRelease('MPL-4')

# check designated version
config.release

"""
Explanation: But let's work with MPL-4:
End of explanation
"""


from marvin.tools.cube import Cube

"""
Explanation: My First Cube
Now let’s play with a Marvin Cube!
Import the Marvin-Tools Cube class:
End of explanation
"""


#----- EDIT THIS CELL -----#

# filename = '/Users/Brian/Work/Manga/redux/v1_5_1/8485/stack/manga-8485-1901-LOGCUBE.fits.gz'
filename = 'path/to/manga/cube/manga-8485-1901-LOGCUBE.fits.gz'

filename = '/Users/andrews/manga/spectro/redux/v1_5_1/8485/stack/manga-8485-1901-LOGCUBE.fits.gz'
filename = '/Users/Brian/Work/Manga/redux/v1_5_1/8485/stack/manga-8485-1901-LOGCUBE.fits.gz'

"""
Explanation: Let's load a cube from a local file. Start by specifying the full path and name of the file, such as:
/Users/Brian/Work/Manga/redux/v1_5_1/8485/stack/manga-8485-1901-LOGCUBE.fits.gz
EDIT Next Cell
End of explanation
"""


cc = Cube(filename=filename)

"""
Explanation: Create a Cube object:
End of explanation
"""


print(cc)

"""
Explanation: Now we have a Cube object:
End of explanation
"""


cc.ra, cc.dec, cc.header['SRVYMODE']

"""
Explanation: How about we look at some meta-data
End of explanation
"""


cc.targetbit

cc.qualitybit

"""
Explanation: ...and the quality and target bits
End of explanation
"""


spax = cc[10,10]

# print the spaxel to see the x,y coord from the lower left, and the coords relative to the cube center, x_cen/y_cen
spax

"""
Explanation: Get a Spaxel
Cubes have several functions currently available: getSpaxel, getMaps, getAperture.  Let's look at spaxels. We can retrieve spaxels from a cube easily via indexing. In this manner, spaxels are 0-indexed from the lower left corner. Let's get spaxel (x=10, y=10):
End of explanation
"""


# let's grab the central spaxel
spax = cc.getSpaxel(x=0, y=0)
spax

spax.spectrum.wavelength

spax.spectrum.flux

"""
Explanation: Spaxels have a spectrum associated with it.  It has the wavelengths and fluxes of each spectral channel:
Alternatively grab a spaxel with getSpaxel.  Use the xyorig keyword to set the coordinate origin point: 'lower' or 'center'.  The default is "center"
End of explanation
"""


# turn on interactive plotting
%matplotlib notebook

spax.spectrum.plot()

"""
Explanation: Plot the spectrum!
End of explanation
"""


# To save the plot, we need to draw it in the same cell as the save command.
spax.spectrum.plot()

import os
plt.savefig(os.getenv('HOME') + '/Downloads/my-first-spectrum.png')

# NOTE - if you are using the latest version of iPython and Jupyter notebooks, then interactive matplotlib plots 
# should be enabled.  You can save the figure with the save icon in the interactive toolbar.

"""
Explanation: Save plot to Downloads directory:
End of explanation
"""


