Curve fitting to a Planck's Curve
Question: So I am aiming to fit my own data points to a blackbody curve, however am
having difficulties.

The outline of what I am doing is on

<http://python4esac.github.io/fitting/example_blackbody.html>

But they use random data, I am trying to use my own CSV data.

This data is:

    
    
    Wavelength
    0.7,
    0.865,
    1.24,
    1.61,
    3.7,
    4.05,
    
    Radiance
    0,
    0.106718,
    0.227031,
    0.373527,
    0.240927,
    0.293215,
    

Is there anyway to get Python to go into the file and use these two columns
instead? Everything I have tried so far has failed.

my code is as follows

    
    
    import csv
    with open('PythonCode1.csv', 'rb') as f:
        reader = csv.reader(f, delimiter=',')
        for row in reader:
    
            from scipy.optimize import curve_fit
    import pylab as plt
    from pylab import plotfile, show, gca
    fid=open('PythonCode1.csv','r')
    import numpy as np
    import matplotlib.cbook as cbook        
    def blackbody_lam(lam, T):
        """ Blackbody as a function of wavelength (um) and temperature (K).
    
        """
        from scipy.constants import h,k,c
        lam = 1e-6 * lam # convert to metres
        return 2*h*c**2 / (lam**5 * (np.exp(h*c / (lam*k*T)) - 1))
    
        wa = np.linspace(0.1, 6, 100)   # wavelengths in um
    T1 = 1000.
    T2 = 2500.
    y1 = blackbody_lam(wa, T1)
    y2 = blackbody_lam(wa, T2)
    ytot = y1 + y2
    
    sigma = np.ones(len(wa)) * 1 * np.median(ytot)
    ydata = ytot + csv.row[1].randn(len(wa)) * sigma
    
    
    and returns
    
    %run "d:\temp\k1339544\tmpskczzu.py"
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    R:\Canpy103.001\Canopy32\App\appdata\canopy-1.0.3.1262.win-x86\lib\site-    packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
        174             else:
        175                 filename = fname
    --> 176             exec compile(scripttext, filename, 'exec') in glob, loc
        177     else:
        178         def execfile(fname, *where):
    
     d:\temp\k1339544\tmpskczzu.py in <module>()
         27 
         28 sigma = np.ones(len(wa)) * 1 * np.median(ytot)
    ---> 29 ydata = ytot + csv.rows[1].randn(len(wa)) * sigma
         30 
         31 # plot the input model and synthetic data
    
    AttributeError: 'module' object has no attribute 'rows'
    


Answer: Instead of setting `wa` and `ydata` to artificial data, as was done in the
example, you want to provide your own data.

If you format your data as two columns:

    
    
    Wavelength,Radiance
    0.7,0
    0.865,0.106718
    1.24,0.227031
    1.61,0.373527
    3.7,0.240927
    4.05,0.293215
    

then you can ingest it with

    
    
    data = np.loadtxt("PythonCode1.csv", delimiter=",", skiprows=1)
    wa = data[:,0]
    ydata = data[:,1]
    

This won't guarantee that the function can fit your data (it can't, the
blackbody function lacks an adjustable scale parameter) or that the fit will
converge, but this is the simplest way to get the data in.

