# Read a .jax file type and change to a .tps file type.
 require(sm)
 require(session)    # includes texteval() function
# Function Deffinition Section
# Make P-inverse matrix
PImat<- function(n) {
I<- matrix(1/n, ncol=n, nrow=n)
I<- diag(n)-I
         }
# pause function
pause<- function() {
cat("Push return to continue!")
indata<- readLines(con=stdin(), n=1, ok = TRUE)
  }

# Ask function
ask<- function(text) {
cat(text,": ", sep="")
indata<- readLines(con=stdin(), n=1, ok = TRUE)
                     }

cat("Directory of JAX files \n")
lsout<- system("ls *.jax*", TRUE, TRUE)
print(lsout)
infile= ask("Enter a .jax file name without quotes")
nskip<- 0
skount<- 0
rkount<- 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("$$ ", junk)), 0) ) {
 junk1<- sub("$$ ", "IMAGE=", junk) 
 junk1<- paste("IMAGE=",junk1[2],sep="")
# use this to write the header for each file
 if(identical(skount, 1)) {
            write("LM=9", file = paste(infile,"n", sep=""), append = FALSE)
                         } else {
            write("LM=9", file = paste(infile,"n", sep=""), append = TRUE)
                                }
                          }
# end of header
rkount<- rkount + 1
out<- strsplit(junk, 'IMAGE=')
cat(out[[2]], "\n")
lk<-  0 #    out[[1]][2]
nskip<- nskip+1
# Read the file for lines with "  *" at the beginning.

# junk<-scan(infile, skip = nskip, nlines = 1, what = "c")
while (identical(((runk <-scan(infile, 
                                            skip = nskip, 
                                            nlines = 1, 
                                            what = "c")
                                    ))[1], "*") ) { lk<- lk +1
                                           nskip<- nskip+1
                                           cat(lk, "\n")
                                write(paste(runk[2],runk[3]), 
                                      file = paste(infile,"n", sep=""), 
                                      append = TRUE)
                                           }
write(junk1, file = paste(infile,"n", sep=""), append = TRUE)
write(paste("ID=", rkount ), file = paste(infile,"n", sep=""), append = TRUE)
lk<-0

#                                             
# Read the file for image file and skip it
#   
junk<- scan(infile, skip=nskip, nlines = 1, what = "c")
if (!identical((1*grep("##", junk)),0) ) {nskip<- nskip +1
 cat("nskip(3) =", nskip, "\n")
 cat(junk, "\n")
 } 
rm(junk) 
#                 }
                 }


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


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