select rainfall events and calculate rainfall event total from time-series data 1

select rainfall events and calculate rainfall event total from time-series data

library(dplyr)

# Set data column as POSIXct, important for calculating duration afterwards
data <- data %>% mutate(DateTime = as.POSIXct(DateTime, format = '%m/%d/%Y %H:%M'))

flags <- data %>% 
  # Set a rain flag if there is rain registered on the gauge
  mutate(rainflag = ifelse(Precip_in > 0, 1, 0)) %>% 
  # Create a column that contains the number of consecutive times there was rain or not.
  # Use `rle`` which indicates how many times consecutive values happen, and `rep`` to repeat it for each row.
  mutate(rainlength = rep(rle(rainflag)$lengths, rle(rainflag)$lengths)) %>% 
  # Set a flag for an event happening, when there is rain there is a rain event, 
  # when it is 0 but not for six consecutive times, it is still a rain event
  mutate(
    eventflag = ifelse(
      rainflag == 1, 
      1, 
      ifelse(
        rainflag == 0 & rainlength < 6, 
        1, 
        0
      )
    )
  ) %>% 
  # Correct for the case when the dataset starts with no rain for less than six consecutive times
  # If within the first six rows there is no rain registered, then the event flag should change to 0
  mutate(eventflag = ifelse(row_number() < 6 & rainflag == 0, 0, eventflag)) %>% 
  # Add an id to each event (rain or not), to group by on the pivot table
  mutate(eventid = rep(seq(1,length(rle(eventflag)$lengths)), rle(eventflag)$lengths))

rain_pivot <- flags %>% 
  # Select only the rain events
  filter(eventflag == 1) %>% 
  # Group by id
  group_by(eventid) %>% 
  summarize(
    precipitation = sum(Precip_in),
    eventStart = first(DateTime),
    eventEnd = last(DateTime)
  ) %>% 
  # Compute time difference as duration of event, add 1 hour, knowing that the timestamp is the time when the rain record ends
  mutate(time = as.numeric(difftime(eventEnd,eventStart, units = 'h')) + 1)

rain_pivot
#> # A tibble: 2 x 5
#>   eventid precipitation eventStart          eventEnd             time
#>                                           
#> 1       2          0.07 2017-10-06 17:00:00 2017-10-06 22:00:00     6
#> 2       4          0.01 2017-10-07 15:00:00 2017-10-07 15:00:00     1

Here is what the above code is Doing:
1. Create a rainflag column that is 1 if there is rain registered on the gauge, 0 otherwise.
2. Create a rainlength column that indicates how many consecutive times there is rain or not.
3. Create an eventflag column that is 1 if there is rain or if there is no rain for less than six consecutive times.
4. Create an eventid column that indicates the id of the event.
5. Filter the data to only include the rain events.
6. Group by the eventid.
7. Summarize the data to get the total precipitation, the start and end time of the event.
8. Compute the duration of the event.

The result is a table with the total precipitation, the start and end time of the event, and the duration of the event.

Similar Posts