#!/bin/sh

  echo "### $0 ###"
  PGM="${HOME}/hysplit"

  ilab1=CAPTEX2 # title
  nc=5		# number of clusters

  rm -f cluster.ps

#################
# cluster1.scr
#################
  dur=48	                # hours to do clustering
  int=1	                        # Endpoint interval to use (hours)
  skip=1	                # Skip trajectory interval
  ODIR="${PGM}/working/"    	# Cluster output directory (with trailing /)
  cd $ODIR
  iproj=0
   
  echo $dur   > CCONTROL
  echo $int  >> CCONTROL 
  echo $skip >> CCONTROL 
  echo $ODIR >> CCONTROL 
  echo $iproj >> CCONTROL
    
# do clustering
  rm -f TCLUS TNOCLUS CLUSTER DELPCT CLUSTERno CMESSAGE
  $PGM/exec/cluster 
  if [ ! -f CLUSTER -o ! -f DELPCT ]; then
     echo "ERROR - cluster failed"
     exit
  fi
  rm TNOCLUS	# not used
  rm TCLUS	# not used

#################
# cluster2.scr
#################
# variables for clusend (determining final number of clusters) --
  mint=15    # min number traj 
  minc=3     # min number of clusters
  maxc=10    # max number of clusters
  pct=30     # min % change in DELPCT from 1 step to the next
             #     ie breakpoint to indicate end of clustering

# TSV plot
  $PGM/exec/clusplot  -iDELPCT -oclusplot.ps -l${ilab1}
  mv clusplot.ps cluster.ps
  if [ ! -s cluster.ps ]; then
     echo "ERROR - clusplot failed"
     exit
  fi
      
# determine final number of clusters
  rm -f CLUSEND
  if $PGM/exec/clusend -iDELPCT -n${minc} -a${maxc} -t${mint} -p${pct} -oCLUSEND; then
     echo $PGM/exec/clusend OK 1st try
  else
     # relax criteria to help get # clusters
     pct=`expr $pct - 10`
     $PGM/exec/clusend -iDELPCT -n${minc} -a${maxc} -t${mint} -p${pct} -oCLUSEND
  fi
  if [ ! -s CLUSEND ]; then
     echo "ERROR - clusend failed"
     exit
  fi

# generate list of trajs in each cluster
  rm -f CLUSLIST_${nc}
  $PGM/exec/cluslist -iCLUSTER -n${nc} -oCLUSLIST_${nc}
  if [ ! -s CLUSLIST_${nc} ]; then
     echo "ERROR - cluslist failed"
     exit
  fi

# add trajs not clustered (cluster #0) to CLUSLIST
  if [ -s CLUSTERno ]; then       
     cat CLUSLIST_${nc} >> CLUSTERno
     mv CLUSTERno CLUSLIST_${nc}
  fi

# create TRAJ.INP.Cxx file for each cluster (list of trajs) 
  rm -f TRAJ.INP*
  if $PGM/exec/clusmem -iCLUSLIST_${nc}; then

     ##############################################
     # for each cluster 
     ##############################################
     run=-1					# for cluster #0 
     while [ $run -lt $nc ]; do
        run=`expr $run + 1`
        if [ -f TRAJ.INP.C${run}_${nc} ]; then	# cluster #0 may not exist
           ##############################################
           # create tdump file of all trajs
           ##############################################
           $PGM/exec/merglist -i+TRAJ.INP.C${run}_${nc} -omdump -ptdump
           if [ -s mdump.tdump ]; then
               mv mdump.tdump C$run.tdump
           else
               echo "ERROR - merglist TRAJ.INP.C${run}_${nc}"
               exit 
           fi
           ##############################################
           # compute mean 
           ##############################################
           $PGM/exec/trajmean -i+TRAJ.INP.C${run}_${nc} 
           if [ -s tmean ]; then
              mv tmean C${run}mean.tdump
           else
              echo "ERROR - trajmean TRAJ.INP.C${run}_${nc}"
              exit 
           fi
        fi
     done

     ##############################################
     # make one tdump for all means
     ##############################################
     # for each cluster  (not zero)
     run=0
     rm -f MEAN.LIST
     while [ $run -lt $nc ]; do
        run=`expr $run + 1`
        echo "C${run}mean.tdump" >> MEAN.LIST
     done

     $PGM/exec/merglist -i+MEAN.LIST -omdump -ptdump
     if [ -s mdump.tdump ]; then
        mv mdump.tdump Cmean.tdump
     else
        echo "ERROR - merglist MEAN.LIST"
        exit 
     fi
     rm -f MEAN.LIST

  else
     echo "ERROR - clusmem"
     exit 
  fi

  #################
  # cluster3.scr
  #################

# plot trajectories for each cluster
  run=-1				# for cluster #0 
  while [ $run -lt $nc ]; do
     run=`expr $run + 1`
     if [ -s C$run.tdump ]; then 
        rm -f LABELS.CFG
        echo "'TITLE&','TRAJECTORIES IN CLUSTER ${run} of ${nc} $ilab1&'" \
                 > LABELS.CFG
        $PGM/exec/trajplot -iC$run.tdump -j${PGM}/graphics/arlmap -e$dur -z80 -k0 -l0 -v4
        if [ -s trajplot.ps ]; then
           cat trajplot.ps >> cluster.ps
           rm  trajplot.ps
        else
           echo "Error in trajplot -iC$run.tdump" 
           exit 
        fi
        rm -f LABELS.CFG
     fi
  done
   
# compute/plot mean traj for each cluster
  tmout=Cmean.tdump 
  if [ -s $tmout ]; then
     rm -f LABELS.CFG
     echo "'TITLE&','CLUSTER MEAN TRAJECTORIES $ilab1&'" > LABELS.CFG
     $PGM/exec/trajplot -j${PGM}/graphics/arlmap -i$tmout -e$dur -z80 -k${nc}:12345 -l-12 -v4
     if [ -s trajplot.ps ]; then
        cat trajplot.ps >> cluster.ps
        rm  trajplot.ps
     else
        echo "ERROR - trajplot -i$tmout"
        exit 
     fi
     rm -f LABELS.CFG
  fi

# fix page numbers in concatenated file
  if [ -s cluster.ps ]; then
     mv cluster.ps cluster.tmp.ps
     ${PGM}/exec/catps2ps -icluster.tmp.ps -ocluster.ps
     rm cluster.tmp.ps
  fi
  if [[ "$OSTYPE" == "darwin"* ]]; then
     open cluster.ps 
  else
     gs cluster.ps  
  fi

# ---------------------------------------------------------------------
# cleanup
# rm -f CCONTROL
# rm -f CLUSTER CLUSTERno CMESSAGE DELPCT
# rm -f CLUSLIST_* CLUSEND 
# rm -f TRAJ.INP.C?_?
# rm -f fdump8309????
# rm -f  *.tdump
