示例#1
0
文件: GGti.cpp 项目: TarekHC/gammalib
/***********************************************************************//**
 * @brief Insert Good Time Interval
 *
 * @param[in] index Index after which interval is inserted.
 * @param[in] tstart Start time of interval.
 * @param[in] tstop Stop time of interval.
 *
 * @exception GException::invalid_argument
 *            Start time later than stop time
 *
 * Inserts a Good Time Interval after the specified @p index in the Good
 * Time Intervals. The method does not reorder the intervals by time,
 * instead the client needs to determine the approriate @p index.
 *
 * Invalid parameters do not produce any exception, but are handled
 * transparently. If the interval is invalid (i.e. @p tstart > @p tstop)
 * an exception is thrown. If the @p index is out of the valid range, the
 * index will be adjusted to either the first or the last element.
 ***************************************************************************/
void GGti::insert_gti(const int& index, const GTime& tstart, const GTime& tstop)
{
    // Throw an exception if time interval is invalid
    if (tstart > tstop) {
        std::string msg = "Invalid time interval specified. Start time "+
                          tstart.print(NORMAL)+" can not be later than "
                          "stop time "+tstop.print(NORMAL)+".";
        throw GException::invalid_argument(G_INSERT_GTI, msg);
    }

    // Set index
    int inx = index;

    // If inx is out of range then adjust it
    if (inx < 0)     inx = 0;
    if (inx > m_num) inx = m_num;

    // Allocate new intervals
    int    num   = m_num+1;
    GTime* start = new GTime[num];
    GTime* stop  = new GTime[num];

    // Copy intervals before GTI to be inserted
    for (int i = 0; i < inx; ++i) {
        start[i] = m_start[i];
        stop[i]  = m_stop[i];
    }

    // Insert GTI
    start[inx] = tstart;
    stop[inx]  = tstop;

    // Copy intervals after GTI to be inserted
    for (int i = inx+1; i < num; ++i) {
        start[i] = m_start[i-1];
        stop[i]  = m_stop[i-1];
    }

    // Free memory
    if (m_start != NULL) delete [] m_start;
    if (m_stop  != NULL) delete [] m_stop;

    // Set new memory
    m_start = start;
    m_stop  = stop;

    // Set number of elements
    m_num = num;
    
    // Set attributes
    set_attributes();

    // Return
    return;
}
示例#2
0
/***********************************************************************//**
 * @brief Invalid Good Time Interval found
 *
 * @param[in] origin Method that throws the error.
 * @param[in] tstart Good Time Interval start time.
 * @param[in] tstop Good Time Interval stop time.
 * @param[in] message Optional error message.
 ***************************************************************************/
GException::gti_invalid::gti_invalid(std::string origin, GTime tstart,
                                     GTime tstop, std::string message)
{
    // Set method name
    m_origin = origin;

    // Set error message
    m_message = "Invalid Good Time Interval (" + tstart.print() + "-" +
                tstop.print() + ") specified.";
    if (message.length() > 0) {
        m_message += " " + message;
    }

    // Return
    return;
}
示例#3
0
文件: GGti.cpp 项目: TarekHC/gammalib
/***********************************************************************//**
 * @brief Reduce Good Time Intervals to specified interval
 *
 * @param[in] tstart Start time of interval.
 * @param[in] tstop Stop time of interval.
 *
 * @exception GException::invalid_argument
 *            Start time is later than stop time
 *
 * Reduces the Good Time Intervals to the specified interval. Reducing means
 * that all Good Time Intervals are dropped that fall outside the specified
 * interval [@p tstart, @p tstop], and Good Time Intervals will be limited
 * to >@p tstart and <=@p tstop in case that their boundaries are outside
 * [@p tstart, @p tstop].
 ***************************************************************************/
void GGti::reduce(const GTime& tstart, const GTime& tstop)
{
    // Throw an exception if time interval is invalid
    if (tstart > tstop) {
        std::string msg = "Invalid time interval specified. Start time "+
                          tstart.print(NORMAL)+" can not be later than "
                          "stop time "+tstop.print(NORMAL)+".";
        throw GException::invalid_argument(G_REDUCE, msg);
    }

    // Adjust existing GTIs. This will limit all GTIs to [tstart,tstop].
    // All GTIs outside [tstart,tstop] will have start > stop. The number
    // of valid GTIs will also be determined.
    int num = 0;
    for (int i = 0; i < m_num; ++i) {
        if (m_start[i] < tstart) {
            m_start[i] = tstart;
        }
        if (m_stop[i] > tstop) {
            m_stop[i] = tstop;
        }
        if (m_start[i] <= m_stop[i]) {
            num++;
        }
    }

    // If we still have valid GTIs then allocate memory for them, copy
    // over the start and stop times, and update the attributes
    if (num > 0) {

        // Allocate new intervals
        GTime* start = new GTime[num];
        GTime* stop  = new GTime[num];

        // Copy valid intervals
        for (int i = 0; i < m_num; ++i) {
            if (m_start[i] <= m_stop[i]) {
                start[i] = m_start[i];
                stop[i]  = m_stop[i];
            }
        }

        // Free old memory
        if (m_start != NULL) delete [] m_start;
        if (m_stop  != NULL) delete [] m_stop;

        // Set new memory
        m_start = start;
        m_stop  = stop;

        // Set attributes
        m_num = num;
        set_attributes();

    } // endif: there were still valid GTIs

    // ... otherwise we remove all GTIs
    else {
        clear();
    }

    // Return
    return;
}