#!/bin/bash
HELPSTRING="^#HELP#"; # string identifying help lines

#HELP# PURPOSE: visualizes networks and solution ...
#HELP# INPUT  : network file (*.txt) (and solution file (*.sol))
#HELP# OUTPUT : picture of the network (*.eps)
#HELP# Syntax : visualize [-o output_file] [--show-demands] network_file [solution_file]
#HELP# Author : Christian Raack

TELEKOMDIR=~/COatWork-Data/1005
TMPCSV=./temp.csv
COUNTRY=nocountry
TXTBASE="some-instance"
SOLBASE="nosol"
EPSFILE="$TXTBASE-$SOLBASE.eps"
COUNTRYMAPPINGS=$TELEKOMDIR/scripts/country.map
SHOWDEMANDS=0
visualize_me(){

LINKFILE=links.csv
NODEFILE=nodes.csv
if [ "$EPSFILE" == "some-instance-nosol.eps" ];then
   EPSFILE=$DIR/$TXTBASE-$SOLBASE.eps
fi

# get country
COUNTRYGUESS=`gawk -v txtfile=$TXTBASE.txt '
//{
if( $1 == txtfile) printf($2);
}
' $COUNTRYMAPPINGS`
if [ "$COUNTRYGUESS" != "" ]; then
   COUNTRY=$COUNTRYGUESS
fi

gawk -v linkfile=$LINKFILE \
     -v nodefile=$NODEFILE \
     -v country=$COUNTRY \
     -v showdemands=$SHOWDEMANDS '
BEGIN{
   INF        = 1000000;
   maxlat     = -INF;
   minlat     = INF;
   maxlong    = -INF;
   minlong    = INF
   refminlong = 7;
   refmaxlong = 14;
   refminlat  = 48;
   refmaxlat  = 54;
   difflong   = refmaxlong-refminlong;
   difflat    = refmaxlat-refminlat;
}
/^#/{next}
/^[NT]/{
   nodename[$2]=$2;
   nodelong[$2]=$4;
   nodelat[$2]=$5;
   if($4 < minlong)
      minlong=$4;
   if($4 > maxlong)
      maxlong=$4;
   if($5 < minlat)
      minlat=$5;
   if($5 > maxlat)
      maxlat=$5;
   nodeterminal[$2]=1;
}
/^T/{
   nodeterminal[$2]=4;
}
/^L/{
   if($4<=$5)
      name=sprintf("%s_%s",$4,$5);
   else
      name=sprintf("%s_%s",$5,$4);
#    if( ! linkname[name] )
#    {
#       linkflow[name]=0
#       linkcap[name]=0
#    }
   linkname[name]=name;
   linksource[name]=$4;
   linktarget[name]=$5;
   linkcost[name]=$10+$13;
}
/^D/{
   demandname[$2]=$2;
   demandsource[$2]=$4;
   demandtarget[$2]=$5;
   demandvalue[$2]=$7*$8;
   nodedemand[$4] += demandvalue[$2];
   nodedemand[$5] += demandvalue[$2];
}
/^S/{
   if($2<=$3)
      name=sprintf("%s_%s",$2,$3);
   else
      name=sprintf("%s_%s",$3,$2);
   linkflow[name]=$4
   linkcap[name]=$4
}
END{
   nofnodes = asorti(nodename);
   printf("# %-13s;%10s;%10s;%15s;%15s\n","name","longitude","latitude","size","color")   > nodefile;
   for (i = 1; i <= nofnodes; i++)
   {
      node=nodename[i];
      # map unknown coordinates to germany coordinates
      if(country == "nocountry")
      {
         long = ( nodelong[node] - minlong )/(maxlong - minlong)*difflong + refminlong;
         lat  = ( nodelat[node]  - minlat  )/(maxlat   - minlat)*difflat  + refminlat;
      }
      else
      {
         long = nodelong[node];
         lat  = nodelat[node];
      }
      capacity=nodeterminal[node];
      if(showdemands)
         capacity=nodedemand[node]

      if( !showdemands && nodeterminal[node] == 1)
         printf("%15s;%10.4f;%10.4f;%15.1f;%14s\n",node,long,lat,capacity,"100/100/100") >> nodefile;
      else
         printf("%15s;%10.4f;%10.4f;%15.1f\n",node,long,lat,capacity) >> nodefile;
   }
   DNVRng_STTLng;DNVRng;STTLng;0;1
   noflinks = asorti(linkname);
   printf("# %-23s;%15s;%15s;%10s;%10s;%10s\n","name","source","target","flow","capacity","cost")   > linkfile;
   for (i = 1; i <= noflinks; i++)
   {
      link=linkname[i];
      printf("%25s;%15s;%15s;%10.2f;%10.2f;%10.1f\n",link,linksource[link],linktarget[link],linkflow[link],linkcap[link], linkcost[link]) >> linkfile;
   }
}' $TMPCSV

##############  GMT #####################
echo ""
echo " ........................................................ "
echo " ........................................................ "
if test -e $EPSFILE; then
   echo " " Moved old eps file $EPSFILE to $EPSFILE.bak
   mv $EPSFILE $EPSFILE.bak
fi

noderadius=20
if [ $SHOWDEMANDS -eq 1 ];then
   noderadius=40;
fi
$TELEKOMDIR/scripts/creategmt.pl -q "$TXTBASE-$SOLBASE" -c $COUNTRY -r $noderadius -w 8 -f 0/0/200 -z 10 -o $EPSFILE $NODEFILE $LINKFILE

# try to visualize the eps file
if test -e $EPSFILE
then

   echo " " Wrote a new network picture to $EPSFILE
   echo " ........................................................ "
   echo " ........................................................ "
   echo ""
   gv --watch $EPSFILE &
else
echo ""
   echo " " There was some problem visualizing your solution
   echo " ........................................................ "
   echo ""
fi

rm -rf $TMPCSV $NODEFILE $LINKFILE
}

# --------------------------------------------------------------------------
usage() {
   grep $HELPSTRING $0 | sed -e s/$HELPSTRING//;
}


#--------------------------------------------------------------------------
#  Main
#--------------------------------------------------------------------------

# Check parameters and preconditions
if [ $# -lt 1 ]; then
   echo "please provide a network file (*.txt) (and a solution file (*.sol))"
   usage; exit;
fi

if [ $# -gt 7 ]; then
   echo "to many input parameters"
   usage; exit;
fi

rm -rf $TMPCSV
umask 007
while [ $# -gt 0 ]; do
   case "$1" in
      --help)
         usage; exit;
         ;;
      --show-demands)
         SHOWDEMANDS=1
         shift
         ;;
      -o)
         if [ "$2" == "" ]; then
            echo "--- no output file given ---"
            usage; exit;
         fi
         EPSFILE=$2
         shift;shift;
         ;;
      *)
         if test ! -e $1
         then
            echo "file $1 not found"
            usage; exit;
         else
            file=$1
            cat $file >> $TMPCSV
            DIR=`dirname $file`
            BASE=`basename $file .txt`
            BASE2=`basename $file .sol`
            # guess output file
            if test -e $DIR/$BASE.txt
            then
               TXTBASE=$BASE
            fi
            if test -e $DIR/$BASE2.sol
            then
               SOLBASE=$BASE2
            fi
         fi
         shift
         ;;
   esac
done
visualize_me;
