add.weather <- function(df, weather.df) { ## set up calculations to compute degrees from radians and vice versa rad <- function(x) x * (pi/180) deg <- function(x) x * (180/pi) ## subset weather data to only include one hour on each end of the ## radar data file weather.df <- subset(weather.df, timestamp > min(df$timestamp) - 60*60 & timestamp < max(df$timestamp) + 60*60) ## create an interpolation function from the weather data for wind speed interp.fun <- approxfun(weather.df$timestamp, weather.df$wspd, rule=2, method = "linear") df$wspd <- interp.fun(df$timestamp) ## create an interpolation function from the weather data for wind direction ## convert first to linear vectors - x and y weather.df$wdir.x <- cos(rad(90 - weather.df$wdir)) weather.df$wdir.y <- sin(rad(90 - weather.df$wdir)) interp.fun <- approxfun(weather.df$timestamp, weather.df$wdir.x, rule=2, method = "linear") temp.x <- interp.fun(df$timestamp) interp.fun <- approxfun(weather.df$timestamp, weather.df$wdir.y, rule=2, method = "linear") temp.y <- interp.fun(df$timestamp) ## convert back to unit vectors df$wdir <- (90 - deg(atan2(temp.y, temp.x))) %% 360 return(df) } # end of function