V, Vg, Ag, Vs, Vs_, Ah, Gamma, Gamma_, CL, CL_, CD, CD_ = d

        Mach = aero.tas2mach(V, alts[:-1])
        Vcas = aero.tas2cas(V, alts[:-1])

        if sf:
            count += 1
            CD0_set.append(CD0)
            CLCD_max_set.append(CLCD_max)

        if count > MAX_DATA_SAMPLE:
            break

        if STEP_FLAG:
            # get the airport
            airport = utils.get_closest_airport(data[0][1], data[0][2])

            plt.suptitle("%s at %s [Take-Off]" % (icao, airport['name']))

            plt.subplot(331)
            plt.xlabel('time (s)')
            plt.ylabel('altitude (m)')
            plt.plot(ts, alts, '.-', ms=5, color='steelblue')
            plt.legend(loc=4, ncol=2)

            plt.subplot(332)
            plt.xlabel('time (s)')
            plt.ylabel('speeds (m/s)')
            plt.plot(ts, Vg, '.-', ms=5, color='g', label="Vg_ / Vtas^")
            plt.plot(ts[:-1], Vcas, '.-', ms=5, color='y', label="Vcas^")
            plt.legend(loc=2, prop={'size': 10})
def extract():
    mongo_client = MongoClient('localhost', PORT)
    mcollin = mongo_client[DB][COLL]

    res = mcollin.find().skip(2000)

    dataset = []

    for r in res:
        data = r['data']
        icao = r['icao']

        # skip data starting too low
        if data[0][3] > ALT_LOW:     # ft
            continue

        # get the takeoff data chunk (h=0)
        chunk = []
        for d in data:
            if d[3] < ALT_LOW:
                continue
            elif d[3] > ALT_LOW and d[3] < ALT_HIGH:
                chunk.append(d)
            else:
                break

        # ignore insufficient chunk size
        if len(chunk) < 10:
            continue
        if chunk[-1][3] < ALT_HIGH * 0.8:
            continue

        chunkNP = np.asarray(chunk)

        ts = chunkNP[:, 0].astype(int)
        ts = ts - ts[0]
        alts = chunkNP[:, 3].astype(int)
        spds = (chunkNP[:, 4]).astype(int)
        hdgs = chunkNP[:, 5].astype(int)

        dhdg = hdgs.max() - hdgs.min()
        if dhdg < 90 or dhdg > 270:
            continue

        p0 = [spds[0], 0, hdgs[0]]

        plsq = optimize.leastsq(residuals, p0,
                                args=([spds, hdgs]),
                                Dfun=dfunc)

        va, vw, xw = plsq[0]
        vcas = aero.tas2cas(va, (ALT_LOW+ALT_HIGH)/2 * aero.ft)
        print plsq
        print ''

        # pkdata = {'speeds': spds, 'angles': hdgs}

        # pickle.dump(pkdata, open("temp.pkl", "wb"))

        airport = utils.get_closest_airport(data[0][1], data[0][2])

        plt.suptitle("%s at %s" % (icao, airport['name']))

        plt.subplot(3, 2, 1)
        plt.xlabel('time (s)')
        plt.ylabel('ground speeds (kts)')
        plt.plot(ts, spds, '.-', ms=5, color='g', label="Ground Speed")
        plt.ylim([0, 600])
        plt.grid()

        plt.subplot(3, 2, 3)
        plt.xlabel('time (s)')
        plt.ylabel('altitude (ft)')
        plt.plot(ts, alts, '.-', ms=5, color='b')
        plt.ylim([0, 40000])
        plt.grid()

        plt.subplot(3, 2, 5)
        plt.xlabel('time (s)')
        plt.ylabel('track angle (deg)')
        plt.plot(ts, hdgs, '.-', ms=5, color='y', label="Track Angle")
        plt.ylim([0, 360])
        plt.grid()

        plt.subplot(3, 2, 2)
        plt.xlabel('time (s)')
        plt.ylabel('TAS (kts)')
        plt.plot(ts, np.ones(ts.shape)*va, '-', lw=3, color='b', label="TAS")
        plt.plot(ts, np.ones(ts.shape)*vcas, '-', lw=3, color='g', label="CAS")
        plt.ylim([0, 600])
        plt.legend()
        plt.grid()

        plt.subplot(3, 2, 4)
        plt.xlabel('time (s)')
        plt.ylabel('Wind Speed (kts)')
        plt.plot(ts, np.ones(ts.shape)*vw, '-', lw=3, color='b')
        plt.grid()

        plt.subplot(3, 2, 6)
        plt.xlabel('time (s)')
        plt.ylabel('Wind Angle (deg)')
        plt.plot(ts, np.ones(ts.shape)*xw, '-', lw=3, color='b')
        plt.ylim([-10, 360])
        plt.grid()

        plt.draw()
        plt.waitforbuttonpress(-1)
        plt.clf()

    return dataset