#!/bin/csh -f
#
#         C-shell script : RNX2CRZ
#             (frontend of RNX2CRX)
#                 1996-12-19  created by Y. Hatanaka.
#                 2007-06-10  updated by Y. Hatanaka.
#                      - file name extension may be lower and upper case
#                 2009-07-07  modified extensively by Y. Hatanaka.
#                      - Ambiguities/bugs in following items are resolved:
#                         * setting of output directory
#                         * case of deletion of input files
#                         * case of overwriting an output file
#                        Options to control them are added.
#                      - Handling of more RINEX file types are added.
#                      - 'gzipped' OBS files can be processed.
#                      - Optionnaly, gzip is applicable instead of
#                        UNIX compress.
#                 2014-03-24  modified by Y. Hatanaka.
#                      - Manipulation of file names in the new file naming
#                        convention (*.rnx/crx) is added.
#                 2018-11-27  modified by Y. Hatanaka.
#                      - Fixing a bug that delete original files by the
#                        option "-d" even if conversions are unsuccessful.
#                 2019-07-12  modified by Y. Hatanaka.
#                      - Displaying files in process and a new option
#                        "-q" to suppress it are added.
#                 2021-12-22  modified by Y. Hatanaka.
#                      - For file compression, "compress" is no longer default 
#                        but still applicable by a new option "-Z".
#                      - The new default is "gzip". The option "-g" is no
#                        longer needed but remains as a dummy for a while.
#                      - use 'gzip -dc' instead of 'zcat'
#
#--------------------------------------------------------------------
set help = 0
foreach var ($argv[*])
   if ( "$var" == '-h' ) set help = 1
end

if($#argv < 1 || $help) then
more << EOF

RNX2CRZ: C-shell script to compress multiple RINEX files.

Usage : RNX2CRZ [-c] [-d] [-Z] [-g] [-f] [-q] [-v] [-h] file ...

       -c : output to the current directory
       -d : delete the input file if the compressed successfully
       -Z : apply UNIX "compress" (default: gzip)
       -g : apply "gzip" (dummy)
       -f : force overwriting output files without inquiring
       -q : quiet mode (suppress display of files in progress)
       -v : verbose mode
       -h : show this message and stop
       file ... : input RINEX (or uncompressed CRINEX) files.
                  Wildcards can be used.

          RINEX           -->    CRINEX      --> compressed RINEX/CRINEX
                               ????????.??d  -->    ????????.??d.gz
       ????????.??o       --> (????????.??d) -->    ????????.??d.gz
       ????????.??o.gz(Z) --> (????????.??d) -->    ????????.??d.gz
       ????????.??n                          -->    ????????.??n.gz
       ????????.??g                          -->    ????????.??g.gz
       ????????.??l                          -->    ????????.??l.gz
       ????????.??p                          -->    ????????.??p.gz
       ????????.??h                          -->    ????????.??h.gz
       ????????.??b                          -->    ????????.??b.gz
       ????????.??m                          -->    ????????.??m.gz
       ????????.??c                          -->    ????????.??C.gz
       *.?O.rnx                              -->    *.?O.crx.gz
       *.?[NM].rnx                           -->    *.[NM]?.rnx.gz

Remarks:
  - Installation of RNX2CRX is necessary to use this tool.
  - The extensions of the input files must conform to the RINEX convention.
  - A compressed file is saved in the same directory as the input file
    unless the option '-c' is specified.
  - An input file is deleted only when the option "-d" is specified and 
    the compression is successful.

   [20211217]

EOF

exit
endif
#--------------------------------------------------------------------

# set default mode
set out_to_current_dir = 0
set delete_input_files = 0
set force_overwrite = 0
set quiet = 0
unset verbose
set COMPRESS = gzip
set EXT = 'gz'

unset noclobber

# check options
foreach var ($argv[*])
   switch ($var)
     case '-c':
       set out_to_current_dir = 1
       shift; breaksw
     case '-d':
       set delete_input_files = 1
       shift; breaksw
     case '-Z':
       set COMPRESS = compress
       set EXT = 'Z'
       shift; breaksw
     case '-g':
       shift; breaksw
     case '-f':
       set force_overwrite = 1
       shift; breaksw
     case '-q':
       set quiet = 1
       shift; breaksw
     case '-v':
       set verbose = 1
       shift; breaksw
     default:
       break
   endsw
end 

# process files
foreach file_in ($argv[*])
    if ( ! $quiet ) echo -n "  $file_in"
    # compose name of output file (excluding ".gz" or ".Z")
    if ( $file_in =~ *.gz || $file_in =~ *.Z ) then
        set file = $file_in:r  # remove ".gz" or ".Z"
        set CAT = 'gzip -dc'
    else
        set file = $file_in
        set CAT = cat;
    endif
    if ( $out_to_current_dir ) set file = $file:t  # remove path
    if      ( $file =~ *.??[oO] ) then
        set file_out = `echo $file | sed -e 's/o$/d/' -e 's/O$/D/' `
    else if ( $file =~ *[oO].rnx || $file =~ *[oO].RNX ) then
        set file_out = `echo $file | sed -e 's/rnx$/crx/' -e 's/RNX$/CRX/' `
    else if ( $file_in =~ *.??[dDnNgGlLpPhHbBmMcC]   ||  \
              $file_in =~ *.rnx || $file_in =~ *.RNX ||  \
              $file_in =~ *.crx || $file_in =~ *.CRX ) then
        set file_out = $file
    else
        if ( ! $quiet ) echo " --- skipped. (already compressed or the file name doesn't fit to the naming convention)"
        continue
    endif
    if ( ! $quiet ) echo -n " --> $file_out.$EXT"
    

    # check if the output file is preexisting
    if ( -e "$file_out.$EXT" && ! $force_overwrite ) then
        if ( ! $quiet ) echo
        echo "The file $file_out already exists. Overwrite?(y/n,default:n)"
        if ( $< !~ [yY] ) then
            if ( ! $quiet ) echo "                 --- skipped."
            continue
        endif
    endif

    # issue the command
    if      ( $file =~ *.??[oO] || $file =~ *[oO].rnx || $file =~ *[oO].RNX ) then
        $CAT $file_in | RNX2CRX - | $COMPRESS -c > $file_out.$EXT
        set stat = $status
    else if ( $file_in =~ *.??[dDnNgGlLpPhHbBmMcC]   || \
              $file_in =~ *.rnx || $file_in =~ *.RNX || \
              $file_in =~ *.crx || $file_in =~ *.CRX) then
        $COMPRESS -c $file_in > $file_out.$EXT
        set stat = $status
    else
        if ( ! $quiet ) echo
        continue
    endif

    # remove the input file
    if ( $stat == 0 && $delete_input_files ) then
        if ( ! $quiet ) echo -n "   --- delete $file_in"
        rm $file_in
    endif
    if ( ! $quiet ) echo

end
