Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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
#>     <int>         <dbl> <dttm>              <dttm>              <dbl>
#> 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
Comment

PREVIOUS NEXT
Code Example
Python :: predict probabilities with xg boost 
Python :: check accessability of the file 
Python :: print a box like the ones below 
Python :: read file python 
Python :: numpy substract subsequent elements 
Python :: k and M to int in pandas 
Python :: computecost pyspark 
Python :: python check if more than 1 is true 
Python :: Lists and for loops 
Python :: python file is writable 
Python :: how to take multiple integer input in python 
Python :: initialize boolean list of size python 
Python :: how to check if a dictionary is empty in python 
Python :: except Exception, e: suds python 
Python :: python3 main.py 
Python :: rolling call on one column and groupby second pandas 
Python :: Slice Age in Python 
Python :: pytest using tempfile 
Python :: password protected mongo server 
Python :: brython implemantation 
Python :: get localapplication python 
Python :: next function with inherited list python 
Python :: how to convert ui file to py file 
Python :: django abstractuser fields 
Python :: python vectorize 
Python :: range() in python 
Python :: django datefield year only 
Python :: python oops 
Python :: select python interpreter vscode 
Python :: list unpacking python 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =