I am trying to read multiple binary (365)files and do some calculations then read to another folder. I faced 2 problems:
First: The code below gives me an error:
Warning message: closing unused connection 3 (C:\PHD\Climate Data\Out\Temperature_1.dat
setwd("C:\\PHD\\Climate Data\\Wind\\")
listfile<-dir()
for (i in c(1:365)) {
conne <- file(`listfile[i], "rb")
file<- readBin(conne, integer(), size=2, n=360*720, signed=T)
file<-file-273.15 #
close(conne)
to.write = file(paste("C:\\PHD\\Climate Data\\Out\\Temperature_",i,".dat",sep=""),"wb")
writeBin(file, to.write)
close(to.write)
}
Second:
As my files are 16bit,size=2, I wanted to make sure that my output data have the same size. So I just added size=2 to writeBin(file, to.write, size=2) but this gave an error:
Error in writeBin(file, to.write, size = 2) : size 2 is unknown on this machine.
Any help on writing binary files properly is appreciated
Based on what I can see you are trying to
writeBin()a vector ofdoublesforcing each element to be of size 2. That is not possible.You may be able to
as.integer(file)and try to write the resulting vector as elements ofsize=2but you’ll have to try this.