############################################################################
#
#	File:     tgrquant.icn
#
#	Subject:  Program to quantize a line chain file
#
#	Authors:  Gregg M. Townsend and William S. Evans
#
#	Date:     December 18, 1999
#
############################################################################
#
#   usage:  tgrquant [n] [file.tgr]
#
#   Tgrquant copies a chain file, deliberately losing precision by
#   rounding down each coordinate value to a multiple of n (default 10).
#
############################################################################

procedure main(args)
   local n, fname, ifile, line
   local prefix, fename, dim, lon, lat, rndlon, rndlat, newrlon, newrlat

   if n := integer(args[1]) then
      get(args)
   else
      n := 10
    
   if fname := get(args) then
      ifile := open(fname) | stop("can't open ", fname)
   else
      ifile := &input

   if *args > 0 then
      stop("usage: ", &progname, " [n] [file.lch]")

   while line := read(ifile) do line ? {
      prefix := move(4)
      if ="|" then
         fename := "|" || tab(upto('|')) || move(1)
      else
         fename := ""
      dim := move(4)
      lon := move(7)
      lat := move(7)
      rndlon := lon - lon % n
      rndlat := lat - lat % n
      writes(prefix, fename, dim, right(rndlon, 7), right(rndlat, 7))
      while (lon +:= move(4) - 5000) & (lat +:= move(4) - 5000) do {
         newrlon := lon - lon % n
         newrlat := lat - lat % n
         writes(right(newrlon-rndlon+5000, 4), right(newrlat-rndlat+5000, 4))
         rndlon := newrlon
         rndlat := newrlat
         }
      write()
   }
end
