# let's grab the H-alpha emission line flux map
from marvin.tools.maps import Maps
mapfile = '/Users/Brian/Work/Manga/analysis/v2_0_1/2.0.2/SPX-GAU-MILESHC/8485/1901/manga-8485-1901-MAPS-SPX-GAU-MILESHC.fits.gz'
maps = Maps(filename=mapfile)
haflux = maps.getMap('emline_gflux', channel='ha_6564')
print(haflux)

"""
Explanation: Saving and Restoring Marvin objects
With all Marvin Tools, you can save the object you are working with locally to your filesystem, and restore it later on.  This works using the Python pickle package.  The objects are pickled (i.e. formatted and compressed) into a pickle file object. All Marvin Tools, Queries, and Results can be saved and restored.
We can save a map...
End of explanation
"""


haflux.save('my_haflux_map')

"""
Explanation: We can save any Marvin object with the save method.  This methods accepts a string filename+path as the name of the pickled file.  If a full file path is not specified, it defaults to the current directory. save also accepts an overwrite boolean keyword in case you want to overwrite an existing file.
End of explanation
"""


# import the individual Map class
from marvin.tools.quantities import Map

# restore the Halpha flux map into a new variable
filename = '/Users/Brian/Work/github_projects/Marvin/docs/sphinx/jupyter/my_haflux_map'
newflux = Map.restore(filename)
print(newflux)

"""
Explanation: Now we have a saved map.  We can restore it anytime we want using the restore class method.  A class method means you call it from the imported class itself, and not on the instance.  restore accepts a string filename as input and returns the instantianted object.
End of explanation
"""


from marvin.tools.query import Query, Results

# let's make a query
f = 'nsa.z < 0.1'
q = Query(searchfilter=f)
print(q)

# and run it
r = q.run()
print(r)

"""
Explanation: We can also save and restore Marvin Queries and Results.  First let's create and run a simple query...
End of explanation
"""


q.save()
r.save()

"""
Explanation: Let's save both the query and results for later use.  Without specifiying a filename, by default Marvin will name the query or results using your provided search filter.
End of explanation
"""


newquery = Query.restore('/Users/Brian/marvin_query_nsa.z<0.1.mpf')
print('query', newquery)
print('filter', newquery.searchfilter)

myresults = Results.restore('/Users/Brian/marvin_results_nsa.z<0.1.mpf')
print(myresults.results)

"""
Explanation: By default, if you don't specify a filename for the pickled file, Marvin will auto assign one for you with extension .mpf (MaNGA Pickle File).
Now let's restore...
End of explanation
"""


