<jupyter_start><jupyter_text>Marvin Workshop (Shanghai 2016)This Jupyter notebook will guide you through the installation of Marvin and will give you a hint of its capabilities. But enough talk, let's begin by installing Marvin. For that, run`pip install sdss-marvin`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](https://api.sdss.org/doc/manga/marvin/general.html) 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:<jupyter_code>from __future__ import print_function, division, absolute_import
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Now, let’s import Marvin:<jupyter_code>import marvin<jupyter_output>INFO: No release version set. Setting default to MPL-5<jupyter_text>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.<jupyter_code>marvin.config.release<jupyter_output><empty_output><jupyter_text>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](https://api.sdss.org/doc/manga/marvin/api/general.htmlmarvin-config-class).<jupyter_code>from marvin import config
config.setRelease('MPL-4')

print('MPL:', config.release)<jupyter_output>MPL: MPL-4<jupyter_text>In general, we recommend using `MPL-5` unless you have already stated a science project using `MPL-4`. So, let's go back to `MPL-5`<jupyter_code>config.setMPL('MPL-5')
print('MPL:', config.release)<jupyter_output>MPL: MPL-5<jupyter_text>My First CubeNow let’s play with a Marvin Cube!Import the Marvin-Tools Cube class:<jupyter_code>from marvin.tools.cube import Cube<jupyter_output><empty_output><jupyter_text>Let's load a cube from a local file. Start by specifying the full path and name of the file, such as:`./manga-8485-1901-LOGCUBE.fits.gz`**EDIT Next Cell if necessary**<jupyter_code>#----- EDIT THIS CELL -----#

# Point filename to the location of the Cube you want to load
filename = './manga-8485-1901-LOGCUBE.fits.gz'
drpall = './drpall-v2_0_1.fits'<jupyter_output><empty_output><jupyter_text>Create a Cube object:<jupyter_code>cc = Cube(filename=filename, drpall=drpall)<jupyter_output>WARNING: local mode failed. Trying remote now.<jupyter_text>Now we have a Cube object:<jupyter_code>print(cc)<jupyter_output><Marvin Cube (plateifu='8485-1901', mode='local', data_origin='file')><jupyter_text>How about we look at some meta-data<jupyter_code>cc.ra, cc.dec, cc.header['SRVYMODE']<jupyter_output><empty_output><jupyter_text>...and the quality and target bits<jupyter_code>cc.targetbit
cc.qualitybit<jupyter_output><empty_output><jupyter_text>Get a SpaxelCubes 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):<jupyter_code>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<jupyter_output><empty_output><jupyter_text>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"<jupyter_code># let's grab the central spaxel
spax = cc.getSpaxel(x=0, y=0)
spax
spax.spectrum.wavelength
spax.spectrum.flux<jupyter_output><empty_output><jupyter_text>Plot the spectrum!<jupyter_code># turn on interactive plotting
%matplotlib notebook
spax.spectrum.plot()<jupyter_output><empty_output><jupyter_text>Save plot to Downloads directory:<jupyter_code># 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.<jupyter_output><empty_output><jupyter_text>By default, the spaxel object contains the DAP properties for that spaxel in the unbinned MAPS.<jupyter_code>spax.properties
# Gets the ha_alpha flux and the ivar
ha_flux = spax.properties['emline_gflux_ha_6564']
print(ha_flux.value, ha_flux.ivar, ha_flux.mask)<jupyter_output>31.4754199982 47.0896835327 0<jupyter_text>Marvin MapsMarvin Maps is how you deal with the DAP MAPS FITS files easily.  You can retrieve maps in several ways.  Let's take a took.   From a Marvin MapsMarvin Maps takes the same inputs as cube: filename, plateifu, or mangaid.  It also accepts keywords **bintype** and **template_kin**.  These uniquely define a DAP MAPS file.  By default, Marvin will load a MAPS file of **bintype=SPX** and **template_kin=GAU-MILESHC** for MPL-5.  For MPL-4, the defaults are **bintype=NONE**, and **template_kin=MIUSCAT-THIN**.<jupyter_code># import the maps
from marvin.tools.maps import Maps

# Load a MPL-5 map
mapfile = './manga-8485-1901-MAPS-SPX-GAU-MILESHC.fits.gz'
# Let's get a default map of

maps = Maps(filename=mapfile)
print(maps)<jupyter_output><Marvin Maps (plateifu='8485-1901', mode='local', data_origin='file', bintype=SPX,template_kin=GAU-MILESHC)><jupyter_text>Once you have a maps object, you can access the raw maps file and header and extensions via **maps.header** and **maps.data**.  Alternatively, you can access individual maps using the **getMap** method.  **getMap** works by specifying a parameter and a channel.  The parameter and channels names are equivalent to those found in the MAPS FITS extensions and headers, albeit lowercased.<jupyter_code># Let's grab the H-alpha flux emission line map
haflux = maps.getMap('emline_gflux', channel='ha_6564')
print(haflux)
# turn on interactive plotting
%matplotlib notebook

# let's plot it
haflux.plot()
# You can get the flux and ivar arrays
haflux.value, haflux.mask<jupyter_output><empty_output><jupyter_text>From the maps object, we can also easily plot the ratio between two maps, e.g. emission-line ratios, using the **getMapRatio** method.  Map ratios are Map objects the same as any other, so you can access their array values or plot them<jupyter_code># Let's look at the NII-to-Halpha emission-line ratio map
niiha = maps.getMapRatio('emline_gflux', 'nii_6585', 'ha_6564')
print(niiha)
niiha.plot()<jupyter_output><Marvin Map (plateifu='8485-1901', property='emline_gflux', channel='nii_6585/ha_6564')><jupyter_text>From a Marvin Cube Once we have a cube, we can get its maps using the **getMaps** method.  getMaps is just a wrapper to the Marvin Maps Tool.  Once we have the maps, we can do all the same things as before.<jupyter_code>maps = cc.getMaps()
print(maps)<jupyter_output><empty_output><jupyter_text>Note that the cube was opened from remote!  Marvin: now with ModelCubes MPL-5 introduced ***LOGCUBE*** files, which contain the fitted spectra for each spaxel. LOGCUBES are called `ModelCube` in Marvin, and they are instantiated in the same way as a `Cube` or a `Maps`. Let's see it.<jupyter_code>from marvin.tools.modelcube import ModelCube
# For the sake of variety, let's open this ModelCube remotely. For that, simply use the plate-ifu of the target.
model_cube = ModelCube(plateifu='8485-1901', bintype='VOR10')
print(model_cube)<jupyter_output><Marvin ModelCube (plateifu='8485-1901', mode='remote', data_origin='api', bintype=u'VOR10', template_kin=u'GAU-MILESHC')><jupyter_text>**IMPORTANT:** the remote mode rocks!Let's see a bit more about the `model_cube`. We can get a spaxel, which will include the properties and the cube spectrum.<jupyter_code>sp = model_cube.getSpaxel(x=0, y=0)
print(sp.cube)
print(sp.maps)
print(sp.modelcube)
print('Spaxel bintype is:', sp.bintype)<jupyter_output><Marvin Cube (plateifu='8485-1901', mode='remote', data_origin='api')>
<Marvin Maps (plateifu='8485-1901', mode='remote', data_origin='api', bintype=SPX,template_kin=GAU-MILESHC)>
<Marvin ModelCube (plateifu='8485-1901', mode='remote', data_origin='api', bintype=u'SPX', template_kin=u'GAU-MILESHC')>
Spaxel bintype is: SPX<jupyter_text>**VERY IMPORTANT:** although the original ModelCube was binned (VOR10) an Spaxel is always unbinned (SPX)!!! We can get the model spectrum for this spaxel. And plot it.<jupyter_code>cube_spectrum = sp.spectrum
model_spectrum = sp.model
ax = cube_spectrum.plot(xlim=[6500, 9000])
ax.plot(model_spectrum.wavelength, model_spectrum.flux, 'r')
print(model_spectrum.mask)<jupyter_output><empty_output><jupyter_text>Saving the spaxel Starting in Marvin Beta, you can pickle all the objects and restore them later. Let's assume you want to save that last spaxel. You simply do.<jupyter_code>sp.save('such_a_great_spaxel.mpf')<jupyter_output><empty_output><jupyter_text>And then you can restore by doing<jupyter_code>from marvin.tools.spaxel import Spaxel
restored_spaxel = Spaxel.restore('such_a_great_spaxel.mpf')
print(restored_spaxel)<jupyter_output>WARNING: The WCS transformation has more axes (3) than the image it is associated with
         (0)
<Marvin Spaxel (x=17, y=17; x_cen=0, y_cen=0><jupyter_text>It works for all the objects!  Downlading dataWe have seen that the remote mode allows you to access all the Manga data without worrying where it actually lives. However, once you have located the data you want to use for your science you probably will want to download it for faster/airplane access. That easy to do with Marvin. Let's start by creating a maps from remote.<jupyter_code>from marvin.tools.maps import Maps
remote_maps = Maps(plateifu='8485-1902', mode='remote')
print(remote_maps)<jupyter_output><empty_output><jupyter_text>Now let's download it!<jupyter_code>filename = remote_maps.download()
print(filename)<jupyter_output><empty_output><jupyter_text>Binned data For many science purposes, you will want to use binned data. The DAP produces MAPS and LOGCUBES with different types of binning schemas and kinnematic templates. Refer to the DAP documentation for more information.Marvin makes very easy to access binned data without needing to know much about the gory details of the data model. We have seen already how to get a binned Maps. Now, let's see how to get a Bin and access its spaxels.<jupyter_code>from marvin.tools.bin import Bin
bin_100 = Bin(binid=100, plateifu='8485-1901', bintype='VOR10')
print(bin_100)<jupyter_output><Marvin Bin (binid=100, n_spaxels=2, bintype=VOR10, template_kin=GAU-MILESHC)><jupyter_text>You can access all the properties and model data for the bin as you do with an spaxel.<jupyter_code>print(bin_100.properties['emline_gflux_ha_6564'])
bin_100.model.plot()<jupyter_output><empty_output><jupyter_text>But you can also access the **(unbinned)** spaxels for that bin.<jupyter_code>print(bin_100.spaxels)<jupyter_output>[<Marvin Spaxel (x=18, y=22, loaded=False), <Marvin Spaxel (x=18, y=23, loaded=False)]<jupyter_text>You will note that the spaxels are not loaded. That means that the spectra and properties have not yet been retrieved for each spaxel. That is call lazy loading and we do it to improve loading time. You can then load any (or all) of the spaxels doing<jupyter_code>bin_100.spaxels[0].load()
print(bin_100.spaxels)
bin_100.spaxels[0].bintype
bin_100.spaxels[0].properties['emline_gflux_ha_6564']<jupyter_output>[<Marvin Spaxel (x=18, y=22; x_cen=1, y_cen=5>, <Marvin Spaxel (x=18, y=23, loaded=False)]
{'fraction_non_alphanumeric': 0.09860710039069136, 'fraction_numerical': 0.023781212841854936, 'mean_word_length': 6.223926380368098, 'pattern_counts': {'":': 0, '<': 124, '<?xml version=': 0, '>': 121, 'https://': 2, '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': '31', 'dir': 'jupyter-structured-clean-dedup', 'id': '417354', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 4266, 'n_tokens_neox': 3911, 'n_words': 1289, 'path': 'docs/sphinx/jupyter/Shanghai_Demo_Tools.ipynb', 'repo_name': 'karenlmasters/marvin', 'size': '11774'}