# Read a tps file type.
require(sm)
require(session)
# Function Deffinition Section
# Make P-inverse matrix
PImat<- function(n) {
I<- matrix(1/n, ncol=n, nrow=n)
I<- diag(n)-I
         }

# End of Function Deffinition Section
newindex<- c(1, 4, 2, 3, 5, 6, 7, 8, 9)

cat("Directory of tps files \n")
ltps<- system('ls *.tps*', TRUE, TRUE)
print(ltps)
infile= ask("Enter a tps file name in quotes")
# file = "sneathd.tps"
nskip<- 0
skount<- 0
# plot it
     op <- par(mfrow = c(2, 3), # 2 x 3 pictures on one plot
               pty = "s")       # square plotting region,
junk<- readLines(infile,  n = -1, ok = TRUE)
filength<- length(junk)
cat("File lines =", filength, "\n")


#
#
# start looping for files
#
while (nskip < filength) {
skount<- skount + 1
rm(junk)
cat("nskip(1) =", nskip, "of", filength, "\n")
# Read the file header to find out landmark number lm
junk<- scan(infile, skip=nskip, nlines = 1, what = "c")
if ( !identical((1*grep("lm=", junk)), 0) ) {
 if(identical(skount, 1)) {
            write(junk, file = paste(infile,"n", sep=""), append = FALSE)
                         } else {
            write(junk, file = paste(infile,"n", sep=""), append = TRUE)
                                }
 junk<- sub("lm=", "LM=", junk) }
out<- strsplit(junk, 'LM=')
lk<- out[[1]][2]
rm(junk)
out<- texteval(paste("lk<-", lk))
rm(out)
nskip<- nskip+1
# Read the file for lk lines of data landmarks.
Data<- matrix(scan(infile, skip = nskip, nlines = lk), ncol = lk, nrow = 2)
if (identical(skount, 1)) { Darray<- array(0, c(210, lk, 2))}
Data[,]<- Data[ , newindex]
nskip<- nskip+lk
cat("nskip(2) =", nskip, "\n")
 write(Data, file = paste(infile,"n", sep=""), ncolumns = 2,  append = TRUE)

#                                                     }
# Read the file for image file and skip it
#   
junk<- scan(infile, skip=nskip, nlines = 1, what = "c")
if (!identical((1*grep("IMAGE=", junk)),0) ) {nskip<- nskip +1
 cat("nskip(3) =", nskip, "\n")
 cat(junk, "\n")
   write(junk, file = paste(infile,"n", sep=""), append = TRUE)
 } 
rm(junk) 
#
# Read the file for file id name
# line 54
junk<- scan(infile, skip = nskip, nlines = 1, what="c")
if ( !identical((1*grep("id=", junk)), 0) ) {
 write(junk, file = paste(infile,"n", sep=""), append = TRUE)
 junk<- sub("id=", "ID=", junk) }
out<- strsplit(junk, 'ID=')
nam<- out[[1]][2]
rm(out, junk)
cat("name:", nam, "\n")
plot(Data[1,], Data[2,], main = nam)
Darray[skount, ,]<- t(Data)
nskip<- nskip+1
 cat("nskip(4) =", nskip, "\n")
 if (!identical((filength-nskip),0)) {
#
# read one line to find number of discrete outline data curves 
# 69                                                     }
# Read the file for image file and skip it
  junk<- scan(infile, skip=nskip, nlines = 1, what = "c")
  if (!identical((regexpr("curves=", junk)[1] + 1), 0)) { 
     rm(junk)
     junk<- scan(infile, skip = nskip, nlines = 1, what="c")
     nskip<- nskip+1
     cat("nskip(5) =", nskip, "\n")
     out<- strsplit(junk, 'curves=')
     nc<- out[[1]]
     cat("Number of outline curves:", nc, "\n")
     rm(junk)
  junk<- texteval(paste("nc<-", nc))
# loop to read nc outline curves 
   for (i in 1:nc) {
    junk<- scan(infile, skip = nskip, nlines = 1, what="c")
    nskip<- nskip+1
 cat("nskip(6) =", nskip, "\n")
    junk<- strsplit(junk[1], 'points=')
    no<- junk[[1]][2]
    rm(junk)
    junk<- texteval(no<- paste("no<-", no))
    Outlin<- matrix(scan(infile, skip = nskip, nlines = no), ncol = no, nrow = 2)
    lines(Outlin[1,],Outlin[2,]) 
    nskip<- nskip+no
 cat("nskip(7) =", nskip, "\n")
    rm(no, nc, junk, i)
                      } } # end of outline points 
 # pause()
                 }
                 }


# *******************************************************************
par(op)


rm(filength, lk, nskip, op, infile, resultText)
