python reading in files for a specific column range
<p>I'm writing a script that reads in one file containing a list of files and performing gaussian fits on each of those files. Each of these files is made up of two columns (wv and flux in the script below). My small issue here is how do I limit the range based "wv" values? I tried using a "for" loop for this but I get errors related to the fit (which I don't get if I don't limit the "wv" range).</p>

<pre><code>import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt


fits = []
wvi_b = []
wvi_r = []

p = open("file_input.txt","r")
for line in p:
    fits.append(str(line.split()[0]))
    wvi_b.append(float(line.split()[1]))
    wvi_r.append(float(line.split()[2]))
p.close()

for j in range(len(fits)):
    wv = []
    flux = []
    f = open("%s"%(fits[j]),"r")
    for line in f:
        wv.append(float(line.split()[0]))
        flux.append(float(line.split()[1]))
    f.close()

    def gauss(x,a,b,c,a1,b1,c1,d):
        func = a*np.exp(-((x-b)**2)/(2.0*(c)**2)) + a1*np.exp(-((x-b1)**2)/(2.0*(c1)**2))+d
        return func

    for wv in range(6450, 6575):
        guess=(0.8,wvi_b[j],3.0,1.0,wvi_r[j],3.0,1.0)
        popt,pconv=curve_fit(gauss,wv,flux,guess)
        print popt[1], popt[4]
        ymod=gauss(wv,*popt)
        plt.plot(wv,ymod)
        plt.plot(wv,flux,marker='.')
        plt.show()
</code></pre>

<p>When you call <code>for wv in range(6450, 6575)</code>, <code>wv</code> is just an integer in that range, not a member of the list. I'd try taking a look at how you're using that variable. If you want to access data from the <code>list wv</code>, you would have to update the syntax to be <code>wv[wv]</code> (which is a little confusing - it might be best to change the variable in your <code>for</code> loop to something else).</p>

python