#!/bin/csh -f
#
#  HOLMES
#
#  This script will drive movie making programs for
#  3-D Hydro code simulations.
#
#  USAGE: holmes

  echo "START HOLMES"

  # ---------------
  # SET ENVIRONMENT
  # ---------------

    setenv SCR /scratch/s1/u15149   
    setenv HOME /users/guests/u15149   
    setenv READDIR $SCR/junk
    setenv BINDIR $HOME/imaging/bin
    setenv DUMPDIR $SCR/junk
    setenv GEOMDIR $HOME/user_data/lsuATP/sdl
    setenv PIXDIR $HOME/user_data/lsuATP/pix
    setenv IMGDIR $SCR/junk/images
    setenv INFODIR $SCR/info

    setenv PROJ  $HOME/user_data/lsuATP
    setenv ALIAS_WORKENV $HOME
    setenv ALIAS_PROJ lsuATP

    set lmax=128128256
    set path = ( $BINDIR /usr/etc /usr/local/bin /usr/ucb /bin /usr/bin /usr/lib )

  # ------------------
  # START MAIN PROGRAM
  # ------------------

  
    # Setup lock for ".flag" file
    set lock = $BINDIR/.lock

    # Check for lock before modifying ".flag" file
    while (-e $lock)
      echo " " > /dev/null
    end

    # Lock out ".flag" file
    touch $lock

    # Update the flag number 
    # This number tells the script whether it is already running
    # and how many more files it has to process
    set nu = `cat $BINDIR/.flag`
    set nu=`expr $nu + 1`
    echo $nu >! $BINDIR/.flag
    echo ".flag =" $nu 

    # Remove lock on ".flag" file
    rm -f $lock

  
    # Exit if another "holmes" process is already running
    # (if .flag > 1 after being updated)
    if($nu > 1) then
      exit
    endif

    # Start the main loop
    # 
    # Remains in the loop as long as there are data files
    # to process
  
    while ($nu > 0)

  
    # Get the current filename
      set FILENM=`$BINDIR/getbase disk`
      echo $FILENM

  
    # Rename the current file to a generic inputfile
      mv -f $READDIR/$FILENM $DUMPDIR/disk.sun

    # Convert the cylindrical gridded data to a cartesian grid 
      echo "start cylindrical conversion"
      $BINDIR/allcargonn256.$lmax

    # Remove the cylindrical inputfile and the old inputfile
    # for the isosurface program
      rm -f $DUMPDIR/disk.sun 
      rm -f $DUMPDIR/den.car

  
    # Strip leading 4 bytes off fortran binary output file
    # (Isosurface program is written in C)
      echo "start 4 byte strip "
      $BINDIR/strip4 $READDIR/disk.car $DUMPDIR/den.car

    # Remove cartesian fortran outputfile
      rm -f $DUMPDIR/disk.car 

  
    # Read in density levels to set isosurfaces at
      set den1 = `cat $BINDIR/.den1` # Highest density isosurface (innermost shell)
      set den2 = `cat $BINDIR/.den2`
      set den3 = `cat $BINDIR/.den3`
      set den4 = `cat $BINDIR/.den4` # Lowest density isosurface (outermost shell)

      echo "isosurfaces at $den1 : $den2 : $den3 : $den4"

    # Create polygonal isodensity surface files for rendering
      $BINDIR/polyr -raw 256 256 256 -S $GEOMDIR/shell_1 $READDIR/den.car $den1  
      $BINDIR/polyr -raw 256 256 256 -S $GEOMDIR/shell_2 $READDIR/den.car $den2
      $BINDIR/polyr -raw 256 256 256 -S $GEOMDIR/shell_3 $READDIR/den.car $den3 
      $BINDIR/polyr -raw 256 256 256 -S $GEOMDIR/shell_4 $READDIR/den.car $den4 

    # Remove old sdl file if it is there
      rm -f $PROJ/sdl/composite.sdl

  
    # Use the C preprocessor to include the SDL files with the
    # isodensity surfaces in the master sdl file
      cpp -C -P $PROJ/sdl/template.sdl > $PROJ/sdl/composite.sdl

    # Remove the isodensity surface sdl files
      rm -f $PROJ/sdl/shell_1.sdl
      rm -f $PROJ/sdl/shell_2.sdl
      rm -f $PROJ/sdl/shell_3.sdl
      rm -f $PROJ/sdl/shell_4.sdl

  
    # Render final image
      renderit -r raytracer $PROJ/sdl/composite.sdl

    # Remove rendering sdl file
      rm -f $PROJ/sdl/composite.sdl

  
    # FTP the images file to our local machine
      set imgnum = `cat $BINDIR/.base` 
      $BINDIR/ftpfile.sh img.$imgnum.tiff

    # Backup the image file on the remote machine
    # (sometimes the network FTP connection fails)
      cp -f $PIXDIR/output.tiff $IMGDIR/img.$imgnum.tiff

  
    # Check for lock before modifying ".flag" file
      while (-e $lock)
        echo " " > /dev/null
      end

    # Lock out ".flag" file
      touch $lock

    # Decrement the flag number 
      set nu = `cat $BINDIR/.flag`
      set nu=`expr $nu - 1`
      echo $nu >! $BINDIR/.flag

    # Remove lock on ".flag" file
      rm -f $BINDIR/.lock 

  end

  
  # ----------------
  # END MAIN PROGRAM
  # ----------------

  echo "END HOLMES"