#! /bin/csh -f
#
#  edit a fitsfile....
#  for vi users:    "vi -b" might also work

if ($?EDITOR) then
  set edit=$EDITOR
else if ($?VISUAL) then
  set edit=$VISUAL
else
  set edit=vi
endif


if ($#argv == 0) then
  echo "Usage: $0 fitsfile(s)"
  echo "Edit primary HDU FITSfile with your favorite editor, needs"
  echo "NEMO's fitshead and scanfits programs"
  echo "Your editor is: $edit" '(change by using $EDITOR or $VISUAL)'
  echo "For vi users:   'vi -b'  may also be good enough"
  exit 0
endif

echo Using EDITOR/VISUAL $edit
set tmp=tmp$$
set bad=0

foreach f ($*)
  if ($bad) then
     echo "### Warning: problems converting $oldfile, skipped"
  else if ($?oldfile) then
     echo "Overwritten $oldfile"
  endif
  set oldfile=$f
  set bad=1
  \rm -f $tmp.* >& /dev/null

  fitshead $f > $tmp.txt
  if ($status) continue
  $edit $tmp.txt
  if ($status) continue
  fitshead $tmp.txt out=$tmp.head
  if ($status) continue
  scanfits $f $tmp.data select=data
  if ($status) continue
  cat $tmp.head $tmp.data > $f
  set bad=0
end

\rm -f $tmp.* >& /dev/null
