# R functions for labeling Date-indexed axes # Days to the beginning of each month ltt.private.dim <- c(0,31,59,90,120,151,181,212,243,273,304,334) ltt.private.dim.leap <- c(0,31,60,91,121,152,182,213,244,274,305,335) # Convert a string formatted as yyyy/mm/dd to a fraction of a year date.string.to.year.fraction <- function(Dstring) { year <- as.integer(substr(Dstring,1,4)) month <- 1 if(nchar(Dstring)>=7) month <- as.integer(substr(Dstring,6,7)) day <- 1 if(nchar(Dstring)>=10) day <- as.integer(substr(Dstring,9,10)) fraction <- 0 if(is.leap.year(year)) { fraction <- year + (ltt.private.dim.leap[month]+day-0.5)/366 } else { fraction <- year + (ltt.private.dim[month]+day-0.5)/365 } return(fraction) } is.leap.year <- function(year) { if(year/400-as.integer(year/400)==0) return(T) if(year/4-as.integer(year/4)==0 && year/100-as.integer(year/100)) return(T) return(F) } # Calculate a set of locations from a set of labels xaxis.label.to.at <- function(label) { at <- rep(0.0,length(label)) for(i in seq(1,length(label))) { at[i] <- date.string.to.year.fraction(label[i]) } return(at) }