<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Basics of Marvin
In this notebook, you will learn the common core functionality across many of the Marvin Tools.  This includes the basics of accessing and handling MaNGA data from different locations, as well as a beginners guide of interacting with data via the core tools of Cubes and Maps.  Much of this information can be found in the Getting Started section of the Marvin documentation.  
Table of Contents
Data Access Modes <br>
Interacting with Data Content <br>
Object Datamodels <br>
Extracting Data <br> 
Accessing Associated Objects<br>
Quality and Target Flags<br>
NSA and DAPall Metadata<br>
Downloading Files<br>
Interacting with Spaxels<br>
<a id="common"></a>
Common Core Functionality
Marvin Tools provides the core functionality for accessing MaNGA data with Marvin. At the lowest level they are class wrappers around data products or elements (datacubes, maps, and spaxels). Their purpose is to provide a more natural way to interact with the data, unconstrained by specific data storage architectures such as files or databases, or by data location such as a laptop or remote server.  
All Marvin Tools are built off a set of common core functionalities and features, and thus behave in very similar ways.  There are four main classes provided in Marvin
Step1:  You may see the info message No release version set. Setting default to DR15.  The default data release Marvin uses is DR15. For this tutorial, we will be using DR15 data so there is no need to do anything further.  In general, to change which release Marvin uses, we can use the Marvin config.setRelease method on the config object.
Step2:  <a id="modes"></a>
Data Access Modes
Let's instantiate our first cube, for galaxy with plate-IFU designation 8485-1901.  Each marvin tool takes a string id input and can one of
Step3:  Once instantiated, each marvin tool indicates a mode and a data-origin.  Note that mode="local" and  data_origin="file".  This indicates that we have accessed a file from our local filesystem.  Our cube now has a filename attribute indicating its location on disk.
Step4:  What if we won't have the MaNGa data products for our target of interest?  We can specify the plateifu or mangaid of the target and Marvin will access it remotely.  Let's open a cube for 8485-1902.
Step5:  Now note that mode="remote" and data_origin="api".  This indicates the cube has been remotely loaded using the built-in Marvin API.  By specifying an input plateifu or mangaid, marvin will always first try to access the data locally, and then fall back to remote access.  You can also explicitly load a file using the filename keyword argument.
Step6:  <a id="interaction"></a>
Interacting with Data Content
Regardless of how you load an object, the way we interact with all the tools, and the way they behave, is the same.  They also all provide some quick access to basic metadata, like coordinates, header information, and WCS.
<a id="metadata"></a>
Step7:  <a id="datamodel"></a>
Object Datamodels
Every Marvin object comes with a built-in datamodel, which shows us what extensions are available, how they are names, and what they contain.
Step8:  The datamodel for a cube contains 3 datacube extensions and 2 spectral extensions.  Let's look at them more closely.
Step9:  This tells us that this cube has three associated 3D datacubes, flux, dispersion, and dispersion_prepixel, and two associated spectra, spectral_resolution and spectral_resolution_prepixel, as well as their associated units. The description attribute provides information on what the extension is.
Step10:  The names of each of the datacubes and spectral extensions are available as object attributes and can be used to access the associated data quantity.  Let's access the flux for our current cube.
Step11:  The flux is represented as a 3D array with units. We can also access the inverse variance and the mask using flux.ivar and flux.mask, respectively.  Marvin uses Astropy quantities to represent all internal multidimensional data, e.g. 3-d datacube and 2-d map arrays, or 1-d spectral data.  
<a id="extract"></a>
Extracting data
All marvin arrays can be sliced to extract subsets of data.  Slices of datacubes produce new datacubes.  Slices of spectra produce new spectra.  A single slice from a datacube extracts a spectrum.  Each spectrum can be easily displayed with its plot attribute.
Step12:  <a id="associates"></a>
Accessing Associated Objects
For a given target galaxy, there are many associated MaNGA Data Products from both the reduction and analysis pipelines.  Marvin seamlessly connects these together so you don't have to access them individually.  Let's access the DAP Maps objects associated with our cube.  To do so we use the getMaps method on cube.  All instances have getXXX methods that allow you to access associated data objects for a given target.
Step13:  When accessing associated objects, Marvin also smartly determines whether to open the file locally or remotely grab it over the API.  In our case, the mode is local and data_origin is file.  DAP Maps have different bintypes.  The default binning is HYB10, a hybrid binning scheme to S/N~10.  To load a different bintype, use the bintype keyword argument in the cube.getMaps method.    
A Maps behaves very similarly to a Cube and everything we have discussed above will still work. Instead of datacubes and spectra, a Maps object contains a set of 2D quantities called Map, each one of them representing a different property measured by the DAP. One can get a full list of all the properties available using the datamodel.
Step14:  Note that some properties such as spx_skycoo have multiple channels (in this case the on-sky x and y coordinates). To access the individual property, simply combine the property name and the channel name with an underscore, i.e. spx_skycoo_on_sky_x.  As before, we can get more information about a property using the description attribute.
Step15:  More information about Marvin datamodels can be found here.  See the DR15 Datamodel for a full description of the available datamodels for DR15.  
We can retrieve the map associated to a specific property directly from the Maps instance. For example, let’s get the Hα emission line flux (fitted by a Gaussian).  As Map quantities are similar to DataCube, we can easily plot the 2d map.
Step16:  Note that the plot method returns the matplotlib Figure and Axes for the plot. We can use those to modify or save the plot. Marvin plotting routines try to select the best parameters, colour maps, and dynamic ranges. You can modify those by passing extra arguments to plot. You can learn more in the Map plotting section. We will talk about the Map class in detail in Working with Astropy Quantities and in Map.
Remember that all associated objects are connected together.  They can be accessed either from within an object instance or loaded independently from the class itself.  From within an instance, the core tools, Maps, RSS, and ModelCube are available using the getXXX methods, as well as additional auxillary tools and object data, such as Image.
Step17:  <a id="flags"></a>
Quality and Target Flags and Maskbits
Each MaNGA target has associated quality assurance flags as well targeting flags.  Quality flags indiciate the overall quality of the data and/or any problems with the data discovered during the reduction and analysis pipeline runs.
Step18:  In this case the MANGA_DRP3QUAL maskbit does not have any bit activated, which means the data is safe to use. See the Maskbits section for more information on how to interact with Marvin Maskbits.
The targeting flags highlight the targeting bits set for this galaxy and tell you which sample the target belongs to.
Step19:  Note that in this case the galaxy belongs to the secondary sample from the final target selection (SECONDARY_v1_2_0) as well as to the secondary samples from several commissioning target selections. The galaxy does not have any ancillary bit (manga_target3).  An manga_target3 bit set indicates the target belongs to one of the MaNGA Ancillary programs.  
<a id="mixin"></a>
NSA, DAPall, and Value-Added catalog data
For each target we can also access additional catalog data
Step20:  SDSS members produce Value-Added Catalogs (VACS) of MaNGA products.  These products can be incorporated into Marvin.  Any incorporated VACS in Marvin are contained in the vacs attribute attached to each tool.  More information on VACs in Marvin can be found here.  Currently the MaNGA-HI VAC for DR15 has been contributed into Marvin.  Let's take a look at it.
Step21:  vacs is a container (list) of all names of the vacs contributed into Marvin.  These names are dottable to access to vac data.  These contributed custom products can be of any format chosen by the VAC owner, a Python class, a dictionary, an array, etc.
Step22:  <a id='download'></a>
Downloading files
While Marvin allows you to access data remotely, frequently you will find that you want to download the file associated to an object so that you can access it more quickly in the future. We can do that using the download method attached to all marvin core tools. Let’s try to load a cube that we know we do not have in out hard drive, and download it.
Step23:  Each tool contains a download method to download that individual file.  If you want to download a list of targets in bulk, you can use the downloadList utility function.  This function uses the built-in Python product sdss_access to download files from the Utah SAS.  downloadList accepts as input a list of target ids, e.g. plateifus or mangaids.  The dltype keyword indicates the type of file you wish to download.  The default file is cube.  Let's use the utility function to download a list of DAP MAPS files for three targets.
Step24:  <a id="spaxels"></a>
Interacting with Spaxels
All of the core Marvin Galaxy Tools have the ability to extract one ore more Spaxel objects.  Spaxel objects are special Marvin objects to make it easier to deal with individual spaxels in galaxies and access their properties.  See the Getting Started with Spaxels for more information on how to interact with Spaxels.  We can either use the standard array slicing notation (0-indexed, origin of coordinates in the lower left corner of the array)
Step25:  or we can use getSpaxel method, which accepts multiple arguments. Note that by default, (x, y) coordinates passed to getSpaxel are measured from the center of the array.
<ASSISTANT_TASK:>
Python Code:
from marvin.tools import Cube
Explanation: Basics of Marvin
In this notebook, you will learn the common core functionality across many of the Marvin Tools.  This includes the basics of accessing and handling MaNGA data from different locations, as well as a beginners guide of interacting with data via the core tools of Cubes and Maps.  Much of this information can be found in the Getting Started section of the Marvin documentation.  
Table of Contents
Data Access Modes <br>
Interacting with Data Content <br>
Object Datamodels <br>
Extracting Data <br> 
Accessing Associated Objects<br>
Quality and Target Flags<br>
NSA and DAPall Metadata<br>
Downloading Files<br>
Interacting with Spaxels<br>
<a id="common"></a>
Common Core Functionality
Marvin Tools provides the core functionality for accessing MaNGA data with Marvin. At the lowest level they are class wrappers around data products or elements (datacubes, maps, and spaxels). Their purpose is to provide a more natural way to interact with the data, unconstrained by specific data storage architectures such as files or databases, or by data location such as a laptop or remote server.  
All Marvin Tools are built off a set of common core functionalities and features, and thus behave in very similar ways.  There are four main classes provided in Marvin: the Cube, the RSS, the Maps, and the ModelCube.
We will start by focusing on a Marvin Cube to demonstrate the common features and handling of Marvin Tools.  Everything discussed here also applies to any of the other available Marvin Tools listed above.   
Let's start by importing the Marvin Cube class.
End of explanation
from marvin import config
# set the release to DR15
config.setRelease("DR15")
Explanation: You may see the info message No release version set. Setting default to DR15.  The default data release Marvin uses is DR15. For this tutorial, we will be using DR15 data so there is no need to do anything further.  In general, to change which release Marvin uses, we can use the Marvin config.setRelease method on the config object.
End of explanation
# instantiate a cube for 8485-1901
cube = Cube('8485-1901')
cube
Explanation: <a id="modes"></a>
Data Access Modes
Let's instantiate our first cube, for galaxy with plate-IFU designation 8485-1901.  Each marvin tool takes a string id input and can one of: a string filepath and name, a plate-IFU designation, or a manga-ID designation.  Marvin will attempt to identify the input format and properly load the target data.
End of explanation
# print the local filename
cube.filename
Explanation: Once instantiated, each marvin tool indicates a mode and a data-origin.  Note that mode="local" and  data_origin="file".  This indicates that we have accessed a file from our local filesystem.  Our cube now has a filename attribute indicating its location on disk.
End of explanation
remote_cube = Cube('8485-1902')
remote_cube
Explanation: What if we won't have the MaNGa data products for our target of interest?  We can specify the plateifu or mangaid of the target and Marvin will access it remotely.  Let's open a cube for 8485-1902.
End of explanation
# Explicitly load a cube from a file on disk 
filename = '/Users/Brian/Work/sdss/sas/dr15/manga/spectro/redux/v2_4_3/8485/stack/manga-8485-1901-LOGCUBE.fits.gz'
cube = Cube(filename=filename)
cube
Explanation: Now note that mode="remote" and data_origin="api".  This indicates the cube has been remotely loaded using the built-in Marvin API.  By specifying an input plateifu or mangaid, marvin will always first try to access the data locally, and then fall back to remote access.  You can also explicitly load a file using the filename keyword argument.
End of explanation
print('Target Coordindates:', cube.ra, cube.dec)
print('Header:')
# access the PRIMARY header for the current object
cube.header
# show the WCS for the current object
cube.wcs
Explanation: <a id="interaction"></a>
Interacting with Data Content
Regardless of how you load an object, the way we interact with all the tools, and the way they behave, is the same.  They also all provide some quick access to basic metadata, like coordinates, header information, and WCS.
<a id="metadata"></a>
End of explanation
# look up the cube datmaodel
datamodel = cube.datamodel
datamodel
Explanation: <a id="datamodel"></a>
Object Datamodels
Every Marvin object comes with a built-in datamodel, which shows us what extensions are available, how they are names, and what they contain.
End of explanation
# see what datacubes are available
print('Datacubes:')
datamodel.datacubes
# see what additional spectral extensions are available
print('Spectra:')
datamodel.spectra
Explanation: The datamodel for a cube contains 3 datacube extensions and 2 spectral extensions.  Let's look at them more closely.
End of explanation
print('Flux description:', datamodel.datacubes.flux.description)
print('Spectral Resolution description:', datamodel.spectra.spectral_resolution.description)
Explanation: This tells us that this cube has three associated 3D datacubes, flux, dispersion, and dispersion_prepixel, and two associated spectra, spectral_resolution and spectral_resolution_prepixel, as well as their associated units. The description attribute provides information on what the extension is.
End of explanation
flux = cube.flux
flux
Explanation: The names of each of the datacubes and spectral extensions are available as object attributes and can be used to access the associated data quantity.  Let's access the flux for our current cube.
End of explanation
# create a small datacube subset in a 10x10 pixel region around the center of the target
central_pixel = int(cube.flux.shape[1]/2)
lo = central_pixel - 5
hi = central_pixel + 5
subset = flux[:, lo:hi, lo:hi]
subset
# extract a single spectrum and display it
spectrum = flux[:, 16, 17]
spectrum
spectrum.plot(show_std=True)
Explanation: The flux is represented as a 3D array with units. We can also access the inverse variance and the mask using flux.ivar and flux.mask, respectively.  Marvin uses Astropy quantities to represent all internal multidimensional data, e.g. 3-d datacube and 2-d map arrays, or 1-d spectral data.  
<a id="extract"></a>
Extracting data
All marvin arrays can be sliced to extract subsets of data.  Slices of datacubes produce new datacubes.  Slices of spectra produce new spectra.  A single slice from a datacube extracts a spectrum.  Each spectrum can be easily displayed with its plot attribute.
End of explanation
# access the Maps object from our cube
maps = cube.getMaps()
maps
Explanation: <a id="associates"></a>
Accessing Associated Objects
For a given target galaxy, there are many associated MaNGA Data Products from both the reduction and analysis pipelines.  Marvin seamlessly connects these together so you don't have to access them individually.  Let's access the DAP Maps objects associated with our cube.  To do so we use the getMaps method on cube.  All instances have getXXX methods that allow you to access associated data objects for a given target.
End of explanation
# look at the datamodel for the maps
maps.datamodel
Explanation: When accessing associated objects, Marvin also smartly determines whether to open the file locally or remotely grab it over the API.  In our case, the mode is local and data_origin is file.  DAP Maps have different bintypes.  The default binning is HYB10, a hybrid binning scheme to S/N~10.  To load a different bintype, use the bintype keyword argument in the cube.getMaps method.    
A Maps behaves very similarly to a Cube and everything we have discussed above will still work. Instead of datacubes and spectra, a Maps object contains a set of 2D quantities called Map, each one of them representing a different property measured by the DAP. One can get a full list of all the properties available using the datamodel.
End of explanation
print('spx_skycoo_on_sky_x: ', maps.datamodel.spx_skycoo_on_sky_x.description)
Explanation: Note that some properties such as spx_skycoo have multiple channels (in this case the on-sky x and y coordinates). To access the individual property, simply combine the property name and the channel name with an underscore, i.e. spx_skycoo_on_sky_x.  As before, we can get more information about a property using the description attribute.
End of explanation
# grab the map of H-alpha emission line flux
ha = maps.emline_gflux_ha_6564
ha
# plot the H-alpha map
fig, ax = ha.plot()
Explanation: More information about Marvin datamodels can be found here.  See the DR15 Datamodel for a full description of the available datamodels for DR15.  
We can retrieve the map associated to a specific property directly from the Maps instance. For example, let’s get the Hα emission line flux (fitted by a Gaussian).  As Map quantities are similar to DataCube, we can easily plot the 2d map.
End of explanation
# load the same Maps object by itself
from marvin.tools import Maps
my_maps = Maps('8485-1901')
my_maps
# get the stellar velocity map
stvel = my_maps.stellar_velocity
# load an auxilliary tool, the gri color image of the target galaxy using the Marvin Image tool
# get the Image of the galaxy and plot it
image = cube.getImage()
image.plot()
Explanation: Note that the plot method returns the matplotlib Figure and Axes for the plot. We can use those to modify or save the plot. Marvin plotting routines try to select the best parameters, colour maps, and dynamic ranges. You can modify those by passing extra arguments to plot. You can learn more in the Map plotting section. We will talk about the Map class in detail in Working with Astropy Quantities and in Map.
Remember that all associated objects are connected together.  They can be accessed either from within an object instance or loaded independently from the class itself.  From within an instance, the core tools, Maps, RSS, and ModelCube are available using the getXXX methods, as well as additional auxillary tools and object data, such as Image.
End of explanation
# look at the overall cube quality flags
cube.quality_flag
Explanation: <a id="flags"></a>
Quality and Target Flags and Maskbits
Each MaNGA target has associated quality assurance flags as well targeting flags.  Quality flags indiciate the overall quality of the data and/or any problems with the data discovered during the reduction and analysis pipeline runs.
End of explanation
# look at the targeting flags for this galaxy
cube.target_flags
Explanation: In this case the MANGA_DRP3QUAL maskbit does not have any bit activated, which means the data is safe to use. See the Maskbits section for more information on how to interact with Marvin Maskbits.
The targeting flags highlight the targeting bits set for this galaxy and tell you which sample the target belongs to.
End of explanation
# look at the NSA properties for this galaxy from the cube instance
nsa = cube.nsa
nsa
# look at the DAP all properties from the maps instance
maps.dapall
Explanation: Note that in this case the galaxy belongs to the secondary sample from the final target selection (SECONDARY_v1_2_0) as well as to the secondary samples from several commissioning target selections. The galaxy does not have any ancillary bit (manga_target3).  An manga_target3 bit set indicates the target belongs to one of the MaNGA Ancillary programs.  
<a id="mixin"></a>
NSA, DAPall, and Value-Added catalog data
For each target we can also access additional catalog data: the associated parameters from the NASA Sloan Atlas, and the DAPall file.  The NSA catalog contains measured global galaxy photometric and shape profile information, while the DAPall catalog contains aggregrate statistics as measured by the MaNGA DAP pipeline.  NSA properties are available on all Marvin objects under the nsa attribute.  DAPall properties are available on all Marvin Maps and Modelcube objects.
End of explanation
# access the available VACs for this object
vacs = cube.vacs
vacs
Explanation: SDSS members produce Value-Added Catalogs (VACS) of MaNGA products.  These products can be incorporated into Marvin.  Any incorporated VACS in Marvin are contained in the vacs attribute attached to each tool.  More information on VACs in Marvin can be found here.  Currently the MaNGA-HI VAC for DR15 has been contributed into Marvin.  Let's take a look at it.
End of explanation
# access the mangahi vac
hi = vacs.mangahi
print(hi)
print('data type:', type(hi))
# access any HI data available for 8485-1901
hi.data
Explanation: vacs is a container (list) of all names of the vacs contributed into Marvin.  These names are dottable to access to vac data.  These contributed custom products can be of any format chosen by the VAC owner, a Python class, a dictionary, an array, etc.
End of explanation
# Check our remote cube we loaded earlier 
remote_cube
# download the remote cube
# remote_cube.download()
# loading the cube a second time loads it from the new file
new_cube = Cube('8485-1902')
new_cube
Explanation: <a id='download'></a>
Downloading files
While Marvin allows you to access data remotely, frequently you will find that you want to download the file associated to an object so that you can access it more quickly in the future. We can do that using the download method attached to all marvin core tools. Let’s try to load a cube that we know we do not have in out hard drive, and download it.
End of explanation
from marvin.utils.general import downloadList
# create a list of target ids
plateifus = ['8485-1901', '8485-1902', '7443-1901']
# download the DAP 2d MAPS files for these targets
# dllist = downloadList(plateifus, dltype='maps')
Explanation: Each tool contains a download method to download that individual file.  If you want to download a list of targets in bulk, you can use the downloadList utility function.  This function uses the built-in Python product sdss_access to download files from the Utah SAS.  downloadList accepts as input a list of target ids, e.g. plateifus or mangaids.  The dltype keyword indicates the type of file you wish to download.  The default file is cube.  Let's use the utility function to download a list of DAP MAPS files for three targets.
End of explanation
# extract the spaxel at array index x=10, y=15
spaxel = cube[15, 10]
spaxel
Explanation: <a id="spaxels"></a>
Interacting with Spaxels
All of the core Marvin Galaxy Tools have the ability to extract one ore more Spaxel objects.  Spaxel objects are special Marvin objects to make it easier to deal with individual spaxels in galaxies and access their properties.  See the Getting Started with Spaxels for more information on how to interact with Spaxels.  We can either use the standard array slicing notation (0-indexed, origin of coordinates in the lower left corner of the array)
End of explanation
# get the spaxel at the center of the target galaxy
central_spaxel = cube.getSpaxel(x=0, y=0)
central_spaxel
# we can plot the spectrum of the spaxel
spaxel.flux.plot()
Explanation: or we can use getSpaxel method, which accepts multiple arguments. Note that by default, (x, y) coordinates passed to getSpaxel are measured from the center of the array.
End of explanation
<END_TASK>