From b3e4b176cfe76beecfd9109e95f3dac4dbe0b84d Mon Sep 17 00:00:00 2001 From: Alexandre Boucaud Date: Sun, 10 Oct 2021 18:02:13 +0200 Subject: [PATCH 1/5] Tentative implementation of functions --- galcheat/utilities.py | 71 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 galcheat/utilities.py diff --git a/galcheat/utilities.py b/galcheat/utilities.py new file mode 100644 index 0000000..ab7988b --- /dev/null +++ b/galcheat/utilities.py @@ -0,0 +1,71 @@ +import astropy.units as u + +from galcheat.helpers import get_filter, get_survey + + +def mag2counts(magnitude, survey_name, filter_name): + """Convert source magnitude to counts for a given filter of a survey + + The calculation includes the effects of atmospheric extinction as + well as exposure time. + + Parameters + ---------- + magnitude: float + magnitude of source + survey_name: str + Name of a given survey + filter_name: str + Name of the survey filter + + Returns + ------- + The corresponding flux in counts + + References + ---------- + The `WeakLensingDeblending` package + https://github.com/LSSTDESC/WeakLensingDeblending + + """ + if not isinstance(magnitude, u.Quantity): + magnitude *= u.mag(u.ct / u.s) + else: + magnitude = magnitude.value * u.mag(u.ct / u.s) + + survey = get_survey(survey_name) + filter = get_filter(filter_name, survey_name) + + delta_airmass = survey.airmass - survey.zeropoint_airmass + mag = magnitude + filter.extinction * delta_airmass + + flux = (mag - filter.zeropoint).to(u.ct / u.s) + counts = flux * filter.exposure_time + + return counts + + +def mean_sky_level(survey_name, filter_name): + """Computes the mean sky level for a given survey and a filter + + Parameters + ---------- + survey_name: str + Name of a given survey + filter_name: str + Name of the survey filter + + Returns + ------- + The corresponding mean sky level in counts + + """ + survey = get_survey(survey_name) + filter = get_filter(filter_name, survey_name) + + sky_brightness_counts = mag2counts(filter.sky_brightness, survey_name, filter_name) + pixel_area = survey.pixel_scale.to_value(u.arcsec) ** 2 + + mean_sky_level = sky_brightness_counts * pixel_area + + return mean_sky_level From 3612debb17a81d8392508dd1ebdf77093163340d Mon Sep 17 00:00:00 2001 From: Alexandre Boucaud Date: Mon, 28 Feb 2022 19:34:44 +0100 Subject: [PATCH 2/5] Simplify the computation for the conversion --- galcheat/utilities.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/galcheat/utilities.py b/galcheat/utilities.py index ab7988b..fc2f1d9 100644 --- a/galcheat/utilities.py +++ b/galcheat/utilities.py @@ -33,13 +33,9 @@ def mag2counts(magnitude, survey_name, filter_name): else: magnitude = magnitude.value * u.mag(u.ct / u.s) - survey = get_survey(survey_name) filter = get_filter(filter_name, survey_name) - delta_airmass = survey.airmass - survey.zeropoint_airmass - mag = magnitude + filter.extinction * delta_airmass - - flux = (mag - filter.zeropoint).to(u.ct / u.s) + flux = (magnitude - filter.zeropoint).to(u.ct / u.s) counts = flux * filter.exposure_time return counts From cb9dc7d947a32dc3146807228c489fef8230d55f Mon Sep 17 00:00:00 2001 From: Alexandre Boucaud Date: Mon, 28 Feb 2022 19:35:02 +0100 Subject: [PATCH 3/5] Add docstring explainations --- galcheat/utilities.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/galcheat/utilities.py b/galcheat/utilities.py index fc2f1d9..d2f0a75 100644 --- a/galcheat/utilities.py +++ b/galcheat/utilities.py @@ -6,8 +6,13 @@ def mag2counts(magnitude, survey_name, filter_name): """Convert source magnitude to counts for a given filter of a survey - The calculation includes the effects of atmospheric extinction as - well as exposure time. + To perform the computation, we use the filter zeropoint computed + with `speclite` under classical atmospheric conditions and at a + given airmass and we integrate over the survey lifetime using the + full filter exposure time. + + Expect a rough estimate from this calculation since e.g. it does not + take into account the atmospheric extinction. Parameters ---------- @@ -44,6 +49,10 @@ def mag2counts(magnitude, survey_name, filter_name): def mean_sky_level(survey_name, filter_name): """Computes the mean sky level for a given survey and a filter + This computation uses the sky brightness parameter from galcheat, + expressed as a magnitude per square arcminute, weights it by the + pixel area and converts it to counts. + Parameters ---------- survey_name: str From 4ee3c02128f926cbbd53b9daf6a2dc041c2600eb Mon Sep 17 00:00:00 2001 From: Alexandre Boucaud Date: Wed, 2 Mar 2022 14:19:18 +0100 Subject: [PATCH 4/5] Cast value to integer --- galcheat/utilities.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/galcheat/utilities.py b/galcheat/utilities.py index d2f0a75..04cafe1 100644 --- a/galcheat/utilities.py +++ b/galcheat/utilities.py @@ -12,7 +12,8 @@ def mag2counts(magnitude, survey_name, filter_name): full filter exposure time. Expect a rough estimate from this calculation since e.g. it does not - take into account the atmospheric extinction. + take into account the atmospheric extinction. Therefore the result + is casted to an integer. Parameters ---------- @@ -43,7 +44,7 @@ def mag2counts(magnitude, survey_name, filter_name): flux = (magnitude - filter.zeropoint).to(u.ct / u.s) counts = flux * filter.exposure_time - return counts + return counts.astype(int) def mean_sky_level(survey_name, filter_name): From c78f5ca467e752d78b84ded6afda87195d1734d9 Mon Sep 17 00:00:00 2001 From: Alexandre Boucaud Date: Wed, 2 Mar 2022 23:34:09 +0100 Subject: [PATCH 5/5] Add tests --- .github/workflows/python-package.yml | 5 ++++- tests/test_utilities.py | 33 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 tests/test_utilities.py diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index f337364..4dfdcc8 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -27,7 +27,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - python -m pip install flake8 + python -m pip install flake8 pytest pytest-cov - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names @@ -43,3 +43,6 @@ jobs: - name: Test the console script run: | galcheat + - name: Test the utility functions + run: | + pytest --cov galcheat/ --cov-branch --cov-report term-missing -vv diff --git a/tests/test_utilities.py b/tests/test_utilities.py new file mode 100644 index 0000000..3d8f89b --- /dev/null +++ b/tests/test_utilities.py @@ -0,0 +1,33 @@ +from galcheat.utilities import mag2counts, mean_sky_level + +BTK_COUNTS_MAG24 = { + "Rubin_u": 15321.782101179268, + "Rubin_g": 121397.91888074754, + "Rubin_r": 240956.73939657194, + "Rubin_i": 179448.18815675998, + "Rubin_z": 108953.51289042353, + "Rubin_y": 50727.240442255556, +} + +BTK_MEAN_SKY_LEVEL = { + "Rubin_u": 1687.9876819744386, + "Rubin_g": 23241.878848667337, + "Rubin_r": 127057.1381640446, + "Rubin_i": 180301.3875959776, + "Rubin_z": 250784.8105213134, + "Rubin_y": 293292.6831817095, +} + + +def test_mag2counts(): + survey = "Rubin" + for filt in "ugrizy": + counts = mag2counts(24, survey, filt).value + assert counts == int(BTK_COUNTS_MAG24[f"{survey}_{filt}"]) + + +def test_mean_sky_level(): + survey = "Rubin" + for filt in "ugrizy": + sky_level = mean_sky_level(survey, filt).value + assert int(sky_level) == int(BTK_MEAN_SKY_LEVEL[f"{survey}_{filt}"])