Esempio n. 1
0
def main(args=None):

    log = get_logger()

    if args is None:
        args = parse()

    if args.lin_step is not None and args.log10_step is not None:
        print(
            "cannot have both linear and logarthmic bins :-), choose either --lin-step or --log10-step"
        )
        return 12
    if args.coadd_cameras and (args.lin_step is not None
                               or args.log10_step is not None):
        print(
            "cannot specify a new wavelength binning along with --coadd-cameras option"
        )
        return 12

    log.info("reading spectra ...")
    spectra = read_spectra(args.infile)

    if args.coadd_cameras:
        log.info("coadding cameras ...")
        spectra = coadd_cameras(spectra, cosmics_nsig=args.nsig)
    else:
        log.info("coadding ...")
        coadd(spectra, cosmics_nsig=args.nsig)

    if args.lin_step is not None:
        log.info("resampling ...")
        spectra = resample_spectra_lin_or_log(spectra,
                                              linear_step=args.lin_step,
                                              wave_min=args.wave_min,
                                              wave_max=args.wave_max,
                                              fast=args.fast,
                                              nproc=args.nproc)
    if args.log10_step is not None:
        log.info("resampling ...")
        spectra = resample_spectra_lin_or_log(spectra,
                                              log10_step=args.log10_step,
                                              wave_min=args.wave_min,
                                              wave_max=args.wave_max,
                                              fast=args.fast,
                                              nproc=args.nproc)

    log.info("writing {} ...".format(args.outfile))
    write_spectra(args.outfile, spectra)

    log.info("done")
Esempio n. 2
0
    def test_fiberstatus(self):
        """Test that FIBERSTATUS=0 isn't included in coadd"""
        def _makespec(nspec, nwave):
            s1 = self._random_spectra(nspec, nwave)
            s1.flux['x'][:,:] = 1.0
            s1.ivar['x'][:,:] = 1.0
            return s1

        #- Nothing masked
        nspec, nwave = 4,10
        s1 = _makespec(nspec, nwave)
        expt = 33 # random number
        s1.fibermap['EXPTIME'][:]=expt
        self.assertEqual(len(s1.fibermap), nspec)
        coadd(s1)
        self.assertEqual(len(s1.fibermap), 1)
        self.assertEqual(s1.fibermap['COADD_NUMEXP'][0], nspec)
        self.assertEqual(s1.fibermap['COADD_EXPTIME'][0], expt*nspec)
        self.assertEqual(s1.fibermap['FIBERSTATUS'][0], 0)
        self.assertTrue(np.all(s1.flux['x'] == 1.0))
        self.assertTrue(np.allclose(s1.ivar['x'], 1.0*nspec))

        #- Two spectra masked
        nspec, nwave = 5,10
        s1 = _makespec(nspec, nwave)
        self.assertEqual(len(s1.fibermap), nspec)

        s1.fibermap['FIBERSTATUS'][0] = fibermask.BROKENFIBER
        s1.fibermap['FIBERSTATUS'][1] = fibermask.BADFIBER
 
        coadd(s1)
        self.assertEqual(len(s1.fibermap), 1)
        self.assertEqual(s1.fibermap['COADD_NUMEXP'][0], nspec-2)
        self.assertEqual(s1.fibermap['FIBERSTATUS'][0], 0)
        self.assertTrue(np.all(s1.flux['x'] == 1.0))
        self.assertTrue(np.allclose(s1.ivar['x'], 1.0*(nspec-2)))

        #- All spectra masked
        nspec, nwave = 5,10
        s1 = _makespec(nspec, nwave)
        self.assertEqual(len(s1.fibermap), nspec)

        s1.fibermap['FIBERSTATUS'] = fibermask.BROKENFIBER
        
        coadd(s1)
        self.assertEqual(len(s1.fibermap), 1)
        self.assertEqual(s1.fibermap['COADD_NUMEXP'][0], 0)
        self.assertEqual(s1.fibermap['FIBERSTATUS'][0], fibermask.BROKENFIBER)
        self.assertTrue(np.all(s1.flux['x'] == 0.0))
        self.assertTrue(np.all(s1.ivar['x'] == 0.0))
Esempio n. 3
0
 def test_coadd(self):
     """Test coaddition"""
     s1 = self._random_spectra(3, 10)
     coadd(s1)
Esempio n. 4
0
def main(args=None):

    log = get_logger()

    if args is None:
        args = parse()

    if args.lin_step is not None and args.log10_step is not None:
        log.critical(
            "cannot have both linear and logarthmic bins :-), choose either --lin-step or --log10-step"
        )
        return 12
    if args.coadd_cameras and (args.lin_step is not None
                               or args.log10_step is not None):
        log.critical(
            "cannot specify a new wavelength binning along with --coadd-cameras option"
        )
        return 12

    if len(args.infile) == 0:
        log.critical("You must specify input files")
        return 12

    log.info("reading input ...")

    # inspect headers
    input_is_frames = False
    input_is_spectra = False
    for filename in args.infile:
        ifile = fitsio.FITS(filename)
        head = ifile[0].read_header()
        identified = False
        if "EXTNAME" in head and head["EXTNAME"] == "FLUX":
            print(filename, "is a frame")
            input_is_frames = True
            identified = True
            ifile.close()
            continue
        for hdu in ifile:
            head = hdu.read_header()
            if "EXTNAME" in head and head["EXTNAME"].find("_FLUX") >= 0:
                print(filename, "is a spectra")
                input_is_spectra = True
                identified = True
                break
        ifile.close()
        if not identified:
            log.error(
                "{} not identified as frame of spectra file".format(filename))
            sys.exit(1)

    if input_is_frames and input_is_spectra:
        log.error("cannot combine input spectra and frames")
        sys.exit(1)

    if input_is_spectra:
        spectra = read_spectra(args.infile[0])
        for filename in args.infile[1:]:
            log.info("append {}".format(filename))
            spectra.update(read_spectra(filename))
    else:  # frames
        frames = dict()
        cameras = {}
        for filename in args.infile:
            frame = read_frame(filename)
            night = frame.meta['NIGHT']
            expid = frame.meta['EXPID']
            camera = frame.meta['CAMERA']
            frames[(night, expid, camera)] = frame
            if args.coadd_cameras:
                cam, spec = camera[0], camera[1]
                # Keep a list of cameras (b,r,z) for each exposure + spec
                if (night, expid) not in cameras.keys():
                    cameras[(night, expid)] = {spec: [cam]}
                elif spec not in cameras[(night, expid)].keys():
                    cameras[(night, expid)][spec] = [cam]
                else:
                    cameras[(night, expid)][spec].append(cam)

        if args.coadd_cameras:
            # If not all 3 cameras are available, remove the incomplete sets
            for (night, expid), camdict in cameras.items():
                for spec, camlist in camdict.items():
                    log.info("Found {} for SP{} on NIGHT {} EXP {}".format(
                        camlist, spec, night, expid))
                    if len(camlist) != 3 or np.any(
                            np.sort(camlist) != np.array(['b', 'r', 'z'])):
                        for cam in camlist:
                            frames.pop((night, expid, cam + spec))
                            log.warning(
                                "Removing {}{} from Night {} EXP {}".format(
                                    cam, spec, night, expid))
        #import pdb
        #pdb.set_trace()
        spectra = frames2spectra(frames)

        #- hacks to make SpectraLite like a Spectra
        spectra.fibermap = Table(spectra.fibermap)

        del frames  #- maybe free some memory

    if args.coadd_cameras:
        log.info("coadding cameras ...")
        spectra = coadd_cameras(spectra, cosmics_nsig=args.nsig)
    else:
        log.info("coadding ...")
        coadd(spectra, cosmics_nsig=args.nsig)

    if args.lin_step is not None:
        log.info("resampling ...")
        spectra = resample_spectra_lin_or_log(spectra,
                                              linear_step=args.lin_step,
                                              wave_min=args.wave_min,
                                              wave_max=args.wave_max,
                                              fast=args.fast,
                                              nproc=args.nproc)
    if args.log10_step is not None:
        log.info("resampling ...")
        spectra = resample_spectra_lin_or_log(spectra,
                                              log10_step=args.log10_step,
                                              wave_min=args.wave_min,
                                              wave_max=args.wave_max,
                                              fast=args.fast,
                                              nproc=args.nproc)

    #- Add input files to header
    if spectra.meta is None:
        spectra.meta = dict()

    for i, filename in enumerate(args.infile):
        spectra.meta['INFIL{:03d}'.format(i)] = os.path.basename(filename)

    log.info("writing {} ...".format(args.outfile))
    write_spectra(args.outfile, spectra)

    log.info("done")