#!/bin/csh 
#
# Packs NetCDF files with ncks with gzip level 4;
# the file is made setgid to indicate that it is packed;
# this is an internal convention.
#
#----------------------------------------------------------------

  set arch = `uname -s`
  set ncks_opts = "--history --abc -4 -L 2 --cnk_dmn lev,1 "
  set ncks = "$BASEDIR/$arch/bin/ncks ${ncks_opts}"

###  set ncks = "$SHARE/dasilva/bin/ncks ${ncks_opts}"
####  set libs = "$SHARE/dasilva/opengrads/Contents/Linux/Versions/2.0.a5.oga.5/x86_64/gex"

  if ( $#argv < 1 ) then
        echo "n4zip - internal gzip packing of NetCDF-4 files"
#	echo "usage:  n4zip [-n] [-nbits n] [-u undef] [-v]  nc4_filename(s)"
	echo "usage:  n4zip [-n] [-v]  nc_filename(s)"
        echo "The option -n means dryrun"
	exit 1
  endif

  set verb = ""
  set dryrun = ""
  set nbits = ""
  set undef = "-u 1.0E+15"

  foreach arg ( $argv )
     if ( "$1" == "-n" ) then
       shift
       set dryrun = "echo"
     endif
     if ( "$1" == "-v" ) then
       shift
       set verb = "-v"
     endif
     if ( "$1" == "-shave" ) then
        shift
        set nbits = "-n 10"
	echo "WARNING: ignoring -shave for now"
     endif
     if ( "$1" == "-nbits" ) then
        shift
        set nbits = "-n $1"
        shift
	echo "WARNING: ignoring -nbits for now"
     endif
     if ( "$1" == "-u" ) then
        shift
        set undef = "-u $1"
        shift
	echo "WARNING: ignoring -u for now"
     endif
  end

# Make sure it finds relevant system libraries
# --------------------------------------------
###  if ( -e $LD_LIBRARY_PATH ) then
###       setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:$libs
###  else
###       setenv LD_LIBRARY_PATH $libs
### endif

# For NC-4 file...
# -----------------
  foreach file ( $argv )

   set ok = 1
   set ext = $file:e
   if ( "$ext" == "gz" ) then
        $dryrun gunzip -v $file
        if ( $status ) then
             set ok = 0
             echo $0": cannot unzip $file; skipping it"
        else
             set file = $file:r
             set ext = $file:e
        endif
   endif

   if ( $ok ) then

#     Make sure output file has extension nc4
#     ------------------------------------
      if ( "$ext" == "nc" ) then
	set basen = $file:r
	mv $file $basen.nc4
        if ( $status ) then
           echo " could not rename ... "
        else
           set file = $basen.nc4
	endif
      endif

#     Packed files have setgid set; skip those
#     ----------------------------------------
      set type = `file $file | awk '{print $2}'`
      if ( "$type" != "setgid" ) then
           echo -n $0": working on" `basename $file` " ..."
#           $dryrun $ncks $verb $nbits $undef -i $file -o $file~ -t '*:GZIP 2'
           $dryrun $ncks $file $file~ 
           if ( $status ) then
                set ok = 0
  	        echo " not ok"
           else
	      echo " ok"
              $dryrun mv $file~ $file
              $dryrun chmod g+s,g+w $file
           endif
      else
           echo $0": $file apparently packed already, skipping it"
      endif

  endif

end

