[R-sig-Geo] Reading National Snow and Ice Data Center binary files

Anthony Fischbach afischbach at usgs.gov
Thu Oct 13 21:31:35 CEST 2011


This works:
http://r-sig-geo.2731867.n2.nabble.com/file/n6890251/test.png 
require(sp)
library(raster)
library(rgdal)

pixel <- 25000 #pixel dimension in meters for both x and y 
xMin <- -3837500 #From NSIDC: ulxmap -3837500
xMax <- xMin + (pixel*304) 
yMax <- 5837500 #From NSIDC: ulymap 5837500
yMin <- yMax - (pixel*448) 
r <- raster(nrow=448, ncol=304, xmn=xMin, xmx=xMax, ymn=yMin, ymx=yMax)
projection(r) <- '+proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +k=1 +x_0=0
+y_0=0 +a=6378273 +b=6356889.449 +units=m +no_defs'
#for(### Place time loop here){  
yyyy<- "2011" ### Use for debugging
mm<- "10" ### Use for debugging
dd = 12 ### Use for debugging
##Near Real Time: 
## 
ftp://sidads.colorado.edu/pub/DATASETS/nsidc0081_nrt_nasateam_seaice/north/
##Best long-term record through 2010: 
## 
ftp://sidads.colorado.edu/pub/DATASETS/nsidc0081_nrt_nasateam_seaice/north/[yyyy]/ 
	filename <-
paste("ftp://sidads.colorado.edu/pub/DATASETS/nsidc0081_nrt_nasateam_seaice/north/", 
	"nt_",yyyy,mm,sprintf("%2.2d", dd),"_f17_nrt_n.bin", sep="")
	
	con <- file(filename, 'rb')
	x <- readBin(con, "raw", 300)
	x <- readBin(con,"int", size=1, signed=FALSE, 150000)
	close(con)
	newx <- numeric(length(x)) #new variable for recoding the 'raw' byte data
	newx[x==251] <- 1 # arbitrarily set the the polar data hole to 100% ice
cover
	newx[x>251] <- 0 # set all non-ice pixels to 0% ice cover
	newx[x<251] <- x[x<251]/250 #scale ice cover to 1
	r <- setValues(r, newx)	# place result in raster
	plot(r) # Plot it 
	#plot(land, add=T) #Plot a land map to for reference
	sp <- as(r, 'SpatialGridDataFrame')
	geoTiffFile<- paste('D:/NSIDC/NearRealTime/',
"nt_",yyyy,mm,sprintf("%2.2d", dd),'.tif',sep='')
	writeGDAL(sp, geoTiffFile, drivername="GTiff") 
#}  ### end time loop


-----
Tony Fischbach, Wildlife Biologist
Walrus Research Program
Alaska Science Center
U.S. Geological Survey
4210 University Drive
Anchorage, AK 99508-4650

AFischbach at usgs.gov
http://alaska.usgs.gov/science/biology/walrus
--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Reading-National-Snow-and-Ice-Data-Center-binary-files-tp6882737p6890251.html
Sent from the R-sig-geo mailing list archive at Nabble.com.



More information about the R-sig-Geo mailing list