Skip to content

Commit

Permalink
use warnings; catch bad PSF model
Browse files Browse the repository at this point in the history
  • Loading branch information
dstndstn committed Jun 28, 2021
1 parent 8cb7052 commit 4dad2fc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
11 changes: 6 additions & 5 deletions py/legacypipe/decam.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import print_function
import warnings
import numpy as np

from legacypipe.image import LegacySurveyImage, validate_version
Expand Down Expand Up @@ -83,7 +84,7 @@ def get_sky_template_filename(self, old_calibs_ok=False):
from astrometry.util.fits import fits_table
dirnm = os.environ.get('SKY_TEMPLATE_DIR', None)
if dirnm is None:
info('decam: no SKY_TEMPLATE_DIR environment variable set.')
warnings.warn('decam: no SKY_TEMPLATE_DIR environment variable set.')
return None
'''
# Create this sky-scales.kd.fits file via:
Expand All @@ -92,19 +93,19 @@ def get_sky_template_filename(self, old_calibs_ok=False):
'''
fn = os.path.join(dirnm, 'sky-scales.kd.fits')
if not os.path.exists(fn):
info('decam: no $SKY_TEMPLATE_DIR/sky-scales.kd.fits file.')
warnings.warn('decam: no $SKY_TEMPLATE_DIR/sky-scales.kd.fits file.')
return None
from astrometry.libkd.spherematch import tree_open
kd = tree_open(fn, 'expnum')
I = kd.search(np.array([self.expnum]), 0.5, 0, 0)
if len(I) == 0:
info('decam: expnum %i not found in file %s' % (self.expnum, fn))
warning.warn('decam: expnum %i not found in file %s' % (self.expnum, fn))
return None
# Read only the CCD-table rows within range.
S = fits_table(fn, rows=I)
S.cut(np.array([c.strip() == self.ccdname for c in S.ccdname]))
if len(S) == 0:
info('decam: ccdname %s, expnum %i not found in file %s' %
warning.warn('decam: ccdname %s, expnum %i not found in file %s' %
(self.ccdname, self.expnum, fn))
return None
assert(len(S) == 1)
Expand All @@ -125,7 +126,7 @@ def get_sky_template_filename(self, old_calibs_ok=False):
tfn = os.path.join(dirnm, 'sky_templates',
'sky_template_%s_%i.fits.fz' % (self.band, sky.run))
if not os.path.exists(tfn):
info('WARNING: Sky template file %s does not exist' % tfn)
warning.warn('WARNING: Sky template file %s does not exist' % tfn)
return None
return dict(template_filename=tfn, sky_template_dir=dirnm, sky_obj=sky, skyscales_fn=fn)

Expand Down
11 changes: 10 additions & 1 deletion py/legacypipe/image.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import print_function
import os, warnings
import os
import warnings
import numpy as np
import fitsio
from tractor.splinesky import SplineSky
Expand Down Expand Up @@ -768,6 +769,11 @@ def get_tractor_image(self, slc=None, radecpoly=None,
tim.psfnorm = self.psf_norm(tim)
# Galaxy-detection norm
tim.galnorm = self.galaxy_norm(tim)
#print('Galnorm:', tim.galnorm)
if not (np.isfinite(tim.psfnorm) and np.isfinite(tim.galnorm)):
# This can happen if there is something very wrong with the PSF model (NaNs, etc)
warnings.warn('Bad (nan) psfnorm or galnorm for %s' % self)
return None
tim.psf = fullpsf

tim.time = tai
Expand Down Expand Up @@ -1198,6 +1204,9 @@ def read_psf_model(self, x0, y0,
# number of terms in polynomial
ne = (degree + 1) * (degree + 2) // 2
Ti.psf_mask = Ti.psf_mask[:ne, :Ti.psfaxis1, :Ti.psfaxis2]

assert(np.all(np.isfinite(Ti.psf_mask)))

# If degree 0, set polname* to avoid assertion error in tractor
if degree == 0:
Ti.polname1 = 'X_IMAGE'
Expand Down

0 comments on commit 4dad2fc

Please sign in to comment.