#!/bin/csh
if ( $#argv == 0 || x$1 == "x-h" ) then
  cat - <<EOF
Syntax: clusterpredict [-pa] file
see clusterexpand for more info.
This command is to be run from within a specific structure directory containing a str.out file.
EOF
exit 1
endif

if ( x$1 == "x-pa" ) then
  set peratom
  shift
endif

foreach file ( ../clusters.out ../$1.ecimult ../lat.in )
if ( ! -e $file ) then
  echo Cannot find $file file
  exit 1
endif

cp -f ../clusters.out .
corrdump -c -s=str.out -l=../lat.in | blanktonl > tmpcorr.tmp
#paste tmpcorr.tmp ../$1.ecimult | awk 'BEGIN {s=0.;} {s+=$1*$2;} END {print s;}' > pred.tmp
awk 'BEGIN {c=0; while (getline < "tmpcorr.tmp") {if ($1!="") {c++; corr[c]=$1;}}; i=0; s=0; } \
{if ($1!="") {\
  i++; s+=corr[i]*$1;\
  if (i==c) {print s; s=0; i=0;}\
}}' ../$1.ecimult > pred.tmp
if ( $?peratom ) then
  cp -f pred.tmp $1
else
  awk '{print '`cat str.out | cellcvrt -pn`'*$1}' pred.tmp >! $1
endif
rm -f clusters.out tmpcorr.tmp pred.tmp
