#!/bin/sh -e
# use autoclass to separate observations into two groups

if [ ! -f simple.db2 ]; then gunzip *.gz; fi

# is this directory writable?
if [ ! -w . ]; then
    echo "permission denied."
    echo 'please copy simple* to a directory where you have write permission'
    exit 1
fi

# to see how the data file simple.db2 was created, see simple.c

# output files may not already exist
rm -f simple.results *data-1 *text-1

# use autoclass to find the groups
autoclass -search simple.db2 simple.hd2 simple.model simple.s-params

# ask autoclass to generate reports
autoclass -reports simple.results simple.search simple.r-params

# copy the oservations into two files,
# depending on the group assigned by autoclass
awk '
/^[0-9]/{
  group_number=$2;
  getline data < "simple.db2";
  print data > "group" group_number;
}' simple.case-data-1

# display the original data (under X windows)
gnuplot -persist <<EOF
set key bottom
set title "original data"
plot "simple.db2"
EOF

# Display the two groups in different colors (under X windows).
# For each class found, the data file has a section like this, from which we extract the mean
# and standard deviation of latitude and longitude:
# numb  t mtt  description           I-jk      Mean     StDev    Mean-*k|/     Mean      StDev
# t   a                                        -jk      -jk       StDev-jk     -*k       -*k
#
# 000 000 R SNcn longitude
#                                      1.266 ( 2.28e-02  9.50e-01)  3.40e+00 ( 3.25e+00  3.36e+00)
# 001 001 R SNcn latitude
#                                      1.266 ( 3.98e+00  9.71e-01)  3.40e+00 ( 7.28e+00  3.41e+00)
cat  simple.influ-o-data-1 | awk '
BEGIN{
  FS="[ ()]+";
  printf("set key bottom\n");
  printf("set title \"groups found by AutoClass\"\n");
  printf("set parametric; set trange [0:6.28]\n");
  printf("plot \\\n");
}
/^[0-9][0-9]/{
  if ($5~/latitude/) {getline; latmean=$3; latsd=$4; ++data}
  if ($5~/longitude/) {getline; lonmean=$3; lonsd=$4; ++data}
  if (data == 2) {
    if (group) printf(",\\\n");
    printf("%s+2.45*%s*sin(t),%s+2.45*%s*cos(t) title \"group %d 95%% contour\",\\\n",
        lonmean, lonsd, latmean, latsd, group);
    printf("  \"group%d\" title \"group %d members\"", group, group);
    data=0; group++;
  }
}
END {  printf("\n");}' | gnuplot -persist
