Skip to content

Instantly share code, notes, and snippets.

@diodon
Last active August 3, 2021 02:38
Show Gist options
  • Select an option

  • Save diodon/ecf01fab6c2916945f39b996da5768e1 to your computer and use it in GitHub Desktop.

Select an option

Save diodon/ecf01fab6c2916945f39b996da5768e1 to your computer and use it in GitHub Desktop.
HeatAccumulation: calculate BK curve
getAcumExposure = function(ts, tmin=27.5, tinc=1){
## calculate accumulated exposure
## this version does not have gaps in temp bins
## ts is a times series of daily values
## tmin is the starint temp to accumulate
## tinc is the number of decimal places to round temperature
## removes NA's
ts = ts[!is.na(ts)]
ts = ts[ts>=tmin]
if (length(ts)==0){
return(NA) ## this is the case when tmin is higher than ts max
}
ts = round(ts, tinc)
tsDF <- data.frame(temp = seq(tmin, max(ts, na.rm=T), by=10^-tinc), nDays=NA)
for (i in 1:nrow(tsDF)){
tsDF$nDays[i] <- sum(ts >= tsDF$temp[i])
}
## add last temp with zero days
tsDF <- rbind(tsDF, data.frame(temp=max(tsDF$temp+10^-tinc), nDays=0))
return(tsDF)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment