#Data Electron Microprobe from Nov3, 2005 (1103)
h1<-w1<-9
h2<-w2<-2

paramIn<- function(typ,xo,yo,xl,yl,th){
	     xl<-xl/2; x2<- x1<-xo-xl; x3<-x4<- xo+xl
	     yl<-yl/2; y1<-y4<-yo-yl; y2<-y3<-yo+yl
	     yl<-c(y1,y2); xl<-c(x1,x2)
        yr<-c(y3,y4); xr<-c(x3,x4)
        yu<-c(y2,y3); xu<-c(x2,x3)
        yd<-c(y4,y1); xd<-c(x4,x1)
	     xleft<-lm(xl~yl)
	     xright<-lm(xr~yr)
	     yup<-lm(yu~xu)
	     ydown<-lm(yd~xd)
	     Out<-c(1, xleft[[1]], xright[[1]], ydown[[1]],yup[[1]])
	     Out	
	     }
TestwParam<- function(p,x,y) {
	 In<-FALSE
	 if(p[1]==1) {
	 	         if(p[2]+p[3]*y <= x) {
	 	         	if(p[4]+p[5]*y >= x) {
	 	         		if(p[6]+p[7]*x <= y){
	 	         			if(p[8]+p[9]*x >= y){
	 	         				In<-TRUE}
	 	         			} } }
	 	 	         }
	  In
	  }
	  
Lines<-read.table("Ascii.listarea", header=TRUE)
attach(Lines)
nlin<- length(Lines[,1])

for (i in 1:nlin) {
filnam<-	jpat[i]
	
quartz(wi=w1, he=h1)
opar<-par(mar=c(0,0,0,0)+0.05, ann=FALSE)
xy<-read.table(paste(filnam,"_2_Ca.txt",sep=''), skip=33)
attach(xy)
YY<-t(as.matrix(xy))
# print(names(xy))
ny<- length(YY[,1])
nx<- length(YY[1,])
image(YY, col=gray(1:255/255),xlab='', ylab='') 
   mtext(paste(filnam, "Ca++", "Xo=", Xo[i], "Yo=",Yo[i]), 
   line=-1.5, col="white" )
detach(xy)

quartz(wi=w1, he=w1)
npar<-par(mar=c(0,0,0,0)+0.05, ann=FALSE)
xy<-read.table(paste(filnam,"_3_P.txt",sep=''), skip=33)
attach(xy)
ZZ<-t(as.matrix(xy))
# print(names(xy))
image(ZZ, col=gray(1:255/255),xlab='', ylab='') 
   mtext(paste(filnam, "PO4", "Xo=", Xo[i], "Yo=",Yo[i]), 
   line=-1.5, col="white" )
detach(xy)
		Pt<-paramIn(1,Xo[i],Yo[i],Xl[i],Yl[i],0)
for (j in 1:nx) {for (k in 1:ny) {
		TF<-TestwParam(Pt,j,k)
		if(TF==TRUE) {points(j/nx,k/ny, color='red', cex=0.2)}
		}
	}

}

detach(Lines)
par(opar)