py/prospect/utilities.py
def load_spectra_zcat_from_targets(targetids, datadir, targetdb, dirtree_type=None, with_redrock_details=False, with_redrock_version=True):
    """Get spectra, redshift catalog and optional detailed Redrock catalog matched to a set of DESI TARGETIDs.

    This works using a "mini-db" of targetids, as returned by `create_targetdb()`.
    The outputs of this utility can be used directly by `viewer.plotspectra()`, to inspect a given list of targetids.
    Output spectra/catalog(s) are sorted according to the input target list.
    When several spectra are available for a given TARGETID, they are all included in the output, in random order.

    Parameters
    ----------
    targetids : :class:`list` or :class:`numpy.ndarray`
        List of TARGETIDs, must be int64.
    datadir : :class:`string`
        No description provided.
    dirtree_type : :class:`string`
        The directory tree and file names must match the types listed in the notes below.
    targetdb : :class:`dict`
        Content of the "mini-db": { (dataset, subset, petal): [list of TARGETIDs] }, see `create_targetdb()`.
    with_redrock_details : :class:`bool`, optional
        If `True`, detailed Redrock output files (.h5 files) are also read
    with_redrock_version : :class:`bool`, optional
        If `True`, a column 'RRVER' is appended to the output redshift catalog, as given by HDU0 in `redrock`/`zbest` files.
        This is used by `viewer.plotspectra()` to track Redrock version in visual inspection files.

    Returns
    -------
    :func:`tuple`
        If with_redrock_details is `False` (default), returns (spectra, zcat), where spectra is `~desispec.spectra.Spectra`
        and zcat is `~astropy.table.Table`.
        If with_redrock_details is `True`, returns (spectra, zcat, redrockcat) where redrockcat is `~astropy.table.Table`.

    Notes
    -----
    * `dirtree_type` must be one of the following, for "coadd", "redrock"/"zbest" (.fits), and "rrdetails"/"redrock" (.h5) files:
      - ``dirtree_type='healpix'``: ``{datadir}/{survey}/{program}/{pixel//100}/{pixel}/redrock-{survey}-{program}-{pixel}.fits``
      - ``dirtree_type='pernight'``: ``{datadir}/{tileid}/{night}/redrock-{petal}-{tile}-{night}.fits``
      - ``dirtree_type='perexp'``: ``{datadir}/{tileid}/{expid}/redrock-{petal}-{tile}-exp{expid}.fits``
      - ``dirtree_type='cumulative'``: ``{datadir}/{tileid}/{night}/redrock-{petal}-{tile}-thru{night}.fits``
      - To use blanc/cascades 'all' (resp 'deep') coadds, use dirtree_type='pernight' and nights=['all'] (resp 'deep')

    """

    targetids = np.asarray(targetids)
    if targetids.dtype not in ['int64', 'i8', '>i8']:
        raise TypeError('TARGETIDs should be int64')
    spectra = None
    ztables, rrtables = [], []

    for dataset, subset, petal in targetdb.keys():
        targets_subset = set(targetdb[dataset, subset, petal])
        targets_subset = targets_subset.intersection(set(targetids))

        # Load spectra for that tile-subset-petal only if one or more target(s) are in the list
        if len(targets_subset)>0 :
            subset_label = get_subset_label(subset, dirtree_type)
            if dirtree_type == 'healpix':
                nside = 64 # dummy, currently
                the_path = os.path.join(datadir, dataset[0], dataset[1], healpix_subdirectory(nside, int(subset)))
                file_label = '-'.join([dataset[0], dataset[1], subset_label])
            else:
                the_path = os.path.join(datadir, dataset, subset)
                file_label = '-'.join([petal, dataset, subset_label])
            the_spec = desispec.io.read_spectra(os.path.join(the_path, "coadd-"+file_label+".fits"))
            the_spec = the_spec.select(targets=sorted(targets_subset))
            if os.path.isfile(os.path.join(the_path, "redrock-"+file_label+".fits")):
                redrock_is_pre_everest = False
                the_zcat = Table.read(os.path.join(the_path, "redrock-"+file_label+".fits"), 'REDSHIFTS')
            else: # pre-everest Redrock file nomenclature
                redrock_is_pre_everest = True
                the_zcat = Table.read(os.path.join(the_path, "zbest-"+file_label+".fits"), 'ZBEST')
            if hasattr(the_zcat['SUBTYPE'], 'mask'):  # work around Table auto-masking in astropy 5
                blanksubtype = the_zcat['SUBTYPE'].mask
                the_zcat['SUBTYPE'][blanksubtype] = ''
            if with_redrock_version:
                if redrock_is_pre_everest:
                    hdulist = astropy.io.fits.open(os.path.join(the_path, "zbest-"+file_label+".fits"))
                else:
                    hdulist = astropy.io.fits.open(os.path.join(the_path, "redrock-"+file_label+".fits"))
                the_zcat['RRVER'] = hdulist[hdulist.index_of('PRIMARY')].header['RRVER']
            the_zcat = match_catalog_to_spectra(the_zcat, the_spec)
            ztables.append(the_zcat)
            if with_redrock_details:
                if redrock_is_pre_everest:
                    rrfile = os.path.join(the_path, "redrock-"+file_label+".h5")
                else:
                    rrfile = os.path.join(the_path, "rrdetails-"+file_label+".h5")
                the_rrcat = match_rrdetails_to_spectra(rrfile, the_spec, Nfit=None)
                rrtables.append(the_rrcat)
            if spectra is None:
                spectra = the_spec
            else:
                #- Still use update() instead of stack(), to handle case when fibermaps differ in different files.
                spectra.update(the_spec)

    #- Sort according to input target list. Check if all targets were found in spectra
    tids_spectra = spectra.fibermap['TARGETID']
    sorted_indices = []
    for target in targetids:
        w, = np.where(tids_spectra == target)
        sorted_indices.extend(w)
        if len(w)==0:
            print("Warning! TARGETID not found:", target)
    assert(len(tids_spectra)==len(sorted_indices)) # check, should always be true
    spectra = spectra[ sorted_indices ]

    zcat = vstack(ztables)
    zcat = zcat[ sorted_indices ]
    if with_redrock_details:
        rrcat = vstack(rrtables)
        rrcat = rrcat[ sorted_indices ]
        return (spectra, zcat, rrcat)
    else:
        return (spectra, zcat)
Get spectra, redshift catalog and optional detailed Redrock catalog matched to a set of DESI TARGETIDs.

    This works using a "mini-db" of targetids, as returned by `create_targetdb()`.
    The outputs of this utility can be used directly by `viewer.plotspectra()`, to inspect a given list of targetids.
    Output spectra/catalog(s) are sorted according to the input target list.
    When several spectra are available for a given TARGETID, they are all included in the output, in random order.

    Parameters
    ----------
    targetids : :class:`list` or :class:`numpy.ndarray`
        List of TARGETIDs, must be int64.
    datadir : :class:`string`
        No description provided.
    dirtree_type : :class:`string`
        The directory tree and file names must match the types listed in the notes below.
    targetdb : :class:`dict`
        Content of the "mini-db": { (dataset, subset, petal): [list of TARGETIDs] }, see `create_targetdb()`.
    with_redrock_details : :class:`bool`, optional
        If `True`, detailed Redrock output files (.h5 files) are also read
    with_redrock_version : :class:`bool`, optional
        If `True`, a column 'RRVER' is appended to the output redshift catalog, as given by HDU0 in `redrock`/`zbest` files.
        This is used by `viewer.plotspectra()` to track Redrock version in visual inspection files.

    Returns
    -------
    :func:`tuple`
        If with_redrock_details is `False` (default), returns (spectra, zcat), where spectra is `~desispec.spectra.Spectra`
        and zcat is `~astropy.table.Table`.
        If with_redrock_details is `True`, returns (spectra, zcat, redrockcat) where redrockcat is `~astropy.table.Table`.

    Notes
    -----
    * `dirtree_type` must be one of the following, for "coadd", "redrock"/"zbest" (.fits), and "rrdetails"/"redrock" (.h5) files:
      - ``dirtree_type='healpix'``: ``{datadir}/{survey}/{program}/{pixel//100}/{pixel}/redrock-{survey}-{program}-{pixel}.fits``
      - ``dirtree_type='pernight'``: ``{datadir}/{tileid}/{night}/redrock-{petal}-{tile}-{night}.fits``
      - ``dirtree_type='perexp'``: ``{datadir}/{tileid}/{expid}/redrock-{petal}-{tile}-exp{expid}.fits``
      - ``dirtree_type='cumulative'``: ``{datadir}/{tileid}/{night}/redrock-{petal}-{tile}-thru{night}.fits``
      - To use blanc/cascades 'all' (resp 'deep') coadds, use dirtree_type='pernight' and nights=['all'] (resp 'deep')

    
Get spectra, redshift catalog and optional detailed Redrock catalog matched to a set of DESI TARGETIDs.
This works using a "mini-db" of targetids, as returned by `create_targetdb()`.
The outputs of this utility can be used directly by `viewer.plotspectra()`, to inspect a given list of targetids.
Output spectra/catalog(s) are sorted according to the input target list.
When several spectra are available for a given TARGETID, they are all included in the output, in random order.

Parameters



Returns
Get spectra redshift catalog and optional detailed Redrock catalog matched to a set of DESI TARGETIDs . This works using a " mini - db " of targetids as returned by ` create_targetdb () ` . The outputs of this utility can be used directly by ` viewer . plotspectra () ` to inspect a given list of targetids . Output spectra / catalog ( s ) are sorted according to the input target list . When several spectra are available for a given TARGETID they are all included in the output in random order . Parameters Returns
def load_spectra_zcat_from_targets(targetids, datadir, targetdb, dirtree_type=None, with_redrock_details=False, with_redrock_version=True):
    targetids = np.asarray(targetids)
    if targetids.dtype not in ['int64', 'i8', '>i8']:
        raise TypeError('TARGETIDs should be int64')
    spectra = None
    ztables, rrtables = [], []
    for dataset, subset, petal in targetdb.keys():
        targets_subset = set(targetdb[dataset, subset, petal])
        targets_subset = targets_subset.intersection(set(targetids))
        if len(targets_subset)>0 :
            subset_label = get_subset_label(subset, dirtree_type)
            if dirtree_type == 'healpix':
                nside = 64 
                the_path = os.path.join(datadir, dataset[0], dataset[1], healpix_subdirectory(nside, int(subset)))
                file_label = '-'.join([dataset[0], dataset[1], subset_label])
            else:
                the_path = os.path.join(datadir, dataset, subset)
                file_label = '-'.join([petal, dataset, subset_label])
            the_spec = desispec.io.read_spectra(os.path.join(the_path, "coadd-"+file_label+".fits"))
            the_spec = the_spec.select(targets=sorted(targets_subset))
            if os.path.isfile(os.path.join(the_path, "redrock-"+file_label+".fits")):
                redrock_is_pre_everest = False
                the_zcat = Table.read(os.path.join(the_path, "redrock-"+file_label+".fits"), 'REDSHIFTS')
            else: 
                redrock_is_pre_everest = True
                the_zcat = Table.read(os.path.join(the_path, "zbest-"+file_label+".fits"), 'ZBEST')
            if hasattr(the_zcat['SUBTYPE'], 'mask'):  
                blanksubtype = the_zcat['SUBTYPE'].mask
                the_zcat['SUBTYPE'][blanksubtype] = ''
            if with_redrock_version:
                if redrock_is_pre_everest:
                    hdulist = astropy.io.fits.open(os.path.join(the_path, "zbest-"+file_label+".fits"))
                else:
                    hdulist = astropy.io.fits.open(os.path.join(the_path, "redrock-"+file_label+".fits"))
                the_zcat['RRVER'] = hdulist[hdulist.index_of('PRIMARY')].header['RRVER']
            the_zcat = match_catalog_to_spectra(the_zcat, the_spec)
            ztables.append(the_zcat)
            if with_redrock_details:
                if redrock_is_pre_everest:
                    rrfile = os.path.join(the_path, "redrock-"+file_label+".h5")
                else:
                    rrfile = os.path.join(the_path, "rrdetails-"+file_label+".h5")
                the_rrcat = match_rrdetails_to_spectra(rrfile, the_spec, Nfit=None)
                rrtables.append(the_rrcat)
            if spectra is None:
                spectra = the_spec
            else:
                spectra.update(the_spec)
    tids_spectra = spectra.fibermap['TARGETID']
    sorted_indices = []
    for target in targetids:
        w, = np.where(tids_spectra == target)
        sorted_indices.extend(w)
        if len(w)==0:
            print("Warning! TARGETID not found:", target)
    assert(len(tids_spectra)==len(sorted_indices)) 
    spectra = spectra[ sorted_indices ]
    zcat = vstack(ztables)
    zcat = zcat[ sorted_indices ]
    if with_redrock_details:
        rrcat = vstack(rrtables)
        rrcat = rrcat[ sorted_indices ]
        return (spectra, zcat, rrcat)
    else:
        return (spectra, zcat)
def load_spectra_zcat_from_targets ( targetids , datadir , targetdb , dirtree_type = None , with_redrock_details = False , with_redrock_version = True ) : targetids = np . asarray ( targetids ) if targetids . dtype not in [ 'int64' , 'i8' , '>i8' ] : raise TypeError ( 'TARGETIDs should be int64' ) spectra = None ztables , rrtables = [ ] , [ ] for dataset , subset , petal in targetdb . keys ( ) : targets_subset = set ( targetdb [ dataset , subset , petal ] ) targets_subset = targets_subset . intersection ( set ( targetids ) ) if len ( targets_subset ) > 0 : subset_label = get_subset_label ( subset , dirtree_type ) if dirtree_type == 'healpix' : nside = 64 the_path = os . path . join ( datadir , dataset [ 0 ] , dataset [ 1 ] , healpix_subdirectory ( nside , int ( subset ) ) ) file_label = '-' . join ( [ dataset [ 0 ] , dataset [ 1 ] , subset_label ] ) else : the_path = os . path . join ( datadir , dataset , subset ) file_label = '-' . join ( [ petal , dataset , subset_label ] ) the_spec = desispec . io . read_spectra ( os . path . join ( the_path , "coadd-" + file_label + ".fits" ) ) the_spec = the_spec . select ( targets = sorted ( targets_subset ) ) if os . path . isfile ( os . path . join ( the_path , "redrock-" + file_label + ".fits" ) ) : redrock_is_pre_everest = False the_zcat = Table . read ( os . path . join ( the_path , "redrock-" + file_label + ".fits" ) , 'REDSHIFTS' ) else : redrock_is_pre_everest = True the_zcat = Table . read ( os . path . join ( the_path , "zbest-" + file_label + ".fits" ) , 'ZBEST' ) if hasattr ( the_zcat [ 'SUBTYPE' ] , 'mask' ) : blanksubtype = the_zcat [ 'SUBTYPE' ] . mask the_zcat [ 'SUBTYPE' ] [ blanksubtype ] = '' if with_redrock_version : if redrock_is_pre_everest : hdulist = astropy . io . fits . open ( os . path . join ( the_path , "zbest-" + file_label + ".fits" ) ) else : hdulist = astropy . io . fits . open ( os . path . join ( the_path , "redrock-" + file_label + ".fits" ) ) the_zcat [ 'RRVER' ] = hdulist [ hdulist . index_of ( 'PRIMARY' ) ] . header [ 'RRVER' ] the_zcat = match_catalog_to_spectra ( the_zcat , the_spec ) ztables . append ( the_zcat ) if with_redrock_details : if redrock_is_pre_everest : rrfile = os . path . join ( the_path , "redrock-" + file_label + ".h5" ) else : rrfile = os . path . join ( the_path , "rrdetails-" + file_label + ".h5" ) the_rrcat = match_rrdetails_to_spectra ( rrfile , the_spec , Nfit = None ) rrtables . append ( the_rrcat ) if spectra is None : spectra = the_spec else : spectra . update ( the_spec ) tids_spectra = spectra . fibermap [ 'TARGETID' ] sorted_indices = [ ] for target in targetids : w , = np . where ( tids_spectra == target ) sorted_indices . extend ( w ) if len ( w ) == 0 : print ( "Warning! TARGETID not found:" , target ) assert ( len ( tids_spectra ) == len ( sorted_indices ) ) spectra = spectra [ sorted_indices ] zcat = vstack ( ztables ) zcat = zcat [ sorted_indices ] if with_redrock_details : rrcat = vstack ( rrtables ) rrcat = rrcat [ sorted_indices ] return ( spectra , zcat , rrcat ) else : return ( spectra , zcat )
Get spectra, redshift catalog and optional detailed Redrock catalog matched to a set of DESI TARGETIDs.
Get spectra redshift catalog and optional detailed Redrock catalog matched to a set of DESI TARGETIDs .
"""Get spectra, redshift catalog and optional detailed Redrock catalog matched to a set of DESI TARGETIDs.

    This works using a "mini-db" of targetids, as returned by `create_targetdb()`.
    The outputs of this utility can be used directly by `viewer.plotspectra()`, to inspect a given list of targetids.
    Output spectra/catalog(s) are sorted according to the input target list.
    When several spectra are available for a given TARGETID, they are all included in the output, in random order.

    Parameters
    ----------
    targetids : :class:`list` or :class:`numpy.ndarray`
        List of TARGETIDs, must be int64.
    datadir : :class:`string`
        No description provided.
    dirtree_type : :class:`string`
        The directory tree and file names must match the types listed in the notes below.
    targetdb : :class:`dict`
        Content of the "mini-db": { (dataset, subset, petal): [list of TARGETIDs] }, see `create_targetdb()`.
    with_redrock_details : :class:`bool`, optional
        If `True`, detailed Redrock output files (.h5 files) are also read
    with_redrock_version : :class:`bool`, optional
        If `True`, a column 'RRVER' is appended to the output redshift catalog, as given by HDU0 in `redrock`/`zbest` files.
        This is used by `viewer.plotspectra()` to track Redrock version in visual inspection files.

    Returns
    -------
    :func:`tuple`
        If with_redrock_details is `False` (default), returns (spectra, zcat), where spectra is `~desispec.spectra.Spectra`
        and zcat is `~astropy.table.Table`.
        If with_redrock_details is `True`, returns (spectra, zcat, redrockcat) where redrockcat is `~astropy.table.Table`.

    Notes
    -----
    * `dirtree_type` must be one of the following, for "coadd", "redrock"/"zbest" (.fits), and "rrdetails"/"redrock" (.h5) files:
      - ``dirtree_type='healpix'``: ``{datadir}/{survey}/{program}/{pixel//100}/{pixel}/redrock-{survey}-{program}-{pixel}.fits``
      - ``dirtree_type='pernight'``: ``{datadir}/{tileid}/{night}/redrock-{petal}-{tile}-{night}.fits``
      - ``dirtree_type='perexp'``: ``{datadir}/{tileid}/{expid}/redrock-{petal}-{tile}-exp{expid}.fits``
      - ``dirtree_type='cumulative'``: ``{datadir}/{tileid}/{night}/redrock-{petal}-{tile}-thru{night}.fits``
      - To use blanc/cascades 'all' (resp 'deep') coadds, use dirtree_type='pernight' and nights=['all'] (resp 'deep')

    """ # Load spectra for that tile-subset-petal only if one or more target(s) are in the list # dummy, currently # pre-everest Redrock file nomenclature # work around Table auto-masking in astropy 5 #- Still use update() instead of stack(), to handle case when fibermaps differ in different files. #- Sort according to input target list. Check if all targets were found in spectra # check, should always be true
targetids
datadir
targetdb
dirtree_type
with_redrock_details
with_redrock_version
func
`tuple`
If with_redrock_details is `False` (default), returns (spectra, zcat), where spectra is `~desispec.spectra.Spectra`
and zcat is `~astropy.table.Table`.
If with_redrock_details is `True`, returns (spectra, zcat, redrockcat) where redrockcat is `~astropy.table.Table`.

Notes

`dirtree_type` must be one of the following, for "coadd", "redrock"/"zbest" (.fits), and "rrdetails"/"redrock" (.h5) files:
``dirtree_type='healpix'``: ``{datadir}/{survey}/{program}/{pixel//100}/{pixel}/redrock-{survey}-{program}-{pixel}.fits``
``dirtree_type='pernight'``: ``{datadir}/{tileid}/{night}/redrock-{petal}-{tile}-{night}.fits``
``dirtree_type='perexp'``: ``{datadir}/{tileid}/{expid}/redrock-{petal}-{tile}-exp{expid}.fits``
``dirtree_type='cumulative'``: ``{datadir}/{tileid}/{night}/redrock-{petal}-{tile}-thru{night}.fits``
To use blanc/cascades 'all' (resp 'deep') coadds, use dirtree_type='pernight' and nights=['all'] (resp 'deep')
` tuple ` If with_redrock_details is ` False ` ( default ) returns ( spectra zcat ) where spectra is ` ~desispec . spectra . Spectra ` and zcat is ` ~astropy . table . Table ` . If with_redrock_details is ` True ` returns ( spectra zcat redrockcat ) where redrockcat is ` ~astropy . table . Table ` . Notes ` dirtree_type ` must be one of the following for " coadd " " redrock " / " zbest " ( . fits ) and " rrdetails " / " redrock " ( . h5 ) files : ` ` dirtree_type = ' healpix ' ` ` : ` ` { datadir } / { survey } / { program } / { pixel // 100 } / { pixel } / redrock - { survey } - { program } - { pixel } . fits ` ` ` ` dirtree_type = ' pernight ' ` ` : ` ` { datadir } / { tileid } / { night } / redrock - { petal } - { tile } - { night } . fits ` ` ` ` dirtree_type = ' perexp ' ` ` : ` ` { datadir } / { tileid } / { expid } / redrock - { petal } - { tile } - exp { expid } . fits ` ` ` ` dirtree_type = ' cumulative ' ` ` : ` ` { datadir } / { tileid } / { night } / redrock - { petal } - { tile } - thru { night } . fits ` ` To use blanc / cascades ' all ' ( resp ' deep ' ) coadds use dirtree_type = ' pernight ' and nights = [ ' all ' ] ( resp ' deep ' )