vic/drivers/shared_all/src/alarms.c
void
reset_alarm(alarm_struct *alarm,
            dmy_struct   *dmy_current)
{
    extern global_param_struct global_param;

    double                     delta;
    double                     current;
    double                     next;
    double                     offset;
    dmy_struct                 dmy_current_offset;

    alarm->count = 0;

    if ((alarm->freq == FREQ_NEVER) || (alarm->freq == FREQ_NSTEPS) ||
        (alarm->freq == FREQ_DATE) || (alarm->freq == FREQ_END)) {
        ;  // Do nothing, already set
    }
    else if (alarm->freq == FREQ_NMONTHS) {
        // If aggregation frequency is NMONTHS, then first shift dmy_current
        // forward for one timestep, advance month(s), then shift backward for
        // one timestep. This is to avoid the following issue: the most common
        // usage of AGGFREQ = NMONTHS is to start a simulation from 00:00:00
        // of the first day of a certain month; since dmy_current is
        // "timestep-beginning", the aggregation window timestamp will be the
        // beginning of the last timestep of a month. This will cause problem
        // because of different number of days in each month. This shift here
        // will avoid this problem.
        // NOTE: if a simulation does not start from the beginning of a month,
        // there might be a problem if startday > 28 !!!

        // Shift forward by one time step
        offset = global_param.dt / (double) SEC_PER_DAY;
        current = date2num(global_param.time_origin_num, dmy_current, 0,
                           global_param.calendar, TIME_UNITS_DAYS);
        current += offset;
        num2date(global_param.time_origin_num, current, 0,
                 global_param.calendar, TIME_UNITS_DAYS,
                 &dmy_current_offset);
        // Advance
        delta = time_delta(&dmy_current_offset, alarm->freq, alarm->n);
        current = date2num(global_param.time_origin_num, &dmy_current_offset,
                           0, global_param.calendar, TIME_UNITS_DAYS);
        next = delta + current;
        // Shift backward by one time step
        next -= offset;
        num2date(global_param.time_origin_num, next, 0,
                 global_param.calendar, TIME_UNITS_DAYS,
                 &(alarm->next_dmy));
    }
    else {
        // If other frequency types, directly advance without shifting
        delta = time_delta(dmy_current, alarm->freq, alarm->n);
        current = date2num(global_param.time_origin_num, dmy_current, 0,
                           global_param.calendar, TIME_UNITS_DAYS);
        next = delta + current;
        num2date(global_param.time_origin_num, next, 0,
                 global_param.calendar, TIME_UNITS_DAYS,
                 &(alarm->next_dmy));
    }
}
/******************************************************************************
 * @brief   This routine resets an alarm
 *****************************************************************************/
@brief   This routine resets an alarm
@brief This routine resets an alarm
void
reset_alarm(alarm_struct *alarm,
            dmy_struct   *dmy_current)
{
    extern global_param_struct global_param;
    double                     delta;
    double                     current;
    double                     next;
    double                     offset;
    dmy_struct                 dmy_current_offset;
    alarm->count = 0;
    if ((alarm->freq == FREQ_NEVER) || (alarm->freq == FREQ_NSTEPS) ||
        (alarm->freq == FREQ_DATE) || (alarm->freq == FREQ_END)) {
        ;  
    }
    else if (alarm->freq == FREQ_NMONTHS) {
        offset = global_param.dt / (double) SEC_PER_DAY;
        current = date2num(global_param.time_origin_num, dmy_current, 0,
                           global_param.calendar, TIME_UNITS_DAYS);
        current += offset;
        num2date(global_param.time_origin_num, current, 0,
                 global_param.calendar, TIME_UNITS_DAYS,
                 &dmy_current_offset);
        delta = time_delta(&dmy_current_offset, alarm->freq, alarm->n);
        current = date2num(global_param.time_origin_num, &dmy_current_offset,
                           0, global_param.calendar, TIME_UNITS_DAYS);
        next = delta + current;
        next -= offset;
        num2date(global_param.time_origin_num, next, 0,
                 global_param.calendar, TIME_UNITS_DAYS,
                 &(alarm->next_dmy));
    }
    else {
        delta = time_delta(dmy_current, alarm->freq, alarm->n);
        current = date2num(global_param.time_origin_num, dmy_current, 0,
                           global_param.calendar, TIME_UNITS_DAYS);
        next = delta + current;
        num2date(global_param.time_origin_num, next, 0,
                 global_param.calendar, TIME_UNITS_DAYS,
                 &(alarm->next_dmy));
    }
}
void reset_alarm ( alarm_struct * alarm , dmy_struct * dmy_current ) { extern global_param_struct global_param ; double delta ; double current ; double next ; double offset ; dmy_struct dmy_current_offset ; alarm -> count = 0 ; if ( ( alarm -> freq == FREQ_NEVER ) || ( alarm -> freq == FREQ_NSTEPS ) || ( alarm -> freq == FREQ_DATE ) || ( alarm -> freq == FREQ_END ) ) { ; } else if ( alarm -> freq == FREQ_NMONTHS ) { offset = global_param . dt / ( double ) SEC_PER_DAY ; current = date2num ( global_param . time_origin_num , dmy_current , 0 , global_param . calendar , TIME_UNITS_DAYS ) ; current += offset ; num2date ( global_param . time_origin_num , current , 0 , global_param . calendar , TIME_UNITS_DAYS , & dmy_current_offset ) ; delta = time_delta ( & dmy_current_offset , alarm -> freq , alarm -> n ) ; current = date2num ( global_param . time_origin_num , & dmy_current_offset , 0 , global_param . calendar , TIME_UNITS_DAYS ) ; next = delta + current ; next -= offset ; num2date ( global_param . time_origin_num , next , 0 , global_param . calendar , TIME_UNITS_DAYS , & ( alarm -> next_dmy ) ) ; } else { delta = time_delta ( dmy_current , alarm -> freq , alarm -> n ) ; current = date2num ( global_param . time_origin_num , dmy_current , 0 , global_param . calendar , TIME_UNITS_DAYS ) ; next = delta + current ; num2date ( global_param . time_origin_num , next , 0 , global_param . calendar , TIME_UNITS_DAYS , & ( alarm -> next_dmy ) ) ; } }
@brief   This routine resets an alarm
@brief This routine resets an alarm
// Do nothing, already set // If aggregation frequency is NMONTHS, then first shift dmy_current // forward for one timestep, advance month(s), then shift backward for // one timestep. This is to avoid the following issue: the most common // usage of AGGFREQ = NMONTHS is to start a simulation from 00:00:00 // of the first day of a certain month; since dmy_current is // "timestep-beginning", the aggregation window timestamp will be the // beginning of the last timestep of a month. This will cause problem // because of different number of days in each month. This shift here // will avoid this problem. // NOTE: if a simulation does not start from the beginning of a month, // there might be a problem if startday > 28 !!! // Shift forward by one time step // Advance // Shift backward by one time step // If other frequency types, directly advance without shifting