[AstroPy] Blackbody plus power law fitting

Rudolf Baer rbaer25 at gmail.com
Mon Apr 19 09:40:06 EDT 2021


I am using the following code to fit a continuum

from astropy.modeling import models
from astropy.modeling import models, fitting
from astropy.modeling.models import BlackBody

from astropy import units as u


x=x
y=y



model_1 = models.PowerLaw1D([1, 1, 1])
model_2 = models.BlackBody(1800*u.K,5)
model_1_2=model_1+model_2
print(model_1_2)

def _parameter_units_for_data_units(self, inputs_unit, outputs_unit):
    return {'amplitude_0': inputs_unit['x'],
            'x_0': inputs_unit['x'],
            'alpha': inputs_unit['x'],
            'temperature': outputs_unit['y'],
            'scale_1': inputs_unit['x'] }


# Fit the data using combined model
g_init = model_1_2(1)
fit_g = fitting.LevMarLSQFitter()
g = fit_g(g_init, x, y)

# Plot the data with the best-fit model
plt.figure(figsize=(8,5))
plt.plot(x, y, 'ko')
plt.plot(x, t(x), 'b-', lw=2, label='Combined model')

plt.xlabel('Wavelength AA')
plt.ylabel('Flux')
plt.legend(loc=2)

x and y are the input data (x: lambda, y: flux) given without units. By definition the blackbody temperature is in Kelvin. This leads to  a Unit conversion error in the combined model. Do I have a basic mistake in the code or what is the solution. Any help would be much appreciated.
With kind regards
Rudolf Baer

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/astropy/attachments/20210419/14d441bc/attachment.html>


More information about the AstroPy mailing list