#! /bin/tcsh -f
# Authors: Rolf Sander & Hartwig Harder, 2009

##############################################################################

if ( "$1" == "" ) then
  echo "xtxt2nc = convert textfile (*.txt) to netcdf (*.nc)"
  echo "usage:"
  echo "   xtxt2nc <myfile.txt>"
  echo "or:"
  echo "   xtxt2nc *.txt"
  exit
endif

foreach inputfile ($argv)
  echo;echo "working on $inputfile..."
  # convert from DOS (CRLF) to UNIX format:
  #fromdos "$inputfile"
  gawk -f txt2nc.awk "$inputfile"
  ferret -batch -script "$inputfile:r"
  # ferret renames all to uppercase. Revert this now:
  ncrename -v PRESS,press            $inputfile:r.nc
  ncrename -v TEMP,temp              $inputfile:r.nc
  # for coordinate variable TIME, also change name of dimension:
  ncrename -v TIME,time -d TIME,time $inputfile:r.nc
  # cleanup temporary files:
  rm $inputfile:r.jnl $inputfile:r_*.dat
  rm time_ax_dum.nc metafile.plt
end

exit
