예제 #1
0
파일: test_qa.py 프로젝트: sdss/lvmspec
 def _write_flat_files(self):
     # Frames
     fb0 = self._make_frame(camera='b0', flavor='flat', nspec=10, objtype='FLAT')
     _ = write_frame(self.frame_b0, fb0)
     fb1 = self._make_frame(camera='b1', flavor='flat', nspec=10, objtype='FLAT')
     _ = write_frame(self.frame_b1, fb1)
     # Fiberflats
     ff0 = get_fiberflat_from_frame(fb0)
     write_fiberflat(self.fflat_b0, ff0)
     ff1 = get_fiberflat_from_frame(fb1)
     write_fiberflat(self.fflat_b1, ff1)
예제 #2
0
    def run_pa(self,
               input_image,
               psf,
               specmin,
               nspec,
               wave,
               regularize=None,
               ndecorr=True,
               bundlesize=25,
               wavesize=50,
               outfile=None,
               fibers=None,
               fibermap=None):
        import specter
        from specter.extract import ex2d
        from lvmspec.frame import Frame as fr

        flux, ivar, Rdata = ex2d(input_image.pix,
                                 input_image.ivar * (input_image.mask == 0),
                                 psf,
                                 specmin,
                                 nspec,
                                 wave,
                                 regularize=regularize,
                                 ndecorr=ndecorr,
                                 bundlesize=bundlesize,
                                 wavesize=wavesize)

        #- Augment input image header for output
        input_image.meta['NSPEC'] = (nspec, 'Number of spectra')
        input_image.meta['WAVEMIN'] = (wave[0], 'First wavelength [Angstroms]')
        input_image.meta['WAVEMAX'] = (wave[-1], 'Last wavelength [Angstroms]')
        input_image.meta['WAVESTEP'] = (wave[1] - wave[0],
                                        'Wavelength step size [Angstroms]')
        input_image.meta['SPECTER'] = (specter.__version__,
                                       'https://github.com/desihub/specter')
        #input_image.meta['IN_PSF']  = (_trim(psf_file), 'Input spectral PSF')
        #input_image.meta['IN_IMG']  = (_trim(input_file), 'Input image')

        frame = fr(wave,
                   flux,
                   ivar,
                   resolution_data=Rdata,
                   fibers=fibers,
                   meta=input_image.meta,
                   fibermap=fibermap)

        if outfile is not None:  #- writing to a frame file if needed.
            from lvmspec import io
            io.write_frame(outfile, frame)
            log.debug("wrote frame output file  %s" % outfile)

        return frame
예제 #3
0
    def run_pa(self, input_frame, skymodel, dumpfile=None):
        from lvmspec.quicklook.quicksky import subtract_sky
        sframe = subtract_sky(input_frame, skymodel)

        if dumpfile is not None:
            from lvmspec import io
            night = sframe.meta['NIGHT']
            expid = sframe.meta['EXPID']
            io.write_frame(dumpfile, sframe)
            log.debug("Wrote intermediate file %s after %s" %
                      (dumpfile, self.name))

        return (sframe, skymodel)
예제 #4
0
    def run_pa(self, input_frame, fiberflat, dumpfile=None):

        from lvmspec.quicklook.quickfiberflat import apply_fiberflat
        fframe = apply_fiberflat(input_frame, fiberflat)

        if dumpfile is not None:
            from lvmspec import io
            night = fframe.meta['NIGHT']
            expid = fframe.meta['EXPID']
            io.write_frame(dumpfile, fframe)
            log.debug("Wrote intermediate file %s after %s" %
                      (dumpfile, self.name))

        return fframe
예제 #5
0
    def test_main(self):
        """
        Test the main program.
        """
        # generate the frame data
        wave, flux, ivar, mask = _get_data()
        nspec, nwave = flux.shape

        #- Setup data for a Resolution matrix
        sigma = 4.0
        ndiag = 11
        xx = np.linspace(-(ndiag - 1) / 2.0, +(ndiag - 1) / 2.0, ndiag)
        Rdata = np.zeros((nspec, ndiag, nwave))
        kernel = np.exp(-xx**2 / (2 * sigma))
        kernel /= sum(kernel)
        for i in range(nspec):
            for j in range(nwave):
                Rdata[i, :, j] = kernel

        #- Convolve the data with the resolution matrix
        convflux = np.empty_like(flux)
        for i in range(nspec):
            convflux[i] = Resolution(Rdata[i]).dot(flux[i])

        # create a fake fibermap
        fibermap = io.empty_fibermap(nspec, nwave)
        for i in range(0, nspec):
            fibermap['OBJTYPE'][i] = 'FAKE'
        io.write_fibermap(self.testfibermap, fibermap)

        #- write out the frame
        frame = Frame(wave,
                      convflux,
                      ivar,
                      mask,
                      Rdata,
                      spectrograph=0,
                      fibermap=fibermap,
                      meta=dict(FLAVOR='flat'))
        write_frame(self.testframe, frame, fibermap=fibermap)

        # set program arguments
        argstr = ['--infile', self.testframe, '--outfile', self.testflat]

        # run it
        args = ffscript.parse(options=argstr)
        ffscript.main(args)
예제 #6
0
파일: procexp.py 프로젝트: sdss/lvmspec
def main(args):

    log = get_logger()

    if (args.fiberflat is None) and (args.sky is None) and (args.calib is None):
        log.critical('no --fiberflat, --sky, or --calib; nothing to do ?!?')
        sys.exit(12)

    frame = read_frame(args.infile)

    if args.cosmics_nsig>0 : # Reject cosmics
        reject_cosmic_rays_1d(frame,args.cosmics_nsig)

    if args.fiberflat!=None :
        log.info("apply fiberflat")
        # read fiberflat
        fiberflat = read_fiberflat(args.fiberflat)

        # apply fiberflat to sky fibers
        apply_fiberflat(frame, fiberflat)

    if args.sky!=None :
        log.info("subtract sky")
        # read sky
        skymodel=read_sky(args.sky)
        # subtract sky
        subtract_sky(frame, skymodel)

    if args.calib!=None :
        log.info("calibrate")
        # read calibration
        fluxcalib=read_flux_calibration(args.calib)
        # apply calibration
        apply_flux_calibration(frame, fluxcalib)

    if args.cosmics_nsig>0 : # Reject cosmics one more time after sky subtraction to catch cosmics close to sky lines
        reject_cosmic_rays_1d(frame,args.cosmics_nsig)

    # save output
    write_frame(args.outfile, frame, units='1e-17 erg/(s cm2 A)')

    log.info("successfully wrote %s"%args.outfile)
예제 #7
0
 def _write_frame(self,
                  flavor='none',
                  camera='b',
                  expid=1,
                  night='20160607'):
     """Write a fake frame"""
     flux = np.ones((self.nspec, self.nwave))
     ivar = np.ones((self.nspec, self.nwave))
     mask = np.zeros((self.nspec, self.nwave), dtype=int)
     Rdata = np.ones((self.nspec, 1, self.nwave))
     fibermap = self._get_fibermap()
     frame = Frame(self.wave,
                   flux,
                   ivar,
                   mask,
                   Rdata,
                   fibermap=fibermap,
                   meta=dict(FLAVOR=flavor,
                             CAMERA=camera,
                             EXPID=expid,
                             NIGHT=night,
                             EXPTIME=1000.))
     io.write_frame(self.framefile, frame)
예제 #8
0
파일: extract.py 프로젝트: sdss/lvmspec
def main_mpi(args, comm=None):

    log = get_logger()

    psf_file = args.psf
    input_file = args.input

    # these parameters are interpreted as the *global* spec range,
    # to be divided among processes.
    specmin = args.specmin
    nspec = args.nspec

    #- Load input files and broadcast

    # FIXME: after we have fixed the serialization
    # of the PSF, read and broadcast here, to reduce
    # disk contention.

    img = None
    if comm is None:
        img = io.read_image(input_file)
    else:
        if comm.rank == 0:
            img = io.read_image(input_file)
        img = comm.bcast(img, root=0)

    psf = load_psf(psf_file)

    # get spectral range

    if nspec is None:
        nspec = psf.nspec
    specmax = specmin + nspec

    camera = img.meta['CAMERA'].lower()  #- b0, r1, .. z9
    spectrograph = int(camera[1])
    fibermin = spectrograph * psf.nspec + specmin

    if args.fibermap is not None:
        fibermap = io.read_fibermap(args.fibermap)
        fibermap = fibermap[fibermin:fibermin + nspec]
        fibers = fibermap['FIBER']
    else:
        fibermap = None
        fibers = np.arange(fibermin, fibermin + nspec, dtype='i4')

    #- Get wavelength grid from options

    if args.wavelength is not None:
        wstart, wstop, dw = [float(tmp) for tmp in args.wavelength.split(',')]
    else:
        wstart = np.ceil(psf.wmin_all)
        wstop = np.floor(psf.wmax_all)
        dw = 0.7

    wave = np.arange(wstart, wstop + dw / 2.0, dw)
    nwave = len(wave)

    #- Confirm that this PSF covers these wavelengths for these spectra

    psf_wavemin = np.max(psf.wavelength(list(range(specmin, specmax)), y=0))
    psf_wavemax = np.min(
        psf.wavelength(list(range(specmin, specmax)), y=psf.npix_y - 1))
    if psf_wavemin > wstart:
        raise ValueError(
            'Start wavelength {:.2f} < min wavelength {:.2f} for these fibers'.
            format(wstart, psf_wavemin))
    if psf_wavemax < wstop:
        raise ValueError(
            'Stop wavelength {:.2f} > max wavelength {:.2f} for these fibers'.
            format(wstop, psf_wavemax))

    # Now we divide our spectra into bundles

    bundlesize = args.bundlesize
    checkbundles = set()
    checkbundles.update(
        np.floor_divide(np.arange(specmin, specmax),
                        bundlesize * np.ones(nspec)).astype(int))
    bundles = sorted(checkbundles)
    nbundle = len(bundles)

    bspecmin = {}
    bnspec = {}
    for b in bundles:
        if specmin > b * bundlesize:
            bspecmin[b] = specmin
        else:
            bspecmin[b] = b * bundlesize
        if (b + 1) * bundlesize > specmax:
            bnspec[b] = specmax - bspecmin[b]
        else:
            bnspec[b] = bundlesize

    # Now we assign bundles to processes

    nproc = 1
    rank = 0
    if comm is not None:
        nproc = comm.size
        rank = comm.rank

    mynbundle = int(nbundle // nproc)
    myfirstbundle = 0
    leftover = nbundle % nproc
    if rank < leftover:
        mynbundle += 1
        myfirstbundle = rank * mynbundle
    else:
        myfirstbundle = ((mynbundle + 1) * leftover) + (mynbundle *
                                                        (rank - leftover))

    if rank == 0:
        #- Print parameters
        log.info("extract:  input = {}".format(input_file))
        log.info("extract:  psf = {}".format(psf_file))
        log.info("extract:  specmin = {}".format(specmin))
        log.info("extract:  nspec = {}".format(nspec))
        log.info("extract:  wavelength = {},{},{}".format(wstart, wstop, dw))
        log.info("extract:  nwavestep = {}".format(args.nwavestep))
        log.info("extract:  regularize = {}".format(args.regularize))

    # get the root output file

    outpat = re.compile(r'(.*)\.fits')
    outmat = outpat.match(args.output)
    if outmat is None:
        raise RuntimeError(
            "extraction output file should have .fits extension")
    outroot = outmat.group(1)

    outdir = os.path.normpath(os.path.dirname(outroot))
    if rank == 0:
        if not os.path.isdir(outdir):
            os.makedirs(outdir)

    if comm is not None:
        comm.barrier()

    failcount = 0

    for b in range(myfirstbundle, myfirstbundle + mynbundle):
        outbundle = "{}_{:02d}.fits".format(outroot, b)
        outmodel = "{}_model_{:02d}.fits".format(outroot, b)

        log.info('extract:  Rank {} starting {} spectra {}:{} at {}'.format(
            rank,
            os.path.basename(input_file),
            bspecmin[b],
            bspecmin[b] + bnspec[b],
            time.asctime(),
        ))
        sys.stdout.flush()

        #- The actual extraction
        try:
            results = ex2d(img.pix,
                           img.ivar * (img.mask == 0),
                           psf,
                           bspecmin[b],
                           bnspec[b],
                           wave,
                           regularize=args.regularize,
                           ndecorr=args.decorrelate_fibers,
                           bundlesize=bundlesize,
                           wavesize=args.nwavestep,
                           verbose=args.verbose,
                           full_output=True,
                           nsubbundles=args.nsubbundles)

            flux = results['flux']
            ivar = results['ivar']
            Rdata = results['resolution_data']
            chi2pix = results['chi2pix']

            mask = np.zeros(flux.shape, dtype=np.uint32)
            mask[results['pixmask_fraction'] > 0.5] |= specmask.SOMEBADPIX
            mask[results['pixmask_fraction'] == 1.0] |= specmask.ALLBADPIX
            mask[chi2pix > 100.0] |= specmask.BAD2DFIT

            #- Augment input image header for output
            img.meta['NSPEC'] = (nspec, 'Number of spectra')
            img.meta['WAVEMIN'] = (wstart, 'First wavelength [Angstroms]')
            img.meta['WAVEMAX'] = (wstop, 'Last wavelength [Angstroms]')
            img.meta['WAVESTEP'] = (dw, 'Wavelength step size [Angstroms]')
            img.meta['SPECTER'] = (specter.__version__,
                                   'https://github.com/desihub/specter')
            img.meta['IN_PSF'] = (_trim(psf_file), 'Input spectral PSF')
            img.meta['IN_IMG'] = (_trim(input_file), 'Input image')

            if fibermap is not None:
                bfibermap = fibermap[bspecmin[b] - specmin:bspecmin[b] +
                                     bnspec[b] - specmin]
            else:
                bfibermap = None

            bfibers = fibers[bspecmin[b] - specmin:bspecmin[b] + bnspec[b] -
                             specmin]

            frame = Frame(wave,
                          flux,
                          ivar,
                          mask=mask,
                          resolution_data=Rdata,
                          fibers=bfibers,
                          meta=img.meta,
                          fibermap=bfibermap,
                          chi2pix=chi2pix)

            #- Write output
            frame.meta['BUNIT'] = 'photon/bin'
            io.write_frame(outbundle, frame)

            if args.model is not None:
                from astropy.io import fits
                fits.writeto(outmodel,
                             results['modelimage'],
                             header=frame.meta)

            log.info('extract:  Done {} spectra {}:{} at {}'.format(
                os.path.basename(input_file), bspecmin[b],
                bspecmin[b] + bnspec[b], time.asctime()))
            sys.stdout.flush()
        except:
            # Log the error and increment the number of failures
            log.error(
                "extract:  FAILED bundle {}, spectrum range {}:{}".format(
                    b, bspecmin[b], bspecmin[b] + bnspec[b]))
            exc_type, exc_value, exc_traceback = sys.exc_info()
            lines = traceback.format_exception(exc_type, exc_value,
                                               exc_traceback)
            log.error(''.join(lines))
            failcount += 1
            sys.stdout.flush()

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

    if failcount > 0:
        # all processes throw
        raise RuntimeError("some extraction bundles failed")

    if rank == 0:
        mergeopts = ['--output', args.output, '--force', '--delete']
        mergeopts.extend(
            ["{}_{:02d}.fits".format(outroot, b) for b in bundles])
        mergeargs = mergebundles.parse(mergeopts)
        mergebundles.main(mergeargs)

        if args.model is not None:
            model = None
            for b in bundles:
                outmodel = "{}_model_{:02d}.fits".format(outroot, b)
                if model is None:
                    model = fits.getdata(outmodel)
                else:
                    #- TODO: test and warn if models overlap for pixels with
                    #- non-zero values
                    model += fits.getdata(outmodel)

                os.remove(outmodel)

            fits.writeto(args.model, model)
예제 #9
0
파일: extract.py 프로젝트: sdss/lvmspec
def main(args):

    if args.mpi:
        from mpi4py import MPI
        comm = MPI.COMM_WORLD
        return main_mpi(args, comm)

    psf_file = args.psf
    input_file = args.input
    specmin = args.specmin
    nspec = args.nspec

    #- Load input files
    psf = load_psf(psf_file)
    img = io.read_image(input_file)

    if nspec is None:
        nspec = psf.nspec
    specmax = specmin + nspec

    camera = img.meta['CAMERA'].lower()  #- b0, r1, .. z9
    spectrograph = int(camera[1])
    fibermin = spectrograph * psf.nspec + specmin

    print('Starting {} spectra {}:{} at {}'.format(
        os.path.basename(input_file), specmin, specmin + nspec,
        time.asctime()))

    if args.fibermap is not None:
        fibermap = io.read_fibermap(args.fibermap)
        fibermap = fibermap[fibermin:fibermin + nspec]
        fibers = fibermap['FIBER']
    else:
        fibermap = None
        fibers = np.arange(fibermin, fibermin + nspec, dtype='i4')

    #- Get wavelength grid from options
    if args.wavelength is not None:
        wstart, wstop, dw = [float(tmp) for tmp in args.wavelength.split(',')]
    else:
        wstart = np.ceil(psf.wmin_all)
        wstop = np.floor(psf.wmax_all)
        dw = 0.7

    wave = np.arange(wstart, wstop + dw / 2.0, dw)
    nwave = len(wave)
    bundlesize = args.bundlesize

    #- Confirm that this PSF covers these wavelengths for these spectra
    psf_wavemin = np.max(psf.wavelength(list(range(specmin, specmax)), y=0))
    psf_wavemax = np.min(
        psf.wavelength(list(range(specmin, specmax)), y=psf.npix_y - 1))
    if psf_wavemin > wstart:
        raise ValueError(
            'Start wavelength {:.2f} < min wavelength {:.2f} for these fibers'.
            format(wstart, psf_wavemin))
    if psf_wavemax < wstop:
        raise ValueError(
            'Stop wavelength {:.2f} > max wavelength {:.2f} for these fibers'.
            format(wstop, psf_wavemax))

    #- Print parameters
    print("""\
#--- Extraction Parameters ---
input:      {input}
psf:        {psf}
output:     {output}
wavelength: {wstart} - {wstop} AA steps {dw}
specmin:    {specmin}
nspec:      {nspec}
regularize: {regularize}
#-----------------------------\
    """.format(input=input_file,
               psf=psf_file,
               output=args.output,
               wstart=wstart,
               wstop=wstop,
               dw=dw,
               specmin=specmin,
               nspec=nspec,
               regularize=args.regularize))

    #- The actual extraction
    results = ex2d(img.pix,
                   img.ivar * (img.mask == 0),
                   psf,
                   specmin,
                   nspec,
                   wave,
                   regularize=args.regularize,
                   ndecorr=args.decorrelate_fibers,
                   bundlesize=bundlesize,
                   wavesize=args.nwavestep,
                   verbose=args.verbose,
                   full_output=True,
                   nsubbundles=args.nsubbundles)
    flux = results['flux']
    ivar = results['ivar']
    Rdata = results['resolution_data']
    chi2pix = results['chi2pix']

    mask = np.zeros(flux.shape, dtype=np.uint32)
    mask[results['pixmask_fraction'] > 0.5] |= specmask.SOMEBADPIX
    mask[results['pixmask_fraction'] == 1.0] |= specmask.ALLBADPIX
    mask[chi2pix > 100.0] |= specmask.BAD2DFIT

    #- Augment input image header for output
    img.meta['NSPEC'] = (nspec, 'Number of spectra')
    img.meta['WAVEMIN'] = (wstart, 'First wavelength [Angstroms]')
    img.meta['WAVEMAX'] = (wstop, 'Last wavelength [Angstroms]')
    img.meta['WAVESTEP'] = (dw, 'Wavelength step size [Angstroms]')
    img.meta['SPECTER'] = (specter.__version__,
                           'https://github.com/desihub/specter')
    img.meta['IN_PSF'] = (_trim(psf_file), 'Input spectral PSF')
    img.meta['IN_IMG'] = (_trim(input_file), 'Input image')

    frame = Frame(wave,
                  flux,
                  ivar,
                  mask=mask,
                  resolution_data=Rdata,
                  fibers=fibers,
                  meta=img.meta,
                  fibermap=fibermap,
                  chi2pix=chi2pix)

    #- Write output
    frame.meta['BUNIT'] = 'photon/bin'
    io.write_frame(args.output, frame)

    if args.model is not None:
        from astropy.io import fits
        fits.writeto(args.model,
                     results['modelimage'],
                     header=frame.meta,
                     clobber=True)

    print('Done {} spectra {}:{} at {}'.format(os.path.basename(input_file),
                                               specmin, specmin + nspec,
                                               time.asctime()))
예제 #10
0
    def run_pa(self,
               input_image,
               psf,
               outwave,
               boxwidth,
               nspec,
               fibers=None,
               fibermap=None,
               dumpfile=None,
               maskFile=None,
               usesigma=False,
               quick_resolution=False):
        from lvmspec.boxcar import do_boxcar
        from lvmspec.frame import Frame as fr
        import lvmspec.psf
        if fibermap['OBJTYPE'][0] == 'ARC':
            psf = lvmspec.psf.PSF(psf)
        flux, ivar, Rdata = do_boxcar(input_image,
                                      psf,
                                      outwave,
                                      boxwidth=boxwidth,
                                      nspec=nspec,
                                      maskFile=maskFile,
                                      usesigma=usesigma,
                                      quick_resolution=quick_resolution)

        #- write to a frame object
        qndiag = 21
        wsigma = None
        if quick_resolution:
            if hasattr(psf, 'wcoeff'):
                wsigma = np.empty(flux.shape)
                if isinstance(nspec, (tuple, list, np.ndarray)):
                    for i, s in enumerate(nspec):
                        #- GD: Need confirmation, but this appears to be missing.
                        wsigma[i] = psf.wdisp(
                            s, outwave) / psf.angstroms_per_pixel(s, outwave)
                else:
                    for i in range(nspec):
                        wsigma[i] = psf.wdisp(
                            i, outwave) / psf.angstroms_per_pixel(i, outwave)
            elif hasattr(psf, 'xsigma_boot'):
                wsigma = np.tile(psf.xsigma_boot, (outwave.shape[0], 1))
        frame = fr(outwave,
                   flux,
                   ivar,
                   resolution_data=Rdata,
                   fibers=fibers,
                   meta=input_image.meta,
                   fibermap=fibermap,
                   wsigma=wsigma,
                   ndiag=qndiag)

        if dumpfile is not None:
            from lvmspec import io
            night = frame.meta['NIGHT']
            expid = frame.meta['EXPID']
            io.write_frame(dumpfile, frame)
            log.debug("Wrote intermediate file %s after %s" %
                      (dumpfile, self.name))

        return frame