
#ifndef AP_RESULT_H
#define AP_RESULT_H

#include "boinc_db.h"
#include <vector>
#include <string>
using std::vector;
using std::string;

static const unsigned MAX_COADS = 12;
static const unsigned TIME_SERIES_LENGTH = 64;

class ResultError {
    const char* kind;
    string s;
public:
    ResultError(const char* kind0, string const& s0)
        : kind(kind0), s(s0) {}
    string what() const { return string(kind) + ": " + s; }
};

class InvalidPulseOut : ResultError {
public:
    InvalidPulseOut(string const& s0) : ResultError("invalid pulse.out", s0) {}
};

struct Pulse {
    size_t index;                 // index where peak occurred; in samples
    float peak_power;                               // power of spike
    unsigned int scale;                             // co-add level
    unsigned int dm;                                // dispersion measure
    float period;                         // period if folded; otherwise 0
    unsigned char time_series[TIME_SERIES_LENGTH];

    Pulse() { clear(); }
    void clear() { memset(this, 0, sizeof(*this)); }
};

typedef vector<Pulse> PulseArray;

struct ResultFile
{
    int protocol;
    float thresholds[MAX_COADS];
    PulseArray pulses;
    PulseArray best_pulses;

    ResultFile(RESULT const&) throw(ResultError);
protected:
    void parse_pulse_out(const char* filename) throw(InvalidPulseOut);
    // void filter_by_server_threshold();
    void sort();
};

#endif // AP_RESULT_H
