4 #> g <- loadGrid("grid.dat") #substitute with your data file
\r
8 Grid <- function(ranges, nr, nc, data) {
\r
10 m <- matrix(data, nr=nr, nc=nc, byrow=TRUE)
\r
12 dX <- (ranges[2]-ranges[1])/nc
\r
13 dY <- (ranges[4]-ranges[3])/nr
\r
16 ans <- list(dX=dX, dY=dY, m=m, xr=xr, yr=yr)
\r
17 return(structure(ans, class="Grid"))
\r
20 #Load an iDMC basins grid from a file f, and returns results as a Grid object
\r
21 loadGrid <- function(f) {
\r
22 f <- gzfile(f, open="rb")
\r
23 ranges <- readBin(f, "double", 4, size=8, endian="big")
\r
24 nr <- readBin(f, "integer", 1, size=4, endian="big")
\r
25 nc <- readBin(f, "integer", 1, size=4, endian="big")
\r
26 data <- readBin(f, "integer", nr*nc, size=4, endian="big")
\r
28 return(Grid(ranges, nr, nc, data ))
\r
31 plot.Grid <- function(this, palette, ...) {
\r
33 lvls <- sort(unique(c(m))) #how many different levels?
\r
34 if(missing(palette))
\r
35 palette <- 1:length(lvls) #set a default palette
\r
38 x <- seq(this$yr[1], this$yr[2], length=nrow(m))
\r
39 y <- seq(this$xr[1], this$xr[2], length=ncol(m))
\r
40 image(x, y, m, col=palette, breaks=c(min(lvls)-1, lvls), ...)
\r