From 2065d88aa794a6b4acb77b367197d838f35b8bbd Mon Sep 17 00:00:00 2001 From: beckermr Date: Thu, 9 May 2024 13:03:24 -0500 Subject: [PATCH] ENH add DES gal cat --- environment.yaml | 1 + montara/__init__.py | 1 + montara/input_desgal.py | 55 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 montara/input_desgal.py diff --git a/environment.yaml b/environment.yaml index 043f000..ba7a5e5 100644 --- a/environment.yaml +++ b/environment.yaml @@ -8,6 +8,7 @@ dependencies: - astropy - pandas - des-eastlake + - des-y6utils - galsim_extra - importlib_metadata - cfitsio # for fpack diff --git a/montara/__init__.py b/montara/__init__.py index 088e2ad..2834d1b 100644 --- a/montara/__init__.py +++ b/montara/__init__.py @@ -20,3 +20,4 @@ from . import badpixfromfits # noqa from . import eastlake_step # noqa from . import eval_gsobject # noqa +from . import input_desgal # noqa diff --git a/montara/input_desgal.py b/montara/input_desgal.py new file mode 100644 index 0000000..a80d436 --- /dev/null +++ b/montara/input_desgal.py @@ -0,0 +1,55 @@ +import galsim +import numpy as np +from galsim.config.input import InputLoader, RegisterInputType, GetInputObj +from galsim.config.value import RegisterValueType +import fitsio +from .utils import add_field + + +class DESGalCatalog(object): + _req_params = {'file_name': str} + _single_params = [] + _takes_rng = False + + def __init__(self, file_name, mag_i_col='mag_i', mag_i_max=None, mag_i_min=None, + _nobjects_only=False): + self.gal_data = fitsio.read(file_name) + self.gal_data = add_field( + self.gal_data, [("catalog_row", int)], + [np.arange(len(self.gal_data))], + ) + self.nobjects = len(self.gal_data) + + def get(self, index, col): + return self.gal_data[index][col] + + def getNObjects(self): + return self.nobjects + + +def _GenerateFromDESGalCatalog(config, base, value_type): + """@brief Return a value read from an input catalog + """ + gal_input = GetInputObj('desgal', config, base, 'DESGalValue') + + # Setup the indexing sequence if it hasn't been specified. + # The normal thing with a Catalog is to just use each object in order, + # so we don't require the user to specify that by hand. We can do it for them. + galsim.config.SetDefaultIndex(config, gal_input.getNObjects()) + + req = {'col': str, 'index': int} + opt = {'num': int} + kwargs, safe = galsim.config.GetAllParams(config, base, req=req, opt=opt) + col = kwargs['col'] + index = kwargs['index'] + val = gal_input.get(index, col) + return val, safe + + +RegisterInputType('desgal', InputLoader(DESGalCatalog, has_nobj=True)) +RegisterValueType( + 'DESGalValue', + _GenerateFromDESGalCatalog, + [float], + input_type='desgal', +)