Example #1
0
def wrap_redrock(info, mp=1, prefix='lenssource'):
    """Simple wrapper on redrock given a simulation table."""

    from redrock.external.desi import rrdesi

    os.environ['OMP_NUM_THREADS'] = '1'

    nchunk = len(set(info['CHUNK']))
    zbestfiles = []

    for ichunk in set(info['CHUNK']):
        specfile = '{}-spectra-chunk{:03d}.fits'.format(prefix, ichunk)
        zbestfile = specfile.replace('-spectra-', '-zbest-')
        rrfile = zbestfile.replace('.fits', '.h5')
        if os.path.isfile(rrfile):
            os.remove(rrfile)
        zbestfiles.append(zbestfile)
        print('Writing redshifts for chunk {}/{} to {}'.format(ichunk, nchunk-1, zbestfile))

        rrdesi(options=['--zbest', zbestfile, '--mp', str(mp), '--output', rrfile, specfile])

    # Gather up all the results and write out a summary table.    
    zbest = Table(np.hstack([fitsio.read(zbestfile) for zbestfile in zbestfiles]))
    zbest = zbest[np.argsort(zbest['TARGETID'])]

    infofile = '{}-zbest.fits'.format(prefix)
    print('Writing {} redshifts to {}'.format(len(zbest), infofile))
    zbest.write(infofile, overwrite=True)

    return zbest
Example #2
0
    def run(self, grph, task, opts, comm=None):
        """
        Run Redrock on a spectral group.

        Args:
            grph (dict): pruned graph with this task and dependencies.
            task (str): the name of this task.
            opts (dict): options to use for this task.
            comm (mpi4py.MPI.Comm): optional MPI communicator.
        """
        nproc = 1
        rank = 0
        if comm is not None:
            nproc = comm.size
            rank = comm.rank

        log = get_logger()

        node = grph[task]

        spectra = "spectra-{}-{}".format(node["nside"], node["pixel"])
        specfile = graph_path(spectra)

        outfile = graph_path(task)
        outdir = os.path.dirname(outfile)
        details = io.findfile('redrock',
                              nside=node["nside"],
                              groupname=node["pixel"],
                              outdir=outdir)

        options = {}
        options["output"] = details
        options["zbest"] = outfile
        options.update(opts)
        optarray = option_list(options)
        optarray.append(specfile)

        # write out the equivalent commandline
        if rank == 0:
            com = ["RUN", "rrdesi_mpi"]
            com.extend(optarray)
            log.info(" ".join(com))

        failcount = 0
        try:
            rrdesi(options=optarray, comm=comm)
        except:
            failcount += 1
            sys.stdout.flush()

        if comm is not None:
            failcount = comm.allreduce(failcount)

        if failcount > 0:
            # all processes throw
            raise RuntimeError("some redshifts failed for task "
                               "{}".format(task))

        return
Example #3
0
    def run(self, grph, task, opts, comm=None):
        """
        Run Redrock on a spectral group.

        Args:
            grph (dict): pruned graph with this task and dependencies.
            task (str): the name of this task.
            opts (dict): options to use for this task.
            comm (mpi4py.MPI.Comm): optional MPI communicator.
        """
        nproc = 1
        rank = 0
        if comm is not None:
            nproc = comm.size
            rank = comm.rank

        log = get_logger()

        node = grph[task]

        spectra = "spectra-{}-{}".format(node["nside"], node["pixel"])
        specfile = graph_path(spectra)

        outfile = graph_path(task)
        outdir = os.path.dirname(outfile)
        details = io.findfile('redrock',
            nside=node["nside"], groupname=node["pixel"], outdir=outdir)

        options = {}
        options["output"] = details
        options["zbest"] = outfile
        options.update(opts)
        optarray = option_list(options)
        optarray.append(specfile)

        # write out the equivalent commandline
        if rank == 0:
            com = ["RUN", "rrdesi_mpi"]
            com.extend(optarray)
            log.info(" ".join(com))

        failcount = 0
        try:
            rrdesi(options=optarray, comm=comm)
        except:
            failcount += 1
            sys.stdout.flush()

        if comm is not None:
            failcount = comm.allreduce(failcount)

        if failcount > 0:
            # all processes throw
            raise RuntimeError("some redshifts failed for task "
                "{}".format(task))

        return
Example #4
0
def bgs_redshifts(sim, simdir, overwrite=False):
    """Fit for the redshifts.

    """
    from redrock.external.desi import rrdesi

    for ii in range(sim['nsim']):
        zbestfile = os.path.join(
            simdir, sim['suffix'],
            'bgs-{}-{:03}-zbest.fits'.format(sim['suffix'], ii))
        spectrafile = os.path.join(
            simdir, sim['suffix'],
            'bgs-{}-{:03}.fits'.format(sim['suffix'], ii))

        if overwrite or not os.path.isfile(zbestfile):
            rrdesi(options=[
                '--zbest', zbestfile, '--mp',
                str(nproc), spectrafile
            ])
        else:
            print('File {} exists...skipping.'.format(zbestfile))
Example #5
0
"""
Run redrock by hand for DC17a (2% sprint)
"""

from __future__ import absolute_import, division, print_function
import sys, os, glob
import numpy as np
from redrock.external import desi

#- run from /scratch2/scratchdirs/sjbailey/desi/dc17a/spectro/redux/dc17a

bricks = list()
for brickfile in glob.glob('bricks/*/brick-b-*.fits'):
    #- extract brickname from /path/to/{brickname}/brick-b-{brickname}.fits
    bricks.append(brickfile[-13:-5])

todo = list()
for brickname in sorted(bricks):
    rrout = 'redrock/rr-{}.h5'.format(brickname)
    zbout = 'redrock/zbest-{}.fits'.format(brickname)
    if os.path.exists(rrout) and os.path.exists(zbout):
        print('---- skipping completed brick {}'.format(brickname))
    else:
        print('---- '+brickname)
        cmd = 'rrdesi -o {} --zbest {}'.format(rrout, zbout)
        for brickfile in glob.glob('bricks/{}/brick-*.fits'.format(brickname)):
            cmd = cmd + ' ' + brickfile
        #print(cmd)
        desi.rrdesi(cmd.split()[1:])
        
Example #6
0
 def _run(self, name, opts, comm, db):
     """See BaseTask.run.
     """
     optlist = self._option_list(name, opts)
     rrdesi(options=optlist, comm=comm)
     return
Example #7
0
 def _run(self, name, opts, comm, db):
     """See BaseTask.run.
     """        
     optlist = self._option_list(name, opts)
     rrdesi(options=optlist, comm=comm)
     return
Example #8
0
from desispec.pipeline import load_prod

import optparse

parser = optparse.OptionParser(usage="%prog [options]")
parser.add_option("--hang", action="store_true", help="make it hang")
opts, args = parser.parse_args()

grph = None
if comm.rank == 0:
    grph = load_prod(nightstr='20190829', spectrographs=None)
    # graph_db_check(grph)

if opts.hang:
    #- This command doesn't hang, but it causes later commands to hang
    grph = comm.bcast(grph, root=0)

#--- DEBUG ---
cmd1 = "rrdesi --zbest /scratch2/scratchdirs/sjbailey/desi/dc17a/spectro/redux/dc17a-test/bricks/0001m067/zbest-0001m067.fits --output /scratch2/scratchdirs/sjbailey/desi/dc17a/spectro/redux/dc17a-test/bricks/0001m067/rr-0001m067.h5 /scratch2/scratchdirs/sjbailey/desi/dc17a/spectro/redux/dc17a-test/bricks/0001m067/brick-b-0001m067.fits /scratch2/scratchdirs/sjbailey/desi/dc17a/spectro/redux/dc17a-test/bricks/0001m067/brick-r-0001m067.fits /scratch2/scratchdirs/sjbailey/desi/dc17a/spectro/redux/dc17a-test/bricks/0001m067/brick-z-0001m067.fits --ncpu 24 --ntargets 4"
cmd2 = "rrdesi --output /scratch2/scratchdirs/sjbailey/desi/dc17a/spectro/redux/dc17a-test/bricks/2766p445/rr-2766p445.h5 --zbest /scratch2/scratchdirs/sjbailey/desi/dc17a/spectro/redux/dc17a-test/bricks/2766p445/zbest-2766p445.fits /scratch2/scratchdirs/sjbailey/desi/dc17a/spectro/redux/dc17a-test/bricks/2766p445/brick-b-2766p445.fits /scratch2/scratchdirs/sjbailey/desi/dc17a/spectro/redux/dc17a-test/bricks/2766p445/brick-r-2766p445.fits /scratch2/scratchdirs/sjbailey/desi/dc17a/spectro/redux/dc17a-test/bricks/2766p445/brick-z-2766p445.fits --ncpu 24 --ntargets 4"
cmds = [cmd1, cmd2]
from redrock.external.desi import rrdesi
rrdesi(cmds[comm.rank].split()[1:])
print('Rank {} done'.format(comm.rank))
sys.stdout.flush()
comm.barrier()
#--- DEBUG ---

# grph = comm.bcast(grph, root=0)