%matplotlib inline

# imports
from astropy import units as u
from astropy.coordinates import SkyCoord

import specdb
from specdb.specdb import SpecDB
from specdb import specdb as spdb_spdb

"""
Explanation: Retrieving Spectra from a Database [v1.0]
End of explanation
"""


db_file = specdb.__path__[0]+'/tests/files/IGMspec_DB_v02_debug.hdf5'
reload(spdb_spdb)
sdb = spdb_spdb.SpecDB(db_file=db_file)

"""
Explanation: Setup
End of explanation
"""


meta = sdb.meta_from_position((0.0019,17.7737), 1*u.arcsec)
meta

"""
Explanation: Spectra from a Meta data Query
Returned spectra in the XSpectrum1D object are aligned to the Table

Perform meta query
End of explanation
"""


spec = sdb.spectra_from_meta(meta)

spec.plot(inline=True)

"""
Explanation: Retrieve spectra
End of explanation
"""


meta = sdb.meta_from_position('001115.23+144601.8', 1*u.arcsec)
spec = sdb.spectra_from_meta(meta)
print('Nspec = {:d}'.format(spec.nspec))
spec

"""
Explanation: Multiple spectra for a single source
End of explanation
"""


spec, meta = sdb.spectra_from_coord('001115.23+144601.8')

meta[['SDSSJ','RA_GROUP','DEC_GROUP','GROUP_ID', 'GROUP', 'IGM_ID', 'DISPERSER']]

"""
Explanation: Spectra for a Single Source
Performs the meta query and retrieves spectra in one call
Default tolerance on position is 0.5 arcseconds

Repeat of multiple spectra request from above
End of explanation
"""


qdict = dict(DISPERSER='R400')
spec, meta = sdb.spectra_from_coord('001115.23+144601.8', query_dict=qdict)
meta[['SDSSJ','RA_GROUP','DEC_GROUP','GROUP_ID', 'GROUP', 'IGM_ID', 'DISPERSER']]

"""
Explanation: One can restrict by group or meta data as in a Meta data Query
Note: The query is performed on both the source catalog and the meta data in each group
End of explanation
"""


spec, meta = sdb.spectra_from_coord((0.0019, 17.7737), tol=10*u.deg)
len(meta)

"""
Explanation: If multiple sources are match to the input coordiantes (e.g. by increasing the tolerance), only the closest is taken
End of explanation
"""


spec, meta = sdb.spectra_from_ID(3244)
meta[['SDSSJ','RA_GROUP','DEC_GROUP','GROUP_ID', 'GROUP', 'IGM_ID', 'DISPERSER']]

"""
Explanation: Using the IDKEY instead
End of explanation
"""


coords = SkyCoord(ra=[0.0028, 0.0019], dec=[14.9747, 17.77374], unit='deg')
spec, meta = sdb.spectra_in_group(coords, 'BOSS_DR12')
spec

"""
Explanation: Spectra from an input set of Coordinates in a group
This is only allowed for one group at a time.
It is risky in that only the first spectrum identified for each source is returned.
And it requires that *all* of the coordinates be found in the group.

Here is a example where each source has only 1 spectrum in the group
End of explanation
"""


coords = SkyCoord(ra=[2.8135,16.5802], dec=[14.7672, 0.8065], unit='deg')
spec, meta = sdb.spectra_in_group(coords, 'GGG')
print("Nspec = {:d}".format(spec.nspec))
meta[['SDSSJ','RA_GROUP','DEC_GROUP','GROUP_ID', 'GROUP', 'IGM_ID', 'DISPERSER']]

"""
Explanation: Here is an example where each source has 2 spectra.
Only the first found is returned. 
In this case, the B600 grating.
End of explanation
"""


qdict = dict(DISPERSER='R400')
spec, meta = sdb.spectra_in_group(coords, 'GGG', query_dict=qdict)
meta[['SDSSJ','RA_GROUP','DEC_GROUP','GROUP_ID', 'GROUP', 'IGM_ID', 'DISPERSER']]

"""
Explanation: Now we request the other grating
End of explanation
"""


