def read_psf_model(self, x0, y0, gaussPsf=False, pixPsf=False, const2psf=False, psf_sigma=1., cx=0, cy=0): psffn = None if gaussPsf: from tractor.basics import GaussianMixturePSF v = psf_sigma**2 psf = GaussianMixturePSF(1., 0., 0., v, v, 0.) print('WARNING: using mock PSF:', psf) psf.version = '0' psf.plver = '' elif pixPsf: # spatially varying pixelized PsfEx from tractor.psfex import PixelizedPsfEx print('Reading PsfEx model from', self.psffn) psf = PixelizedPsfEx(self.psffn) psf.shift(x0, y0) psffn = self.psffn elif const2psf: from tractor.psfex import PsfExModel from tractor.basics import GaussianMixtureEllipsePSF # 2-component constant MoG. print('Reading PsfEx model from', self.psffn) psffn = self.psffn psfex = PsfExModel(self.psffn) psfim = psfex.at(cx, cy) psfim = psfim[5:-5, 5:-5] print('Fitting PsfEx model as 2-component Gaussian...') psf = GaussianMixtureEllipsePSF.fromStamp(psfim, N=2) del psfim del psfex else: assert (False) print('Using PSF model', psf) if psffn is not None: hdr = fitsio.read_header(psffn) psf.version = hdr.get('LEGSURV', None) if psf.version is None: psf.version = str(os.stat(psffn).st_mtime) psf.plver = hdr.get('PLVER', '').strip() return psf
def read_psf_model(self, x0, y0, gaussPsf=False, pixPsf=False, const2psf=False, psf_sigma=1., cx=0, cy=0): psffn = None if gaussPsf: from tractor.basics import GaussianMixturePSF v = psf_sigma**2 psf = GaussianMixturePSF(1., 0., 0., v, v, 0.) print('WARNING: using mock PSF:', psf) psf.version = '0' psf.plver = '' elif pixPsf: # spatially varying pixelized PsfEx from tractor.psfex import PixelizedPsfEx print('Reading PsfEx model from', self.psffn) psf = PixelizedPsfEx(self.psffn) psf.shift(x0, y0) psffn = self.psffn elif const2psf: from tractor.psfex import PsfExModel from tractor.basics import GaussianMixtureEllipsePSF # 2-component constant MoG. print('Reading PsfEx model from', self.psffn) psffn = self.psffn psfex = PsfExModel(self.psffn) psfim = psfex.at(cx, cy) psfim = psfim[5:-5, 5:-5] print('Fitting PsfEx model as 2-component Gaussian...') psf = GaussianMixtureEllipsePSF.fromStamp(psfim, N=2) del psfim del psfex else: assert(False) print('Using PSF model', psf) if psffn is not None: hdr = fitsio.read_header(psffn) psf.version = hdr.get('LEGSURV', None) if psf.version is None: psf.version = str(os.stat(psffn).st_mtime) psf.plver = hdr.get('PLVER', '').strip() return psf
def run_calibs(self, ra, dec, pixscale, W=2112, H=4644, se=True, astrom=True, psfex=True, psfexfit=True): ''' pixscale: in degrees/pixel ''' for fn in [self.wcsfn, self.sefn, self.psffn, self.psffitfn]: print('exists?', os.path.exists(fn), fn) self.makedirs() run_fcopy = False run_se = False run_astrom = False run_psfex = False run_psfexfit = False sedir = 'NGVS-g-Single' if not all([os.path.exists(fn) for fn in [self.sefn]]): run_se = True run_fcopy = True if not all([ os.path.exists(fn) for fn in [self.wcsfn, self.corrfn, self.sdssfn] ]): run_astrom = True if not os.path.exists(self.psffn): run_psfex = True if not os.path.exists(self.psffitfn): run_psfexfit = True if run_fcopy and (run_se and se): tmpimgfn = create_temp(suffix='.fits') cmd = 'imcopy %s"+%i" %s' % (self.imgfn, self.hdu, tmpimgfn) print(cmd) if os.system(cmd): raise RuntimeError('Command failed: ' + cmd) if run_astrom or run_se: # grab header values... primhdr = self.read_image_primary_header() hdr = self.read_image_header() magzp = hdr['PHOT_C'] + 2.5 * np.log10(hdr['EXPTIME']) seeing = self.seeing print('Seeing', seeing, 'arcsec') if run_se and se: #'-SEEING_FWHM %f' % seeing, #'-PIXEL_SCALE 0', #'-PIXEL_SCALE %f' % (pixscale * 3600), #'-MAG_ZEROPOINT %f' % magzp, cmd = ' '.join([ 'sex', '-c', os.path.join(sedir, 'psfex.sex'), '-PARAMETERS_NAME', os.path.join(sedir, 'psfex.param'), '-FILTER_NAME', os.path.join(sedir, 'default.conv'), '-CATALOG_NAME', self.sefn, tmpimgfn ]) print(cmd) if os.system(cmd): raise RuntimeError('Command failed: ' + cmd) if run_astrom and astrom: cmd = ' '.join([ 'solve-field --config', an_config, '-D . --temp-dir', tempdir, '--ra %f --dec %f' % (ra, dec), '--radius 1', '-L %f -H %f -u app' % (0.9 * pixscale * 3600, 1.1 * pixscale * 3600), '--continue --no-plots --no-remove-lines --uniformize 0', '--no-fits2fits', '-X x_image -Y y_image -s flux_auto --extension 2', '--width %i --height %i' % (W, H), '--crpix-center', '-N none -U none -S none -M none --rdls', self.sdssfn, '--corr', self.corrfn, '--wcs', self.wcsfn, '--temp-axy', '--tag-all', self.sefn ]) print(cmd) if os.system(cmd): raise RuntimeError('Command failed: ' + cmd) if run_psfex and psfex: cmd = ('psfex -c %s -PSF_DIR %s %s -NTHREADS 1' % (os.path.join(sedir, 'gradmap.psfex'), os.path.dirname(self.psffn), self.sefn)) print(cmd) if os.system(cmd): raise RuntimeError('Command failed: ' + cmd) dotpsf = self.psffn.replace('.fits', '.psf') if os.path.exists(dotpsf) and not os.path.exists(self.psffn): cmd = 'mv %s %s' % (dotpsf, self.psffn) print(cmd) if os.system(cmd): raise RuntimeError('Command failed: ' + cmd) if run_psfexfit and psfexfit: print('Fit PSF...') from tractor.basics import GaussianMixtureEllipsePSF, GaussianMixturePSF from tractor.psfex import PsfEx iminfo = self.get_image_info() #print 'img:', iminfo H, W = iminfo['dims'] psfex = PsfEx(self.psffn, W, H, ny=13, nx=7, psfClass=GaussianMixtureEllipsePSF) psfex.savesplinedata = True print('Fitting MoG model to PsfEx') psfex._fitParamGrid(damp=1) pp, XX, YY = psfex.splinedata # Convert to GaussianMixturePSF ppvar = np.zeros_like(pp) for iy in range(psfex.ny): for ix in range(psfex.nx): psf = GaussianMixtureEllipsePSF(*pp[iy, ix, :]) mog = psf.toMog() ppvar[iy, ix, :] = mog.getParams() psfexvar = PsfEx(self.psffn, W, H, ny=psfex.ny, nx=psfex.nx, psfClass=GaussianMixturePSF) psfexvar.splinedata = (ppvar, XX, YY) psfexvar.toFits(self.psffitfn, merge=True) print('Wrote', self.psffitfn)
def run_calibs(self, ra, dec, pixscale, mock_psf, W=2048, H=4096, se=True, astrom=True, psfex=True, sky=True, morph=False, se2=False, psfexfit=True, funpack=True, fcopy=False, use_mask=True, just_check=False): ''' pixscale: in arcsec/pixel just_check: if True, returns True if calibs need to be run. ''' print 'run_calibs:', str(self), 'near RA,Dec', ra,dec, 'with pixscale', pixscale, 'arcsec/pix' for fn in [self.wcsfn, self.sefn, self.psffn, self.psffitfn, self.skyfn]: print 'exists?', os.path.exists(fn), fn self.makedirs() if mock_psf: psfex = False psfexfit = False run_funpack = False run_se = False run_se2 = False run_astrom = False run_psfex = False run_psfexfit = False run_morph = False run_sky = False if se and not all([os.path.exists(fn) for fn in [self.sefn]]): run_se = True run_funpack = True if se2 and not all([os.path.exists(fn) for fn in [self.se2fn]]): run_se2 = True run_funpack = True #if not all([os.path.exists(fn) for fn in [self.wcsfn,self.corrfn,self.sdssfn]]): if astrom and not os.path.exists(self.wcsfn): run_astrom = True if psfex and not os.path.exists(self.psffn): run_psfex = True if psfexfit and not (os.path.exists(self.psffitfn) and os.path.exists(self.psffitellfn)): run_psfexfit = True if morph and not os.path.exists(self.morphfn): run_morph = True run_funpack = True if sky and not os.path.exists(self.skyfn): run_sky = True if just_check: return (run_se or run_se2 or run_astrom or run_psfex or run_psfexfit or run_morph or run_sky) if run_funpack and (funpack or fcopy): tmpimgfn = create_temp(suffix='.fits') tmpmaskfn = create_temp(suffix='.fits') if funpack: cmd = 'funpack -E %i -O %s %s' % (self.hdu, tmpimgfn, self.imgfn) else: cmd = 'imcopy %s"+%i" %s' % (self.imgfn, self.hdu, tmpimgfn) print cmd if os.system(cmd): raise RuntimeError('Command failed: ' + cmd) if use_mask: cmd = 'funpack -E %i -O %s %s' % (self.hdu, tmpmaskfn, self.dqfn) print cmd if os.system(cmd): raise RuntimeError('Command failed: ' + cmd) if run_astrom or run_morph or run_se or run_se2: # grab header values... primhdr = self.read_image_primary_header() hdr = self.read_image_header() magzp = primhdr['MAGZERO'] fwhm = hdr['FWHM'] seeing = pixscale * fwhm print 'FWHM', fwhm, 'pix' print 'pixscale', pixscale, 'arcsec/pix' print 'Seeing', seeing, 'arcsec' if run_se: maskstr = '' if use_mask: maskstr = '-FLAG_IMAGE ' + tmpmaskfn cmd = ' '.join([ 'sex', '-c', os.path.join(sedir, 'DECaLS-v2.sex'), maskstr, '-SEEING_FWHM %f' % seeing, '-PIXEL_SCALE 0', #'-PIXEL_SCALE %f' % (pixscale), '-MAG_ZEROPOINT %f' % magzp, '-CATALOG_NAME', self.sefn, tmpimgfn]) print cmd if os.system(cmd): raise RuntimeError('Command failed: ' + cmd) if run_se2: cmd = ' '.join([ 'sex', '-c', os.path.join(sedir, 'DECaLS-v2-2.sex'), '-FLAG_IMAGE', tmpmaskfn, '-SEEING_FWHM %f' % seeing, '-PIXEL_SCALE %f' % (pixscale), '-MAG_ZEROPOINT %f' % magzp, '-CATALOG_NAME', self.se2fn, tmpimgfn]) print cmd if os.system(cmd): raise RuntimeError('Command failed: ' + cmd) if run_astrom: cmd = ' '.join([ 'solve-field --config', an_config, '-D . --temp-dir', tempdir, '--ra %f --dec %f' % (ra,dec), '--radius 1', '-L %f -H %f -u app' % (0.9 * pixscale, 1.1 * pixscale), '--continue --no-plots --no-remove-lines --uniformize 0', '--no-fits2fits', '-X x_image -Y y_image -s flux_auto --extension 2', '--width %i --height %i' % (W,H), '--crpix-center', '-N none -U none -S none -M none', #'--rdls', self.sdssfn, #'--corr', self.corrfn, '--rdls none --corr none', '--wcs', self.wcsfn, '--temp-axy', '--tag-all', self.sefn]) print cmd if os.system(cmd): raise RuntimeError('Command failed: ' + cmd) if not os.path.exists(self.wcsfn): # Run a second phase... an_config_2 = os.path.join(decals_dir, 'calib', 'an-config', 'cfg2') cmd = ' '.join([ 'solve-field --config', an_config_2, '-D . --temp-dir', tempdir, '--ra %f --dec %f' % (ra,dec), '--radius 1', '-L %f -H %f -u app' % (0.9 * pixscale, 1.1 * pixscale), '--continue --no-plots --uniformize 0', '--no-fits2fits', '-X x_image -Y y_image -s flux_auto --extension 2', '--width %i --height %i' % (W,H), '--crpix-center', '-N none -U none -S none -M none', '--rdls none --corr none', '--wcs', self.wcsfn, '--temp-axy', '--tag-all', self.sefn]) #--no-remove-lines print cmd if os.system(cmd): raise RuntimeError('Command failed: ' + cmd) if run_psfex: cmd = ('psfex -c %s -PSF_DIR %s %s' % (os.path.join(sedir, 'DECaLS-v2.psfex'), os.path.dirname(self.psffn), self.sefn)) print cmd if os.system(cmd): raise RuntimeError('Command failed: ' + cmd) if run_psfexfit: print 'Fit PSF...' from tractor.basics import GaussianMixtureEllipsePSF, GaussianMixturePSF from tractor.psfex import PsfEx iminfo = self.get_image_info() #print 'img:', iminfo H,W = iminfo['dims'] psfex = PsfEx(self.psffn, W, H, ny=13, nx=7, psfClass=GaussianMixtureEllipsePSF) psfex.savesplinedata = True print 'Fitting MoG model to PsfEx' psfex._fitParamGrid(damp=1) pp,XX,YY = psfex.splinedata psfex.toFits(self.psffitellfn, merge=True) print 'Wrote', self.psffitellfn # Convert to GaussianMixturePSF ppvar = np.zeros_like(pp) for iy in range(psfex.ny): for ix in range(psfex.nx): psf = GaussianMixtureEllipsePSF(*pp[iy, ix, :]) mog = psf.toMog() ppvar[iy,ix,:] = mog.getParams() psfexvar = PsfEx(self.psffn, W, H, ny=psfex.ny, nx=psfex.nx, psfClass=GaussianMixturePSF) psfexvar.splinedata = (ppvar, XX, YY) psfexvar.toFits(self.psffitfn, merge=True) print 'Wrote', self.psffitfn if run_morph: cmd = ' '.join(['sex -c', os.path.join(sedir, 'CS82_MF.sex'), '-FLAG_IMAGE', tmpmaskfn, '-SEEING_FWHM %f' % seeing, '-MAG_ZEROPOINT %f' % magzp, '-PSF_NAME', self.psffn, '-CATALOG_NAME', self.morphfn, tmpimgfn]) print cmd if os.system(cmd): raise RuntimeError('Command failed: ' + cmd) if run_sky: img = self.read_image() wt = self.read_invvar() img = img[wt > 0] try: skyval = estimate_mode(img, raiseOnWarn=True) except: skyval = np.median(img) sky = ConstantSky(skyval) tt = type(sky) sky_type = '%s.%s' % (tt.__module__, tt.__name__) hdr = fitsio.FITSHDR() hdr.add_record(dict(name='SKY', value=sky_type, comment='Sky class')) sky.toFitsHeader(hdr, prefix='SKY_') fits = fitsio.FITS(self.skyfn, 'rw', clobber=True) fits.write(None, header=hdr)
def run_calibs(self, ra, dec, pixscale, W=2112, H=4644, se=True, astrom=True, psfex=True, psfexfit=True): ''' pixscale: in degrees/pixel ''' for fn in [self.wcsfn,self.sefn,self.psffn,self.psffitfn]: print 'exists?', os.path.exists(fn), fn self.makedirs() run_fcopy = False run_se = False run_astrom = False run_psfex = False run_psfexfit = False sedir = 'NGVS-g-Single' if not all([os.path.exists(fn) for fn in [self.sefn]]): run_se = True run_fcopy = True if not all([os.path.exists(fn) for fn in [self.wcsfn,self.corrfn,self.sdssfn]]): run_astrom = True if not os.path.exists(self.psffn): run_psfex = True if not os.path.exists(self.psffitfn): run_psfexfit = True if run_fcopy and (run_se and se): tmpimgfn = create_temp(suffix='.fits') cmd = 'imcopy %s"+%i" %s' % (self.imgfn, self.hdu, tmpimgfn) print cmd if os.system(cmd): raise RuntimeError('Command failed: ' + cmd) if run_astrom or run_se: # grab header values... primhdr = self.read_image_primary_header() hdr = self.read_image_header() magzp = hdr['PHOT_C'] + 2.5 * np.log10(hdr['EXPTIME']) seeing = self.seeing print 'Seeing', seeing, 'arcsec' if run_se and se: #'-SEEING_FWHM %f' % seeing, #'-PIXEL_SCALE 0', #'-PIXEL_SCALE %f' % (pixscale * 3600), #'-MAG_ZEROPOINT %f' % magzp, cmd = ' '.join([ 'sex', '-c', os.path.join(sedir, 'psfex.sex'), '-PARAMETERS_NAME', os.path.join(sedir, 'psfex.param'), '-FILTER_NAME', os.path.join(sedir, 'default.conv'), '-CATALOG_NAME', self.sefn, tmpimgfn]) print cmd if os.system(cmd): raise RuntimeError('Command failed: ' + cmd) if run_astrom and astrom: cmd = ' '.join([ 'solve-field --config', an_config, '-D . --temp-dir', tempdir, '--ra %f --dec %f' % (ra,dec), '--radius 1', '-L %f -H %f -u app' % (0.9 * pixscale * 3600, 1.1 * pixscale * 3600), '--continue --no-plots --no-remove-lines --uniformize 0', '--no-fits2fits', '-X x_image -Y y_image -s flux_auto --extension 2', '--width %i --height %i' % (W,H), '--crpix-center', '-N none -U none -S none -M none --rdls', self.sdssfn, '--corr', self.corrfn, '--wcs', self.wcsfn, '--temp-axy', '--tag-all', self.sefn]) print cmd if os.system(cmd): raise RuntimeError('Command failed: ' + cmd) if run_psfex and psfex: cmd = ('psfex -c %s -PSF_DIR %s %s -NTHREADS 1' % (os.path.join(sedir, 'gradmap.psfex'), os.path.dirname(self.psffn), self.sefn)) print cmd if os.system(cmd): raise RuntimeError('Command failed: ' + cmd) dotpsf = self.psffn.replace('.fits', '.psf') if os.path.exists(dotpsf) and not os.path.exists(self.psffn): cmd = 'mv %s %s' % (dotpsf, self.psffn) print cmd if os.system(cmd): raise RuntimeError('Command failed: ' + cmd) if run_psfexfit and psfexfit: print 'Fit PSF...' from tractor.basics import GaussianMixtureEllipsePSF, GaussianMixturePSF from tractor.psfex import PsfEx iminfo = self.get_image_info() #print 'img:', iminfo H,W = iminfo['dims'] psfex = PsfEx(self.psffn, W, H, ny=13, nx=7, psfClass=GaussianMixtureEllipsePSF) psfex.savesplinedata = True print 'Fitting MoG model to PsfEx' psfex._fitParamGrid(damp=1) pp,XX,YY = psfex.splinedata # Convert to GaussianMixturePSF ppvar = np.zeros_like(pp) for iy in range(psfex.ny): for ix in range(psfex.nx): psf = GaussianMixtureEllipsePSF(*pp[iy, ix, :]) mog = psf.toMog() ppvar[iy,ix,:] = mog.getParams() psfexvar = PsfEx(self.psffn, W, H, ny=psfex.ny, nx=psfex.nx, psfClass=GaussianMixturePSF) psfexvar.splinedata = (ppvar, XX, YY) psfexvar.toFits(self.psffitfn, merge=True) print 'Wrote', self.psffitfn