R/temperature.R

# Author: Robert J. Hijmans
# License GPL3
# Version 0.1  January 2009

diurnalTemp <- function(lat, date, tmin, tmax) {
	TC<-4.0
    P<-1.5
	dayl <- daylength(lat, doyFromDate(date))
	nigthl <- 24-dayl
    sunris <- 12 - 0.5 * dayl
    sunset <- 12 + 0.5 * dayl
	hrtemp <- vector(length=24)
	for (hr in 1:24) {
#    period a: dhour between midnight and sunrise;
		if ( hr < sunris)  {
			tsunst <- tmin+(tmax-tmin)*sin(pi*(dayl/(dayl+2*P)))
			hrtemp[hr] <- (tmin-tsunst*exp(-nigthl/TC)+(tsunst-tmin)*exp(-(hr+24-sunset)/TC))/(1-exp(-nigthl/TC))
		} else if ( hr < (12+P) ) {
#  period b: dhour between sunrise and normal time that tmax is reached (after noon)
			hrtemp[hr] <- tmin+(tmax-tmin)*sin(pi*(hr-sunris)/(dayl+2*P))
		} else if (hr < sunset) {
#  period c: dhour between time of tmax and sunset;
			hrtemp[hr] <- tmin+(tmax-tmin)*sin(pi*(hr-sunris)/(dayl+2*P))
		} else {
#  period d: dhour between sunset and midnight;
			tsunst <- tmin+(tmax-tmin)*sin(pi*(dayl/(dayl+2*P)))
			hrtemp[hr] <- (tmin-tsunst*exp(-nigthl/TC)+(tsunst-tmin)*exp(-(hr-sunset)/TC))/(1-exp(-nigthl/TC))
		}
	}
	return(hrtemp)
}

Try the weather package in your browser

Any scripts or data that you put into this service are public.

weather documentation built on May 2, 2019, 4:56 p.m.