-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmask.check.R
More file actions
32 lines (28 loc) · 1.37 KB
/
mask.check.R
File metadata and controls
32 lines (28 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
mask.check <- function(dSS=NA,cells=NA,n.cells=NA,n.cells.x=NA,n.cells.y=NA,res=NA,xlim=NA,ylim=NA,
x.vals=NA,y.vals=NA){
if(nrow(dSS)!=n.cells)stop("'dSS' must have 'n.cells' rows")
if(ncol(dSS)!=2)stop("'dSS' must have 2 columns")
if(dim(cells)[1]!=n.cells.x)stop("'cells' should be of length 'n.cells.x'")
if(dim(cells)[2]!=n.cells.y)stop("'cells' should be of length 'n.cells.y'")
if(!all(abs(range(x.vals) + c(-res/2, res/2) - xlim) < 1e-9))stop("x.vals doesn't match up with xlim")
if(!all(abs(range(y.vals) + c(-res/2, res/2) - ylim) < 1e-9))stop("y.vals doesn't match up with ylim")
expected.dSS <- as.matrix(expand.grid(x.vals, y.vals))
if(!all(abs(dSS - expected.dSS) < 1e-9)){
stop("'dSS' rows are not in the correct order: x must cycle through all values before y increments to the next value")
}
for(i in 1:n.cells){
s.cell.x <- i%%n.cells.x
s.cell.y <- floor(i/n.cells.x)+1
if(s.cell.x==0){
s.cell.x=n.cells.x
s.cell.y=s.cell.y-1
}
match=which(cells==i,arr.ind=TRUE)
if(!all(c(s.cell.x,s.cell.y)==match))stop("error 1")
xlim.cell=c(s.cell.x-1,s.cell.x)*res
ylim.cell=c(s.cell.y-1,s.cell.y)*res
if(max(x.vals[s.cell.x]+c(-res/2,res/2)-xlim.cell)>1e-6)stop("error 2")
if(max(y.vals[s.cell.y]+c(-res/2,res/2)-ylim.cell)>1e-6)stop("error 3")
}
print("all tests passed")
}