############################# ### ### read_sint - reads signed binary ### ### read_uint - reads unsigned binary ### ### read_real - reads real number ### ### read_GISS_data_Ts.GHCN.CL ### ftp://data.giss.nasa.gov/pub/gistemp/GISS_Obs_analysis/ARCHIVE/2008_05/binary_files/Ts.GHCN.CL.5 ### uses: read_record_Ts.GHCN.CL, read_header_Ts.GHCN.C ### ### read_GISS_data_SBBX ### ### make.grid.info ### ### ### read_sint <- function(handle) { return(readBin(handle, "int", 1, size=4, signed = TRUE, endian = "big")); } read_uint <- function(handle) { return(readBin(handle, "int", 1, size=4, signed = FALSE, endian = "big")); } read_real <- function(handle) { return(readBin(handle, "numeric", 1, size=4, endian = "big")); } ######################################################################################## ### Ts.GHCN.CL read_GISS_data_Ts.GHCN.CL <- function(handle,method="Ts.GHCN.CL.PA") { header_data <- read_header_Ts.GHCN.CL(handle); records <- list(); while(TRUE) { record <- read_record_Ts.GHCN.CL(handle,method); if( length(record) == 1 ) { return(pairlist(header=header_data, records=records)); } else { records <- append(records, record); } } } read_header_Ts.GHCN.CL <- function(handle) { len <- read_uint(handle); if( len < 9*4 ) { print("invalid file format"); return(NA); } vers <- read_sint(handle); unk1 <- read_sint(handle); unk2 <- read_sint(handle); MTOT <- read_sint(handle); MTOT2 <- read_sint(handle); iyear1 <- read_sint(handle); inv1 <- read_sint(handle); inv2 <- read_sint(handle); MTOT_again <- read_sint(handle); title <- rawToChar(readBin(handle, "raw", len-9*4)); if( read_uint(handle) != len ) { print("invalid file format"); return(NA); } return(pairlist(vers=vers, unk1=unk1, unk2=unk2, MTOT=MTOT, MTOT2=MTOT2, iyear1=iyear1, inv1=inv1, inv2=inv2, MTOT_again=MTOT_again, title=title)); } read_record_Ts.GHCN.CL <- function(handle,method="Ts.GHCN.CL.PA") { len <- read_uint(handle); if( length(len) == 0 ) { # no more data return(NA); } if( len <= 44+16 ) { print("invalid file format"); return(NA); } if(method=="Ts.GHCN.CL.PA") {call2="int"; call3=(len-44-16)/4;call4=TRUE} if(method=="fred") {call2="numeric";call3=Mnow; ca;;4=FALSE} temp_data <- readBin(handle, call2, call3, size=4, call4, endian = "big"); #temp_data <- readBin(handle, "int", (len-44-16)/4, size=4, signed = TRUE, endian = "big"); #temp_data <- readBin(handle, "numeric", Mnow, size=4, endian = "big"); lato <- read_uint(handle); longo <- read_uint(handle); id <- read_uint(handle); ht <- read_uint(handle); name <- rawToChar(readBin(handle, "raw", 36)); vers <- read_uint(handle); MTOT <- read_uint(handle); if( read_uint(handle) != len ) { print("invalid file format"); return(NA); } return(pairlist(temp_data=temp_data, lato=lato, longo=longo, id=id, ht=ht, name=name, vers=vers, MTOT=MTOT)); } ############################################################################################# ## SBBX read_sbbx=function(url,method="download") { if(method=="download") {download.file(url, "temp.bin",mode="wb"); loc="temp.bin"} else loc=url ##url="ftp://data.giss.nasa.gov/pub/gistemp/SBBX1880.Ts.GHCN.CL.PA.250" #18577 ##download.file(url,"temp.bin",mode="wb") handle <- file(loc, "rb"); (header_data= read_header_SBBX(handle)) M=header_data$Mnow sbbx=array(NA,dim=c(M,8000)) info= array(NA,dim=c(8000, 10) ) info=data.frame(info); names(info)=c("len","Next","lats","latn","lonw","lone","nr1","nr2","dl","test") for(i in 1:8000) { (len= info$len[i]= try( read_uint(handle)) ) #6272 # if ( !(length(len)==0 ) ) { info$Next[i] <- read_sint(handle) info$lats[i] <- read_sint(handle); info$latn[i] <- read_sint(handle); info$lonw[i] <- read_sint(handle); info$lone[i] <- read_sint(handle); info$nr1[i] <- read_sint(handle); info$nr2[i] <- read_sint(handle); info$dl <- read_uint(handle) #info[i,] if ( !(len==36) ) {y<- readBin(handle, "numeric",M, size=4, signed=TRUE, endian = "big") ; #1570) y[y==9999]=NA; sbbx[,i]=y } else read_real(handle) # this looks like something (info$test[i]=read_uint(handle) ) } return(list(info=info,sbbx=sbbx) ) } #url="ftp://data.giss.nasa.gov/pub/gistemp/SBBX1880.Ts.GHCN.CL.PA.250" #18577 #test=read_sbbx(url) read_GISS_data_SBBX <- function(handle) { header_data <- read_header_SBBX(handle); Mnow = header_data$Mnow; records <- vector("pairlist", 1); num <- 1; while(TRUE) { record <- read_record_SBBX(handle, Mnow); if( length(record) == 1 ) { return(pairlist(header=header_data, records=records)); } else { records[[num]] = record; num <- num+1; Mnow = record$Next; } } } read_header_SBBX <- function(handle) { len <- read_uint(handle); if( len < 8*4 ) { print("invalid file format"); return(NA); } Mnow <- read_sint(handle); unk1 <- read_sint(handle); unk2 <- read_sint(handle); MONM <- read_sint(handle); unk3 <- read_sint(handle); IYRBEG <- read_sint(handle); BAD <- read_sint(handle); unk4 <- read_sint(handle); title <- rawToChar(readBin(handle, "raw", len-8*4)); if( read_uint(handle) != len ) { print("invalid file format"); return(NA); } return(pairlist(Mnow=Mnow, unk1=unk1, unk2=unk2, MONM=MONM, unk3=unk3, IYRBEG=IYRBEG, BAD=BAD, unk4=unk4, title=title)); } read_record_SBBX <- function(handle, Mnow) { len <- read_uint(handle); if( length(len) == 0 ) { # no more data return(NA); } if( len != (8+Mnow)*4 ) { print("invalid file format"); return(NA); } Next <- read_sint(handle); LATS <- read_sint(handle); LATN <- read_sint(handle); LONW <- read_sint(handle); LONE <- read_sint(handle); NR1 <- read_sint(handle); NR2 <- read_sint(handle); DL <- read_sint(handle); temp_data <- readBin(handle, "numeric", Mnow, size=4, endian = "big"); if( read_uint(handle) != len ) { print("invalid file format"); return(NA); } return(pairlist(temp_data=temp_data, Next=Next, LATS=LATS, LATN=LATN, LONW=LONW, LONE=LONE, NR1=NR1, NR2=NR2, DL=DL)); } ##MAKE GRID INFO make.grid.info=function (Data){ f=function(X) unlist(X[2:9]) grid.info=sapply(Data[[2]],f) grid.info=t(grid.info) grid.info=data.frame(grid.info) #the params LATS, LATN, LONW,LONE define the four parameters of the box #decimal values appear to be used grid.info[,2:5]=grid.info[,2:5]/100 #order1=order(-grid.info$LATS,grid.info$LONW) #grid.info[order1,][1:100,] make.grid.info=grid.info make.grid.info } ############################################################################## ### read.Zon.Ts=function(url,freqts=12) { download.file(url, "temp.bin",mode="wb") #about 176 kb handle <- file("temp.bin", "rb"); header_data <- read_header_Ts.GHCN.CL(handle); records <- list(); num=1 len <- read_uint(handle); if( length(len) == 0 ) { # no more data return(NA); } if( len <= 44+16 ) { print("invalid file format"); return(NA); } chron=NULL for (i in 1:14) { x<- readBin(handle, "numeric",(len-80)/8, size=4, signed=TRUE, endian = "big"); x[x==9999]=NA #plot.ts(x,main=paste(i)) chron=cbind(chron,x) y<- readBin(handle, "numeric",(len+96)/8, size=4, signed=TRUE, endian = "big"); #1570 #plot.ts(y) } #end i close(handle) dimnames(chron)[[2]]=1:14 #dim(chron) # 1560 14 count=apply(!is.na(chron),1,sum) chron=ts(chron[count>0,],start=1880,freq=freqts) return(chron) } #function ############################################################################## ### lpl read.lpl= function(url) { #url="ftp://data.giss.nasa.gov/pub/gistemp/GISS_Obs_analysis/lplots/LOW.Ts.GHCN.CL.PA.lpl" fred=read.table(url,skip=4,na.strings="****") x=ts( fred[,2],start=c(1880,1),freq=12) temp=!is.na(x) zon=window(x,end=max(time(x)[temp]) ) return(zon) } use0="pairwise.complete.obs" ############################################################################### ### Zon.web ###################### read.zon.web=function(url) { #url="ftp://data.giss.nasa.gov/pub/gistemp/GISS_Obs_analysis/ARCHIVE/2009_04/ZON/LOTI.zon.web" download.file(url, "temp.bin",mode="wb") #about 303 KB handle <- file("temp.bin", "rb"); (len <- read_uint(handle)); #276 (name0 <- rawToChar(readBin(handle, "raw", 80))) unk1=read_uint(handle) unk2= read_uint(handle) lat=rep(NA,46) for(i in 1:46) lat[i]= read_uint(handle) # [1] -90 -86 -82 -78 -74 -70 -66 -62 -58 -54 -50 -46 -42 -38 -34 -30 -26 -22 -18 -14 -10 -6 -2 2 6 10 14 18 22 26 30 34 38 42 46 50 #[37] 54 58 62 66 70 74 78 82 86 90 na.string=read_real(handle) #9999 ( test= read_uint(handle) ) #276 num=1; fred=TRUE working=NULL while(fred==TRUE) { (len= try( read_uint(handle)) ) #192 if ( !(length(len)==0 ) ) { (year =read_uint(handle)) #1880 (month =read_uint(handle)) # 1 y<- readBin(handle, "numeric",46, size=4, signed=TRUE, endian = "big") ; #1570) y[y==9999]=NA; # this looks like something (test=read_uint(handle) ) working=rbind(working,y) num=num+1 } else {fred=FALSE} } dimnames(working)[[2]]=lat working=ts(working,start=1880,freq=12) close(handle) return(working) }