So here's the R code (for two elements here Zn and Si maps, provided interface is slopping in correct direction, just off from horizontal, with right of image lower) Extracts a box 600 um wide, 250um high., for pixel spacing of 1um. (To change from 600um wide, change 1-600 lines. To change from 250 high, change from 250 for vertical start line). Units in mm.
Attached is a generalised code where width and height are set using boxheight and boxwidth. Also can do either direction from horizontal. The automatic script .r file which guides through.
Initialize and read maps from .grd files
library(rgdal)
library(raster)
library(rasterVis)
#enter directory
setwd('')
enter Zn & Si map grid file name
grdZn<-readGDAL('')
grdSi<-readGDAL('')
rgrdZn<-raster(grdZn)
rgrdSi<-raster(grdSi)
calculate angle of interface, by clicking on two points along interface, then right click and end
windows.options(width=20, height=10)
plot(rgrdSi,asp=1)
#grid(3,5)
bs<-click(rgrdSi,xy=T) # click on boundary in two places to determine angle
xs<-max(bs[,1])-min(bs[,1])
ys<-max(bs[,2])-min(bs[,2])
xs100<-(.001/xs)*xs #.0o1 is 1um - so move 100 pixels in x
ys100<-(.001/xs)*ys
a<-max(bs[,1])-min(bs[,1])
o<-max(bs[,2])-min(bs[,2])
sang<-((atan(o/a))*180)/pi
sang_rad<-atan(o/a)
determine start position of extraction box. [i.e. the first line to extract, length of line 250um, orientated perpendicular to interface]
#determine vertical second point
plot(rgrdSi,asp=1)
st<-click(rgrdSi,xy=T) # click on start point
ed<-cbind(st[,1],st[,2]+.25)
st<-cbind(st[,1],st[,2])
#op<-tan(sang_rad)*0.25 #shift along slope for vertical of .25 or 250um
hop<-sin(sang_rad)*0.25#shift along slope for vertical of .25 or 250um
vop<-tan(sang_rad)*hop
#ed<-cbind(ed[,1]+op,ed[,2]-vop)
ed<-cbind(ed[,1]+hop,ed[,2]-vop)
#h<-sqrt(xs^2+ys^2) #pythagoras calculate hypotenuse which #set to step
#hxs<-(1/h)*xs
#hys<-(1/h)*ys
#ed<-cbind(ed[,1]-(hxs*op),ed[,2]-(hys*op))
#check
a<-ed[1,2]-st[1,2]
o<-ed[1,1]-st[1,1]
((atan(o/a))*180)/pi
90-((atan(o/a))*180)/pi
#create line
fL<-rbind(st,ed) #create first line
sfL<-SpatialLines(list(Lines(Line(fL),ID="a")))
p<-(levelplot(rgrdSi,at=seq(minValue(rgrdSi),5,length=100),margin=FALSE,ylim=c(ymax(rgrdSi),ymin(rgrdSi)),xlim=c(xmax(rgrdSi),xmin(rgrdSi)),main=list("Si Wt %",cex=0.8)))
p+layer(sp.lines(sfL,col="red",lwd=1))
extract data from box area, as series of individual lines
lxxyyc<-fL
for(i in 1:600){
assign(paste("L",i,sep="."),cbind(lxxyyc[,1]-xs100,lxxyyc[,2]+ys100))
assign(paste("spL",i,sep="."),SpatialLines(list(Lines(Line(eval(parse(text=paste("L",i,sep=".")))),ID="a"))))
lxxyyc<-cbind(lxxyyc[,1]-xs100,lxxyyc[,2]+ys100)
# to check extracting correct area, comment out following lines, to speed up for test extract
assign(paste("eLSi",i,sep="."),extract(rgrdSi,eval(parse(text=paste("spL",i,sep=".")))))
assign(paste("eLZn",i,sep="."),extract(rgrdZn,eval(parse(text=paste("spL",i,sep=".")))))
}
# angle between lines
o<-fL[1,1]-L.1[1,1]
a<-L.1[1,2]-fL[1,2]
((atan(o/a))*180)/pi
-90-((atan(o/a))*180)/pi
# plot lines on map
LTop<-rbind(L.1[1,],L.600[1,])
LBottom<-rbind(L.1[2,],L.600[2,])
SpTop<-SpatialLines(list(Lines(Line(LTop),ID="a")))
SpBottom<-SpatialLines(list(Lines(Line(LBottom),ID="a")))
png("Position of line extraction_box.png",height=512,width=1024,units="px")
p<-(levelplot(rgrdSi,at=seq(minValue(rgrdSi),5,length=100),margin=FALSE,ylim=c(ymin(rgrdSi),ymax(rgrdSi)),xlim=c(xmin(rgrdSi),xmax(rgrdSi)),colorkey=F))
p<-p+layer(sp.lines(eval(parse(text=paste("spL",1,sep="."))),col="red",lwd=3))
#p<-p+layer(sp.lines(eval(parse(text=paste("spL",100,sep="."))),col="red",lwd=1))
#p<-p+layer(sp.lines(eval(parse(text=paste("spL",200,sep="."))),col="red",lwd=1))
#p<-p+layer(sp.lines(eval(parse(text=paste("spL",300,sep="."))),col="red",lwd=1))
#p<-p+layer(sp.lines(eval(parse(text=paste("spL",400,sep="."))),col="red",lwd=1))
#p<-p+layer(sp.lines(eval(parse(text=paste("spL",500,sep="."))),col="red",lwd=1))
p<-p+layer(sp.lines(eval(parse(text=paste("spL",600,sep="."))),col="red",lwd=3))
p<-p+layer(sp.lines(SpTop,col="red",lwd=3))
p<-p+layer(sp.lines(SpBottom,col="red",lwd=3))
p
dev.off()
extracted lines to stack; average lines using median; save results
eLSi_stack<-eLSi.1[[1]]
for(i in 2:600){
eLSi_stack<-rbind(eLSi_stack,eval(parse(text=paste("eLSi",i,sep=".")))[[1]][])
}
eLZn_stack<-eLZn.1[[1]]
for(i in 2:600){
eLZn_stack<-rbind(eLZn_stack,eval(parse(text=paste("eLZn",i,sep=".")))[[1]][])
}
library(matrixStats)
#save extracted data
avg_res_Si<-cbind(seq(1,length(eLSi_stack[1,]),1),colMedians(eLSi_stack),colMeans(eLSi_stack))
write.table(avg_res_Si,'Si line extract median and mean.csv',row.names=F,sep=",")
avg_res_Zn<-cbind(seq(1,length(eLSi_stack[1,]),1),colMedians(eLZn_stack),colMeans(eLZn_stack))
write.table(avg_res_Zn,'Zn line extract median and mean.csv',row.names=F,sep=",")
write.table(eLSi_stack,'Si 600 lines extracted.csv',sep=",")
write.table(eLZn_stack,'Zn 600 lines extracted.csv',sep=",")
plot results as graph and save
#view
sy <-250
sx<-5
sxm<-1
windows.options(width=20, height=10)
plot(colMedians(eLSi_stack),seq(1,length(eLSi_stack[1,]),1),pch='',xlim=c(-0.3,sx),ylim=c(0,sy),ylab="distance (microns)",xlab="(wt. %)",cex.lab=1.5,cex.axis=1.5)
lines(colMedians(eLSi_stack),seq(1,length(eLSi_stack[1,]),1),col="red",lwd=3)
lines(colMedians(eLZn_stack),seq(1,length(eLSi_stack[1,]),1),col="blue",lwd=3)
legend(sx-sxm,sy,legend=c("Si","Zn"),col=c("red","blue"),lty=1:1,cex=1.5,lwd=3)
#save
png("Zn Si extract lines horiz.png",height=512,width=1024,units="px")
plot(colMedians(eLSi_stack),seq(1,length(eLSi_stack[1,]),1),pch='',xlim=c(-0.3,sx),ylim=c(0,sy),ylab="distance (microns)",xlab="(wt. %)",cex.lab=1.5,cex.axis=1.5)
lines(colMedians(eLSi_stack),seq(1,length(eLSi_stack[1,]),1),col="red",lwd=3)
lines(colMedians(eLZn_stack),seq(1,length(eLSi_stack[1,]),1),col="blue",lwd=3)
legend(sx-sxm,sy,legend=c("Si","Zn"),col=c("red","blue"),lty=1:1,cex=1.5,lwd=3)
dev.off()