Reading in files for a specific range of values in a list
<p>I have a small issue with my script. I'm reading in values from a file in two columns and performing a fit and other calculations on those values. My issue is that I'm trying to preform these fits and calculations only on a range of one of the columns ("wv" in my script below). I've attached my script below where I'm using a "for" loop to do this ("for wv in range(6450, 6635):"). The desired result is to perform the fit, but only on wv values range from 6450 to 6635 (instead of all values of wv). The problem is that I get an error related to the x and y having the same first dimension, can you see what I'm doing wrong?</p>

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

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

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

MJD_eph = 43366.275
SP_orb = 1667.0
P_orb = 26.4960
lam = 6562.801
c = 3.0e5

g = open("test_write.txt", "w")
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()
    print "%s   %0.2f   %0.2f"%(fits[j],wvi_b[j],wvi_r[j])

    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, 6635):
        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)
        wb = popt[1]
        wr = popt[4]
        V = popt[0] + 1
        R = popt[3] + 1
        print popt[0]+1, popt[1], popt[3]+1, popt[4]
        e1 = (MJD[j] - MJD_eph)/P_orb
        phase = e1 - int(e1)
        dV = wr-wb
        dVel = (dV/lam)*c
        dVb = wb-lam
        Vblue = (dVb/lam)*c
        dVr = wr-lam
        Vred = (dVr/lam)*c

        ymod = gauss(wv,*popt)
        plt.xlim((6450, 6635))
        plt.ylim((0.5, 2.5))
        plt.plot(wv, ymod)
        plt.plot(wv, flux, marker='.')
        plt.show()
        g.write("%s     %0.6f       %0.3f   %0.3f     %0.3f     %0.3f   %0.3f   %0.3f   %0.3f\n" %(fits[j], MJD[j], phase, Vblue, Vred, V, R, wb, wr))
g.close()
</code></pre>