#! /usr/bin/perl -w
use strict;
use Getopt::Long;           # options
use File::Basename;
use POSIX;
use File::Copy;
$Getopt::Long::ignorecase=0;
#---/----1----/----2----/----3----/----4----/----5----/----6----/----7----/----8----/----9----/----0
# ... notes
#---/----1----/----2----/----3----/----4----/----5----/----6----/----7----/----8----/----9----/----0
#:1001.25,  JG, begin development
#---/----1----/----2----/----3----/----4----/----5----/----6----/----7----/----8----/----9----/----0

if ( @ARGV != 2 ) {
  print "\n";
  print "Usage: perl comp_corr.pl <ant.info> <simul.inp>\n";
  print "\n";
  print "This script computes ENU position corrections (in mm) for each record in ant.info\n";
  print "The corrections are based on the coefficents of the latitude-dependant models in\n";
  print "simul.inp. Each record in ant.info is appened with the ENU position correction and\n";
  print "printed to standard output.\n";
  print "\n";
  print "The ant.info file contains seven fields, e.g.:\n";
  print "\n";
  print " ALGO  A   281.929   45.956  AOAD/M_B        NONE  1991:019:00000  1994:047:69180\n";
  print " ALGO  A   281.929   45.956  AOAD/M_T        NONE  1994:047:69180  0000:000:00000\n";
  print "\n";
  print "where,\n";
  print "\n";
  print "field 1: 4-char id\n";
  print "field 2: pt id\n";
  print "field 3: longitude (decimal degrees)\n";
  print "field 4: latitude (decimal degrees)\n";
  print "field 5: antenna name (IGS adopted)\n";
  print "field 6: radome name (IGS adopted)\n";
  print "field 7: installation date\n";
  print "field 8: removal date\n";
  print "\n";
  print "The simul.inp file contains a space-delimited set of coefficients for the latitude-\n";
  print "dependent simulation model. The file provided by the IGS is available at:\n";
  print "\n";
  print "ftp://igs-rf.ign.fr/pub/IGS08/new_calib/lat_models.txt\n";
  print "ftp://igs-rf.ensg.eu/pub/IGS08/new_calib/lat_models.txt\n";
  print "\n";
  print "If there is no correction model available in the lat_models.txt file, then dashes\n";
  print "are printed in place of correction values.\n";
  print "\n";
  print "\n";
  print "Example:\n";
  print "\n";
  print "perl comp_corr.pl ant.info lat_models.txt > ant.info_corr 2> log\n";
  print "\n";
  print "In the Korn shell environment, the above execution will write the standard output\n";
  print "to ant.info_corr, which contains the ENU position corrections. The log file\n";
  print "captures STDERR, which contains errors and other diagnostic information experienced\n";
  print "in reading the input files and computing the position corrections.\n";
  print "\n";
  print "\n";
  exit;
}
  
my $ants = $ARGV[0];
my $mods = $ARGV[1];
my $i=();
my $lon=();
my $lat=();
my $lrad=();
my $code=();
my $pt=();
my $ant=();
my $dome=();
my $ant_dome=();
my $epoch_f=();
my $epoch_t=();
my $e_corr=();
my $n_corr=();
my $u_corr=();
my $e0=();
my $e1=();
my $e2=();
my $e3=();
my $e4=();
my $e5=();
my $eRMS=();
my $n0=();
my $n1=();
my $n2=();
my $n3=();
my $n4=();
my $n5=();
my $nRMS=();
my $u0=();
my $u1=();
my $u2=();
my $u3=();
my $u4=();
my $u5=();
my $uRMS=();
my $pi=3.141592653589793;
my %H_mods = ();

# ... store models
#     ------------
open (MODS, "<$mods") ||
      die "\n***** ERROR: via open (r) of file $mods !\n             $!\n\n";
$i=0;
while (<MODS>) {
  next if ( m/^#/ );
  $_ =~ s/\|/ /g;  # remove any | 
  ($ant,$dome,$e0,$e1,$e2,$e3,$e4,$e5,$eRMS,$n0,$n1,$n2,$n3,$n4,$n5,$nRMS,$u0,$u1,$u2,$u3,$u4,$u5,$uRMS)=split ' ', $_;
  $ant_dome=sprintf("%-15s %4s",$ant,$dome);

  print STDERR "$_";

  # ... east model
  $H_mods{$ant_dome}[0] = $e0; print STDERR "$H_mods{$ant_dome}[0]\n";
  $H_mods{$ant_dome}[1] = $e1; print STDERR "$H_mods{$ant_dome}[1]\n";
  $H_mods{$ant_dome}[2] = $e2; print STDERR "$H_mods{$ant_dome}[2]\n";
  $H_mods{$ant_dome}[3] = $e3; print STDERR "$H_mods{$ant_dome}[3]\n";
  $H_mods{$ant_dome}[4] = $e4; print STDERR "$H_mods{$ant_dome}[4]\n";
  $H_mods{$ant_dome}[5] = $e5; print STDERR "$H_mods{$ant_dome}[5]\n";

  # ... north model
  $H_mods{$ant_dome}[6] = $n0; print STDERR "$H_mods{$ant_dome}[6]\n";
  $H_mods{$ant_dome}[7] = $n1; print STDERR "$H_mods{$ant_dome}[7]\n";
  $H_mods{$ant_dome}[8] = $n2; print STDERR "$H_mods{$ant_dome}[8]\n";
  $H_mods{$ant_dome}[9] = $n3; print STDERR "$H_mods{$ant_dome}[9]\n";
  $H_mods{$ant_dome}[10] = $n4; print STDERR "$H_mods{$ant_dome}[10]\n";
  $H_mods{$ant_dome}[11] = $n5; print STDERR "$H_mods{$ant_dome}[11]\n";

  # ... up model
  $H_mods{$ant_dome}[12] = $u0; print STDERR "$H_mods{$ant_dome}[12]\n";
  $H_mods{$ant_dome}[13] = $u1; print STDERR "$H_mods{$ant_dome}[13]\n";
  $H_mods{$ant_dome}[14] = $u2; print STDERR "$H_mods{$ant_dome}[14]\n";
  $H_mods{$ant_dome}[15] = $u3; print STDERR "$H_mods{$ant_dome}[15]\n";
  $H_mods{$ant_dome}[16] = $u4; print STDERR "$H_mods{$ant_dome}[16]\n";
  $H_mods{$ant_dome}[17] = $u5; print STDERR "$H_mods{$ant_dome}[17]\n";
}
close MODS;

# ... read ant info, compute correction and print all fields
#     ------------------------------------------------------
print "#CODE PT  LON_____ LAT_____  ANTENNA________ DOME  FROM__________ TO_____________  E_CORR N_CORR U_CORR\n";
open (ANTS, "<$ants") ||
      die "\n***** ERROR: via open (r) of file $ants !\n             $!\n\n";
while (<ANTS>) {
  next if ( m/^#/ );
  print STDERR "$_";

  ($code,$pt,$lon,$lat,$ant,$dome,$epoch_f,$epoch_t)=split ' ', $_;
  $lrad = $lat*($pi/180);
  $ant_dome=sprintf("%-15s %4s",$ant,$dome);
  if ( defined $H_mods{$ant_dome} ) {

    # ... east correction
    $e0 = $H_mods{$ant_dome}[0];
    $e1 = $H_mods{$ant_dome}[1];
    $e2 = $H_mods{$ant_dome}[2];
    $e3 = $H_mods{$ant_dome}[3];
    $e4 = $H_mods{$ant_dome}[4];
    $e5 = $H_mods{$ant_dome}[5];
    $e_corr = $e0 + $e1*$lrad + $e2*cos(2*$lrad) + $e3*sin(2*$lrad) + $e4*cos(4*$lrad) + $e5*sin(4*$lrad);

    # ... north correction
    $n0 = $H_mods{$ant_dome}[6];
    $n1 = $H_mods{$ant_dome}[7];
    $n2 = $H_mods{$ant_dome}[8];
    $n3 = $H_mods{$ant_dome}[9];
    $n4 = $H_mods{$ant_dome}[10];
    $n5 = $H_mods{$ant_dome}[11];
    $n_corr = $n0 + $n1*$lrad + $n2*cos(2*$lrad) + $n3*sin(2*$lrad) + $n4*cos(4*$lrad) + $n5*sin(4*$lrad);

    # ... up correction
    $u0 = $H_mods{$ant_dome}[12];
    $u1 = $H_mods{$ant_dome}[13];
    $u2 = $H_mods{$ant_dome}[14];
    $u3 = $H_mods{$ant_dome}[15];
    $u4 = $H_mods{$ant_dome}[16];
    $u5 = $H_mods{$ant_dome}[17];
    $u_corr = $u0 + $u1*$lrad + $u2*cos(2*$lrad) + $u3*sin(2*$lrad) + $u4*cos(4*$lrad) + $u5*sin(4*$lrad);

    # ... print
    printf(" %4s %2s  %8.3f %8.3f  %-15s %-4s  %s  %s  %6.2f %6.2f %6.2f\n",$code,$pt,$lon,$lat,$ant,$dome,$epoch_f,$epoch_t,$e_corr,$n_corr,$u_corr);
  } else {

    # ... print
    printf(" %4s %2s  %8.3f %8.3f  %-15s %-4s  %s  %s  %6s %6s %6s\n",$code,$pt,$lon,$lat,$ant,$dome,$epoch_f,$epoch_t,'------','------','------');
  }
}
close ANTS;
