Example #1
0
    def test_main_defaults(self):
        night = self.night
        expid = self.expid
        camera = 'r0'
        nspec = 3
        ncpu = 3
        obs.new_exposure('arc', night=night, expid=expid, nspec=nspec)

        #- run pixsim
        opts = ['--night', night, '--expid', expid]
        if ncpu is not None:
            opts.extend( ['--ncpu', ncpu] )
            
        log.debug('testing pixsim.main({})'.format(opts))
        pixsimargs = desisim.scripts.pixsim.parse(opts)
        desisim.scripts.pixsim.main(pixsimargs)

        #- verify outputs
        simpixfile = io.findfile('simpix', night, expid)
        self.assertTrue(os.path.exists(simpixfile))
        rawfile = desispec.io.findfile('raw', night, expid)
        self.assertTrue(os.path.exists(rawfile))
        fx = fits.open(rawfile)

        self.assertTrue('B0' in fx)
        self.assertTrue('R0' in fx)
        self.assertTrue('Z0' in fx)
        fx.close()

        #- cleanup as we go
        os.remove(simpixfile)
        os.remove(rawfile)
Example #2
0
    def test_quickgen_airmass_simspec(self):
        night=self.night
        camera = 'r0'
        expid0 = 100
        expid1 = 101

        # generate exposures of varying airmass
        obscond = simexp.reference_conditions['DARK']
        obscond['AIRMASS'] = 1.5
        obs.new_exposure('dark',night=night,expid=expid0,nspec=1,seed=1,obsconditions=obscond)
        simspec0 = io.findfile('simspec', night, expid0)
        fibermap0 = desispec.io.findfile('fibermap', night, expid0)
        obscond['AIRMASS'] = 1.0
        obs.new_exposure('dark',night=night,expid=expid1,nspec=1,seed=1,obsconditions=obscond)
        simspec1 = io.findfile('simspec', night, expid1)
        fibermap1 = desispec.io.findfile('fibermap', night, expid1)

        # generate quickgen output for each airmass
        cmd = "quickgen --simspec {} --fibermap {}".format(simspec0,fibermap0)
        quickgen.main(quickgen.parse(cmd.split()[1:]))
        cmd = "quickgen --simspec {} --fibermap {}".format(simspec1,fibermap1)
        quickgen.main(quickgen.parse(cmd.split()[1:]))

        cframe0=desispec.io.findfile("cframe",night,expid0,camera)
        cframe1=desispec.io.findfile("cframe",night,expid1,camera)
        cf0=desispec.io.read_frame(cframe0)
        cf1=desispec.io.read_frame(cframe1)
        self.assertLess(np.median(cf0.ivar),np.median(cf1.ivar))
Example #3
0
    def test_pixsim_cosmics(self):
        night = self.night
        expid = self.expid
        camera = 'r0'
        obs.new_exposure('arc', night=night, expid=expid, nspec=3)
        simspecfile = io.findfile('simspec', night, expid)
        rawfile = desispec.io.findfile('desi', night, expid)
        simpixfile = io.findfile('simpix', night, expid, camera)

        self.assertFalse(os.path.exists(simpixfile))
        self.assertFalse(os.path.exists(rawfile))

        pixsim.simulate_exposure(simspecfile,
                                 rawfile,
                                 camera,
                                 nspec=3,
                                 wavemin=6000,
                                 wavemax=6050,
                                 addcosmics=True,
                                 ccdshape=self.ccdshape)

        self.assertTrue(os.path.exists(rawfile))

        #- No simpixfile option, shouldn't exist
        self.assertFalse(os.path.exists(simpixfile))
Example #4
0
    def test_main_defaults(self):
        night = self.night
        expid = self.expid
        camera = 'r0'
        nspec = 3
        ncpu = 3
        obs.new_exposure('arc', night=night, expid=expid, nspec=nspec)

        #- run pixsim
        opts = ['--night', night, '--expid', expid]
        if ncpu is not None:
            opts.extend(['--ncpu', ncpu])

        log.debug('testing pixsim.main({})'.format(opts))
        pixsimargs = desisim.scripts.pixsim.parse(opts)
        desisim.scripts.pixsim.main(pixsimargs)

        #- verify outputs
        simpixfile = io.findfile('simpix', night, expid)
        self.assertTrue(os.path.exists(simpixfile))
        rawfile = desispec.io.findfile('raw', night, expid)
        self.assertTrue(os.path.exists(rawfile))
        fx = fits.open(rawfile)

        self.assertTrue('B0' in fx)
        self.assertTrue('R0' in fx)
        self.assertTrue('Z0' in fx)
        fx.close()

        #- cleanup as we go
        os.remove(simpixfile)
        os.remove(rawfile)
Example #5
0
    def test_quickgen_moonzenith_simspec(self):
        night = self.night
        camera = 'r0'
        expid0 = 100
        expid1 = 101

        # generate exposures
        obs.new_exposure('bgs',night=night,expid=expid0,nspec=1,seed=1)
        simspec0 = io.findfile('simspec', night, expid0)
        fibermap0 = desispec.io.findfile('fibermap', night, expid0)
        obs.new_exposure('bgs',night=night,expid=expid1,nspec=1,seed=1)
        simspec1 = io.findfile('simspec', night, expid1)
        fibermap1 = desispec.io.findfile('fibermap', night, expid1)

        # generate quickgen output for each moon phase
        cmd = "quickgen --simspec {} --fibermap {} --moon-zenith 0".format(simspec0,fibermap0)
        quickgen.main(quickgen.parse(cmd.split()[1:]))
        cmd = "quickgen --simspec {} --fibermap {} --moon-zenith 90".format(simspec1,fibermap1)
        quickgen.main(quickgen.parse(cmd.split()[1:]))

        cframe0=desispec.io.findfile("cframe",night,expid0,camera)
        cframe1=desispec.io.findfile("cframe",night,expid1,camera)
        cf0=desispec.io.read_frame(cframe0)
        cf1=desispec.io.read_frame(cframe1)
        self.assertLess(np.median(cf0.ivar),np.median(cf1.ivar))
Example #6
0
    def test_quickgen_moonzenith_simspec(self):
        night = self.night
        camera = 'r0'
        expid0 = 100
        expid1 = 101

        # generate exposures
        obs.new_exposure('bgs', night=night, expid=expid0, nspec=1, seed=1)
        simspec0 = io.findfile('simspec', night, expid0)
        fibermap0 = desispec.io.findfile('fibermap', night, expid0)
        obs.new_exposure('bgs', night=night, expid=expid1, nspec=1, seed=1)
        simspec1 = io.findfile('simspec', night, expid1)
        fibermap1 = desispec.io.findfile('fibermap', night, expid1)

        # generate quickgen output for each moon phase
        cmd = "quickgen --simspec {} --fibermap {} --moon-zenith 0".format(
            simspec0, fibermap0)
        quickgen.main(quickgen.parse(cmd.split()[1:]))
        cmd = "quickgen --simspec {} --fibermap {} --moon-zenith 90".format(
            simspec1, fibermap1)
        quickgen.main(quickgen.parse(cmd.split()[1:]))

        cframe0 = desispec.io.findfile("cframe", night, expid0, camera)
        cframe1 = desispec.io.findfile("cframe", night, expid1, camera)
        cf0 = desispec.io.read_frame(cframe0)
        cf1 = desispec.io.read_frame(cframe1)
        self.assertLess(np.median(cf0.ivar), np.median(cf1.ivar))
Example #7
0
    def test_expand_args(self):
        night = self.night
        expid = self.expid
        obs.new_exposure('arc', night=night, expid=expid, nspec=4)

        simspec = io.findfile('simspec', self.night, self.expid)
        fibermap = desispec.io.findfile('fibermap', self.night, self.expid)
        opts = ['--simspec', simspec, '--fibermap', fibermap]
        args = quickgen.parse(opts)
        self.assertEqual(args.simspec, simspec)
        self.assertEqual(args.fibermap, fibermap)
Example #8
0
 def test_newexp(self):
     night = '20150101'
     for expid, flavor in enumerate(['arc', 'flat', 'science']):
         fibermap, true = obs.new_exposure(flavor, nspec=5, night=night, expid=expid)
         simspecfile = io.findfile('simspec', night, expid=expid)
         self.assertTrue(os.path.exists(simspecfile))
         simspec = io.read_simspec(simspecfile)
         self.assertEqual(simspec.flavor, flavor)
     
     #- confirm that night and expid are optional
     fibermap, true = obs.new_exposure('arc', nspec=2)
Example #9
0
    def test_expand_args(self):
        night = self.night
        expid = self.expid
        obs.new_exposure('arc', night=night, expid=expid, nspec=4)

        simspec = io.findfile('simspec', self.night, self.expid)
        fibermap = desispec.io.findfile('fibermap', self.night, self.expid)
        opts = ['--simspec', simspec, '--fibermap', fibermap]
        args = quickgen.parse(opts)
        self.assertEqual(args.simspec, simspec)
        self.assertEqual(args.fibermap, fibermap)
Example #10
0
    def test_pixsim_cosmics(self):
        night = self.night
        expid = self.expid
        camera = 'r0'
        obs.new_exposure('arc', night=night, expid=expid, nspec=3)
        pixsim.simulate_frame(night, expid, camera, nspec=3, cosmics=self.cosmics, ccdshape=self.ccdshape)

        self.assertTrue(os.path.exists(io.findfile('simspec', night, expid)))
        simspec = io.read_simspec(io.findfile('simspec', night, expid))
        self.assertTrue(os.path.exists(io.findfile('simpix', night, expid, camera)))
        self.assertTrue(os.path.exists(io.findfile('pix', night, expid, camera)))
Example #11
0
    def test_pixsim_cosmics(self):
        night = '20150105'
        expid = 124
        camera = 'r0'
        obs.new_exposure('arc', night=night, expid=expid, nspec=3)
        pixsim.simulate(night, expid, camera, nspec=3, trimxy=True, cosmics=self.cosmics)

        self.assertTrue(os.path.exists(io.findfile('simspec', night, expid)))
        simspec = io.read_simspec(io.findfile('simspec', night, expid))
        self.assertTrue(os.path.exists(io.findfile('simpix', night, expid, camera)))
        self.assertTrue(os.path.exists(io.findfile('pix', night, expid, camera)))
Example #12
0
    def test_quickgen_simspec(self):
        night = self.night
        expid = self.expid
        camera = 'r0'
        # flavors = ['flat','dark','gray','bright','bgs','mws','elg','lrg','qso','arc']
        flavors = ['flat', 'dark', 'bright']
        for i in range(len(flavors)):
            flavor = flavors[i]
            obs.new_exposure(flavor, night=night, expid=expid, nspec=2)

            #- output to same directory as input
            os.environ['DESI_SPECTRO_REDUX'] = os.path.join(
                os.getenv('DESI_SPECTRO_SIM'), os.getenv('PIXPROD'))

            #- run quickgen
            simspec = io.findfile('simspec', night, expid)
            fibermap = io.findfile('simfibermap', night, expid)

            self.assertTrue(os.path.exists(simspec))
            self.assertTrue(os.path.exists(fibermap))

            if flavor == 'flat':
                try:
                    cmd = "quickgen --simspec {} --fibermap {}".format(
                        simspec, fibermap)
                    quickgen.main(quickgen.parse(cmd.split()[1:]))
                except SystemExit:
                    pass

                #- verify flat outputs
                fiberflatfile = desispec.io.findfile('fiberflat', night, expid,
                                                     camera)
                self.assertTrue(os.path.exists(fiberflatfile))

            else:
                cmd = "quickgen --simspec {} --fibermap {}".format(
                    simspec, fibermap)
                quickgen.main(quickgen.parse(cmd.split()[1:]))

                #- verify outputs
                framefile = desispec.io.findfile('frame', night, expid, camera)
                self.assertTrue(os.path.exists(framefile))
                cframefile = desispec.io.findfile('cframe', night, expid,
                                                  camera)
                self.assertTrue(os.path.exists(cframefile))
                skyfile = desispec.io.findfile('sky', night, expid, camera)
                self.assertTrue(os.path.exists(skyfile))
                fluxcalibfile = desispec.io.findfile('calib', night, expid,
                                                     camera)
                self.assertTrue(os.path.exists(fluxcalibfile))

            os.remove(simspec)
            os.remove(fibermap)
Example #13
0
    def test_newexp(self):
        night = self.night
        seed = np.random.randint(2**30)
        #- programs 'bgs' and 'bright' not yet implemented
        for expid, program in enumerate(['arc', 'flat', 'dark', 'mws']):
            sim, fibermap, meta, obsconditions, objmeta = obs.new_exposure(program, nspec=10, night=night, expid=expid, seed=seed)
            simspecfile = io.findfile('simspec', night, expid=expid)
            fibermapfile = io.findfile('simfibermap', night, expid=expid)
            self.assertTrue(os.path.exists(simspecfile))
            self.assertTrue(os.path.exists(fibermapfile))
            simspec = io.read_simspec(simspecfile)
            if program in ('arc', 'flat'):
                self.assertEqual(simspec.flavor, program)
            else:
                self.assertEqual(simspec.flavor, 'science')

            #- Check that photons are in a reasonable range
            self.assertGreater(len(simspec.cameras), 0)
            for camera in simspec.cameras.values():
                maxphot = camera.phot.max()
                self.assertTrue(maxphot > 1, 'suspiciously few {} photons ({}); wrong units?'.format(program, maxphot))
                self.assertTrue(maxphot < 1e6, 'suspiciously many {} photons ({}); wrong units?'.format(program, maxphot))
                if program not in ('arc', 'flat'):
                    self.assertTrue(camera.skyphot.max() > 1, 'suspiciously few sky photons; wrong units?')
                    self.assertTrue(camera.skyphot.max() < 1e6, 'suspiciously many sky photons; wrong units?')

            if program in ('arc', 'flat'):
                self.assertTrue(meta is None)
                self.assertTrue(obsconditions is None)
            else:
                flux, fluxhdr = fits.getdata(simspecfile, 'FLUX', header=True)
                skyflux, skyfluxhdr = fits.getdata(simspecfile, 'SKYFLUX', header=True)
                self.assertTrue(fluxhdr['BUNIT'].startswith('1e-17'))
                self.assertTrue(skyfluxhdr['BUNIT'].startswith('1e-17'))
                for i in range(flux.shape[0]):
                    objtype = simspec.truth['OBJTYPE'][i]
                    maxflux = flux[i].max()
                    maxsky = skyflux[i].max()
                    self.assertTrue(maxsky > 1, 'suspiciously low {} sky flux ({}); wrong units?'.format(objtype, maxsky))
                    self.assertTrue(maxsky < 1e5, 'suspiciously high {} sky flux ({}); wrong units?'.format(objtype, maxsky))
                    if objtype != 'SKY':
                        ### print('---> {} maxflux {}'.format(objtype, maxflux))
                        self.assertTrue(maxflux > 0.01, 'suspiciously low {} flux ({}) using seed {}; wrong units?'.format(objtype, maxflux, seed))
                        self.assertTrue(maxflux < 1e5, 'suspiciously high {} flux ({}) using seed {}; wrong units?'.format(objtype, maxflux, seed))
                    else:
                        self.assertTrue(np.all(flux[i] == 0.0))

            os.remove(simspecfile)
            os.remove(fibermapfile)

        #- confirm that night and expid are optional
        results = obs.new_exposure('arc', nspec=2)
Example #14
0
    def test_pixsim(self):
        night = '20150105'
        expid = 123
        camera = 'r0'
        obs.new_exposure('arc', night=night, expid=expid, nspec=3)
        pixsim.simulate(night, expid, camera, nspec=3, trimxy=True)

        self.assertTrue(os.path.exists(io.findfile('simspec', night, expid)))
        simspec = io.read_simspec(io.findfile('simspec', night, expid))
        self.assertTrue(
            os.path.exists(io.findfile('simpix', night, expid, camera)))
        self.assertTrue(
            os.path.exists(io.findfile('pix', night, expid, camera)))
Example #15
0
 def test_newexp_sky(self):
     "Test different levels of sky brightness"
     night = self.night
     #- programs 'bgs' and 'bright' not yet implemented
     sim_dark, fmap_dark, meta_dark, obscond_dark, objmeta_dark = obs.new_exposure('dark', nspec=10, night=night, expid=0, exptime=1000)
     dark = sim_dark.simulated.copy()
     sim_mws, fmap_mws, meta_mws, obscond_mws, objmeta_mws = obs.new_exposure('mws', nspec=10, night=night, expid=1, exptime=1000)
     mws = sim_dark.simulated.copy()
     for channel in ['b', 'r', 'z']:
         sky_mws = mws['num_sky_electrons_'+channel]
         sky_dark = dark['num_sky_electrons_'+channel]
         nonzero = (sky_mws != 0.0)
         self.assertTrue(np.all(sky_mws[nonzero] > sky_dark[nonzero]))
Example #16
0
    def test_main_override(self):
        night = self.night
        expid = self.expid
        camera = 'r0'
        nspec = 3
        ncpu = 3
        obs.new_exposure('arc', night=night, expid=expid, nspec=nspec)

        #- derive night from simspec input while overriding expid
        #- Include wavelengths covering z, but only ask for b and r
        simspecfile = io.findfile('simspec', night, expid)
        altexpid = expid + 1
        altrawfile = desispec.io.findfile('raw', night, altexpid) + '.blat'
        opts = [
            '--simspec',
            simspecfile,
            '--expid',
            altexpid,
            '--rawfile',
            altrawfile,
            '--cameras',
            'b0,r0',
            '--wavemin',
            5500,
            '--wavemax',
            7000.0,
            '--ccd_npix_x',
            2000,
        ]
        if ncpu is not None:
            opts.extend(['--ncpu', ncpu])

        log.debug('testing pixsim.main({})'.format(opts))
        pixsimargs = desisim.scripts.pixsim.parse(opts)
        desisim.scripts.pixsim.main(pixsimargs)
        simpixfile = io.findfile('simpix', night, altexpid)

        self.assertTrue(os.path.exists(simpixfile))
        self.assertTrue(os.path.exists(altrawfile))
        fx = fits.open(altrawfile)
        self.assertTrue('B0' in fx)
        self.assertTrue('R0' in fx)
        self.assertTrue('Z0' not in fx)
        fx.close()

        #- cleanup as we go
        os.remove(simpixfile)
        os.remove(altrawfile)
Example #17
0
    def test_quickgen_simspec(self):
        night = self.night
        expid = self.expid
        camera = 'r0'
        # flavors = ['flat','dark','gray','bright','bgs','mws','elg','lrg','qso','arc']
        flavors = ['flat', 'dark', 'bright']
        for i in range(len(flavors)):
            flavor = flavors[i]
            obs.new_exposure(flavor, night=night, expid=expid, nspec=2)
    
            #- output to same directory as input
            os.environ['DESI_SPECTRO_REDUX'] = os.path.join(os.getenv('DESI_SPECTRO_SIM'), os.getenv('PIXPROD'))
    
            #- run quickgen
            simspec = io.findfile('simspec', night, expid)
            fibermap = io.findfile('simfibermap', night, expid)
            
            self.assertTrue(os.path.exists(simspec))
            self.assertTrue(os.path.exists(fibermap))

            if flavor == 'flat':
                try:
                    cmd = "quickgen --simspec {} --fibermap {}".format(simspec,fibermap)
                    quickgen.main(quickgen.parse(cmd.split()[1:]))
                except SystemExit:
                    pass

                #- verify flat outputs
                fiberflatfile = desispec.io.findfile('fiberflat', night, expid, camera)
                self.assertTrue(os.path.exists(fiberflatfile))

            else:
                cmd = "quickgen --simspec {} --fibermap {}".format(simspec,fibermap)
                quickgen.main(quickgen.parse(cmd.split()[1:]))
    
                #- verify outputs
                framefile = desispec.io.findfile('frame', night, expid, camera)
                self.assertTrue(os.path.exists(framefile))
                cframefile = desispec.io.findfile('cframe', night, expid, camera)
                self.assertTrue(os.path.exists(cframefile))
                skyfile = desispec.io.findfile('sky', night, expid, camera)
                self.assertTrue(os.path.exists(skyfile))
                fluxcalibfile = desispec.io.findfile('calib', night, expid, camera)
                self.assertTrue(os.path.exists(fluxcalibfile))

            os.remove(simspec)
            os.remove(fibermap)
Example #18
0
    def test_parse(self):
        night = self.night
        expid = self.expid
        obs.new_exposure('dark', night=night, expid=expid, nspec=4)

        simspec = io.findfile('simspec', self.night, self.expid)
        fibermap = desispec.io.findfile('fibermap', self.night, self.expid)
        opts = ['--simspec', simspec, '--fibermap', fibermap]
        opts += ['--spectrograph', '3', '--config', 'desi']
        args = quickgen.parse(opts)
        self.assertEqual(args.simspec, simspec)
        self.assertEqual(args.fibermap, fibermap)
        self.assertEqual(args.spectrograph, 3)
        self.assertEqual(args.config, 'desi')

        with self.assertRaises(SystemExit):
            quickgen.parse([])
Example #19
0
    def test_parse(self):
        night = self.night
        expid = self.expid
        obs.new_exposure('dark', night=night, expid=expid, nspec=4)

        simspec = io.findfile('simspec', self.night, self.expid)
        fibermap = desispec.io.findfile('fibermap', self.night, self.expid)
        opts = ['--simspec', simspec, '--fibermap', fibermap]
        opts += ['--spectrograph', '3', '--config', 'desi']
        args = quickgen.parse(opts)
        self.assertEqual(args.simspec, simspec)
        self.assertEqual(args.fibermap, fibermap)
        self.assertEqual(args.spectrograph, 3)
        self.assertEqual(args.config, 'desi')

        with self.assertRaises(ValueError):
            quickgen.parse([])
Example #20
0
    def test_simulate(self):
        import desispec.image
        night = self.night
        expid = self.expid
        camera = 'r0'
        nspec = 3
        obs.new_exposure('arc', night=night, expid=expid, nspec=nspec)
        simspec = io.read_simspec(io.findfile('simspec', night, expid))
        psf = desimodel.io.load_psf(camera[0])
        psf.npix_y, psf.npix_x = self.ccdshape

        image, rawpix, truepix = pixsim.simulate(camera, simspec, psf, nspec=nspec)

        self.assertTrue(isinstance(image, desispec.image.Image))
        self.assertTrue(isinstance(rawpix, np.ndarray))
        self.assertTrue(isinstance(truepix, np.ndarray))
        self.assertEqual(image.pix.shape, truepix.shape)
        self.assertEqual(image.pix.shape[0], rawpix.shape[0])
        self.assertLess(image.pix.shape[1], rawpix.shape[1])  #- raw has overscan
Example #21
0
    def test_simulate(self):
        import desispec.image
        night = self.night
        expid = self.expid
        camera = 'r0'
        nspec = 3
        obs.new_exposure('arc', night=night, expid=expid, nspec=nspec)
        simspec = io.read_simspec(io.findfile('simspec', night, expid))
        psf = desimodel.io.load_psf(camera[0])
        psf.npix_y, psf.npix_x = self.ccdshape

        image, rawpix, truepix = pixsim.simulate(camera, simspec, psf, nspec=nspec)

        self.assertTrue(isinstance(image, desispec.image.Image))
        self.assertTrue(isinstance(rawpix, np.ndarray))
        self.assertTrue(isinstance(truepix, np.ndarray))
        self.assertEqual(image.pix.shape, truepix.shape)
        self.assertEqual(image.pix.shape[0], rawpix.shape[0])
        self.assertLess(image.pix.shape[1], rawpix.shape[1])  #- raw has overscan
Example #22
0
    def test_pixsim_cosmics(self):
        night = self.night
        expid = self.expid
        cameras = ['r0']
        obs.new_exposure('arc', night=night, expid=expid, nspec=3)
        simspecfile = io.findfile('simspec', night, expid)
        rawfile = desispec.io.findfile('desi', night, expid)
        simpixfile = io.findfile('simpix', night, expid, cameras)

        self.assertFalse(os.path.exists(simpixfile))
        self.assertFalse(os.path.exists(rawfile))

        pixsim.simulate_exposure(simspecfile, rawfile, cameras,
                addcosmics=True, ccdshape=self.ccdshape)

        self.assertTrue(os.path.exists(rawfile))

        #- No simpixfile option, shouldn't exist
        self.assertFalse(os.path.exists(simpixfile))
Example #23
0
    def test_main_override(self):
        night = self.night
        expid = self.expid
        camera = 'r0'
        nspec = 3
        ncpu = 3
        obs.new_exposure('arc', night=night, expid=expid, nspec=nspec)

        #- derive night from simspec input while overriding expid
        #- Include wavelengths covering z, but only ask for b and r
        simspecfile = io.findfile('simspec', night, expid)
        altexpid = expid+1
        altrawfile = desispec.io.findfile('raw', night, altexpid) + '.blat'
        opts = [
            '--simspec', simspecfile,
            '--expid', altexpid,
            '--rawfile', altrawfile,
            '--cameras', 'b0,r0',
            '--wavemin', 5500, '--wavemax', 7000.0,
            '--ccd_npix_x', 2000,
            ]
        if ncpu is not None:
            opts.extend( ['--ncpu', ncpu] )

        log.debug('testing pixsim.main({})'.format(opts))
        pixsimargs = desisim.scripts.pixsim.parse(opts)
        desisim.scripts.pixsim.main(pixsimargs)
        simpixfile = io.findfile('simpix', night, altexpid)

        self.assertTrue(os.path.exists(simpixfile))
        self.assertTrue(os.path.exists(altrawfile))
        fx = fits.open(altrawfile)
        self.assertTrue('B0' in fx)
        self.assertTrue('R0' in fx)
        self.assertTrue('Z0' not in fx)
        fx.close()

        #- cleanup as we go
        os.remove(simpixfile)
        os.remove(altrawfile)
Example #24
0
    def test_quickgen_seed_simspec(self):
        night=self.night
        camera = 'r0'
        expid0 = 100
        expid1 = 101
        expid2 = 102

        # generate exposures seed 1 & 2
        obs.new_exposure('dark',night=night,expid=expid0,nspec=1,seed=1)
        simspec0 = io.findfile('simspec', night, expid0)
        fibermap0 = desispec.io.findfile('fibermap', night, expid0)
        obs.new_exposure('dark',night=night,expid=expid1,nspec=1,seed=1)
        simspec1 = io.findfile('simspec', night, expid1)
        fibermap1 = desispec.io.findfile('fibermap', night, expid1)
        obs.new_exposure('dark',night=night,expid=expid2,nspec=1,seed=2)
        simspec2 = io.findfile('simspec', night, expid2)
        fibermap2 = desispec.io.findfile('fibermap', night, expid2)

        s0=fits.open(simspec0)
        s1=fits.open(simspec1)
        s2=fits.open(simspec2)

        self.assertEqual(s0['TRUTH'].data['OBJTYPE'][0], s1['TRUTH'].data['OBJTYPE'][0])
        self.assertEqual(s0['TRUTH'].data['REDSHIFT'][0], s1['TRUTH'].data['REDSHIFT'][0])
        self.assertNotEqual(s0['TRUTH'].data['REDSHIFT'][0], s2['TRUTH'].data['REDSHIFT'][0])

        self.assertTrue(np.all(s0['WAVE'].data == s1['WAVE'].data))
        self.assertTrue(np.all(s0['FLUX'].data == s1['FLUX'].data))

        # generate quickgen output for each exposure
        # spawn with subprocess so that it will really be restarting fresh;
        # using quickgen.main re-uses a specsim Simulator that consumes random
        # state when using it.
        # See https://github.com/desihub/specsim/issues/94
        cmd = "{} {}/quickgen --simspec {} --fibermap {} --seed 1".format(
            sys.executable, self.binDir, simspec0, fibermap0)
        ### quickgen.main(quickgen.parse(cmd.split()[1:]))
        subprocess.check_call(cmd.split())

        cmd = "{} {}/quickgen --simspec {} --fibermap {} --seed 1".format(
            sys.executable, self.binDir, simspec1, fibermap1)
        ### quickgen.main(quickgen.parse(cmd.split()[1:]))
        subprocess.check_call(cmd.split())

        cmd = "{} {}/quickgen --simspec {} --fibermap {} --seed 2".format(
            sys.executable, self.binDir, simspec2, fibermap2)
        ### quickgen.main(quickgen.parse(cmd.split()[1:]))
        subprocess.check_call(cmd.split())

        cframe0=desispec.io.findfile("cframe",night,expid0,camera)
        cframe1=desispec.io.findfile("cframe",night,expid1,camera)
        cframe2=desispec.io.findfile("cframe",night,expid2,camera)
        cf0=desispec.io.read_frame(cframe0)
        cf1=desispec.io.read_frame(cframe1)
        cf2=desispec.io.read_frame(cframe2)

        self.assertTrue(np.all(cf0.flux == cf1.flux))   #- same seed
        self.assertTrue(np.all(cf0.ivar == cf1.ivar))
        self.assertTrue(np.any(cf0.flux != cf2.flux))   #- different seed
Example #25
0
    def test_quickgen_airmass_simspec(self):
        night = self.night
        camera = 'r0'
        expid0 = 100
        expid1 = 101

        # generate exposures of varying airmass
        obscond = simexp.reference_conditions['DARK']
        obscond['AIRMASS'] = 1.5
        obs.new_exposure('dark',
                         night=night,
                         expid=expid0,
                         nspec=1,
                         seed=1,
                         obsconditions=obscond)
        simspec0 = io.findfile('simspec', night, expid0)
        fibermap0 = desispec.io.findfile('fibermap', night, expid0)
        obscond['AIRMASS'] = 1.0
        obs.new_exposure('dark',
                         night=night,
                         expid=expid1,
                         nspec=1,
                         seed=1,
                         obsconditions=obscond)
        simspec1 = io.findfile('simspec', night, expid1)
        fibermap1 = desispec.io.findfile('fibermap', night, expid1)

        # generate quickgen output for each airmass
        cmd = "quickgen --simspec {} --fibermap {}".format(simspec0, fibermap0)
        quickgen.main(quickgen.parse(cmd.split()[1:]))
        cmd = "quickgen --simspec {} --fibermap {}".format(simspec1, fibermap1)
        quickgen.main(quickgen.parse(cmd.split()[1:]))

        cframe0 = desispec.io.findfile("cframe", night, expid0, camera)
        cframe1 = desispec.io.findfile("cframe", night, expid1, camera)
        cf0 = desispec.io.read_frame(cframe0)
        cf1 = desispec.io.read_frame(cframe1)
        self.assertLess(np.median(cf0.ivar), np.median(cf1.ivar))
Example #26
0
    def test_pixsim(self):
        night = self.night
        expid = self.expid
        cameras = ['r0']
        obs.new_exposure('arc', night=night, expid=expid, nspec=3)
        self.assertTrue(os.path.exists(io.findfile('simspec', night, expid)))

        simspecfile = io.findfile('simspec', night, expid)
        rawfile = desispec.io.findfile('desi', night, expid)
        simpixfile = io.findfile('simpix', night, expid)

        self.assertFalse(os.path.exists(simpixfile))
        self.assertFalse(os.path.exists(rawfile))

        pixsim.simulate_exposure(simspecfile,
                                 rawfile,
                                 cameras,
                                 ccdshape=self.ccdshape,
                                 addcosmics=False,
                                 simpixfile=simpixfile)

        self.assertTrue(os.path.exists(simpixfile))
        self.assertTrue(os.path.exists(rawfile))
Example #27
0
    def test_quickgen_seed_simspec(self):
        night = self.night
        camera = 'r0'
        expid0 = 100
        expid1 = 101
        expid2 = 102

        # generate exposures seed 1 & 2
        obs.new_exposure('dark', night=night, expid=expid0, nspec=1, seed=1)
        simspec0 = io.findfile('simspec', night, expid0)
        fibermap0 = desispec.io.findfile('fibermap', night, expid0)
        obs.new_exposure('dark', night=night, expid=expid1, nspec=1, seed=1)
        simspec1 = io.findfile('simspec', night, expid1)
        fibermap1 = desispec.io.findfile('fibermap', night, expid1)
        obs.new_exposure('dark', night=night, expid=expid2, nspec=1, seed=2)
        simspec2 = io.findfile('simspec', night, expid2)
        fibermap2 = desispec.io.findfile('fibermap', night, expid2)

        # generate quickgen output for each exposure
        cmd = "quickgen --simspec {} --fibermap {} --seed 1".format(
            simspec0, fibermap0)
        quickgen.main(quickgen.parse(cmd.split()[1:]))
        cmd = "quickgen --simspec {} --fibermap {} --seed 1".format(
            simspec1, fibermap1)
        quickgen.main(quickgen.parse(cmd.split()[1:]))
        cmd = "quickgen --simspec {} --fibermap {} --seed 2".format(
            simspec2, fibermap2)
        quickgen.main(quickgen.parse(cmd.split()[1:]))

        cframe0 = desispec.io.findfile("cframe", night, expid0, camera)
        cframe1 = desispec.io.findfile("cframe", night, expid1, camera)
        cframe2 = desispec.io.findfile("cframe", night, expid2, camera)
        cf0 = desispec.io.read_frame(cframe0)
        cf1 = desispec.io.read_frame(cframe1)
        cf2 = desispec.io.read_frame(cframe2)

        self.assertTrue(np.all(cf0.flux == cf1.flux))  #- same seed
        self.assertTrue(np.all(cf0.ivar == cf1.ivar))
        self.assertTrue(np.any(cf0.flux != cf2.flux))  #- different seed

        s0 = fits.open(simspec0)
        s1 = fits.open(simspec1)
        s2 = fits.open(simspec2)

        self.assertEqual(s0['TRUTH'].data['OBJTYPE'][0],
                         s1['TRUTH'].data['OBJTYPE'][0])
        self.assertEqual(s0['TRUTH'].data['REDSHIFT'][0],
                         s1['TRUTH'].data['REDSHIFT'][0])
        self.assertNotEqual(s0['TRUTH'].data['REDSHIFT'][0],
                            s2['TRUTH'].data['REDSHIFT'][0])
Example #28
0
    def test_quickgen_seed_simspec(self):
        night = self.night
        camera = 'r0'
        expid0 = 100
        expid1 = 101
        expid2 = 102

        # generate exposures seed 1 & 2
        obs.new_exposure('dark', night=night, expid=expid0, nspec=1, seed=1)
        simspec0 = io.findfile('simspec', night, expid0)
        fibermap0 = desispec.io.findfile('fibermap', night, expid0)
        obs.new_exposure('dark', night=night, expid=expid1, nspec=1, seed=1)
        simspec1 = io.findfile('simspec', night, expid1)
        fibermap1 = desispec.io.findfile('fibermap', night, expid1)
        obs.new_exposure('dark', night=night, expid=expid2, nspec=1, seed=2)
        simspec2 = io.findfile('simspec', night, expid2)
        fibermap2 = desispec.io.findfile('fibermap', night, expid2)

        s0 = fits.open(simspec0)
        s1 = fits.open(simspec1)
        s2 = fits.open(simspec2)

        self.assertEqual(s0['TRUTH'].data['OBJTYPE'][0],
                         s1['TRUTH'].data['OBJTYPE'][0])
        self.assertEqual(s0['TRUTH'].data['REDSHIFT'][0],
                         s1['TRUTH'].data['REDSHIFT'][0])
        self.assertNotEqual(s0['TRUTH'].data['REDSHIFT'][0],
                            s2['TRUTH'].data['REDSHIFT'][0])

        self.assertTrue(np.all(s0['WAVE'].data == s1['WAVE'].data))
        self.assertTrue(np.all(s0['FLUX'].data == s1['FLUX'].data))

        # generate quickgen output for each exposure
        # spawn with subprocess so that it will really be restarting fresh;
        # using quickgen.main re-uses a specsim Simulator that consumes random
        # state when using it.
        # See https://github.com/desihub/specsim/issues/94
        cmd = "{} {}/quickgen --simspec {} --fibermap {} --seed 1".format(
            sys.executable, self.binDir, simspec0, fibermap0)
        ### quickgen.main(quickgen.parse(cmd.split()[1:]))
        subprocess.check_call(cmd.split())

        cmd = "{} {}/quickgen --simspec {} --fibermap {} --seed 1".format(
            sys.executable, self.binDir, simspec1, fibermap1)
        ### quickgen.main(quickgen.parse(cmd.split()[1:]))
        subprocess.check_call(cmd.split())

        cmd = "{} {}/quickgen --simspec {} --fibermap {} --seed 2".format(
            sys.executable, self.binDir, simspec2, fibermap2)
        ### quickgen.main(quickgen.parse(cmd.split()[1:]))
        subprocess.check_call(cmd.split())

        cframe0 = desispec.io.findfile("cframe", night, expid0, camera)
        cframe1 = desispec.io.findfile("cframe", night, expid1, camera)
        cframe2 = desispec.io.findfile("cframe", night, expid2, camera)
        cf0 = desispec.io.read_frame(cframe0)
        cf1 = desispec.io.read_frame(cframe1)
        cf2 = desispec.io.read_frame(cframe2)

        self.assertTrue(np.all(cf0.flux == cf1.flux))  #- same seed
        self.assertTrue(np.all(cf0.ivar == cf1.ivar))
        self.assertTrue(np.any(cf0.flux != cf2.flux))  #- different seed
print('     (without knowing that it was simulated)')
print('Pipeline will write processed data to {}'.format(
    desispec.io.specprod_root()))

# Generate the fibermap and specsim files needed by quickgen
# fibermap: table of simulated information about the position of each target in the DESI focal plane
# simspec:  "truth" table of spectra and the intrinsic properties of each object (e.g. redshift,
#               noiseless photometry, [OII] flux, etc.)
# If you want to use your own mix of objects, you need to write your own fibermap and specsim files
# following the particular format (rather than calling new_exposure)

# new_exposure generates random exposures of various types
output = new_exposure(args.flavor,
                      nspec=args.nspec,
                      seed=args.seed,
                      night=args.night,
                      expid=args.expid,
                      tileid=None,
                      exptime=None)

# Check in on the data we just wrote
rawdata_dir = desispec.io.rawdata_root()
os.system('find {} | sort'.format(rawdata_dir))

fiberfile = desispec.io.findfile('fibermap',
                                 night=args.night,
                                 expid=args.expid)
simspecfile = desisim.io.findfile('simspec',
                                  night=args.night,
                                  expid=args.expid)