## this is meant to be called in the context of a batch of runs, it expects a weather file called "weather.wth" in a directory called "rundir"
weatherfix <- function()
{
library(lubridate)
options("stringsAsFactors" = FALSE)
mywth <- read.table("rundir/weather.wth",col.names=c("dom","month","year","doy","tmax","tmin","ppt"))
mywth$tmax <- ifelse(mywth$tmax<=mywth$tmin, mywth$tmin, mywth$tmax) #set tmax==tmin if it's below tmin
mywth$ppt <- ifelse(mywth$ppt<0, 0, mywth$ppt) #get rid of negative precip values
mywth$date <- as.Date(paste(mywth$year,mywth$month,mywth$dom,sep="-")) #converts year-doy to a formal date
mywth <- mywth[!duplicated(mywth),]
# construct the calendar scaffold for this, then you can use merge() with all.x=T to create the leap-day rows
daterange <- data.frame(date=seq(mywth[1,"date"],mywth[length(mywth$date),"date"], by="days")) #vector of dates in 1901-2016
mywth <- merge(daterange,mywth,all.x=T) #all.x means non-matching leapdays from datedf are preserved and will have NA for weather variables
mywth <- mywth[order(mywth$date),]
missingdays <- which(is.na(mywth$doy)) #all other fields get padded with NA when they're missing from the input weather file
mywth[missingdays,] <- mywth[missingdays-1,] #this way you backfill any missing days, whether leapdays or Dec 31 or whatever
mywth$dom <- mday(daterange$date)
mywth$doy <- yday(daterange$date)
mywth$year <- year(daterange$date)
mywth$month <- month(daterange$date)
write.table(cbind(mywth[,c("dom","month","year","doy"),],sprintf("%.3f",mywth$tmax),sprintf("%.3f",mywth$tmin),sprintf("%.3f",mywth$ppt)), #sprintf forces trailing zeroes so fortran accepts the values as floating-points
file="rundir/weather.wth",sep="\t",quote=F,row.names=F,col.names=F,append=F)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.