diff --git a/measure_extinction/modeldata.py b/measure_extinction/modeldata.py index f0fcd2f..b2a1483 100644 --- a/measure_extinction/modeldata.py +++ b/measure_extinction/modeldata.py @@ -209,7 +209,7 @@ def stellar_sed(self, params, velocity=None): return sed - def dust_extinguished_sed(self, params, sed, velocity=0.0): + def dust_extinguished_sed(self, params, sed, fit_range="all", velocity=0.0): """ Dust extinguished sed given the extinction parameters @@ -221,6 +221,9 @@ def dust_extinguished_sed(self, params, sed, velocity=0.0): sed : dict fluxes for each spectral piece + fit_range : string, optional + keyword to toggle SED fitting to be done with G23 only or to also include curve_F99_method + velocity : float, optional velocity of dust @@ -231,42 +234,56 @@ def dust_extinguished_sed(self, params, sed, velocity=0.0): """ Rv = params[1] g23mod = G23(Rv=Rv) - optnir_axav_x = np.flip(1.0 / (np.arange(0.35, 30.0, 0.1) * u.micron)) - optnir_axav_y = g23mod(optnir_axav_x) - - # updated F04 C1-C2 correlation - C1 = 2.18 - 2.91 * params[2] # create the extinguished sed ext_sed = {} - for cspec in self.fluxes.keys(): - # get the dust extinguished SED (account for the - # systemic velocity of the galaxy [opposite regular sense]) - shifted_waves = (1.0 - velocity / 2.998e5) * self.waves[cspec] - axav = _curve_F99_method( - shifted_waves, - Rv, - C1, - params[2], - params[3], - params[4], - params[5], - params[6], - optnir_axav_x.value, - optnir_axav_y, - [0.033, 11.0], - "FM90_G23_measure_extinction", - ) - ext_sed[cspec] = sed[cspec] * (10 ** (-0.4 * axav * params[0])) + if fit_range.lower() == "g23": + for cspec in self.fluxes.keys(): + shifted_waves = (1.0 - velocity / 2.998e5) * self.waves[cspec] + axav = g23mod(shifted_waves) + ext_sed[cspec] = sed[cspec] * (10 ** (-0.4 * axav * params[0])) + + elif fit_range.lower() == "all": + optnir_axav_x = np.flip(1.0 / (np.arange(0.35, 30.0, 0.1) * u.micron)) + optnir_axav_y = g23mod(optnir_axav_x) + + # updated F04 C1-C2 correlation + C1 = 2.18 - 2.91 * params[2] + + for cspec in self.fluxes.keys(): + # get the dust extinguished SED (account for the + # systemic velocity of the galaxy [opposite regular sense]) + shifted_waves = (1.0 - velocity / 2.998e5) * self.waves[cspec] + axav = _curve_F99_method( + shifted_waves, + Rv, + C1, + params[2], # C2 + params[3], # C3 + params[4], # C4 + xo=params[5], # xo + gamma=params[6], # gamma + optnir_axav_x=optnir_axav_x.value, + optnir_axav_y=optnir_axav_y, + valid_x_range=[0.033, 11.0], + model_name="FM90_G23_measure_extinction", + ) + + ext_sed[cspec] = sed[cspec] * (10 ** (-0.4 * axav * params[0])) - # # create the extinguished sed - # ext_sed = {} - # for cspec in self.fluxes.keys(): - # # get the dust extinguished SED (account for the - # # systemic velocity of the galaxy [opposite regular sense]) - # shifted_waves = (1.0 - velocity / 2.998e5) * self.waves[cspec] - # axav = g23mod(shifted_waves) - # ext_sed[cspec] = sed[cspec] * (10 ** (-0.4 * axav * params[0])) + # # create the extinguished sed + # ext_sed = {} + # for cspec in self.fluxes.keys(): + # # get the dust extinguished SED (account for the + # # systemic velocity of the galaxy [opposite regular sense]) + # shifted_waves = (1.0 - velocity / 2.998e5) * self.waves[cspec] + # axav = g23mod(shifted_waves) + # ext_sed[cspec] = sed[cspec] * (10 ** (-0.4 * axav * params[0])) + + else: + raise ValueError( + "Incorrect input for fit_range argument in dust_extinguished_sed(). Available options are: g23, all" + ) return ext_sed diff --git a/measure_extinction/utils/fit_model.py b/measure_extinction/utils/fit_model.py index 76cc9bb..f5d6eea 100755 --- a/measure_extinction/utils/fit_model.py +++ b/measure_extinction/utils/fit_model.py @@ -64,7 +64,7 @@ def __init__( self.weights = weights self.velocities = np.array([stellar_velocity, 0.0]) - def lnlike(self, params, obsdata, modeldata): + def lnlike(self, params, obsdata, modeldata, fit_range="all"): """ Compute the natural log of the likelihood that the data fits the model. @@ -74,6 +74,7 @@ def lnlike(self, params, obsdata, modeldata): ---------- params : floats parameters of the model + params = [logT, logg, logZ, Av, Rv, C2, C3, C4, x0, gamma, HI_gal, HI_mw] obsdata : StarData object observed data for a reddened star @@ -86,7 +87,7 @@ def lnlike(self, params, obsdata, modeldata): # dust_extinguished sed ext_modsed = modeldata.dust_extinguished_sed( - params[3:10], modsed, velocity=self.velocities[0] + params[3:10], modsed, fit_range=fit_range, velocity=self.velocities[0] ) # hi_abs sed @@ -97,7 +98,12 @@ def lnlike(self, params, obsdata, modeldata): lnl = 0.0 for cspec in hi_ext_modsed.keys(): - gvals = (self.weights[cspec] > 0) & (np.isfinite(hi_ext_modsed[cspec])) + try: + gvals = (self.weights[cspec] > 0) & (np.isfinite(hi_ext_modsed[cspec])) + except ValueError: + raise ValueError( + "Oops! The model data and reddened star data did not match.\n Hint: Make sure that the BAND name in the .dat files match." + ) chiarr = np.square( ( ( @@ -146,7 +152,7 @@ def lnprior(self, params): return lnp @staticmethod - def lnprob(params, obsdata, modeldata, fitinfo): + def lnprob(params, obsdata, modeldata, fitinfo, fit_range="all"): """ Compute the natural log of the probability @@ -170,7 +176,7 @@ def lnprob(params, obsdata, modeldata, fitinfo): if lnp == lnp_bignnum: return lnp else: - return lnp + fitinfo.lnlike(params, obsdata, modeldata) + return lnp + fitinfo.lnlike(params, obsdata, modeldata, fit_range=fit_range) def check_param_limits(self, params): """