#! /usr/bin/gawk -f 
# Last edited on 1999-07-28 01:44:34 by stolfi

function error(msg)
{
   printf "%s\n", msg > "/dev/stderr"; 
   abort=1; exit(1);
}

BEGIN{
  abort = -1;
  usage = "project-points ORG VEC POSDIR TG1 ... TGn > OUT";
  #
  # where ORG, VEC are names of files, POSDIR is a directory,
  # and TG1 ... TGn are tags such that POSDIR/TGi.pos is a file.
  # All these files should contain the same number of lines
  # with format COORD LABEL, where COORD is a real number and LABEL
  # is any word. (The LABELs must be sorted and must match in both
  # files.)  The COORD field on line k of file FOO will be denoted
  # by FOO[k].
  # 
  # This script prints to stdout a list PROJi TGi
  # for each given file TGi, where PROJi is the sum 
  # of (PTi[k] - ORG[k])*VEC[k] for all lines k.
  
  if (ARGC < 5) { error(("ARGC = " ARGC " - usage: " usage)); }
  org = ARGV[1]; if (org == "") { error(("usage: " usage)); }
  vec = ARGV[2]; if (vec == "") { error(("usage: " usage)); }
  dir = ARGV[3]; if (dir == "") { error(("usage: " usage)); }
  
  k = 0;
  while ((getline < org) > 0)
    { k++;
      if (NF != 2) { error((org ", line " k ": bad format")); }
      wk = $2; w[k] = wk
      o[k] = $1;
      getline < vec;
      if (ERRNO != "0") { error((vec ": " ERRNO)); }
      if ((NF != 2) || (wk != $2)) { error((vec ", line " k ": bad format")); }
      v[k] = $1;
    }
  if (ERRNO != "0") { error((org ": " ERRNO)); }
  close(org);
  close(vec);
  printf "reading points: ", tgi > "/dev/stderr";
  N = k;
  for(i=4;i<ARGC;i++)
    { tgi = ARGV[i];
      pti = (dir "/" tgi ".pos");
      printf " %s", tgi > "/dev/stderr";
      k = 0;
      prod = 0;
      while ((getline < pti) > 0)
        { k++;
          if (k > N) { error((pti ", line " k ": too many lines")); }
          wk = w[k];
          if ((NF != 2) || (wk != $2)) { error((pti ", line " k ": bad format")); }
          pk = $1;
          prod += (pk-o[k])*v[k];
        }
      if (ERRNO != "0") { error((pti ": " ERRNO)); }
      if (k < N) { error((pti ", line " k ": not enough lines")); }
      close(pti);
      printf "%+8.5f %s\n",  prod, tgi;
    }
  printf "\n", tgi > "/dev/stderr";
    
}