#!/usr/local/bin/perl

#--------------------------------------------------------------------------
# Program:
#   tec_dump3
# 
# Purpose:
#   This routine dumps TEC files to text with biases removed, 
#   and computes additional information, such as iono pierce points, 
#   GIM value at IPP.
#
# Call:
#   tec_dump3 tecfile biases nmlfile \
#   [-l stlocs] [-s stn]             \
#   [-f coefs] [-rt tecrate] [-do dice_output_file]
#
# See help-text below for information on inputs.
#
# Side Effects/Restrictions:
#   Writes to standard out.
# 
# History:
#   Written  - 5/25/99, BDW/AJM
#   Added dice_output_file - 00/03/21, BDW
#--------------------------------------------------------------------------
if (($ARGV[0] eq "-H") || ($ARGV[0] eq "-Help") || 
    ($ARGV[0] eq "-h") || ($#ARGV < 2)) {

print<<"EOF";

Purpose: Dump a TEC file to text, biases removed.  Additional information
	provided. See file format description below.

tecdump3 tecfile biases nmlfile \
         [-i] [-l stlocs] [-s stn] \
         [-f coefs] [-rt tecrate] [-do dice_output_file]
         [-rejected/used/both] [+iri]

Required Inputs:
----------------
  tecfile - TEC file (qm file format)
  biases - GIM style bias file, containing GPS satellite and receiver biases
  nmlfile - File containing ionosphere_nml namelist inputs, that define
	the ionospheric geometry.

Optional Inputs:
---------------
  i       - cancels intermap time interpolation
  stlocs  - file containing geographic coordinates for stations to be output.
            Default: Environment variable STLOCS.
  stn     - A station for which output is received. If specified, then only
	    data from this station is output. Use 4-character ID.
	    Default: all stations output.
  coefs   - A GIM coefficients file (e.g. gim*.dat). If specified, then a GIM vertical
	    TEC value is computed at each IPP from stn.
	    Default; none.
  tecrate - Decimation rate for TEC file, in seconds.
	    Default: no decimation.
  +iri    - calculate IRI values (default no to save time)

  The following switches rely on the tecfile having already been processed by
  QMFLAG, setting the error columns to negative to demark unused data.
  Otherwise, these switches have no function an always return the BOTH setting.

  rejected - Only output those points which were rejected by GIM
  used     - Only output those points which were used by GIM
  both     - Output both points which were rejected or used by GIM (default)

Output:
------
  A text file to stdout. Format as follows (columns):
	time (seconds past J2000, GPS time)
	receiver name
	transmitter name
	azimuth (degrees)
	elevation (degrees)
	IPP latitude, in Earth-fixed degrees
	IPP longitude, Earth-fixed
	IPP longitude, Sun-fixed
        (placeholder)
	Local Time
        IRI VTEC (or 0.0)
	Hour in the day (UTC time)
	Human-readable date-time (GPS time)
	Slant tec, unbiased, TECU
	Vertical TEC, unbiased, TECU, using mapping function in namelist file
	GIM delay (TECU, if applicable)
	Difference between GIM and measured vertical TEC
	Data noise
	Elevation scaling ratio for this measurement
	Satellite bias
	Receiver bias
        IPP latitude, Solar-magnetic coordinates
        IPP longitude, Solar-magnetic coordinates

EOF
  exit(1);
} 

# Set defaults for optional inputs.
my $iri=0;
$iarg = "";
$stlocs = $ENV{'STLOCS'};
$stn = "''";
$coefs = "''";
$tecrate = "0.";
#$dice_output_file = "''";
$rejected_points=1; #code for include everything

# Get the required arguments

$tecfile = $ARGV[0];
shift @ARGV;
$biases = $ARGV[0];
shift @ARGV;
$nmlfile = $ARGV[0];
shift @ARGV;

# Now process optional arguments.

foreach $i (0 .. $#ARGV) {
  $arg = $ARGV[$i];
  if ($arg eq "-i") {$iarg = $arg;}
  if ($arg eq "-l") { 
    $i++;
    $stlocs = $ARGV[$i];
  }
  if ($arg eq "-s") { 
    $i++;
    $stn = $ARGV[$i];
  }
  if ($arg eq "-f") { 
    $i++;
    $coefs = $ARGV[$i];
  }
  if ($arg eq "-rt") { 
    $i++;
    $tecrate = $ARGV[$i];
  }
#  if ($arg eq "-do") { 
#    $i++;
#    $dice_output_file = $ARGV[$i];
#  }
  if ($arg eq "-rejected") {
    $i++;
    $rejected_points=2;
  }
  if ($arg eq "-used"    ) {
      $i++;
      $rejected_points=0;
  }
  if ($arg eq "-both"    ) {
      $i++;
      $rejected_points=1;
  }
  if ($arg eq "+iri"    ) {
      $iri=1;
  }
}

if ($stlocs eq '') {
    warn "*** tec_dump3: no -l switch and \$STLOCS is not defined\n";
    exit(1);
} elsif (! -e $stlocs) {
    warn "*** tec_dump3: stlocs = $stlocs does not exist\n";
    exit(1);
} else {
    @stat = stat $stlocs;
    if (! $stat[7]) {
       warn "*** tec_dump3: stlocs = $stlocs is empty\n";
       exit(1);
    }
}
# Now call the program

print STDERR "$ENV{'SOURCE'}/tec_dump3/$ENV{'ARCH'}/$ENV{'MODE'}/tec_dump3.e $tecfile $biases $nmlfile $iarg $stlocs $stn $tecrate $coefs $rejected_points $iri\n";

open (WORKFILE,">/tmp/tec_dump_temp.$$");
print WORKFILE `$ENV{'SOURCE'}/tec_dump3/$ENV{'ARCH'}/$ENV{'MODE'}/tec_dump3.e $tecfile $biases $nmlfile $iarg $stlocs $stn $tecrate $coefs $rejected_points $iri`;
close (WORKFILE);
print `grep NUSE /tmp/tec_dump_temp.$$`;
#`grep -v NUSE /tmp/tec_dump_temp.$$ > tec_dump.$$.messages`;
`rm /tmp/tec_dump_temp.$$`;
if (-e "fort.102") { `mv fort.102 tec_dump3_errors.$$`; }
