Source code for piff.input
# Copyright (c) 2016 by Mike Jarvis and the other collaborators on GitHub at
# https://github.com/rmjarvis/Piff All rights reserved.
#
# Piff is free software: Redistribution and use in source and binary forms
# with or without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the disclaimer given in the accompanying LICENSE
# file.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the disclaimer given in the documentation
# and/or other materials provided with the distribution.
"""
.. module:: input
"""
import numpy as np
import scipy
import glob
import os
import galsim
import yaml
from .util import run_multi
from .star import Star, StarData
from .config import LoggerWrapper
[docs]
class Input(object):
"""The base class for handling inputs for building a Piff model.
This is essentially an abstract base class intended to define the methods that should be
implemented by any derived class.
"""
# This class-level dict will store all the valid input types.
# Each subclass should set a cls._type_name, which is the name that should
# appear in a config dict. These will be the keys of valid_input_types.
# The values in this dict will be the Input sub-classes.
valid_input_types = {}
nproc = 1 # Sub-classes can overwrite this as an instance attribute.
# These shouldn't ever be used, but give these defaults before they are set correctly.
wcs_list = []
pointing = None
bandpass = None
[docs]
@classmethod
def process(cls, config, base=None, logger=None):
"""Parse the input field of the config dict.
:param config: The configuration dict for the input field.
:param base: The base configuration dict.
:param logger: A logger object for logging debug info. [default: None]
:returns: stars, wcs, pointing, bandpass
stars is a list of Star instances with the initial data.
wcs is a dict of WCS solutions indexed by chipnum.
pointing is either a galsim.CelestialCoord or None.
bandpass is either a galsim.Bandpass or None.
"""
# Get the class to use for handling the input data
# Default type is 'Files'
input_type = config.get('type','Files')
base = base if base is not None else {'input' : config}
if input_type not in Input.valid_input_types:
raise ValueError("type %s is not a valid model type. "%input_type +
"Expecting one of %s"%list(Input.valid_input_types.keys()))
input_class = Input.valid_input_types[input_type]
# Build handler object
input_handler = input_class(config, base, logger)
# Creat a lit of StarData objects
stars = input_handler.makeStars(logger)
if len(stars) == 0:
raise RuntimeError("No objects read in from input catalog(s).")
# Get the wcs for all the input chips
wcs = input_handler.getWCS(logger)
# Get the pointing (the coordinate center of the field of view)
pointing = input_handler.getPointing(logger)
# Get the observing bandpass, if any
bandpass = input_handler.getBandpass(logger)
return stars, wcs, pointing, bandpass
@classmethod
def __init_subclass__(cls):
if hasattr(cls, '_type_name') and cls._type_name is not None:
if cls._type_name in Input.valid_input_types:
raise ValueError('Input type %s already registered'%cls._type_name +
'Maybe you subclassed and forgot to set _type_name?')
Input.valid_input_types[cls._type_name] = cls
[docs]
def makeStars(self, logger=None):
"""Process the input images and star data, cutting out stamps for each star along with
other relevant information.
The base class implementation expects the derived class to have appropriately set the
following attributes:
:stamp_size: The size of the postage stamp to use for the cutouts
:x_col: The name of the column in the catalogs to use for the x position.
:y_col: The name of the column in the catalogs to use for the y position.
:param logger: A logger object for logging debug info. [default: None]
:returns: a list of Star instances
"""
logger = LoggerWrapper(logger)
if self.nimages == 1:
logger.debug("Making star list")
else:
logger.debug("Making star list from %d catalogs", self.nimages)
args = [(self.image_kwargs[k], self.cat_kwargs[k], self.wcs_list[k], self.chipnums[k])
for k in range(self.nimages)]
kwargs = dict(stamp_size=self.stamp_size,
pointing=self.pointing, bandpass=self.bandpass, use_partial=self.use_partial,
invert_weight=self.invert_weight,
remove_signal_from_weight=self.remove_signal_from_weight,
config=self.config, base=self.base)
all_stars = run_multi(self._makeStarsFromImage, self.nproc, raise_except=True,
args=args, logger=logger, kwargs=kwargs)
# Concatenate the star lists into a single list
stars = [s for slist in all_stars if slist is not None for s in slist if slist]
logger.info("Read a total of %d objects from %d image%s",len(stars),self.nimages,
"s" if self.nimages > 1 else "")
return stars
[docs]
def getWCS(self, logger=None):
"""Get the WCS solutions for all the chips in the field of view.
:param logger: A logger object for logging debug info. [default: None]
:returns: a dict of WCS solutions (galsim.BaseWCS instances) indexed by chipnum
"""
return { chipnum : w for w, chipnum in zip(self.wcs_list, self.chipnums) }
[docs]
def getPointing(self, logger=None):
"""Get the pointing coordinate of the (noinal) center of the field of view.
:param logger: A logger object for logging debug info. [default: None]
:returns: a galsim.CelestialCoord of the pointing direction.
"""
return self.pointing
[docs]
def getBandpass(self, logger=None):
"""Get the observing bandpass for the input images.
:param logger: A logger object for logging debug info. [default: None]
:returns: a galsim.Bandpass or None.
"""
return self.bandpass
[docs]
class InputFiles(Input):
"""An Input handler that just takes a list of image files and catalog files.
Parse the input config dict (Normally the 'input' field in the overall configuration dict).
The two required fields in the input dict are:
:image_file_name: The file name(s) of the input image(s).
:cat_file_name: The file name(s) of the input catalog(s).
There are a number of ways to specify these file names.
1. A string giving a single file name. e.g.::
image_file_name: image.fits
cat_file_name: input_cat.fits
2. A list of several file names. e.g.::
image_file_name: [image_00.fits, image_01.fits, image_02.fits]
cat_file_name: [input_cat_00.fits, input_cat_01.fits, input_cat_02.fits]
3. A string that glob can recognize to list several file names. e.g.::
image_file_name: image_*.fits
cat_file_name: input_cat_*.fits
4. A dict parseable as a string value according to the GalSim configuration parsing types.
In this case, you also must specify nimages to say how many file names to generate
in this way. e.g.::
nimages: 20
image_file_name:
type: FormattedStr
format: image_%03d_%02d.fits.fz
items:
- { type : Sequence, first: 0, repeat: 4 } # Exposure number
- { type : Sequence, first: 1, last: 4 } # Chip number
cat_file_name:
type: Eval
str: "image_file_name.replace('image','input_cat')"
simage_file_name: '@input.image_file_name'
See the description of the GalSim config parser for more details about the various
types that are valid here.
`https://github.com/GalSim-developers/GalSim/wiki/Config-Values`_
There are many other optional parameters, which help govern how the input files are
read or interpreted:
:chipnum: The id number of this chip used to reference this image [default:
image_num]
:image_hdu: The hdu to use in the image files. [default: None, which means use
either 0 or 1 as typical given the compression sceme of the file]
:weight_hdu: The hdu to use for weight images. [default: None, which means a weight
image with all 1's will be automatically created]
:badpix_hdu: The hdu to use for badpix images. Pixels with badpix != 0 will be given
weight == 0. [default: None]
:noise: Rather than a weight image, provide the noise variance in the image.
(Useful for simulations where this is a known value.) [default: None]
:weight_file_name: If the weight image is in a different file than the main image,
this gives the name of the file to use. [default: None]
:badpix_file_name: If the badpix image is in a different file than the main image,
this gives the name of the file to use. [default: None]
:badpix_zeros: Whether to treat all zeros in the input image as bad pixels.
[default: False]
:cat_hdu: The hdu to use in the catalog files. [default: 1]
:x_col: The name of the X column in the input catalogs. [default: 'x']
:y_col: The name of the Y column in the input catalogs. [default: 'y']
:ra_col: (Alternative to x_col, y_col) The name of a right ascension column in
the input catalogs. Will use the WCS to find (x,y) [default: None]
:dec_col: (Alternative to x_col, y_col) The name of a declination column in
the input catalogs. Will use the WCS to find (x,y) [default: None]
:flag_col: The name of a flag column in the input catalogs. [default: None]
By default, this will skip any objects with flag != 0, but see
skip_flag and use_flag for other possible meanings for how the
flag column can be used to select stars.
:skip_flag: The flag indicating which items to not use. [default: -1]
Items with flag & skip_flag != 0 will be skipped.
:use_flag: The flag indicating which items to use. [default: None]
Items with flag & use_flag == 0 will be skipped.
:property_cols: A list of column names of star properties (e.g. star colors).
[default: None]
:properties: A dict of constant star properties to attach to every star from each
input image. The keys are property names. Values are parsed using
the GalSim config parser for each ``image_num``. Plain strings that
begin with ``$`` or ``@`` are treated as config expressions; other
strings are left as literal strings. The explicit ``type``/``value``
dict form is also supported. [default: None]
:sed_col: The name of a column with per-star SED selectors. If ``sed_file_name``
is not provided, selectors are interpreted as SED file names. If
``sed_file_name`` is provided, selectors are treated as keys in the
SED mapping. [default: None]
:sed_file_name: A single SED file name to use for all stars (if ``sed_col`` is
not provided), or a dict / YAML file mapping ``sed_col`` selectors to
SED file names. [default: None]
:sed_wave_type: Wavelength unit for SED files (e.g. ``nm``, ``Angstrom``).
[required if sed_col or sed_file_name is not None]
:sed_flux_type: Flux convention for SED files (e.g. ``flambda``, ``fnu``,
``fphotons`` or units parseable by astropy.Unit).
[required if sed_col or sed_file_name is not None]
:sed_wave_key: FITS-table column name to use for wavelength when loading
SEDs from FITS tables. [default: ``WAVE``]
:sed_flux_key: FITS-table column name to use for flux when loading SEDs
from FITS tables. [default: ``FLUX``]
:sed_tol: Relative tolerance for reducing the number of samples of input SEDs
based on the calculated flux over the given bandpass. The SEDs are each
reduced on first access, and the reduced SED object is then shared
by all matching stars. Values <= 0 disable this step. [default: 0]
:sed_max_samples:
Maximum number of samples to keep in the effective SED. If set, use a
greedy knot-selection algorithm to choose up to this many samples, then
refit their values for the best piecewise-linear approximation of the
original effective SED. Values <= 0 disable this step. [default: 0]
:bandpass: GalSim bandpass config dict describing the observing bandpass for the
input image(s). Required when using ``sed_col`` or ``sed_file_name``.
[default: None]
.. note::
For chromatic PSF fitting, you must define the SED of each star in one of the
following ways:
1. Use a single SED for each every star.
In this case, the SED should probably be some kind of median SED of the stars
in your sample. You won't be capturing the variation in the chromaticity among
the various stars, but at least you will be using the correct average chromatic
effect of the stellar population. For this case, give the SED file name as
``sed_file_name``.
Sample config file excerpt::
input:
[...]
sed_file_name: 'stellar_sed.fits'
sed_wave_type: 'nm'
sed_flux_type: 'erg cm**(-2) s**(-1) angstrom**(-1)'
2. Specify a file name for the SED of each star.
You may instead want to specify a (potentially) different SED file for each star.
In this case, you can give that file name in the input catalog as one of the
columns. Set ``sed_col`` to the name of the catalog column containing the file names.
Sample config file excerpt::
input:
[...]
sed_col: 'sed_file'
sed_wave_type: 'nm'
sed_flux_type: 'erg cm**(-2) s**(-1) angstrom**(-1)'
3. Specify the SED of each star by some label.
Another way to give each star its own SED is to have the values in the input catalog
be some kind of label indicating which file to use. The mapping of these labels to
file names would then be in a YAML file, which should parse as a dict giving
``sed_file_name[label]``. In this case, specify ``sed_col`` to be the input column
with the labels and ``sed_file_name`` to be the YAML file with the mapping.
Sample config file excerpt::
input:
[...]
sed_col: 'sed_label'
sed_file_name: 'sed_mapping.yaml'
sed_wave_type: 'nm'
sed_flux_type: 'erg cm**(-2) s**(-1) angstrom**(-1)'
If you want, you can also put the mapping dict directly in the config file::
input:
[...]
sed_col: 'stellar_type'
sed_file_name:
'F': 'xsl_spectrum_X0529_merged.fits'
'G': 'xsl_spectrum_X0203_merged.fits'
'K': 'xsl_spectrum_X0066_merged_scl.fits'
'M': 'xsl_spectrum_X0527_merged.fits'
sed_wave_type: 'nm'
sed_flux_type: 'erg cm**(-2) s**(-1) angstrom**(-1)'
In each case, ``sed_wave_type`` and ``sed_flux_type`` are required values, specifying
the units to use for the wavelengths and flux values in the SED file. For SED files
that are FITS tables, you may also want to set ``sed_wave_key`` and ``sed_flux_key`` to
specify which columns correspond to the wave and flux arrays.
:sky_col: The name of a column with sky values. [default: None]
:gain_col: The name of a column with gain values. [default: None]
:sky: The sky level to subtract from the image values. [default: None]
Note: It is an error to specify both sky and sky_col. If both are None,
no sky level will be subtracted off.
.. note::
The special value sky = 'median' means to compute the median of
the image and use that as the sky level. Any other string value
(rather than a float) indicates to get the value from the FITS
header.
:gain: The gain to use for adding Poisson noise to the weight map. [default:
None] It is an error for both gain and gain_col to be specified.
If both are None, then no additional noise will be added to account
for the Poisson noise from the galaxy flux.
:satur: The staturation level. If any pixels for a star exceed this, then
the star is skipped. [default: None]
:trust_pos: Whether to trust the input position as essentially exact (True), or to
let the stars move slightly from their nominal positions (False).
[default: False]
:use_partial: Whether to use stars whose postage stamps are only partially on the
full image. [default: False]
:nstars: Stop reading each input catalog at this many stars. This legacy option
is deprecated; prefer ``select.nstars``, which applies globally after the
selection cuts and keeps the highest-S/N stars. [default: None]
:nproc: How many multiprocessing processes to use for reading in data from
multiple files at once. [default: 1]
:wcs: Normally, the wcs is automatically read in when reading the image.
However, this parameter allows you to optionally provide a different
WCS. It should be defined using the same style as a wcs object
in GalSim config files. [defulat: None]
.. note::
Some values that are nominally floats may be given as FITS header key words instead.
These include sky, gain, and satur. If these are strings (other than sky=median, which
is special), then they should be a valid key word in the FITS header giving the value
to use.
The above values are parsed separately for each input image/catalog. In addition, there
are a couple other parameters that are just parsed once:
:stamp_size: The size of the postage stamps to use for the cutouts. Note: some
stamps may be smaller than this if the star is near a chip boundary.
[default: 32]
:ra, dec: The RA, Dec of the telescope pointing. [default: None; See
`setPointing` for details about how this can be specified]
:param config: The configuration dict used to define the above parameters.
:param base: The base configuration dict if needed for value parsing. [default: None]
:param logger: A logger object for logging debug info. [default: None]
"""
_type_name = 'Files'
def __init__(self, config, base=None, logger=None):
import copy
logger = LoggerWrapper(logger)
req = { 'image_file_name': str,
'cat_file_name': str,
}
opt = {
'dir' : str,
'chipnum' : int,
'x_col' : str,
'y_col' : str,
'ra_col' : str,
'dec_col' : str,
'ra_units' : str,
'dec_units' : str,
'property_cols' : list,
'properties' : dict,
'sed_col' : str,
'sed_file_name' : (str, dict),
'sed_wave_type' : str,
'sed_flux_type' : str,
'sed_wave_key' : str,
'sed_flux_key' : str,
'sed_tol' : float,
'sed_max_samples' : int,
'sky_col' : str,
'gain_col' : str,
'flag_col' : str,
'skip_flag' : int,
'use_flag' : int,
'image_hdu' : int,
'weight_hdu' : int,
'badpix_hdu' : int,
'weight_file_name' : str,
'badpix_file_name' : str,
'badpix_zeros' : bool,
'sky_file_name' : str,
'sky_hdu': int,
'cat_hdu' : int,
'invert_weight' : bool,
'remove_signal_from_weight' : bool,
'stamp_size' : int,
'use_partial' : bool,
'nstars' : int,
}
ignore = [ 'nproc', 'nimages', 'ra', 'dec', 'wcs', 'bandpass',
'sky', 'gain', 'satur', 'noise', 'trust_pos' ]
# We're going to change the config dict a bit. Make a copy so we don't mess up the
# user's original dict (in case they care).
config = copy.deepcopy(config)
base = copy.deepcopy(base) if base is not None else {'input': config}
# In GalSim, the base dict holds additional parameters that may be of use.
# For now, add index_kex, as this will be the only thing we index anything on.
base['index_key'] = 'image_num'
# Convert options 2 and 3 above into option 4. (1 is also parseable by GalSim's config.)
nimages = None
image_list = None
cat_list = None
dir = None
if 'nproc' in config:
self.nproc = galsim.config.ParseValue(config, 'nproc', base, int)[0]
top_level_opt = {
'stamp_size' : int,
'invert_weight' : bool,
'remove_signal_from_weight' : bool,
'use_partial' : bool,
}
top_level_params = galsim.config.GetAllParams(
config, base, opt=top_level_opt, ignore=list(req) + list(opt) + ignore)[0]
config.pop('_get')
self.stamp_size = top_level_params.get('stamp_size', 32)
self.invert_weight = top_level_params.get('invert_weight', False)
self.remove_signal_from_weight = top_level_params.get('remove_signal_from_weight', False)
self.use_partial = top_level_params.get('use_partial', False)
if 'nstars' in config:
logger.warning("input.nstars is deprecated; use select.nstars for the new global "
"highest-S/N behavior.")
if 'nimages' in config:
nimages = galsim.config.ParseValue(config, 'nimages', base, int)[0]
if nimages < 1:
raise ValueError('input.nimages must be >= 1')
if 'image_hdu' in config and isinstance(config['image_hdu'], list):
if len(config['image_hdu']) == 0:
raise ValueError("image_hdu may not be an empty list")
if nimages is None:
nimages = len(config['image_hdu'])
elif nimages != len(config['image_hdu']):
raise ValueError("nimages = %s doesn't match length of image_hdu list (%d)"%(
nimages, len(config['image_hdu'])))
# Deal with dir here, since sometimes we need to have it already attached for glob
# to work.
if 'dir' in config:
dir = galsim.config.ParseValue(config, 'dir', base, str)[0]
del config['dir']
# Convert Current/Eval shorthand before literal filenames are expanded as glob patterns.
for key in ['image_file_name', 'cat_file_name', 'sky_file_name',
'weight_file_name', 'badpix_file_name']:
value = config.get(key)
if isinstance(value, str) and value.startswith('@'):
config[key] = {'type': 'Current', 'key': value[1:]}
elif isinstance(value, str) and value.startswith('$'):
config[key] = {'type': 'Eval', 'str': value[1:]}
if 'image_file_name' not in config:
raise TypeError('Parameter image_file_name is required')
elif isinstance(config['image_file_name'], list):
image_list = config['image_file_name']
if len(image_list) == 0:
raise ValueError("image_file_name may not be an empty list")
if dir is not None:
image_list = [os.path.join(dir, n) for n in image_list]
elif isinstance(config['image_file_name'], str):
image_file_name = config['image_file_name']
if dir is not None:
image_file_name = os.path.join(dir, image_file_name)
image_list = sorted(glob.glob(image_file_name))
if len(image_list) == 0:
raise ValueError("No files found corresponding to "+config['image_file_name'])
elif isinstance(config['image_file_name'], dict):
if nimages is None:
raise TypeError(
'input.nimages is required if not using a list or simple string for ' +
'file names')
else:
raise ValueError("image_file_name should be either a dict or a string")
if image_list is not None:
logger.debug('image_list = %s',image_list)
if len(image_list) == 1 and nimages is not None and nimages > 1:
logger.verbose("Using the same image file for all images")
image_list = image_list * nimages
elif nimages is not None and nimages != len(image_list):
raise ValueError("nimages = %s doesn't match length of image_file_name list (%d)"%(
nimages, len(image_list)))
nimages = len(image_list)
logger.debug('nimages = %d',nimages)
config['image_file_name'] = image_list
logger.debug('nimages = %d',nimages)
assert nimages is not None
# Set up sky_file_name if appropriate
if 'sky_file_name' not in config:
pass
elif isinstance(config['sky_file_name'], str):
sky_file_name = config['sky_file_name']
if dir is not None:
sky_file_name = os.path.join(dir, sky_file_name)
sky_list = sorted(glob.glob(sky_file_name))
if len(sky_list) == 0:
raise ValueError("No files found corresponding to "+config['sky_file_name'])
if len(sky_list) == 1 and nimages is not None and nimages > 1:
logger.verbose("Using the same sky file for all images")
sky_list = sky_list * nimages
elif nimages is not None and len(sky_list) != nimages:
raise ValueError("nimages = %s doesn't match length of sky_file_name list (%d)"%(
nimages, len(sky_list)))
config['sky_file_name'] = sky_list
elif not isinstance(config['sky_file_name'], dict):
raise ValueError("sky_file_name should be either a dict or a string")
if 'weight_file_name' not in config:
pass
elif isinstance(config['weight_file_name'], str):
weight_file_name = config['weight_file_name']
if dir is not None:
weight_file_name = os.path.join(dir, weight_file_name)
weight_list = sorted(glob.glob(weight_file_name))
if len(weight_list) == 0:
raise ValueError("No files found corresponding to "+config['weight_file_name'])
if len(weight_list) == 1 and nimages is not None and nimages > 1:
logger.verbose("Using the same weight file for all images")
weight_list = weight_list * nimages
elif nimages is not None and len(weight_list) != nimages:
raise ValueError("nimages = %s doesn't match length of weight_file_name list (%d)"%(
nimages, len(weight_list)))
config['weight_file_name'] = weight_list
elif not isinstance(config['weight_file_name'], dict):
raise ValueError("weight_file_name should be either a dict or a string")
if 'badpix_file_name' not in config:
pass
elif isinstance(config['badpix_file_name'], str):
badpix_file_name = config['badpix_file_name']
if dir is not None:
badpix_file_name = os.path.join(dir, badpix_file_name)
badpix_list = sorted(glob.glob(badpix_file_name))
if len(badpix_list) == 0:
raise ValueError("No files found corresponding to "+config['badpix_file_name'])
if len(badpix_list) == 1 and nimages is not None and nimages > 1:
logger.verbose("Using the same badpix file for all images")
badpix_list = badpix_list * nimages
elif nimages is not None and len(badpix_list) != nimages:
raise ValueError("nimages = %s doesn't match length of badpix_file_name list (%d)"%(
nimages, len(badpix_list)))
config['badpix_file_name'] = badpix_list
elif not isinstance(config['badpix_file_name'], dict):
raise ValueError("badpix_file_name should be either a dict or a string")
if 'cat_file_name' not in config:
raise TypeError('Parameter cat_file_name is required')
elif isinstance(config['cat_file_name'], list):
cat_list = config['cat_file_name']
if len(cat_list) == 0:
raise ValueError("cat_file_name may not be an empty list")
if dir is not None:
cat_list = [os.path.join(dir, n) for n in cat_list]
elif isinstance(config['cat_file_name'], str):
cat_file_name = config['cat_file_name']
if dir is not None:
cat_file_name = os.path.join(dir, cat_file_name)
cat_list = sorted(glob.glob(cat_file_name))
if len(cat_list) == 0:
raise ValueError("No files found corresponding to "+config['cat_file_name'])
elif not isinstance(config['cat_file_name'], (dict, type(None))):
raise ValueError("cat_file_name should be either a dict or a string")
if cat_list is not None:
logger.debug('cat_list = %s',cat_list)
if len(cat_list) == 1 and nimages > 1:
logger.verbose("Using the same catlist for all image")
cat_list = cat_list * nimages
elif nimages != len(cat_list):
raise ValueError("nimages = %s doesn't match length of cat_file_name list (%d)"%(
nimages, len(cat_list)))
config['cat_file_name'] = cat_list
self.nimages = nimages
self.chipnums = list(range(nimages))
self.image_file_name = []
self.cat_file_name = []
self.image_kwargs = []
self.cat_kwargs = []
self.config = galsim.config.CleanConfig(config)
self.base = galsim.config.CleanConfig(base)
logger.verbose("Reading in %d images",nimages)
for image_num in range(nimages):
# This changes for each input image.
galsim.config.RemoveCurrent(base)
base['index_key'] = 'image_num'
base['image_num'] = image_num
logger.debug("config = %s", config)
params = galsim.config.GetAllParams(config, base, req=req, opt=opt, ignore=ignore)[0]
logger.debug("image_num = %d: params = %s", image_num, params)
# Update the chipnum if not just using image_num
if 'chipnum' in params:
self.chipnums[image_num] = params['chipnum']
# Read the image
image_file_name = params['image_file_name']
image_hdu = params.get('image_hdu', None)
weight_file_name = params.get('weight_file_name', None)
weight_hdu = params.get('weight_hdu', None)
badpix_file_name = params.get('badpix_file_name', None)
badpix_hdu = params.get('badpix_hdu', None)
badpix_zeros = params.get('badpix_zeros', False)
sky_file_name = params.get('sky_file_name', None)
sky_hdu = params.get('sky_hdu', None)
self.image_file_name.append(image_file_name)
self.image_kwargs.append({
'image_file_name' : image_file_name,
'image_hdu' : image_hdu,
'weight_file_name' : weight_file_name,
'weight_hdu' : weight_hdu,
'badpix_file_name' : badpix_file_name,
'badpix_hdu' : badpix_hdu,
'badpix_zeros' : badpix_zeros,
'sky_file_name' : sky_file_name,
'sky_hdu' : sky_hdu,
'image_num' : image_num,
'config' : self.config,
'base' : self.base,
})
# Read the catalog
cat_file_name = params['cat_file_name']
cat_hdu = params.get('cat_hdu', None)
x_col = params.get('x_col', 'x')
y_col = params.get('y_col', 'y')
ra_col = params.get('ra_col', None)
dec_col = params.get('dec_col', None)
ra_units = params.get('ra_units', 'deg')
dec_units = params.get('dec_units', 'deg')
flag_col = params.get('flag_col', None)
skip_flag = params.get('skip_flag', -1)
use_flag = params.get('use_flag', None)
property_cols = params.get('property_cols', None)
properties = params.get('properties', None)
sed_col = params.get('sed_col', None)
sed_file_name = params.get('sed_file_name', None)
sed_wave_type = params.get('sed_wave_type', None)
sed_flux_type = params.get('sed_flux_type', None)
sed_wave_key = params.get('sed_wave_key', 'WAVE')
sed_flux_key = params.get('sed_flux_key', 'FLUX')
sed_tol = params.get('sed_tol', 0.0)
sed_max_samples = params.get('sed_max_samples', 0)
if 'bandpass' in config:
bandpass = galsim.config.BuildBandpass(config, 'bandpass', base, logger)[0]
else:
if sed_col is not None or sed_file_name is not None:
raise ValueError("bandpass is required when using sed_col or sed_file_name")
bandpass = None
sky_col = params.get('sky_col', None)
gain_col = params.get('gain_col', None)
nstars = params.get('nstars', None)
if sky_col is not None and 'sky' in config:
raise ValueError("Cannot provide both sky_col and sky.")
if gain_col is not None and 'gain' in config:
raise ValueError("Cannot provide both gain_col and gain.")
self.cat_file_name.append(cat_file_name)
self.cat_kwargs.append({
'cat_file_name' : cat_file_name,
'cat_hdu' : cat_hdu,
'x_col' : x_col,
'y_col' : y_col,
'ra_col' : ra_col,
'dec_col' : dec_col,
'ra_units' : ra_units,
'dec_units' : dec_units,
'flag_col' : flag_col,
'skip_flag' : skip_flag,
'use_flag' : use_flag,
'property_cols': property_cols,
'properties': properties,
'sed_col': sed_col,
'sed_file_name': sed_file_name,
'sed_wave_type': sed_wave_type,
'sed_flux_type': sed_flux_type,
'sed_wave_key': sed_wave_key,
'sed_flux_key': sed_flux_key,
'sed_tol': sed_tol,
'sed_max_samples': sed_max_samples,
'sky_col' : sky_col,
'gain_col' : gain_col,
'nstars' : nstars,
'stamp_size' : self.stamp_size,
'config' : self.config,
'base' : self.base,
})
self.bandpass = bandpass
# Read all the wcs's, since we'll need this for the pointing, which in turn we'll
# need for when we make the stars.
self.setWCS(config, base, logger)
# Finally, set the pointing coordinate.
# Note: setPointing will do the config parsing of these if appropriate.
ra = config.get('ra', None)
dec = config.get('dec', None)
self.setPointing(ra, dec, logger)
[docs]
def load_images(self, stars, logger=None):
"""Load the image data into a list of Stars.
We don't store the image data for Stars when we write them to a file, since that
would take up a lot of space and is usually not desired. However, we do store the
bounds in the original image where the star was cutout, so if you want to load back in
the original data from the image file(s), you can do so with this function.
:param stars: A list of Star instances.
:param logger: A logger object for logging debug info. [default: None]
:returns: a new list of Stars with the images information loaded.
"""
logger = LoggerWrapper(logger)
images = {}
weight_images = {}
for image_num in range(self.nimages):
image, weight, _, _ = self.getRawImageData(image_num)
images[self.chipnums[image_num]] = image
weight_images[self.chipnums[image_num]] = weight
loaded_stars = []
for star in stars:
chipnum = star['chipnum']
image = images[chipnum][star.data.image.bounds].copy()
if 'sky' in star.data.properties:
image -= star['sky']
weight = weight_images[chipnum][star.data.weight.bounds].copy()
data = star.data.withNew(image=image, weight=weight)
loaded_stars.append(Star(data=data, fit=star.fit))
return loaded_stars
[docs]
def getRawImageData(self, image_num, logger=None):
"""Read an image and catalog.
:param image_num: The index of the image to read.
:param logger: A logger object for logging debug info. [default: None]
:returns: image, weight, image_pos, extra_props.
"""
self.base['index_key'] = 'image_num'
self.base['image_num'] = image_num
return self._getRawImageData(self.image_kwargs[image_num], self.cat_kwargs[image_num],
self.wcs_list[image_num], self.invert_weight,
self.remove_signal_from_weight,
config=self.config, base=self.base, logger=logger)
@staticmethod
def _handle_depr_image_value(value, image, name, logger):
# gain, satur, noise, and sky used to be allowed to be in a FITS header.
# Now this functionality should use the ImageHeaderValue type to be more explicit.
# The old behavior still works, but is deprecated.
if isinstance(value, str) and value[0] not in '@$':
logger.error("WARNING: Using a bare FITS header key for %s is deprecated."
"Use { 'type': 'ImageHeaderValue': 'key': %s } instead.",
name, value)
value = float(image.header[value])
return value
@staticmethod
def _getRawImageData(image_kwargs, cat_kwargs, wcs,
invert_weight, remove_signal_from_weight,
config, base, logger=None):
from .config import LoggerWrapper
logger = LoggerWrapper(logger)
image, weight = InputFiles.readImage(logger=logger, **image_kwargs)
if invert_weight:
weight.invertSelf()
# Update the wcs
image.wcs = wcs
image_pos, extra_props = InputFiles.readStarCatalog(
logger=logger, image=image, **cat_kwargs)
if remove_signal_from_weight:
# Subtract off the mean sky, since this isn't part of the "signal" we want to
# remove from the weights.
if 'sky' in extra_props:
signal = image - np.mean(extra_props['sky'])
else:
signal = image
if 'gain' in extra_props:
# If given, use the mean gain when removing the signal.
# This isn't quite right, but hopefully the gain won't vary too much for
# different objects, so it should be close.
gain = np.mean(extra_props['gain'])
weight, _ = InputFiles._removeSignalFromWeight(signal, weight, gain=gain)
else:
# If no gain provided, then we want to estimate the gain from the weight image.
weight, g = InputFiles._removeSignalFromWeight(signal, weight)
extra_props['gain'] = np.array([g] * len(image_pos), dtype=float)
logger.info("Empirically determined gain = %f",g)
logger.verbose("Removed signal from weight image.")
return image, weight, image_pos, extra_props
@staticmethod
def _makeStarsFromImage(image_kwargs, cat_kwargs, wcs, chipnum,
stamp_size, pointing, bandpass, use_partial,
invert_weight, remove_signal_from_weight,
config, base, logger):
"""Make "stars" from a single input image
"""
image, wt, image_pos, extra_props = InputFiles._getRawImageData(
image_kwargs, cat_kwargs, wcs, invert_weight, remove_signal_from_weight,
config, base, logger)
logger.verbose("Processing catalog %s with %d objects",chipnum,len(image_pos))
objects = []
# Make a dict of the types to use for the extra_props, so we can preserve the type
# if we need to output them (e.g. in HSMCatalog).
prop_types = {key: extra_props[key].dtype.type for key in extra_props}
for k in range(len(image_pos)):
x = image_pos[k].x
y = image_pos[k].y
icen = int(x+0.5)
jcen = int(y+0.5)
half_size = stamp_size // 2
bounds = galsim.BoundsI(icen+half_size-stamp_size+1, icen+half_size,
jcen+half_size-stamp_size+1, jcen+half_size)
if not image.bounds.includes(bounds):
bounds = bounds & image.bounds
if not bounds.isDefined():
logger.warning("Object at position %f,%f is off the edge of the image.", x, y)
logger.warning("Skipping this object.")
continue
if use_partial:
logger.verbose("Object at position %f,%f overlaps the edge of the image. "
"Using smaller than the full stamp size: %s", x, y, bounds)
else:
logger.info("Object at position %f,%f overlaps the edge of the image.", x, y)
logger.info("Skipping this object.")
continue
stamp = image[bounds].copy()
wt_stamp = wt[bounds].copy()
props = { 'chipnum' : chipnum }
# if an object is totally masked, then don't add it!
if np.all(wt_stamp.array == 0):
logger.warning("Object at position %f,%f is completely masked.", x, y)
logger.warning("Skipping this object.")
continue
# Add this object's entry in each list of the extra_props dictionary
# to the StarData properties dictionary
for key in extra_props:
logger.debug("Assigning {} value = {}".format(key, extra_props[key][k]))
props[key] = extra_props[key][k]
# If any pixels are saturated, skip it.
if 'satur' in props:
max_val = np.max(stamp.array)
if max_val > props['satur']:
logger.info("Object at position %f,%f has saturated pixels.", x, y)
logger.info("Maximum value is %f.", max_val)
logger.info("Skipping this object.")
continue
# Subtract the sky
if 'sky' in props:
sky = props['sky']
logger.debug("Subtracting off sky = %f", sky)
logger.debug("Median pixel value = %f", np.median(stamp.array))
stamp -= sky
pos = galsim.PositionD(x,y)
data = StarData(stamp, pos, weight=wt_stamp, pointing=pointing,
bandpass=bandpass,
properties=props, property_types=prop_types)
# Attach an attribute so we can tell later whether the weight is real or
# if it's just the default w=1.0.
data._piff_default_weight = getattr(wt, '_piff_default_weight', False)
star = Star(data, None)
objects.append(star)
return objects
def setWCS(self, config, base, logger):
self.wcs_list = []
self.center_list = []
for image_num, kwargs in enumerate(self.image_kwargs):
galsim.config.RemoveCurrent(base) # Makes any @ items work correctly
base['image_num'] = image_num
image_file_name = kwargs['image_file_name']
image_hdu = kwargs['image_hdu']
image = galsim.fits.read(image_file_name, hdu=image_hdu, read_header=True)
if 'wcs' in config:
logger.info("Using custom wcs from config for %s",image_file_name)
base['index_key'] = 'image_num'
base['image_num'] = image_num
base['_current_image'] = image
wcs = galsim.config.BuildWCS(config, 'wcs', base, logger)
else:
logger.info("Getting wcs from image file %s",image_file_name)
wcs = image.wcs
self.wcs_list.append(wcs)
self.center_list.append(image.true_center)
@staticmethod
def _removeSignalFromWeight(image, weight, gain=None):
"""Remove the image signal from the weight map.
:param image: The image to use as the signal
:param weight: The weight image.
:param gain: Optionally, the gain to use as the proportionality relation.
If gain is None, then it will be estimated automatically and returned.
[default: None]
:returns: newweight, gain
"""
signal = image.array
variance = 1./weight.array
use = (weight.array != 0.) & np.isfinite(signal)
if gain is None:
fit = np.polyfit(signal[use].flatten(), variance[use].flatten(), deg=1)
gain = 1./fit[0] # fit is [ 1/gain, sky_var ]
variance[use] -= signal[use] / gain
newweight = weight.copy()
newweight.array[use] = 1. / variance[use]
return newweight, gain
[docs]
@staticmethod
def readImage(image_file_name, image_hdu, weight_file_name, weight_hdu,
badpix_file_name, badpix_hdu, badpix_zeros, sky_file_name, sky_hdu,
image_num, config, base, logger):
"""Read in the image and weight map (or make one if no weight information is given
:param image_file_name: The name of the file to read.
:param image_hdu: The hdu of the main image.
:param weight_file_name: The name if the file with the weight image (if any).
:param weight_hdu: The hdu of the weight image (if any).
:param badpix_file_name: The name of the file with the bad pixel mask (if any).
:param badpix_hdu: The hdu of the bad pixel mask (if any).
:param badpix_zeros: Whether to treat all zeros in the input image as bad pixels.
[default: False]
:param sky_file_name: A file to use for a sky background to subtract from the image
(if any).
:param sky_hdu: The hdu to use in the sky_file_name (if any).
:param config: The input section of the config dict.
:param base: The base config dict.
:param logger: A logger object for logging debug info.
:returns: image, weight
"""
# Read in the image
logger.info("Reading image file %s",image_file_name)
galsim.config.RemoveCurrent(base)
base['image_num'] = image_num
image = galsim.fits.read(image_file_name, hdu=image_hdu, read_header=True)
base['_current_image'] = image
noise = None
if 'noise' in config:
noise = galsim.config.ParseValue(config, 'noise', base, (float, str))[0]
sky = None
if 'sky' in config:
sky = galsim.config.ParseValue(config, 'sky', base, (float, str))[0]
# Make sure dtype is at least float32
if image.dtype not in [np.float32, np.float64]:
header = image.header # This doesn't get copied by view()
image = image.view(dtype=np.float32)
image.header = header
config['_current_image'] = image
if noise is not None:
noise = galsim.config.ParseValue(config, 'noise', base, None)[0]
noise = InputFiles._handle_depr_image_value(noise, image, 'noise', logger)
# Either read in the weight image, or build a dummy one
if weight_file_name is not None or weight_hdu is not None:
weight_file_name = weight_file_name or image_file_name
weight_hdu = weight_hdu or (1 if weight_file_name.endswith('.fz') else 0)
logger.verbose("Reading weight image from %s, hdu %d.", weight_file_name, weight_hdu)
weight = galsim.fits.read(weight_file_name, hdu=weight_hdu)
if np.all(weight.array == 0):
logger.error("According to the weight mask in %s, all pixels have zero weight!",
image_file_name)
if np.any(weight.array < 0):
logger.warning("Warning: weight map has invalid negative-valued pixels. "+
"Taking them to be 0.0")
weight.array[weight.array < 0] = 0.
weight._piff_default_weight = False
elif noise is not None:
logger.debug("Making uniform weight image based on noise variance = %f", noise)
weight = galsim.ImageF(image.bounds, init_value=1./noise)
weight._piff_default_weight = False
else:
logger.debug("Making trivial (wt==1) weight image")
weight = galsim.ImageF(image.bounds, init_value=1)
weight._piff_default_weight = True
# Make sure to do this before anything that could modify the input image.
if badpix_zeros:
weight.array[image.array == 0] = 0
# If requested, subtract a sky image
if sky_file_name is not None:
hdu_str = " from hdu %s"%(sky_hdu) if sky_hdu is not None else ""
logger.verbose("Reading sky image %s.", sky_file_name + hdu_str)
sky = galsim.fits.read(sky_file_name, hdu=sky_hdu)
image -= sky
# If requested, set wt=0 for any bad pixels
if badpix_file_name is not None or badpix_hdu is not None:
badpix_file_name = badpix_file_name or image_file_name
badpix_hdu = badpix_hdu or (1 if badpix_file_name.endswith('.fz') else 0)
logger.verbose("Reading badpix image from %s, hdu %d.", badpix_file_name, badpix_hdu)
badpix = galsim.fits.read(badpix_file_name, hdu=badpix_hdu)
# The badpix image may be offset by 32768 from the true value.
# If so, subtract it off.
if np.any(badpix.array > 32767): # pragma: no cover
logger.debug('min(badpix) = %s',np.min(badpix.array))
logger.debug('max(badpix) = %s',np.max(badpix.array))
logger.debug("subtracting 32768 from all values in badpix image")
badpix -= 32768
if np.any(badpix.array < -32767): # pragma: no cover
logger.debug('min(badpix) = %s',np.min(badpix.array))
logger.debug('max(badpix) = %s',np.max(badpix.array))
logger.debug("adding 32768 to all values in badpix image")
badpix += 32768
if np.all(badpix.array != 0): # pragma: no cover
logger.error("According to the bad pixel array in %s, all pixels are masked!",
image_file_name)
weight.array[badpix.array != 0] = 0
if sky == 'median':
ok = weight.array != 0
sky = np.median(image.array[ok])
image.array[ok] -= sky
return image, weight
@staticmethod
def _flag_select(col, flag):
if len(col.shape) == 1:
# Then just treat this as a straightforward bitmask.
return col & flag
else:
# Then treat this as an array of bools rather than a bitmask
mask = np.zeros(col.shape[0], dtype=bool)
for bit in range(col.shape[1]): # pragma: no branch
if flag % 2 == 1:
mask |= col[:,bit]
flag = flag // 2
if flag == 0: break
return mask
@classmethod
def _read_cat(cls, cat_file_name, cat_hdu):
if cat_file_name.endswith('.fits'):
return cls._read_fits_cat(cat_file_name, cat_hdu)
elif cat_file_name.endswith('.parquet'):
return cls._read_parquet_cat(cat_file_name)
else:
# For other endings, try parquet then fits so error message is for fits.
try:
return cls._read_parquet_cat(cat_file_name)
except Exception:
return cls._read_fits_cat(cat_file_name, cat_hdu)
@classmethod
def _read_fits_cat(cls, cat_file_name, cat_hdu):
import fitsio
return fitsio.read(cat_file_name, cat_hdu)
@classmethod
def _read_parquet_cat(cls, cat_file_name):
import pyarrow.parquet as pq
tab = pq.read_table(cat_file_name)
# This works, but requires pandas
#return tab.to_pandas().to_records(index=False)
# This doesn't required pandas
return np.rec.fromarrays(tab.columns, names=tab.schema.names)
@staticmethod
def _resolve_sed_file_name(value, cat_file_name):
sed_file_name = os.fsdecode(value).strip()
if sed_file_name == '':
raise ValueError("Encountered blank SED file name")
if os.path.isabs(sed_file_name):
return sed_file_name
cat_dir = os.path.dirname(os.path.abspath(cat_file_name))
cat_relative = os.path.join(cat_dir, sed_file_name)
if os.path.isfile(cat_relative):
return cat_relative
else:
return sed_file_name
@staticmethod
def _read_sed_mapping(sed_file_name, cat_file_name):
if isinstance(sed_file_name, dict):
return sed_file_name
sed_map_file = InputFiles._resolve_sed_file_name(sed_file_name, cat_file_name)
with open(sed_map_file) as f:
sed_map = yaml.safe_load(f)
if not isinstance(sed_map, dict):
raise ValueError("SED mapping file must parse to a dict: %r" % sed_map_file)
return sed_map
[docs]
@staticmethod
def readStarCatalog(cat_file_name, cat_hdu, x_col, y_col,
ra_col, dec_col, ra_units, dec_units, image,
flag_col, skip_flag, use_flag, property_cols, sed_col, sed_wave_type,
sed_file_name, sed_flux_type, sed_wave_key, sed_flux_key,
sed_tol, sed_max_samples,
properties, sky_col, gain_col, nstars, stamp_size,
config, base, logger):
"""Read in the star catalogs and return lists of positions for each star in each image.
:param cat_file_name: The name of the catalog file to read in.
:param cat_hdu: The hdu to use.
:param x_col: The name of the column with x values.
:param y_col: The name of the column with y values.
:param ra_col: The name of a column with RA values.
:param dec_col: The name of a column with Dec values.
:param ra_units: The units of the ra column.
:param dec_units: The units of the dec column.
:param image: The image that was already read in (mostly for the wcs).
:param flag_col: The name of a column with flag values.
:param skip_flag: The flag indicating which items to not use. [default: -1]
Items with flag & skip_flag != 0 will be skipped.
:param use_flag: The flag indicating which items to use. [default: None]
Items with flag & use_flag == 0 will be skipped.
:param property_cols: A list of column names with star properties (e.g. star colors).
:param properties: A dict of additional star properties to attach to every star.
:param sed_col: A column with per-star SED file names.
:param sed_file_name: A single SED file name for all stars (if sed_col is None).
:param sed_wave_type: Wave type for SED files.
:param sed_flux_type: Flux type for SED files.
:param sed_wave_key: FITS-table wavelength column name for SED files.
:param sed_flux_key: FITS-table flux column name for SED files.
:param sed_tol: Relative tolerance for reducing loaded SEDs.
:param sed_max_samples: Maximum number of retained SED samples.
:param sky_col: A column with sky (background) levels.
:param gain_col: A column with gain values.
:param nstars: Optionally a maximum number of stars to use from this catalog.
:param stamp_size: The stamp size being used for the star stamps.
:param config: The input section of the config dict.
:param base: The base config dict.
:param logger: A logger object for logging debug info. [default: None]
:returns: lists image_pos, extra_props
"""
import fitsio
base['_current_image'] = image
if cat_file_name is None:
# This is possible e.g. when loading images into an existing list of star instances.
logger.info("Not reading input catalog.")
return None, None
# Read in the star catalog
logger.info("Reading input catalog %s.",cat_file_name)
cat = InputFiles._read_cat(cat_file_name, cat_hdu)
sky = None
if 'sky' in config:
sky = galsim.config.ParseValue(config, 'sky', base, (float, str))[0]
gain = None
if 'gain' in config:
gain = galsim.config.ParseValue(config, 'gain', base, (float, str))[0]
satur = None
if 'satur' in config:
satur = galsim.config.ParseValue(config, 'satur', base, (float, str))[0]
trust_pos = None
if 'trust_pos' in config:
trust_pos = galsim.config.ParseValue(config, 'trust_pos', base, bool)[0]
if flag_col is not None:
if flag_col not in cat.dtype.names:
raise ValueError("flag_col = %s is not a column in %s"%(flag_col,cat_file_name))
col = cat[flag_col]
if len(col.shape) == 2:
logger.info("Flag col (%s) is multidimensional. Treating as an array of bool",
flag_col)
if use_flag is not None:
# Remove any objects with flag & use_flag == 0
mask = InputFiles._flag_select(col, use_flag) == 0
logger.verbose("Removing objects with flag (col %s) & %d == 0",flag_col,use_flag)
if skip_flag != -1:
mask |= InputFiles._flag_select(col, skip_flag) != 0
logger.verbose("Removing objects with flag (col %s) & %d != 0",flag_col,skip_flag)
else:
# Remove any objects with flag & skip_flag != 0
mask = InputFiles._flag_select(col, skip_flag) != 0
if skip_flag == -1:
logger.verbose("Removing objects with flag (col %s) != 0",flag_col)
else:
logger.verbose("Removing objects with flag (col %s) & %d != 0",flag_col,skip_flag)
cat = cat[mask == 0]
# Limit to nstars objects
if nstars is not None and nstars < len(cat):
logger.verbose("Limiting to %d objects for %s", nstars, cat_file_name)
cat = cat[:nstars]
# Make the list of positions:
if ra_col is not None or dec_col is not None:
if ra_col is None or dec_col is None:
raise ValueError("ra_col and dec_col are both required if one is provided.")
if ra_col not in cat.dtype.names:
raise ValueError("ra_col = %s is not a column in %s"%(ra_col,cat_file_name))
if dec_col not in cat.dtype.names:
raise ValueError("dec_col = %s is not a column in %s"%(dec_col,cat_file_name))
logger.debug("Starting to make a list of positions from ra, dec")
ra_values = cat[ra_col]
dec_values = cat[dec_col]
ra_units = galsim.AngleUnit.from_name(ra_units)
dec_units = galsim.AngleUnit.from_name(dec_units)
ra = ra_values * ra_units
dec = dec_values * dec_units
logger.debug("Initially %d positions",len(ra))
# First limit to only those that could possibly be on the image by checking the
# min/max ra and dec from the image corners.
cen = image.wcs.toWorld(image.center)
logger.debug("Center at %s",cen)
x_corners = [image.xmin, image.xmin, image.xmax, image.xmax]
y_corners = [image.ymin, image.ymax, image.ymax, image.ymin]
corners = [image.wcs.toWorld(galsim.PositionD(x,y))
for (x,y) in zip(x_corners, y_corners)]
logger.debug("Corners at %s",corners)
min_ra = np.min([c.ra.wrap(cen.ra) for c in corners])
max_ra = np.max([c.ra.wrap(cen.ra) for c in corners])
min_dec = np.min([c.dec.wrap(cen.dec) for c in corners])
max_dec = np.max([c.dec.wrap(cen.dec) for c in corners])
logger.debug("RA range = %s .. %s",min_ra,max_ra)
logger.debug("Dec range = %s .. %s",min_dec,max_dec)
use = [(r.wrap(cen.ra) > min_ra) & (r.wrap(cen.ra) < max_ra) &
(d.wrap(cen.dec) > min_dec) & (d.wrap(cen.dec) < max_dec)
for r,d in zip(ra,dec)]
ra = ra[use]
dec = dec[use]
logger.debug("After limiting to image ra,dec range, len = %s",len(ra))
# Now convert to x,y
def safe_to_image(wcs, ra, dec):
try:
return wcs.toImage(galsim.CelestialCoord(ra, dec))
except galsim.GalSimError: # pragma: no cover
# If the ra,dec is way off the image, this might fail to converge.
# In this case return None, which we can get rid of simply.
return None
image_pos = [ safe_to_image(image.wcs,r,d) for r,d in zip(ra, dec) ]
image_pos = [ pos for pos in image_pos if pos is not None ]
logger.debug("Resulting image_pos list has %s positions",len(image_pos))
else:
if x_col not in cat.dtype.names:
raise ValueError("x_col = %s is not a column in %s"%(x_col,cat_file_name))
if y_col not in cat.dtype.names:
raise ValueError("y_col = %s is not a column in %s"%(y_col,cat_file_name))
x_values = cat[x_col]
y_values = cat[y_col]
logger.debug("Initially %d positions",len(x_values))
image_pos = [ galsim.PositionD(x,y) for x,y in zip(x_values, y_values) ]
# Check for objects well off the edge. We won't use them.
big_bounds = image.bounds.expand(stamp_size)
image_pos = [ pos for pos in image_pos if big_bounds.includes(pos) ]
logger.debug("After remove those that are off the image, len = %s",len(image_pos))
# Make a dictionary of the star properties:
extra_props = {}
if property_cols is not None:
logger.debug('property_cols = %s'%property_cols)
for col_name in property_cols:
if col_name not in cat.dtype.names:
raise ValueError("Entry in property_cols = " +
"%s is not a column in %s"%(col_name,cat_file_name))
extra_props[col_name] = cat[col_name]
if sed_col is not None:
if sed_col not in cat.dtype.names:
raise ValueError("sed_col = %s is not a column in %s" % (sed_col, cat_file_name))
sed_map = None
if sed_file_name is not None:
sed_map = InputFiles._read_sed_mapping(sed_file_name, cat_file_name)
sed_file_name_values = []
for sed_label in cat[sed_col]:
if sed_map is not None:
sed_label = os.fsdecode(sed_label).strip()
if sed_label not in sed_map:
raise ValueError(
"sed_col value %r is not in sed_file_name mapping" % sed_label
)
sed_file_name = sed_map[sed_label]
else:
sed_file_name = sed_label
sed_file_name = InputFiles._resolve_sed_file_name(sed_file_name, cat_file_name)
sed_file_name_values.append(sed_file_name)
extra_props['sed_file_name'] = np.array(sed_file_name_values, dtype=object)
extra_props['sed_wave_type'] = np.array([sed_wave_type] * len(cat), dtype=object)
extra_props['sed_flux_type'] = np.array([sed_flux_type] * len(cat), dtype=object)
extra_props['sed_wave_key'] = np.array([sed_wave_key] * len(cat), dtype=object)
extra_props['sed_flux_key'] = np.array([sed_flux_key] * len(cat), dtype=object)
extra_props['sed_tol'] = np.full(len(cat), sed_tol, dtype=float)
extra_props['sed_max_samples'] = np.full(len(cat), sed_max_samples, dtype=int)
elif sed_file_name is not None:
sed_file_name = InputFiles._resolve_sed_file_name(sed_file_name, cat_file_name)
extra_props['sed_file_name'] = np.array([sed_file_name] * len(cat), dtype=object)
extra_props['sed_wave_type'] = np.array([sed_wave_type] * len(cat), dtype=object)
extra_props['sed_flux_type'] = np.array([sed_flux_type] * len(cat), dtype=object)
extra_props['sed_wave_key'] = np.array([sed_wave_key] * len(cat), dtype=object)
extra_props['sed_flux_key'] = np.array([sed_flux_key] * len(cat), dtype=object)
extra_props['sed_tol'] = np.full(len(cat), sed_tol, dtype=float)
extra_props['sed_max_samples'] = np.full(len(cat), sed_max_samples, dtype=int)
# If we used a flag column, keep it as a property.
if flag_col is not None:
extra_props[flag_col] = cat[flag_col]
# Make the list of sky values:
if sky_col is not None:
if sky_col not in cat.dtype.names:
raise ValueError("sky_col = %s is not a column in %s"%(sky_col,cat_file_name))
extra_props['sky'] = np.float32(cat[sky_col])
elif sky is not None and sky != 'median':
sky = galsim.config.ParseValue(config, 'sky', base, None)[0]
sky = InputFiles._handle_depr_image_value(sky, image, 'sky', logger)
extra_props['sky'] = np.array([sky]*len(cat), dtype=np.float32)
# Make the list of gain values:
# TODO: SV and Y1 DES images have two gain values, GAINA, GAINB. It would be nice if we
# could get the right one properly. OTOH, Y3+ will be in electrons, so gain=1 will
# the right value for all images. So maybe not worth worrying about.
if gain_col is not None:
if gain_col not in cat.dtype.names:
raise ValueError("gain_col = %s is not a column in %s"%(gain_col,cat_file_name))
extra_props['gain'] = cat[gain_col]
elif gain is not None:
gain = galsim.config.ParseValue(config, 'gain', base, None)[0]
gain = InputFiles._handle_depr_image_value(gain, image, 'gain', logger)
extra_props['gain'] = np.array([gain]*len(cat), dtype=float)
# Get the saturation level
if satur is not None:
satur = galsim.config.ParseValue(config, 'satur', base, None)[0]
satur = InputFiles._handle_depr_image_value(satur, image, 'satur', logger)
logger.debug("Using saturation value: %s",satur)
extra_props['satur'] = np.array([satur]*len(cat), dtype=float)
# Check whether we should trust the positions
if trust_pos is not None:
extra_props['trust_pos'] = np.array([trust_pos]*len(cat), dtype=bool)
if properties is not None:
logger.debug('properties = %s', properties)
extra_props.update(InputFiles._evaluate_properties(
properties, extra_props, len(cat), base, logger))
return image_pos, extra_props
@staticmethod
def _evaluate_properties(properties, extra_props, nstars, base, logger):
if not isinstance(properties, dict):
raise ValueError("properties should be a dict")
base['eval_variables'] = {}
# Load the existing extra properties into eval_variables so they are available
# to be used in eval strings.
for prop_name, value in extra_props.items():
base['eval_variables']['x' + prop_name] = value
parsed = {}
for key, value in properties.items():
try:
result = galsim.config.ParseValue({'x': value}, 'x', base, None)[0]
if np.ndim(result) == 0:
result = np.array([result] * nstars)
else:
result = np.asarray(result)
except Exception as e:
logger.debug("Caught exception trying vector eval for property %r: %r", key, e)
result = []
row_base = base.copy()
row_base['eval_variables'] = {}
for i in range(nstars):
for k, v in base['eval_variables'].items():
row_base['eval_variables'][k] = v[i]
result.append(galsim.config.ParseValue({'x': value}, 'x', row_base, None)[0])
result = np.array(result)
else:
if len(result) != nstars:
raise ValueError("properties[%r] did not evaluate to %d values" % (key, nstars))
base['eval_variables']['x' + key] = result
parsed[key] = result
return parsed
[docs]
def setPointing(self, ra, dec, logger=None):
"""Set the pointing attribute based on the input ra, dec (given in the initializer)
There are a number of ways the pointing can be specified.
Even this is probably not sufficiently generic for all applications, but it's a start.
1. numerical values (in Hours, Degrees respective) for ra, dec
2. hh:mm:ss.ssss, dd:mm:ss.ssss strings giving hours/degrees, minutes, seconds for each
3. FITS header key words to read to get the ra, dec values
4. None, which will attempt to find the spatial center of all the input images using the
midpoint of the min/max ra and dec values of the image corners according to their
individual WCS functions. [Not implemented currently.]
5. dict with GalSim-style config types to be parsed.
"""
import fitsio
logger = LoggerWrapper(logger)
if (ra is None) != (dec is None):
raise ValueError("Only one of ra, dec was specified")
if ra is None:
if self.wcs_list[0].isCelestial():
if self.nimages == 1:
# Here we can just use the image center.
wcs = self.wcs_list[0]
center = self.center_list[0]
self.pointing = wcs.toWorld(center)
logger.verbose("Setting pointing to image center: %.3f h, %.3f d",
self.pointing.ra / galsim.hours,
self.pointing.dec / galsim.degrees)
else:
# Use the mean of all the image centers
plist = [wcs.toWorld(center)
for wcs,center in zip(self.wcs_list,self.center_list)]
# Do this in x,y,z coords, not ra, dec so we don't mess up near ra=0.
xlist, ylist, zlist = zip(*[p.get_xyz() for p in plist])
x = np.mean(xlist)
y = np.mean(ylist)
z = np.mean(zlist)
self.pointing = galsim.CelestialCoord.from_xyz(x,y,z)
logger.verbose("Setting pointing to mean of image centers: %.3f h, %.3f d",
self.pointing.ra / galsim.hours,
self.pointing.dec / galsim.degrees)
else:
self.pointing = None
elif (isinstance(ra, dict) or isinstance(dec, dict) or
(isinstance(ra, str) and (ra[0] in ['$','@'])) or
(isinstance(dec, str) and (dec[0] in ['$','@']))):
image_file_name = self.image_kwargs[0]['image_file_name']
image_hdu = self.image_kwargs[0]['image_hdu']
image = galsim.fits.read(image_file_name, hdu=image_hdu, read_header=True)
base = galsim.config.CopyConfig(self.base)
base['index_key'] = 'image_num'
base['image_num'] = 0
base['_current_image'] = image
ra = galsim.config.ParseValue({'ra': ra}, 'ra', base, None)[0]
dec = galsim.config.ParseValue({'dec': dec}, 'dec', base, None)[0]
self.setPointing(ra, dec, logger)
elif type(ra) in [float, int]:
ra = float(ra) * galsim.hours
dec = float(dec) * galsim.degrees
self.pointing = galsim.CelestialCoord(ra,dec)
logger.verbose("Setting pointing to: %.3f h, %.3f d",
self.pointing.ra / galsim.hours,
self.pointing.dec / galsim.degrees)
elif str(ra) != ra:
raise ValueError("Unable to parse input ra: %s"%ra)
elif str(dec) != dec:
raise ValueError("Unable to parse input dec: %s"%dec)
elif ':' in ra and ':' in dec:
ra = galsim.Angle.from_hms(ra)
dec = galsim.Angle.from_dms(dec)
self.pointing = galsim.CelestialCoord(ra,dec)
logger.verbose("Setting pointing to: %.3f h, %.3f d",
self.pointing.ra / galsim.hours,
self.pointing.dec / galsim.degrees)
else:
file_name = self.image_file_name[0]
if len(self.chipnums) == 1:
logger.verbose("Setting pointing from keywords %s, %s", ra, dec)
else:
logger.verbose("Setting pointing from keywords %s, %s in %s", ra, dec, file_name)
fits = fitsio.FITS(file_name)
hdu = 1 if file_name.endswith('.fz') else 0
header = fits[hdu].read_header()
if ra not in header:
raise KeyError("Key %s not found in FITS header"%ra)
ra = header[ra]
if dec not in header:
raise KeyError("Key %s not found in FITS header"%dec)
dec = header[dec]
# Recurse to do further parsing.
self.setPointing(ra, dec, logger)