##Collates 2011 CRUTEM data #http://www.metoffice.gov.uk/research/climate/climate-monitoring/land-and-atmosphere/surface-station-records ##COLLATION FUNCTIONS ########################### ##function to read information on 5113 stations get.info=function() { loc="http://www.cru.uea.ac.uk/cru/data/temperature/station-data/headers.txt" download.file(loc,"temp") class0=rep("numeric",10);class0[c(1,5,6)]="character" x= read.fwf("temp",widths=c(6,4,5,6,21,14,4,4,4,4),colClasses=class0,fill=TRUE) names(x)=c("id","lat","long","alt","name","country","start","end","code","code1") x$id=trim(x$id) n=nchar(x$id); temp= substr(x$id,1,1)==" " ; x$id[temp]=paste("0",substr(x$id[temp],2,6),sep="") x=x[!is.na(x$id),] x$name=trim(x$name); x$country=trim(x$country) x$lat=x$lat/10;x$long=x$long/10; x$long= -x$long x$name= gsub("-+$","",x$name) x$country= gsub("-+$","",x$country) x$alt[x$alt ==-999]=NA return(x) } #function to make time series from individual station getcru=function(filename="01/010010") { handle=unz("temp.zip",filename) fred=scan(handle,what="",sep="\n") close(handle) m= grep("Obs.",fred) if(length(m)==0) return(NA) else { fred=fred[(m+1):length(fred)] writeLines(fred,"temp") test=read.table("temp") test[test==-99.0]=NA N=nrow(test) if(test[N,1]-test[1,1]+1 == N) getcru=ts( c(t(test[,2:13])),freq=12,start=test[1,1]) return(getcru)} } ## station=getcru(filename="01/010010") #example #collate into list from HAD version make.cru=function(Info=info) { download.file("http://www.metoffice.gov.uk/media/zip/e/0/station_files.20110720.zip","temp.zip",mode="wb") K=nrow(Info) cru= rep(list(NA),K) ;names(cru)=Info$id for (i in (1:K) { loc= paste(substr(Info$id[i],1,2),"/",Info$id[i],sep="") cru[[i]]=getcru(loc) } return(cru) } info=get.info() cru=make.cru() info$length=unlist(sapply(cru,length)) info$length[info$length==1]=0 # save(info,file="d:/climate/data/station/cru/2011/info.tab") #www.climateaudit.info/data/station/cru/2011/info.tab # save(cru,file="d:/climate/data/station/cru/2011/cru.tab") #www.climateaudit.info/data/station/cru/2011/cru.tab