#!/bin/csh -f
#
#   Script for converting an NCGM file to an MPEG movie.
#
#   This script should be invoked with:
#
#       ncgm2mpeg [options] ncgm_filename
#      
#   If the file "ncgm_filename" is of the form "xxx.ncgm", then the MPEG
#   file will be called "xxx.mpg".  Otherwise, the MPEG file will be
#   called "ncgm_filename.mpg"
#
#   The basic conversion steps taken are:
#
#     NCGM -> raster file -> PPM -> YUV -> MPEG
#
#   Several pieces of software are required to run this script:
#
#       1. NCAR Graphics (ctrans, ncgmstat, and rassplit)
#
#       2. rasttopnm - part of netpbm package available at
#             http://sourceforge.net/projects/netpbm/
#
#       3. ppm2cyuv - available at
#             http://sentosa.sas.ntu.edu.sg:8000/image/jpeg/cv/
#
#       4. mpeg - see 
#           http://www.planetmirror.com/pub/hpux/X11/Graphics/MPEG-1.2.2/
#
#   Options:
#       -res {width}x{height} : specifies output resolution in pixels
#                               where {width} and {height} are integers.   
#       -i                    : prompt user whenever a file might be
#                               overwritten
#  Other options to add later:
#     1. Changing raster output used
#     2. "-clean" option
#     3. Allow to select which frames you want
#        (like Tom's ncgmgrab script)
#     4. Allow to change output file name
#

#***************#
#               #
# Set defaults. #
#               #
#***************#
set wresltn = 400
set hresltn = 400
set rasfmt = sun
set clobberfiles

if ($#argv < 1) goto usage

while ($#argv > 0)

  switch ($1)

    case "-res":
    case "-resolution":
      shift
      set wresltn = `expr $1 : '\(.*\)x.*'`
      set hresltn = `expr $1 : '.*x\(.*\)'`
      shift
      breaksw

    case "-i":
      shift
      unset clobberfiles
    breaksw

    case "-*":
      echo "$0 : Unknown option <$1>"
      exit 1
      breaksw

    default:
      set ncgmfile = $1
      shift
      breaksw
    endsw
end


#***********************#
#                       #
# Get the root filename #
#                       #
#***********************#
if (! -f $ncgmfile) then
  echo ""
  echo "    $ncgmfile doesn't exist"
  echo ""
  exit 1
endif

if ($ncgmfile:e == "ncgm") then
  set rootname = $ncgmfile:r
else
  set rootname = $ncgmfile
endif


#************#
#            #
# Initialize #
#            #
#************#

set rasfile = $rootname.$rasfmt
set mpgfile = $rootname.mpg

#****************************************#
#                                        #
# Make sure we have all the apps we need #
#                                        #
#****************************************#
#set apps = (ctrans ncgmstat rassplit rasttopnm ppm2cyuv mpeg)
#
#echo ""
#echo "    Checking to be sure we have all the necessary applications..."
#
#foreach app($apps)
#  echo -n "        $app..."
#  which $app >& /dev/null
#  if ($status != 0) then
#    echo "not found"
#    echo ""
#    exit 1
#  endif
#  echo "okay"
#end


#***************************#
#                           #
# Count frames in NCGM file #
#                           #
#***************************#
set nframes = `ncgmstat -c $ncgmfile`

if ($nframes == 0) then 
  echo ""
  echo "    Not measuring any frames in '$ncgmfile'."
  echo "    Are you sure it's a valid metafile?"
  echo ""
  exit 1
endif

if ($nframes == 1) then 
  echo ""
  echo "    You only have one frame in '$ncgmfile'."
  echo ""
  exit 1
endif

if ($nframes > 9999) then 
  echo ""
  echo "    This script was not set up to convert more than 9999 frames."
  echo ""
  exit 1
endif

#*******************************#
#                               #
# Convert NCGM to a raster file #
# and then split it             #
#                               #
#*******************************#

if (! $?clobberfiles) then
#*********************************#
#                                 #
# First check to be sure we don't #
# overwrite existing files.       #
#                                 #
#*********************************#
  if (-f $rasfile) then
    echo ""
    echo "    '$rasfile' already exists."
    echo -n "    Overwrite? (y/n) (n) "
    set answer = $<
    if ("$answer" != "y" && "$answer" != "Y") then
      exit 1
    endif
  endif

  if (-f $mpgfile) then
    echo ""
    echo "    '$mpgfile' already exists."
    echo -n "    Overwrite? (y/n) (n) "
    set answer = $<
    if ("$answer" != "y" && "$answer" != "Y") then
      exit 1
    endif
  endif
endif

#******************************#
#                              #
# Convert NCGM to raster file. #
#                              #
#******************************#
echo ""
echo "    Converting your $nframes-frame metafile to a $rasfmt raster file..."
ctrans -d $rasfmt -res ${wresltn}x${hresltn} $ncgmfile >! $rasfile
if ($status != 0) then
  echo ""
  echo "    The 'ctrans' command failed. Please check your metafile"
  echo "    to be sure it is valid."
  echo ""
  exit 1
endif

#********************#
#                    #
# Split raster file. #
#                    #
#********************#
echo ""
echo "    Splitting the $nframes-frame raster file into individual raster files..."
rassplit $rasfile
if ($status != 0) then
  echo ""
  echo "    The 'rassplit' command failed. Please check '$ncgmfile'"
  echo "    and '$rasfile' to be sure they are valid."
  echo ""
  exit 1
endif

#***********************************#
#                                   #
# rename raster files, convert them #
# to PNM files, then to YUV files   #
#                                   #
#***********************************#
echo ""
echo "    Converting the individual raster files into other formats..."
echo ""
@ num = 1
while ($num <= $nframes)
  set oldnum = `echo $num | awk '{printf("%04d\n",$1)}'`

#
# Rename
#
  /bin/mv $rootname.$oldnum.$rasfmt $rootname{$num}.$rasfmt

#
# Convert to PNM
#
  rasttopnm $rootname{$num}.$rasfmt >! $rootname{$num}.pnm
  if ($status != 0) then
    echo ""
    echo "    The 'rasttopnm' command failed.'"
    echo ""
    exit 1
  endif

#
# Convert to YUV
#
  ppm2cyuv $rootname{$num}.pnm $rootname{$num}
#  if ($status != 0) then
#    echo ""
#    echo "    The 'ppm2cyuv' command failed.'"
#    echo ""
#    exit 1
#  endif

  @ num+=1
end

#*********************************************#
#                                             #
# Okay, take this whole mess of Y, U, V files #
# and convert them to a single MPEG movie     #
#                                             #
#*********************************************#
echo ""
echo "    Converting the Y,U,V files into the final MPEG file..."
echo ""
mpeg -PF -a 1 -b $nframes -h $hresltn -v $wresltn $rootname -s $mpgfile
if ($status != 0) then
  echo ""
  echo "    The 'mpeg' command failed.'"
  echo ""
  exit 1
endif


#*********************************#
#                                 #
# Remove all files created except #
# the original NCGM file and      #
# and the resultant MPEG file     #
#                                 #
#*********************************#
echo ""
echo "    Cleaning up..."
echo ""
/bin/rm $rasfile
@ i = 1
while ($i <= $nframes)
  /bin/rm $rootname{$i}.$rasfmt $rootname{$i}.pnm
  /bin/rm $rootname{$i}.Y $rootname{$i}.U $rootname{$i}.V
  @ i+=1
end

echo ""
echo "    'ncgm2mpeg' completed successfully."
echo "     MPEG file is named '$mpgfile'."
echo ""

exit

#***************************#
#                           #
# ncgm2mpeg usage statement #
#                           #
#***************************#
usage:
echo ""
echo "    usage: ncgm2mpeg [options] ncgm_filename"
echo ""
echo "    Options:"
echo ""
echo "    -res {width}x{height}"
echo "    -i"
echo ""
exit

