问题
A heatwave is defined if the maximum temperature at a meteorological station is 3 °C or more than the normal temperature consecutively for 3 days or more (Adopted from India Meteorological Department, Pune). I have calculated the daily average (daily normal) from multiple-year daily maximum temperature data like
df <- data.frame("date"= seq(from = as.Date("1970-1-1"), to = as.Date("2000-12-31"), by = "day"),
"MaxT" = runif(length(seq.Date(as.Date("1970-1-1"), as.Date("2000-12-31"), "days")), 20, 40))
df$day <- format(df$date, format='%m-%d')
daily_mean <- aggregate(MaxT ~ day, data=df, FUN=mean)
Now it has to be matched with every year's daily maximum temperature and identify the dates when the maximum temperature is 3 °C or more than the normal daily temperature consecutively for 3 days or more. Those events will be considered as heatwaves. My question is how to implement it in R?
回答1:
Here is a way using dplyr
and zoo
's rollapplyr
.
library(dplyr)
df_out <- df %>%
left_join(daily_mean %>% rename(mean_temp = MaxT), by = 'day') %>%
mutate(is_heatwave = zoo::rollapplyr(MaxT > (mean_temp + 3),
3, all,fill = NA))
some instances of heatwave :
df_out[31:50, ]
# date MaxT day mean_temp is_heatwave
#31 1970-01-31 26.31675 01-31 28.31451 FALSE
#32 1970-02-01 22.05946 02-01 29.83059 FALSE
#33 1970-02-02 34.22469 02-02 29.84562 FALSE
#34 1970-02-03 33.03264 02-03 29.87919 FALSE
#35 1970-02-04 36.62357 02-04 31.50603 TRUE
#36 1970-02-05 29.82134 02-05 30.22581 FALSE
#37 1970-02-06 28.13625 02-06 29.64073 FALSE
#38 1970-02-07 29.95754 02-07 29.54277 FALSE
#39 1970-02-08 21.40026 02-08 30.96619 FALSE
#40 1970-02-09 33.10983 02-09 28.16146 FALSE
#41 1970-02-10 30.87346 02-10 29.37693 FALSE
#42 1970-02-11 31.08721 02-11 28.89930 FALSE
#43 1970-02-12 27.34925 02-12 29.27882 FALSE
#44 1970-02-13 31.88582 02-13 29.35825 FALSE
#45 1970-02-14 30.05155 02-14 28.24995 FALSE
#46 1970-02-15 35.07049 02-15 29.02716 FALSE
#47 1970-02-16 39.49029 02-16 32.75644 FALSE
#48 1970-02-17 37.41917 02-17 31.44022 TRUE
#49 1970-02-18 36.03564 02-18 29.56212 TRUE
#50 1970-02-19 36.48052 02-19 30.18766 TRUE
TRUE
values are where heatwave was present. As we can see in row 33, 34 and 35 we had 3 consecutive days when MaxT
was greater than mean_temp
by more than 3 degrees. Similarly, we can verify for other days.
To get yearly heatwave occurrences, we can do :
df_year <- df_out %>%
group_by(year = format(date, "%Y")) %>%
summarise(total_heat = with(rle(is_heatwave),
sum(values, na.rm = TRUE)))
and sum(df_year$total_heat)
would give overall count.
来源:https://stackoverflow.com/questions/61183109/heatwave-calculation-based-on-maximum-temperature-in-r