Commit ff055172 authored by Wei Chengliang's avatar Wei Chengliang
Browse files

fix: handle invalid flux in ABMAG calc to prevent UnboundLocalError

parent bb11d56d
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -133,6 +133,18 @@ class Galaxy(MockObject):
                self.logger.error(e)
            return 2, None

        pos_shear = None
        if not np.isfinite(full) or full <= 0:
            msg = "obj %s has invalid SED flux in band %s (full=%.6g)" % (
                self.id,
                filt.filter_type,
                full,
            )
            print(msg)
            if self.logger:
                self.logger.error(msg)
            return 2, pos_shear

        # # [C6 TEST]
        # print('hlr_disk = %.4f, hlr_bulge = %.4f'%(self.hlr_disk, self.hlr_bulge))
        # tracemalloc.start()
+13 −0
Original line number Diff line number Diff line
@@ -148,6 +148,19 @@ class MockObject(object):
            if self.logger:
                self.logger.error(e)
            return 2, None

        pos_shear = None
        if not np.isfinite(full) or full <= 0:
            msg = "obj %s has invalid SED flux in band %s (full=%.6g)" % (
                self.id,
                filt.filter_type,
                full,
            )
            print(msg)
            if self.logger:
                self.logger.error(msg)
            return 2, pos_shear

        # Set Galsim Parameters
        if self.getMagFilter(filt) <= 15:
            folding_threshold = 5.0e-8
+13 −1
Original line number Diff line number Diff line
@@ -40,7 +40,19 @@ class Stamp(MockObject):
        except Exception as e:
            print(e)
            self.logger.error(e)
            return False
            return 2, None

        pos_shear = None
        if not np.isfinite(full) or full <= 0:
            msg = "obj %s has invalid SED flux in band %s (full=%.6g)" % (
                self.id,
                filt.filter_type,
                full,
            )
            print(msg)
            if self.logger:
                self.logger.error(msg)
            return 2, pos_shear

        # nphotons_sum = 0
        # photons_list = []
+6 −0
Original line number Diff line number Diff line
@@ -659,7 +659,13 @@ def getABMAG(interFlux, bandpass):
    ABMAG_zero = getABMagAverageVal(
        ABmag=0, norm_thr=throughtput, sWave=sWave, eWave=eWave
    )
    if not np.isfinite(ABMAG_zero) or ABMAG_zero <= 0:
        return np.nan
    flux_ave = interFlux / (eWave - sWave)
    if not np.isfinite(flux_ave):
        return np.nan
    if flux_ave <= 0:
        return np.inf
    ABMAG_spec = -2.5 * np.log10(flux_ave / ABMAG_zero)
    return ABMAG_spec

+26 −1
Original line number Diff line number Diff line
@@ -118,6 +118,8 @@ def add_objects(self, chip, filt, tel, pointing, catalog, obs_param):
    missed_obj = 0
    bright_obj = 0
    dim_obj = 0
    invalid_obj = 0
    draw_error_obj = 0
    for j in range(nobj):
        # # [DEBUG] [TODO]
        # if j >= 10:
@@ -164,6 +166,23 @@ def add_objects(self, chip, filt, tel, pointing, catalog, obs_param):
        # [TODO] Testing
        # self.chip_output.Log_info("mag_%s = %.3f"%(filt.filter_type.lower(), obj.param["mag_%s"%filt.filter_type.lower()]))

        mag_keys = [
            "mag_%s" % filt.filter_type.lower(),
            "mag_%s" % cut_filter.filter_type.lower(),
        ]
        bad_mags = [
            "%s=%s" % (key, obj.param[key])
            for key in mag_keys
            if not np.isfinite(obj.param[key])
        ]
        if bad_mags:
            self.chip_output.Log_error(
                "obj %s has invalid magnitude: %s" % (obj.id, ", ".join(bad_mags))
            )
            invalid_obj += 1
            obj.unload_SED()
            continue

        # Exclude very bright/dim objects (for now)
        if cut_filter.is_too_bright(
            mag=obj.param[
@@ -296,8 +315,8 @@ def add_objects(self, chip, filt, tel, pointing, catalog, obs_param):
                missed_obj += 1
                self.chip_output.Log_error("Object missed-999: %s" % (obj.id))
            else:
                draw_error_obj += 1
                self.chip_output.Log_error("Draw error, object omitted: %s" % (obj.id))
                continue
        except Exception as e:
            traceback.print_exc()
            self.chip_output.Log_error(e)
@@ -367,6 +386,12 @@ def add_objects(self, chip, filt, tel, pointing, catalog, obs_param):
    self.chip_output.Log_info(
        "# objects that are too dim %d out of %d" % (dim_obj, nobj)
    )
    self.chip_output.Log_info(
        "# objects with invalid (nan/inf) magnitude %d out of %d" % (invalid_obj, nobj)
    )
    self.chip_output.Log_info(
        "# objects with drawing errors %d out of %d" % (draw_error_obj, nobj)
    )
    self.chip_output.Log_info(
        "# objects that are missed %d out of %d" % (missed_obj, nobj)
    )