############################################################################
#
#	File:     tgrmerge.icn
#
#	Subject:  Program to merge line chain files
#
#	Authors:  Gregg M. Townsend and William S. Evans
#
#	Date:     June 9, 2000
#
############################################################################
#
#   usage:  tgrmerge file.lch ...
#
#   Tgrmerge merges multiple line chain files to produce a single
#   output file.
#
############################################################################

procedure main(args)
   local f, fname, line, lat, lon
   local minlat, maxlat, minlon, maxlon

   if *args = 0 then
      stop("usage: ", &progname, " file.lch ...")
   minlat := minlon := 9999999
   maxlat := maxlon := 0

   every fname := !args do {
      f := open(fname) | stop("can't open ", fname)
      line := read(f) | stop("empty file: ", fname)
      line ? {
         move(8)
         lon := move(7)
         lat := move(7)
         minlon >:= lon
         minlat >:= lat
         }
      line := read(f) | stop("truncated file: ", fname)
      line ? {
         move(8)
         lon := move(7)
         lat := move(7)
         maxlon <:= lon
         maxlat <:= lat
         }
      close(f)
      }

   write("        ", right(minlon, 7), right(minlat, 7))
   write("        ", right(maxlon, 7), right(maxlat, 7))
   every fname := !args do {
      f := open(fname) | stop("can't open ", fname)
      read(f)
      read(f)
      while write(read(f))
      close(f)
      }
end
