%matplotlib inline
from marvin import config
config.switchSasUrl('local')
config.forceDbOff()

from marvin.tools.cube import Cube
plateifu='8485-1901'
cube = Cube(plateifu=plateifu)
print(cube)
maps = cube.getMaps(bintype='HYB10')
print(maps)

"""
Explanation: What's New in Marvin 2.2!
Lots of things are new in Marvin 2.2.0.  See the list with links to individual sections here http://sdss-marvin.readthedocs.io/en/latest/whats-new.html
Marvin now includes MPL-6 data
End of explanation
"""


from marvin.tools.maps import Maps
maps = Maps(plateifu)

# or a filename
maps = Maps('/Users/Brian/Work/Manga/analysis/v2_3_1/2.1.3/SPX-GAU-MILESHC/8485/1901/manga-8485-1901-MAPS-SPX-GAU-MILESHC.fits.gz')
print(maps)

"""
Explanation: Smarter handling of inputs
You can still specify plateifu, mangaid, or filename but now Marvin will try to guess your input type if you do not specify an input keyword argument.
End of explanation
"""


# grab an H-alpha flux map
ha = maps['emline_gflux_ha_6564']

# fuzzy name indexing
ha = maps['gflux_ha']

# all map properties are available as class attributes.  If using iPython, you can tab complete to see them all.
ha = maps.emline_gflux_ha_6564

"""
Explanation: Fuzzy indexing and extraction
Marvin now includes fuzzy lists and dictionaries in the Maps and Datamodels.  This means Marvin will try to guess what you mean by what you type. For example, all of these methods grab the H-alpha flux map.
End of explanation
"""


# see the datamodel on maps
maps.datamodel

"""
Explanation: New DRP, DAP and Query Datamodels
There are new datamodels representing the MaNGA data for DRP, DAP and Query parameters.  The datamodel is attached to every object you instantiate, or it can be accessed independently. For example, the Maps datamodel will list all the available map properties.  See http://sdss-marvin.readthedocs.io/en/latest/datamodel/datamodels.html for details.
End of explanation
"""


haew_prop = maps.datamodel['emline_gew_ha']
haew_prop

print(haew_prop.name, haew_prop.unit, haew_prop.description)

"""
Explanation: Each Property contains a name, a channel, the unit of the property, and a description
End of explanation
"""


dapdm = maps.datamodel.parent
print(dapdm)

# get a list of all available DAP datamodels
from marvin.utils.datamodel.dap import datamodel
print(datamodel)

# let's get the MPL-6 datamodel
dapdm = datamodel['MPL-6']
print(dapdm)

"""
Explanation: The fulll datamodel is available as a parent attribute or you can import it directly
End of explanation
"""


# the cube datamodel shows the available datacubes
cube.datamodel.datacubes

# and spectra
cube.datamodel.spectra

"""
Explanation: Cubes, Maps, ModelCubes now utilize Quantity-based Objects
Most Marvin Tools now use new objects to represent their data.  DataCubes represent 3-d data, while a Spectrum represents a 1-d array of data.  These sub-class from Astropy Quantities.  This means now most properties have associated units.  We also now track and propagate inverse variances and masks.
End of explanation
"""


print(type(cube.flux))
print('flux', cube.flux)
print('mask', cube.flux.mask)
print('wavelength', cube.flux.wavelength)

"""
Explanation: The cube flux is now a DataCube, has proper units, has an ivar, mask, and wavelength attached to it
End of explanation
"""


spec = cube.flux[:,17,17]
print(type(spec))
print(spec)
print(spec.unit)
spec.plot()

"""
Explanation: Slicing a Datacube in 2-d will return a new DataCube, while slicing in 3-d will return a Spectrum
End of explanation
"""


# H-alpha DAP quality flag
ha.quality_flag

ha.target_flags

ha.pixmask

# bits for mask value 1027
print('bits', ha.pixmask.values_to_bits(1027))
print('labels', ha.pixmask.values_to_labels(1027))

# convert the H-alpha mask into an list of labels
ha.pixmask.labels

"""
Explanation: Maskbits
There is a new Maskbit class for improved maskbit handling.  All objects now include new Maskbit versions of the DRP/DAP quality flag (quality_flag), targeting bits (target_flags), and pixel masks (pixmask).  Now you can easily look up the labels for bits and create custom masks.  See http://sdss-marvin.readthedocs.io/en/latest/utils/maskbit.html for details
End of explanation
"""


from marvin.tools.query import Query
config.setRelease('MPL-4')
q = Query(searchfilter='nsa.z < 0.1', returnparams=['cube.ra', 'cube.dec', 'absmag_g_r', 'nsa.elpetro_ba'])
r = q.run()

# your results are now in Sets
r.results

# see the available columns
r.columns

# quickly plot the redshift vs g-r color
output = r.plot('nsa.z', 'absmag_g_r')

# or a histogram of the elpetro b/a axis ratio
output=r.hist('elpetro_ba')

# get all of the g-r colors as a list
gr = r.getListOf('absmag_g_r', return_all=True)
gr

# the results currently only have 100 out of some total
print(r.count, r.totalcount)

# let's extend our result set by the next chunk of 100
r.extendSet()

print(r.count, r.totalcount)
print(r.results)

"""
Explanation: Improved Query and Results Handling
The handling of Queries and Results has been improved to provider better means of retrieving all the results of a query, extracting columns of parameters, and quickly plotting results.  

See http://sdss-marvin.readthedocs.io/en/latest/query.html for Query handling
See http://sdss-marvin.readthedocs.io/en/latest/results.html for Results handling
See http://sdss-marvin.readthedocs.io/en/latest/datamodel/query_dm.html for how to use the Query Datamodel
See http://sdss-marvin.readthedocs.io/en/latest/utils/plot-scatter.html for quick scatter plotting
See http://sdss-marvin.readthedocs.io/en/latest/utils/plot-hist.html for quick histogram plotting
End of explanation
"""


qdm = q.datamodel
qdm

qdm.groups

# look at all the available NSA parameters
qdm.groups['nsa'].parameters

"""
Explanation: The Query Datamodel shows you every parameter that is available to search on.  It groups parameters together into common types.
End of explanation
"""


