Skip to content

Commit 10e623e

Browse files
authored
Merge pull request #17 from LSSTDESC/issue/16/copy_obscond
Copying observing_condition_degrader from rail_attic
2 parents fbf42cc + 5456793 commit 10e623e

2 files changed

Lines changed: 69 additions & 10 deletions

File tree

src/rail/creation/degradation/observing_condition_degrader.py

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ObsCondition(Degrader):
4545
uses the same arguments as LSSTErrorModel (from PhotErr).
4646
The following arguments, if supplied, may contain either
4747
a single number (as in the case of LSSTErrorModel), or a path:
48-
[m5, nVisYr, airmass, gamma, msky, theta, km, tvis]
48+
[m5, nVisYr, airmass, gamma, msky, theta, km, tvis, EBV]
4949
For the following keys:
5050
[m5, nVisYr, gamma, msky, theta, km]
5151
numbers/paths for specific bands must be passed.
@@ -121,6 +121,7 @@ def __init__(self, args, comm=None):
121121
"theta",
122122
"km",
123123
"tvis",
124+
"EBV",
124125
]
125126

126127
# validate input parameters
@@ -165,10 +166,12 @@ def _validate_obs_config(self):
165166

166167
# Check if extra keys are passed
167168
# get lsst_error_model keys
168-
lsst_error_model_keys = [field.name for field in fields(LsstErrorParams)]
169+
lsst_error_model_keys = list(LsstErrorParams.__dataclass_fields__.keys())
169170
if len(set(self.config["map_dict"].keys()) - set(lsst_error_model_keys)) != 0:
170171
extra_keys = set(self.config["map_dict"].keys()) - set(lsst_error_model_keys)
171-
raise ValueError("Extra keywords are passed to the configuration: \n" + str(extra_keys))
172+
# now we added EBV, which is not in LsstErrorParams:
173+
if extra_keys != {"EBV"}:
174+
raise ValueError("Extra keywords are passed to the configuration: \n" + str(extra_keys))
172175

173176
# Check data type for the keys:
174177
# Note that LSSTErrorModel checks
@@ -189,7 +192,7 @@ def _validate_obs_config(self):
189192
elif key in self.obs_cond_keys:
190193

191194
# band-independent keys:
192-
if key in ["airmass", "tvis"]:
195+
if key in ["airmass", "tvis", "EBV"]:
193196

194197
# check if the input is a string or number
195198
if not (
@@ -261,7 +264,7 @@ def _get_maps(self):
261264
for key in self.config["map_dict"]:
262265
if key in self.obs_cond_keys:
263266
# band-independent keys:
264-
if key in ["airmass", "tvis"]:
267+
if key in ["airmass", "tvis", "EBV"]:
265268
if isinstance(self.config["map_dict"][key], str):
266269
maps[key] = hp.read_map(self.config["map_dict"][key])[pixels]
267270
elif isinstance(self.config["map_dict"][key], float):
@@ -304,9 +307,10 @@ def get_pixel_conditions(self, pixel: int) -> dict:
304307
# For keys that may contain the survey condition maps
305308
if key in self.obs_cond_keys:
306309
# band-independent keys:
307-
if key in ["airmass", "tvis"]:
308-
obs_conditions[key] = float(self.maps[key][ind])
309-
# band-dependent keys:
310+
if key in ["airmass", "tvis", "EBV"]:
311+
if key != "EBV":# exclude EBV because it is not in LsstErrorModel
312+
obs_conditions[key] = float(self.maps[key][ind])
313+
# band-dependent keys
310314
else:
311315
obs_conditions[key] = {}
312316
for band in (self.maps[key]).keys():
@@ -333,6 +337,45 @@ def assign_pixels(self, catalog: pd.DataFrame) -> pd.DataFrame:
333337
catalog = pd.concat([catalog, assigned_pix], axis=1)
334338

335339
return catalog
340+
341+
# this is milky way extinction, should be added before other observing conditions is applied
342+
def apply_galactic_extinction(self, pixel: int, pixel_cat: pd.DataFrame) -> pd.DataFrame:
343+
"""
344+
MW extinction reddening of the magnitudes
345+
"""
346+
# set the A_lamba/E(B-V) values for the six LSST filters
347+
band_a_ebv = {
348+
"u":4.81,
349+
"g":3.64,
350+
"r":2.70,
351+
"i":2.06,
352+
"z":1.58,
353+
"y":1.31,
354+
}
355+
356+
# find the corresponding ebv for the pixel
357+
ind = self.maps["pixels"]==pixel
358+
ebvvec = self.maps["EBV"][ind]
359+
360+
if "renameDict" in self.maps.keys():
361+
bands = self.maps["renameDict"]
362+
elif "renameDict" not in self.maps.keys():
363+
bands = {
364+
"u":"u",
365+
"g":"g",
366+
"r":"r",
367+
"i":"i",
368+
"z":"z",
369+
"y":"y",
370+
}
371+
372+
for b in bands.keys():
373+
key=bands[b]
374+
# update pixel_cat to the reddened magnitudes
375+
pixel_cat[key] = (pixel_cat[key].copy())+ebvvec*band_a_ebv[b]
376+
377+
return pixel_cat
378+
336379

337380
def run(self):
338381
"""
@@ -356,12 +399,17 @@ def run(self):
356399
# assign each galaxy to a pixel
357400
print("Assigning pixels.")
358401
catalog = self.assign_pixels(catalog)
359-
402+
360403
# loop over each pixel
361404
pixel_cat_list = []
362405
for pixel, pixel_cat in catalog.groupby("pixel"):
363406
# get the observing conditions for this pixel
364407
obs_conditions = self.get_pixel_conditions(pixel)
408+
409+
# apply MW extinction if supplied,
410+
# replace the Mag column with reddened magnitudes:
411+
if "EBV" in self.maps.keys():
412+
pixel_cat = self.apply_galactic_extinction(pixel, pixel_cat)
365413

366414
# creating the error model for this pixel
367415
errorModel = LsstErrorModel(**obs_conditions)
@@ -402,4 +450,4 @@ def __repr__(self):
402450

403451
printMsg += str(self.config["map_dict"])
404452

405-
return printMsg
453+
return printMsg

tests/astro_tools/test_degraders.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ def test_ObsCondition_extended(data):
213213
weight = ""
214214
map_dict = {
215215
"airmass": find_rail_file('examples_data/creation_data/data/survey_conditions/minion_1016_dc2_Median_airmass_i_and_nightlt1825_HEAL.fits'),
216+
"EBV": 0.0,
216217
"nVisYr": {"u": 50.0},
217218
"tvis": 30.0,
218219
}
@@ -242,6 +243,16 @@ def test_ObsCondition_empty_map_dict(data):
242243
assert degraded_data1.equals(degraded_data2)
243244

244245
os.remove(degrader1.get_output(degrader1.get_aliased_tag("output"), final_name=True))
246+
247+
248+
def test_ObsCondition_renameDict(data):
249+
"""Test with no renameDict included"""
250+
degrader1 = ObsCondition.make_stage(random_seed=0, map_dict={"EBV": 0.0,"renameDict": {"u": "u"},})
251+
252+
# make sure setting the same seeds yields the same output
253+
degraded_data1 = degrader1(data).data
254+
255+
os.remove(degrader1.get_output(degrader1.get_aliased_tag("output"), final_name=True))
245256

246257

247258
def test_LSSTErrorModel_returns_correct_columns(data):

0 commit comments

Comments
 (0)