############## RClimate Script: Mauna Loa Monthly CO2 ################################### ## Script stored on http://chartsgraphs.wordpress.com account for Users to source() ## ## ## Download and process Monthly CO2 Data File ## ## Developed by D Kelly O'Day to demonstrate use of source() function for climate data ## ## http:chartsgraphs.wordpress.com 1/16/10 ## ############################################################################################ library(plyr); library(reshape); par(las=1); par(ps=10) par(oma=c(2.5,1,1,1)); par(mar=c(2.5,4,2,1)) ## STEP 1: SETUP link <-url("ftp://ftp.cmdl.noaa.gov/ccg/co2/trends/co2_mm_mlo.txt") ## STEP 2: READ DATA CO2_data <- read.table(link, sep = "", row.names = NULL,header = F,colClasses = rep("numeric", 7), comment.char = "#", na.strings = -99.99) names(CO2_data) <- c("Yr", "Mo", "Mo_Yr", "CO2", "Trnd", "X", "dys") with(CO2_data,{ ## STEP 3: Determine Month, Year for last reading c <- nrow(CO2_data) # Find number of data rows mo <- Mo[c] # Find last month of data yr <- Yr[c] # Find last year od data lco2 <- CO2[c] # Find last CO2 reading ## STEP 4: CREATE PLOT ###### Set Chart Parameters par(las=1);par(ps = c(10));par(pin = c(6,3)) par(oma=c(2.5,1,3,1)); par(mar=c(2,4,0,0)) ### Make Basic Plot plot(CO2 ~ Mo_Yr, CO2_data, ylim = c(300,400), xlim = c(1958, yr+2), type="l", xaxs = "i", yaxs="i", col = "grey", cex.main=1, cex.axis = .8, cex.lab = .8, xlab = "", ylab = expression(paste(CO[2] - ppmv))) ### Add last point & Yr - month note points(Mo_Yr[c], lco2, type = "p",pch=16, col = "red") points(1959, 395, type = "p",pch=16, col = "red") text(1960, 303, "Source NOAA:ftp://ftp.cmdl.noaa.gov/ccg/co2/trends/co2_mm_mlo.txt", adj=0, cex = 0.8) ## Plot Annotation mo_names <- c("Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec") thru <- paste(mo_names[mo],",",yr, "- ", lco2, " ppmv") # Note on last data point text(1960,395, thru, cex=0.8, adj=0) Title <- expression(paste("C", O[2], " (ppmv)Trends Since 1958, Mauna Loa, Hawaii")) mtext(Title, 3,2, outer = TRUE) mtext(paste("Data Updated Through ", mo_names[mo], ",", yr), 3,1, outer = TRUE, cex = 0.85) mtext("D Kelly O'Day - http://chartgraphs.wordpress.com", 1,1, adj = 0, cex = 0.8, outer=TRUE) mtext(format(Sys.time(), "%m/%d/ %Y"), 1, 1, adj = 1, cex = 0.8, outer=TRUE) }) closeAllConnections()