#!/bin/csh -f

set progname = `basename $0`

if ( ! $#argv ) then
more << EOT
 $progname simple wrapper for posmrg.e

Usage:
 $progname -i *.stacovs -o output.stacov [options]

Options:
 -dir        : directory with stacov files
 -re         : regular expression see below.
 -step n     : include every n file 
 -v YYMMMDD  : epoch
 -nc         : no correlations
 -if FILENAME: FILENAME list of stacov files 
               similar to the content in input.files.
 -showfiles  : show input.files content and exit.

 $progname can skip files using the step option to stay 
 within the 3000 file limit. 
 Using step=2 should be a good approximation.

 $progname can avoid using the -i option, which in stamrg
 is used to  select files with wildcards.
 The -i option could be a problem if the number of selected files 
 is too big for the command line. 
 To minimize this effect stamrg should be executed inside the stacov dir.
 $progname uses a directory name and a simple regular expression to
 select the files :
 ls \$dir |grep .stacov\$| grep -e \$regex > input.files 

 if no regular expression is given, all the stacov files
 in the directory are used.

 You can create your own input.files in a similar way and then
 supply the filename in the command line.

Example:
 $progname -dir stacovdir -re '2004-0.-..' -o comb.stacov

 For more information about stamrg flags type:
 stamrg -H   

Marcelo Santillan
Geodesy Lab & PANGA
Central Washington University
Sept. 2005
EOT
exit
endif 
#setenv WEIGHT FULL
setenv VERBOSE_MODE 0
setenv MODEL 'xvj' 
setenv MIN_SEASON 2.5   # minimum time span to compute seasonal terms  
setenv WEIGHT DIAG
setenv POLE ''
setenv EPOCH ''
setenv EVENT ''

setenv TIE ''

setenv OUTFILE '' 
set test = 0
set regex = "[.]"

set input_files = '' 
set dir = '' 
set step = 1
set showfiles = 0


while ($#argv )
  set input = ( $argv )
  switch($input[1])
    case -i: 
         \rm -f input.files >& /dev/null
         shift argv
         set f = ( $argv[*] )
         while ( $#f &&  ( T$f[1] !~ T-* ) ) 
            #echo $f[1]
            echo $f[1] >> input.files
            shift f
         end
    breaksw
    case -step:
         set step = $input[2]
    breaksw
    case -show*:
         set showfiles  = 1 
    breaksw
    case -inputfile:
         set input_files = $input[2] 
    breaksw
    case -dir:
        if ( -d $dir/$input[2] ) then
            set dir = $dir/$input[2]
        else if ( -d $input[2] ) then
            set dir = $input[2]
        else
          echo "$input[2] doesn't exists!"
        endif 
    breaksw
    case -re:
        set regex = "$input[2]"
    breaksw
    case -test:
       set test = 1
    breaksw
    case -debug:
       setenv VERBOSE_MODE 2
       set test = 1
    breaksw
    case -verb*:
       setenv VERBOSE_MODE 1
    breaksw
    case -nc:
       setenv WEIGHT DIAG
    breaksw
    case -o:
       setenv OUTFILE $input[2] 
    breaksw
    case -v:
        setenv EPOCH `echo $input[2]|tr '[a-z]' '[A-Z]'`
    breaksw
    case -model:
        setenv MODEL $input[2]
    breaksw
    case -min_season:
        setenv MIN_SEASON $input[2]
    breaksw
    case -e:
        setenv EVENT $input[2]
    breaksw
  endsw
  if ( $#argv ) shift argv
end

if (  $input_files != '' ) then
    cat $input_files > input.files
endif
if ( $dir != '' )  then
  #ls $dir |grep '.stacov$' | grep -e "$regex"| awk '{print "'$dir'/"$0}' >! input.files 
 \rm -f input.files >& /dev/null
 foreach f ( `ls $dir |grep '.stacov$' | grep -e "$regex"` ) 
     echo "$dir/$f" >> input.files
 end
endif

if ( $step > 1 ) then
 
 set i = 1
 foreach f ( `cat input.files` ) 
   if ( ! ( $i % $step ) ) then 
     echo $f >> input.files.$$
   endif 
   @ i++
 end
 mv input.files.$$ input.files
endif

if ( $showfiles ) then
   more input.files
   exit
endif



if ( ! -s input.files ) then
    echo "empty input.files"
    exit
endif
      
if ( $OUTFILE == '' ) then
   echo "NO output file !"
   exit
else if ( -e $OUTFILE ) then
     \rm -f  $OUTFILE >& /dev/null
endif

if ( $EPOCH == '' ) then
    cat <<EOT
    No epoch ! , you must supply  an epoch 
    -v YYMMMDD
EOT
    exit
endif
if ( $VERBOSE_MODE ) echo " EPOCH: $EPOCH"

set stacov = $OUTFILE
if ( $test ) then 
  setenv OUTFILE "p$stacov"
endif

if ( $VERBOSE_MODE )  echo " OUTFILE: $OUTFILE"
set  cmd = "$ARCH/posmrg.e"
set  cmd = "$GPS_HOME_DIR/bin/$ARCH/posmrg.e"
if ( $VERBOSE_MODE || $test ) echo " running " `basename $cmd`
if ( $test ) set cmd = "time $cmd"

#Run POSMRG

$cmd

#=============
exit
if ( $test  ) then 
  echo " running  stamrg"
#time /goa/source/stafit/$ARCH/OFFICIAL/stafit.e 
  setenv OUTFILE "g$stacov"
  time /goa/source/stamrg/$ARCH/OFFICIAL/stamrg.e 
  station_diff g$stacov p$stacov > gp_diff
  echo "FINISHED" 
  more gp_diff
endif


#\rm input.files >& /dev/null
