LogCompressor.cc 6.13 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief Implementation of class LogCompressor
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

pixhawk's avatar
pixhawk committed
31 32 33
#include <QFile>
#include <QTextStream>
#include <QStringList>
34
#include <QFileInfo>
pixhawk's avatar
pixhawk committed
35 36 37 38
#include "LogCompressor.h"

#include <QDebug>

39 40 41
/**
 * It will only get active upon calling startCompression()
 */
42
LogCompressor::LogCompressor(QString logFileName, QString outFileName, int uasid) :
pixhawk's avatar
pixhawk committed
43
        logFileName(logFileName),
44 45 46 47
        outFileName(outFileName),
        running(true),
        currentDataLine(0),
        dataLines(1),
pixhawk's avatar
pixhawk committed
48 49 50 51 52 53 54 55 56
        uasid(uasid)
{
}

void LogCompressor::run()
{
    QString separator = "\t";
    QString fileName = logFileName;
    QFile file(fileName);
57
    QFile outfile(outFileName);
pixhawk's avatar
pixhawk committed
58 59 60 61
    QStringList* keys = new QStringList();
    QStringList* times = new QStringList();

    if (!file.exists()) return;
62
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
pixhawk's avatar
pixhawk committed
63 64
        return;

65 66 67 68 69 70 71 72 73
    if (outFileName != "")
    {
        // Check if file is writeable
        if (!QFileInfo(outfile).isWritable())
        {
            return;
        }
    }

pixhawk's avatar
pixhawk committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    // Find all keys
    QTextStream in(&file);
    while (!in.atEnd()) {
        QString line = in.readLine();
        // Accumulate map of keys
        // Data field name is at position 2
        QString key = line.split(separator).at(2);
        if (!keys->contains(key)) keys->append(key);
    }
    keys->sort();

    QString header = "";
    QString spacer = "";
    for (int i = 0; i < keys->length(); i++)
    {
        header += keys->at(i) + separator;
        spacer += " " + separator;
    }

93
    //qDebug() << header;
pixhawk's avatar
pixhawk committed
94

95
    //qDebug() << "NOW READING TIMES";
pixhawk's avatar
pixhawk committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

    // Find all times
    //in.reset();
    file.reset();
    in.reset();
    in.resetStatus();
    while (!in.atEnd()) {
        QString line = in.readLine();
        // Accumulate map of keys
        // Data field name is at position 2
        QString time = line.split(separator).at(0);
        if (!times->contains(time))
        {
            times->append(time);
        }
    }
112 113 114

    dataLines = times->length();

pixhawk's avatar
pixhawk committed
115 116 117 118 119 120 121 122 123 124 125 126
    times->sort();

    // Create lines
    QStringList* outLines = new QStringList();
    for (int i = 0; i < times->length(); i++)
    {
        outLines->append(times->at(i) + separator + spacer);
    }

    // Fill in the values for all keys
    file.reset();
    QTextStream data(&file);
127
    int linecounter = 0;
128 129 130
    quint64 lastTimeIndex = 0;
    while (!data.atEnd())
    {
131 132
        linecounter++;
        currentDataLine = linecounter;
pixhawk's avatar
pixhawk committed
133 134 135 136 137 138
        QString line = data.readLine();
        QStringList parts = line.split(separator);
        // Get time
        QString time = parts.first();
        QString field = parts.at(2);
        QString value = parts.at(3);
139 140 141 142 143
        // Enforce NaN if no value is present
        if (value.length() == 0 || value == "" || value == " " || value == "\t" || value == "\n")
        {
            value = "NaN";
        }
pixhawk's avatar
pixhawk committed
144
        // Get matching output line
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

        // Constraining the search area might result in not finding a key,
        // but it significantly reduces the time needed for the search
        // setting a window of 1000 entries means that a 1 Hz data point
        // can still be located
        int offsetLimit = 200;
        quint64 offset;
        quint64 index = -1;
        while (index == -1)
        {
            if (lastTimeIndex > offsetLimit)
            {
                offset = lastTimeIndex - offsetLimit;
            }
            else
            {
                offset = 0;
            }
            quint64 index = times->indexOf(time, offset);
            if (index == -1)
            {
                qDebug() << "INDEX NOT FOUND DURING LOGFILE PROCESSING, RESTARTING SEARCH";
                // FIXME Reset and start without offset heuristic again
                offsetLimit+=1000;
            }
        }
        lastTimeIndex = index;
pixhawk's avatar
pixhawk committed
172 173 174 175 176 177 178 179 180 181 182 183
        QString outLine = outLines->at(index);
        QStringList outParts = outLine.split(separator);
        // Replace measurement placeholder with current value
        outParts.replace(keys->indexOf(field)+1, value);
        outLine = outParts.join(separator);
        outLines->replace(index, outLine);
    }



    // Add header, write out file
    file.close();
184 185 186 187 188 189 190

    if (outFileName == "")
    {
        QFile::remove(file.fileName());
        outfile.setFileName(file.fileName());
    }
    if (!outfile.open(QIODevice::WriteOnly | QIODevice::Text))
pixhawk's avatar
pixhawk committed
191
        return;
192
    outfile.write(QString(QString("unix_timestamp") + separator + header.replace(" ", "_") + QString("\n")).toLatin1());
pixhawk's avatar
pixhawk committed
193 194
    //QString fileHeader = QString("unix_timestamp") + header.replace(" ", "_") + QString("\n");

195
    // File output
pixhawk's avatar
pixhawk committed
196 197 198
    for (int i = 0; i < outLines->length(); i++)
    {
        //qDebug() << outLines->at(i);
199
        outfile.write(QString(outLines->at(i) + "\n").toLatin1());
pixhawk's avatar
pixhawk committed
200 201 202

    }

203 204
    currentDataLine = 0;
    dataLines = 1;
pixhawk's avatar
pixhawk committed
205 206
    delete keys;
    qDebug() << "Done with logfile processing";
207
    emit finishedFile(outfile.fileName());
208 209 210
    running = false;
}

211 212 213 214 215
void LogCompressor::startCompression()
{
    start();
}

216 217 218 219 220 221 222 223 224 225 226 227 228
bool LogCompressor::isFinished()
{
    return !running;
}

int LogCompressor::getCurrentLine()
{
    return currentDataLine;
}

int LogCompressor::getDataLines()
{
    return dataLines;
pixhawk's avatar
pixhawk committed
229
}