##################
download_html =function(url) {
download.file(url, "temp.html");
html_handle <- file("temp.html", "rt");
html_data <- readLines(html_handle);
close(html_handle);
unlink("temp.html");
return(html_data);
}
trim=function(x) window( x, start= min(time(x)[!is.na(x)]), end= max(time(x)[!is.na(x)]) )
get.knmi=function(field="crutem3",long=c(0,360),lat=c(-90,90),email="someone@somewhere") {
prefix= paste("http://climexp.knmi.nl/get_index.cgi?email=",email,sep="")
fieldfix=paste("field",field,sep="=")
suffix="standardunits=true"
latfix= paste("lat1=",lat[1],"&lat2=",lat[2],"&lon1=",long[1],"&lon2=",long[2],sep="") # 0 to 360 longitude
dest="d:/temp/temp.dat"
#logon
loc= paste("http://climexp.knmi.nl/start.cgi?",email,sep="")
logon=download_html(loc )
Sys.sleep(1)
#ping data
url=paste(prefix,fieldfix,suffix,latfix,sep="&")
my_info=download_html(url)
Sys.sleep(4)
#rawdata call
work=my_info[grep("\\.dat",my_info)][1]
work=strsplit(work,",")[[1]][3] # " raw data"
rawdata_call= substr(work,11,nchar(work)-14);
loc=paste("http://climexp.knmi.nl/",rawdata_call,sep="") ; #loc
Sys.sleep(1)
#read data
download.file(loc, dest)
work=read.table(dest,skip=3)
out= make_ts(work)
return(out)
}
make_ts=function(work) { # allows for missing years
target= work[1,1]:work[nrow(work),1]
missing= target[is.na(match(target,work[,1]))]
missing= cbind( missing, array(NA, dim=c( length(missing),12)) )
work= rbind(as.matrix(work),missing)
work=work[order(work[,1]),]
work[work==-999.9]=NA
out= ts( c(t(work[,2:13])),start=work[1,1],freq=12)
return(out)
}
##test=get.knmi(field="tlt", long=c(0,360), lat=c(20,90) )