#!/opt/perl/bin/perl
use strict;
use warnings;
use GPS::DATES;
use GPS::Defaults;
use File::Copy;
use File::Basename;
use Cwd;
use Getopt::Long;
my $progname = basename $0;
my $usage = qq(
$progname is a wrapper for gd2p.pl
Usage:
  $progname -site site -date date -orb [rapid|final]
);
my $site = '';
my $date = '';
my $ac = 'igs';
my $orb_type = 'final';
my $help = '';
my $here = cwd();
my $args = @ARGV;
my $proj = '';
my $debug = '';
my $abs_pc = 1;
my $frame='itrf05';
GetOptions( 'proj:s' => \$proj,
            'site:s' => \$site,
            'date:s' => \$date,
            'orb:s'  => \$orb_type,
            'debug'  => \$debug,
            'ac:s'   => \$ac,
            'abs_pc' => \$abs_pc,
            'help'   => \$help,
)or die "Error in options\n";

if ( ! $args || $help ) {
  die $usage;
}
if ( ! $date  ) {
  die "No date! \n $usage\n";
}
if ( $orb_type !~ /(rapid|final)/   ) {
  die "Wrong orbit type $orb_type! \n $usage\n";
}
my %par = get_defaults( env => \%ENV , abs_pc => $abs_pc );
my ( $yyyy,$doy ) = split ' ', get_date( $date,'doy');
my $cal = get_date("$yyyy,$doy",'cal');
my $yy = unpack('x2a2',$yyyy);
my $wd = $debug ? $here  : "/dev/shm/gipsynet/pp_work/";
my $work_dir = "$wd/$proj/$cal.$site";
if ( -d $work_dir ) {
     system("\\rm -rf $work_dir");
}
system("mkdir -p $work_dir");
 

chdir $work_dir or die "Couldn't change to $work_dir $!\n";

$site = lc $site;

my $rnx = "$site${doy}0.${yy}o"; 
my $ok_rnx =  `get_rnx -site $site -date $yyyy,$doy` if ! -e $rnx;
#my $ok_rnx =  `get_pbo_rnx $yyyy,$doy $site` if ! -e $rnx;
chomp $ok_rnx;
my $ok_orb =  `get_orbits.pl -date $cal -ac $ac -type $orb_type `;
#`gps_sat_offsets -ag igs -date $cal -tdp TDPfile -abs`;
chomp $ok_orb;
symlink 'pos','eci';
symlink 'TDPfile', 'tdp_clk_yaw';
symlink 'Shadow', 'shadow';
symlink 'tpeo.nml', 'tpnml';

#`igsclk2tdp -tdp TDPfile -date $cal`;

my $cmd = "$ENV{GOA}/bin/gd2p.pl ";

$cmd .= "-i $rnx -n $site ";
$cmd .= "-r 300 -type s ";  #data rate 300; type s: static k: kinematic
$cmd .= "-d $cal ";
$cmd .= '-add_ocnld " -c /share/apps/goa-var/sta_info/ocnld_coeffs " ';
#$cmd .= '-tides WahrK1 PolTid FreqDepLove OctTid  OcnldCpn ';
$cmd .= '-tides WahrK1 PolTid ';
$cmd .= '-trop_z_rw  5E-8 -wetzgrad 5E-9 -w_elmin 15 ';
$cmd .= '-post_wind 5.0E-2 5.0E-4 ';
$cmd .= '-edtpnt_max 10 ';
$cmd .= '-stacov ';
$cmd .= "-sta_info $par{STA_INFO_DIR} ";
$cmd .= "-goa_prod_here ";
#$cmd .= "-amb_res 3";
print $cmd,"\n";
system("$cmd");
exit;
system( "
    $cmd  -stop_before qregres
    qr_nml.gn -ac igs -pcm_grn -pcm_sat -igs_abs -date $cal
    $cmd -start_at qregres
    rm smcov.nio 2> /dev/null
    smap4fix accume.nio smooth.nio \\
             SMCOV.nio > smapper.log 2> smapper.error
    stacov -i SMCOV.nio -o station.stacov \\
           -tp tpeo.nml -q qregres.nml > stacov.log 2> stacov.error
" );
   
   rename 'SMCOV.nio', "$cal.$site.smcov.nio";
   copy 'point.txt', "$cal.$site.point.log";
   copy 'point.txt', 'point.log';
   my %ot = ( rapid => 'itrf05.r.stacov',
              final => 'itrf05.stacov',
              final_fid => 'itrf05.stacov',
              
   );
   rename 'stacov_final', "$cal.$site.$ot{$orb_type}";
   if ( $orb_type eq 'final' ) {
     symlink "$cal.$site.$ot{$orb_type}","$cal.$site.raw.stacov";
   }
   my $pb = `ls postbreak_postEdit.*|tail -1`; chomp $pb;
     #compress -c postfit.log  > $cal.$site.postfit.log.Z
   my $out_dir = "$par{PPP_DIR}/$ac/$yyyy/$cal/$site";
   
   system ("
     ln -sf logs/editor.log qfront.log
     mv $pb postbreak.log
     compress -c smapper.log > $cal.$site.smapper.log.Z
     compress -c qregres.nml  > $cal.$site.qregres.nml.Z
     compress -c wash.nml     > $cal.$site.wash.nml.Z
     compress -c qmfile       > $cal.$site.qm.Z
     make_process_log $cal $site
     if [ ! -e $out_dir ]; then
        mkdir -p $out_dir
     fi
     \\cp  $cal.$site.* $out_dir
     cd /tmp
   ");
   
     
     system("\\rm -rf $work_dir") if ! $debug;
