コード例 #1
0
ファイル: pixsim.py プロジェクト: sdss/lvmsim
def simulate_frame(night, expid, camera, ccdshape=None, **kwargs):
    """
    Simulate a single frame, including I/O

    Args:
        night: YEARMMDD string
        expid: integer exposure ID
        camera: b0, r1, .. z9

    Options:
        ccdshape = (npix_y, npix_x) primarily used to limit memory while testing

    Additional keyword args are passed to pixsim.simulate()

    Reads:
        $LVM_SPECTRO_SIM/$PIXPROD/{night}/simspec-{expid}.fits

    Writes:
        $LVM_SPECTRO_SIM/$PIXPROD/{night}/simpix-{camera}-{expid}.fits
        $LVM_SPECTRO_SIM/$PIXPROD/{night}/desi-{expid}.fits
        $LVM_SPECTRO_SIM/$PIXPROD/{night}/pix-{camera}-{expid}.fits

    For a lower-level pixel simulation interface that doesn't perform I/O,
    see pixsim.simulate()
    """
    #- night, expid, camera -> input file names
    simspecfile = io.findfile('simspec', night=night, expid=expid)

    #- Read inputs
    psf = lvmmodel.io.load_psf(camera[0])
    simspec = io.read_simspec(simspecfile)

    #- Trim effective CCD size; mainly to limit memory for testing
    if ccdshape is not None:
        psf.npix_y, psf.npix_x = ccdshape

    if 'cosmics' in kwargs:
        shape = (psf.npix_y, psf.npix_x)
        kwargs['cosmics'] = io.read_cosmics(kwargs['cosmics'],
                                            expid,
                                            shape=shape)

    image, rawpix, truepix = simulate(camera, simspec, psf, **kwargs)

    #- Outputs; force "real" data files into simulation directory
    simpixfile = io.findfile('simpix', night=night, expid=expid, camera=camera)
    io.write_simpix(simpixfile, truepix, camera=camera, meta=image.meta)

    simdir = io.simdir(night=night)
    rawfile = lvmspec.io.findfile('desi', night=night, expid=expid)
    rawfile = os.path.join(simdir, os.path.basename(rawfile))
    lvmspec.io.write_raw(rawfile, rawpix, image.meta, camera=camera)

    pixfile = lvmspec.io.findfile('pix',
                                  night=night,
                                  expid=expid,
                                  camera=camera)
    pixfile = os.path.join(simdir, os.path.basename(pixfile))
    lvmspec.io.write_image(pixfile, image)
コード例 #2
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)))
コード例 #3
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 = lvmsim.scripts.pixsim.parse(opts)
        lvmsim.scripts.pixsim.main(pixsimargs)

        #- verify outputs
        simpixfile = io.findfile('simpix', night, expid)
        self.assertTrue(os.path.exists(simpixfile))
        rawfile = lvmspec.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)
コード例 #4
0
 def tearDown(self):
     rawfile = lvmspec.io.findfile('raw', self.night, self.expid)
     if os.path.exists(rawfile):
         os.remove(rawfile)
     fibermap = lvmspec.io.findfile('fibermap', self.night, self.expid)
     if os.path.exists(fibermap):
         os.remove(fibermap)
     simspecfile = io.findfile('simspec', self.night, self.expid)
     if os.path.exists(simspecfile):
         os.remove(simspecfile)
     for camera in ('b0', 'r0', 'z0'):
         pixfile = lvmspec.io.findfile('pix', self.night, self.expid, camera=camera)
         if os.path.exists(pixfile):
             os.remove(pixfile)
         simpixfile = io.findfile('simpix', self.night, self.expid, camera=camera)
         if os.path.exists(simpixfile):
             os.remove(simpixfile)
コード例 #5
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
        simspecfile = io.findfile('simspec', night, expid)
        altrawfile = lvmspec.io.findfile('raw', night, expid) + '.blat'
        opts = [
            '--simspec', simspecfile,
            '--expid', expid+1,
            '--rawfile', altrawfile,
            '--cameras', 'b0,r0',
            '--preproc',
            '--wavemin', 5000, '--wavemax', 7000.0,
            '--ccd_npix_x', 2000,
            ]
        if ncpu is not None:
            opts.extend( ['--ncpu', ncpu] )

        log.debug('testing pixsim.main({})'.format(opts))
        pixsimargs = lvmsim.scripts.pixsim.parse(opts)
        lvmsim.scripts.pixsim.main(pixsimargs)
        simpixfile = io.findfile('simpix', night, expid+1)
        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)
コード例 #6
0
    def test_simulate(self):
        import lvmspec.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 = lvmmodel.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, lvmspec.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
コード例 #7
0
ファイル: pixsim.py プロジェクト: sdss/lvmsim
def expand_args(args):
    '''expand camera string into list of cameras
    '''
    # if simspec:
    #     if not night:
    #         get night from simspec
    #     if not expid:
    #         get expid from simspec
    # else:
    #     assert night and expid are set
    #     get simspec from (night, expid)
    #
    # if not outrawfile:
    #     get outrawfile from (night, expid)
    #
    # if outpixfile or outsimpixfile:
    #     assert len(cameras) == 1

    if args.simspec is None:
        if args.night is None or args.expid is None:
            msg = 'Must set --simspec or both --night and --expid'
            log.error(msg)
            raise ValueError(msg)
        args.simspec = io.findfile('simspec', args.night, args.expid)

    if args.fibermap is None:
        if (args.night is not None) and (args.expid is not None):
            args.fibermap = io.findfile('simfibermap', args.night, args.expid)

    if (args.cameras is None) and (args.spectrographs is None):
        from astropy.io import fits
        try:
            data = fits.getdata(args.simspec, 'B')
            nspec = data['PHOT'].shape[1]
        except KeyError:
            # - Try old specsim format instead
            hdr = fits.getheader(args.simspec, 'PHOT_B')
            nspec = hdr['NAXIS2']

        nspectrographs = (nspec - 1) // 500 + 1
        args.spectrographs = list(range(nspectrographs))

    if (args.night is None) or (args.expid is None):
        from astropy.io import fits
        hdr = fits.getheader(args.simspec)
        if args.night is None:
            args.night = str(hdr['NIGHT'])
        if args.expid is None:
            args.expid = int(hdr['EXPID'])

    if isinstance(args.spectrographs, str):
        args.spectrographs = [int(x) for x in args.spectrographs.split(',')]

    # - expand camera list
    if args.cameras is None:
        args.cameras = list()
        for arm in args.arms.split(','):
            for ispec in args.spectrographs:
                args.cameras.append(arm + str(ispec))
    else:
        args.cameras = args.cameras.split(',')

    # - write to same directory as simspec
    if args.rawfile is None:
        rawfile = os.path.basename(lvmspec.io.findfile('raw', args.night, args.expid))
        args.rawfile = os.path.join(os.path.dirname(args.simspec), rawfile)

    if args.preproc:
        if args.preproc_dir is None:
            args.preproc_dir = os.path.dirname(args.rawfile)

    if args.simpixfile is None:
        args.simpixfile = io.findfile(
            'simpix', night=args.night, expid=args.expid,
            outdir=os.path.dirname(os.path.abspath(args.rawfile)))