#+
#  Name:
#     convert.csh
#
#  Purpose:
#     Starts CONVERT from the UNIX shell.
#
#  Type of Module:
#     C-shell script
#
#  Invocation:
#     convert
#
#  Description:
#     This procedure starts CONVERT for use from UNIX by defining the aliases
#     and environment variables needed to execute each application or command.
#     It also initialises ADAM, if this has not already been done.
#
#  Authors:
#     MJC: Malcolm J. Currie (STARLINK)
#     AJC: A.J.Chipperfield (STARLINK)
#     {enter_new_authors_here}
#
#  History:
#     1993 August 20 (MJC):
#        Original version.
#     1993 September 5 (MJC):
#        Added IRCAM2NDF.
#     1993 November 5 (MJC):
#        Removed DIPSO2NDF and NDF2DIPSO.
#     1995 October 30 (AJC):
#        Updated to version 0.6.  Organized to be modified at install
#        time.
#     1995 November 30 (AJC):
#        Added TIFF, GIF and PGM.
#     1997 August 1 (MJC):
#        Added DA2NDF, FITS2NDF, and NDF2DA tasks.  Added automatic
#        conversions for STREAM and GZIP.  Added FITS and STREAM synonym
#        file extensions.  Some reordering of NDF_FORMATS_IN/OUT; also
#        used multi-line creation for clarity.  Corrected con_tiff2ndf
#        and con_ndf2tiff aliases.
#     {enter_further_changes_here}
#
#  Bugs:
#     {note_any_bugs_here}
#
#-
#
#  Prepare to run ADAM applications if this has not been done already.
#  ===================================================================
#
#  Here look to see if there is ADAM_USER directory.  If there is not
#  check whether or not there is an adam file that is not a directory.
#  If there is, issue a warning and exit.  Otherwise create the required
#  directory.
#
if (-d $HOME/adam) then
   echo -n
else
   if (-f $HOME/adam) then
      echo "You have a file called adam in your home directory."
      echo "Please rename it since adam must be a directory for ADAM files."
      exit
   else
      mkdir $HOME/adam
   endif
endif
#
#  Define environment variables.
#  =============================
#
#setenv CONVERT_DIR /usr1/src/gaia/install/bin/convert
#setenv CONVERT_HELP /usr1/src/gaia/install/help/convert/convert
#
#  Define symbols for the applications.
#  ====================================
#
alias ascii2ndf  $CONVERT_DIR/ascii2ndf
alias conhelp    $CONVERT_DIR/conhelp
alias dst2ndf    $CONVERT_DIR/dst2ndf
alias da2ndf     $CONVERT_DIR/da2ndf
alias dst2ndf    $CONVERT_DIR/dst2ndf
alias fits2ndf   $CONVERT_DIR/fits2ndf
alias gif2ndf    $CONVERT_DIR/gif2ndf
alias iraf2ndf   $CONVERT_DIR/iraf2ndf
alias ircam2ndf  $CONVERT_DIR/ircam2ndf
alias tiff2ndf   $CONVERT_DIR/tiff2ndf
alias unf2ndf    $CONVERT_DIR/unf2ndf
alias ndf2ascii  $CONVERT_DIR/ndf2ascii
alias ndf2da     $CONVERT_DIR/ndf2da
alias ndf2dst    $CONVERT_DIR/ndf2dst
alias ndf2fits   $CONVERT_DIR/ndf2fits
alias ndf2gasp   $CONVERT_DIR/ndf2gasp
alias ndf2gif    $CONVERT_DIR/ndf2gif
alias ndf2iraf   $CONVERT_DIR/ndf2iraf
alias ndf2pgm    $CONVERT_DIR/ndf2pgm
alias ndf2tiff   $CONVERT_DIR/ndf2tiff
alias ndf2unf    $CONVERT_DIR/ndf2unf
#
#  Now repeat for the full command names.
#
alias con_ascii2ndf  $CONVERT_DIR/ascii2ndf
alias con_conhelp    $CONVERT_DIR/conhelp
alias con_da2ndf     $CONVERT_DIR/da2ndf
alias con_dst2ndf    $CONVERT_DIR/dst2ndf
alias con_fits2ndf   $CONVERT_DIR/fits2ndf
alias con_gasp2ndf   $CONVERT_DIR/gasp2ndf
alias con_gif2ndf    $CONVERT_DIR/gif2ndf
alias con_iraf2ndf   $CONVERT_DIR/iraf2ndf
alias con_ircam2ndf  $CONVERT_DIR/ircam2ndf
alias con_tiff2ndf   $CONVERT_DIR/tiff2ndf
alias con_unf2ndf    $CONVERT_DIR/unf2ndf
alias con_ndf2ascii  $CONVERT_DIR/ndf2ascii
alias con_ndf2da     $CONVERT_DIR/ndf2da
alias con_ndf2dst    $CONVERT_DIR/ndf2dst
alias con_ndf2fits   $CONVERT_DIR/ndf2fits
alias con_ndf2gasp   $CONVERT_DIR/ndf2gasp
alias con_ndf2gif    $CONVERT_DIR/ndf2gif
alias con_ndf2iraf   $CONVERT_DIR/ndf2iraf
alias con_ndf2pgm    $CONVERT_DIR/ndf2pgm
alias con_ndf2tiff   $CONVERT_DIR/ndf2tiff
alias con_ndf2unf    $CONVERT_DIR/ndf2unf
#
#  Now set the environment variables for NDF automatic conversion.
#  ===============================================================

#  Define input and output formats recognised.
#  ==========================================

#  Formats recognised when accessing pre-existing datasets.  There are
#  numerous products in FITS each uses a different file extension.
setenv NDF_FORMATS_IN \
'FITS(.fit),FIGARO(.dst),IRAF(.imh),STREAM(.das),UNFORMATTED(.unf)'

setenv NDF_FORMATS_IN \
$NDF_FORMATS_IN',UNF0(.dat),ASCII(.asc),TEXT(.txt),GIF(.gif),TIFF(.tif)'

setenv NDF_FORMATS_IN \
$NDF_FORMATS_IN',GASP(.hdr),COMPRESSED(.sdf.Z),GZIP(.sdf.gz),FITS(.fits)'

setenv NDF_FORMATS_IN \
$NDF_FORMATS_IN',FITS(.fts),FITS(.FTS),FITS(.FITS),FITS(.FIT)'

setenv NDF_FORMATS_IN \
$NDF_FORMATS_IN',FITS(.lilo),FITS(.lihi),FITS(.silo),FITS(.sihi)'

setenv NDF_FORMATS_IN \
$NDF_FORMATS_IN',FITS(.mxlo),FITS(.rilo),FITS(.rihi),FITS(.vdlo)'

setenv NDF_FORMATS_IN \
$NDF_FORMATS_IN',FITS(.vdhi),STREAM(.str)'

#  Formats recognised when creating new datasets.
setenv NDF_FORMATS_OUT \
'.,FITS(.fit),FIGARO(.dst),IRAF(.imh),STREAM(.das),UNFORMATTED(.unf)'

setenv NDF_FORMATS_OUT \
$NDF_FORMATS_OUT',UNF0(.dat),ASCII(.asc),TEXT(.txt),GIF(.gif),TIFF(.tif)'

setenv NDF_FORMATS_OUT \
$NDF_FORMATS_OUT',GASP(.hdr),COMPRESSED(.sdf.Z),GZIP(.sdf.gz)'

#  Define format conversion commands.
#  =================================

#  FITS conversions.
#  ----------------
#  Because NDF2FITS uses HDS_WILD which will only accept container files 
#  as input, NDF_TEMP_FITS must specify an alternative to the default
#  HDS scratch file.
if ( $?HDS_SCRATCH ) then
   setenv NDF_TEMP_FITS "$HDS_SCRATCH/temp_fits_^name"
else
   setenv NDF_TEMP_FITS 'temp_fits_^name'
endif

setenv NDF_FROM_FITS \
'$CONVERT_DIR/convertndf from \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

setenv NDF_TO_FITS \
'$CONVERT_DIR/convertndf to \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

#  FIGARO conversions.
#  ------------------
setenv NDF_FROM_FIGARO \
'$CONVERT_DIR/convertndf from \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

setenv NDF_TO_FIGARO \
'$CONVERT_DIR/convertndf to \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

#  IRAF conversions:
#  ----------------
setenv NDF_FROM_IRAF \
'$CONVERT_DIR/convertndf from \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

setenv NDF_TO_IRAF \
'$CONVERT_DIR/convertndf to \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

#  N.B. deletion of IRAF datasets requires a separate command, since two
#  files are involved.
setenv NDF_DEL_IRAF \
'f='\'^dir^name\'';touch "$f.imh" "$f.pix";rm "$f.imh" "$f.pix"'

#  GASP conversions:
#  ----------------
setenv NDF_FROM_GASP \
'$CONVERT_DIR/convertndf from \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

setenv NDF_TO_GASP \
'$CONVERT_DIR/convertndf to \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

#  N.B. deletion of GASP datasets requires a separate command, since two
#  files are involved.
setenv NDF_DEL_GASP \
'f='\'^dir^name\'';touch "$f.hdr" "$f.dat";rm "$f.hdr" "$f.dat"'

#  UNFORMATTED conversions (with FITS headers).
#  -------------------------------------------
setenv NDF_FROM_UNFORMATTED \
'$CONVERT_DIR/convertndf from \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

setenv NDF_TO_UNFORMATTED \
'$CONVERT_DIR/convertndf to \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

#  UNF0 conversions (no FITS headers).
#  ----------------------------------
setenv NDF_FROM_UNF0 \
'$CONVERT_DIR/convertndf from \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

setenv NDF_TO_UNF0 \
'$CONVERT_DIR/convertndf to \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

#  STREAM (direct-access) conversions
#  ----------------------------------
setenv NDF_FROM_STREAM \
'$CONVERT_DIR/convertndf from \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

setenv NDF_TO_STREAM \
'$CONVERT_DIR/convertndf to \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

#  ASCII conversions (with FITS headers).
#  -------------------------------------
setenv NDF_FROM_ASCII \
'$CONVERT_DIR/convertndf from \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

setenv NDF_TO_ASCII \
'$CONVERT_DIR/convertndf to \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

#  TEXT conversions (like ASCII but without FITS headers).
#  ------------------------------------------------------
setenv NDF_FROM_TEXT \
'$CONVERT_DIR/convertndf from \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

setenv NDF_TO_TEXT \
'$CONVERT_DIR/convertndf to \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

#  GIF conversions.
#  ----------------
setenv NDF_FROM_GIF \
'$CONVERT_DIR/convertndf from \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

setenv NDF_TO_GIF \
'$CONVERT_DIR/convertndf to \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

#  TIFF conversions.
#  -----------------
setenv NDF_FROM_TIFF \
'$CONVERT_DIR/convertndf from \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

setenv NDF_TO_TIFF \
'$CONVERT_DIR/convertndf to \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

#  COMPRESSED conversions.
#  ----------------------
#  N.B. These commands sometimes return an error status to the calling
#  process, even when they appear to have worked OK. It's not clear why.
setenv NDF_FROM_COMPRESSED \
'$CONVERT_DIR/convertndf from \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

setenv NDF_TO_COMPRESSED \
'$CONVERT_DIR/convertndf to \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

if ( $?HDS_SCRATCH ) then
   setenv NDF_TEMP_COMPRESSED "$HDS_SCRATCH/temp_Z_^name"
else
   setenv NDF_TEMP_COMPRESSED 'temp_Z_^name'
endif
 
#  GZIP compression conversions.
#  -----------------------------
setenv NDF_FROM_GZIP \
'$CONVERT_DIR/convertndf from \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

setenv NDF_TO_GZIP \
'$CONVERT_DIR/convertndf to \
'\'^fmt\'' '\'^dir\'' '\'^name\'' '\'^type\'' '\'^ndf\'

if ( $?HDS_SCRATCH ) then
   setenv NDF_TEMP_GZIP "$HDS_SCRATCH/temp_Z_^name"
else
   setenv NDF_TEMP_GZIP 'temp_Z_^name'
endif
 
#  DEBUG
#  -----
#  Switch off display all conversion operations for debugging.
setenv NDF_SHCVT 0

#
#  Tell the user that CONVERT commands are now available.

echo ""
echo "   CONVERT commands are now available -- (Version 1.0, 1997 August)"
echo " "
echo "   Defaults for automatic NDF conversion are set."
echo " "
echo "   Type conhelp for help on CONVERT commands."
echo '   Type "showme sun55" to browse the hypertext documentation.'
echo " "
#
#  Exit the procedure.
#
# end
