16 From the Quick Archives

 16.1 FITS to MPEG

16.1 FITS to MPEG

A question that has come up more than once as a QUICK request is how to turn a series of FITS files into a movie. Antony Holloway has written a script to automatically convert a series of FITS images into an MPEG movie. The script makes use of the Starlink CONVERT and the Berkely mpeg_encode packages.

The script, along with copies of the mpeg_encode source and documentation is available via anonymous FTP:

The mpeg_encode program should be installed and accessible somewhere in your $PATH, the fitstompeg script and default.param.head file should be copied into the directory containing the FITS file you want to convert into a movie. By default the script will take all the files in the current directory whose filename ends in “.FITS” and convert them into PGM images using the Starlink CONVERT program. At this stage if you have the pbmplus package installed (which should be the case for most Starlink sites) you can then modify the output PGM files before they are used to produce the final animation. Two examples of such tinkering are shown in the script (shown below).

  #!/bin/csh
  
  #+
  #  Name:
  #     fitstompeg
  
  #  Purpose:
  #     Convert a series of FITS images to PGM format and from these
  #     generate an mpeg animation file.
  
  #  Description:
  #     The script tries to find the files ( *.FITS) which must be in
  #     the correct order when listed with ls. The program then generates
  #     the pnm files in the local directory and finally produces the
  #     mpeg animation.
  
  #  Authors:
  #     AJH: Anthony Holloway (Starlink, Manchester)
  #     {add_further_authors_here}
  
  #  History:
  #     1-DEC-1998 (AJH):
  #       Original version.
  #     {add_further_changes_here}
  #-
  
  # Source Starlink setup scripts
  
  source /star/etc/login
  source /star/etc/cshrc
  
  # Enable Convert commands
  
  convert
  
  # Convert FITS files to NDF
  
  echo "Converting FITS files to NDF"
  
  fits2ndf in="*.FITS" out="*"
  
  # Convert and count each NDF to PGM format, animframeX.pgm
  
  echo "Converting NDF files to PGM format"
  
  set i = 1
  
  foreach file (*.sdf)
  
      set outname = $file:r
  
  # Original attempt - fails on BITPIX -32 files
  #   fits2pnm $file >! $outname".pnm"
  
      ndf2pgm in=$outname out="animframe"$i".pgm"
  
  # Optional delete of input file
  #    rm -f $file
  
  
  # Optional pgm image manipulation
  # NB: Final image must be animframe$i.pgm, hence mv command
  # e.g. Scaling by a factor 0.25
  #
  #    pnmscale 0.25 animframe$i.pgm > animframe-s$i.pgm
  #    mv animframe-s$i.pgm animframe$i.pgm
  
  # e.g. Change the contrast of the images
  #    pgmnorm animframe$i.pgm > animframe-s$i.pgm
  #    mv animframe-s$i.pgm animframe$i.pgm
  
      @ i = $i + 1
  
  end
  
  @ i = $i - 1
  
  echo "Total number of frames is $i"
  echo "Editing the default.param file to set this value"
  
  # Edit the mpeg_encode parameter file to set the number of frames
  
  cp default.param.head default.param
  echo "animframe*.pgm  [1-$i]" >> default.param
  echo "END_INPUT" >> default.param
  
  # Run the mpeg encoding command
  
  mpeg_encode default.param
  
  # Optional delete of PGM files
  # rm -f animframe*
  
  # end