#!/usr/bin/perl
# =============================================================================
#
# Name:       STD.pl
#
# Purpose:    Plot orbit misclosures from STDELE program output.
#             (1D3D2.PCF)
#             Expected input file names : OUT/F1_yyssssS.OUT
#                                         OUT/F3_yyssssS.OUT
#             Output file names         : PLT/F1_GPS.PLT
#                                         PLT/F1_GLO.PLT
#                                         PLT/F3_GPS.PLT
#                                         PLT/F3_GLO.PLT
#
# Authors:    S. Lutz
# Created:    14-May-2013
#
# Changes:    __-___-____ __:
#
# Example:    cd $K/SL_1D3D;STD.pl;
#
# =============================================================================
use strict;
use warnings;

use File::Basename qw(basename);

use lib "$ENV{X}/EXE";
use Gps_Date qw(gps_date);

# Variables
# ---------
my $verb = 0;  # 0: non-verbose, 1: verbose

print localtime(time)." Starting script\n";

my($i,$j);
my $ext = "_0";  # _0: POLFIL no, _1: POLFIL yes
$ext = "_$ARGV[0]" if($ARGV[0] && ($ARGV[0] == "0" || $ARGV[0] == "1"));

print localtime(time)." Get STDELE output files\n";

mkdir("PLT") unless(-d "PLT");
foreach $i(
##  "F1_","NUL_",
##  "ELEa",##"ELEq","ELEe","ELEc","ELEg","ELEb","ELEf","ELEd","ELEh",
##  "ELEi","ELEm","ELEk","ELEo","ELEj","ELEn","ELEl",
##  "ELEp",##"ELEr",
##  "ELEs","ELEt",
##  "SF1","SF3","SFy",  # SL_R02
  "F1_","F3_","F3P1",
) {
  open(G,">PLT/${i}GPS.TMP");
  open(R,">PLT/${i}GLO.TMP");

  print "sort -n PLT/${i}GPS.TMP > PLT/${i}GPS.PLT\n";
  print "sort -n PLT/${i}GLO.TMP > PLT/${i}GLO.PLT\n";

  foreach my $f(glob("OUT/${i}?????S.OUT")) { # <<< STDELE program outputs
##  foreach my $f(glob("OUT/${i}9411?.OUT$ext")) {  # for tests only
    print "$f\n" if($verb);
    my($mjd) = &gps_date("-yd ".substr(&basename($f),-10,5)." -o %J"); # <<<
    open(I,"<$f");
    my($gps,$glo,$mea) = ("","","");

    while(<I>) {
      chomp;
      $gps .= " $_" if($_ =~ /^ g /);
      $glo .= " $_" if($_ =~ /^ r /);
      $mea .= " $_" if($_ =~ /^ M /);
    }

    close(I);
    my(@gps,@glo,@meg,@mer) = ();
    my $format = "A3A4A5A7A7A7A7A8A7A7A7A7";
    $gps[$_] = "nan" foreach(0..27);
    $glo[$_] = "nan" foreach(0..27);
    $meg[$_] = "nan" foreach(0..23);
    $mer[$_] = "nan" foreach(0..47);
##    @gps = split(/\s+/,$gps) if($gps);
    @gps = unpack("${format}A7A7${format}A7A7",$gps) if($gps);
    @glo = unpack("${format}A7A7${format}A7A7",$glo) if($glo);
    @meg = unpack("${format}${format}",$mea);
    @mer = unpack("${format}${format}${format}${format}",$mea) if($glo);
# Columns:
# 01-04 Date:                  UNIX YYYYMMDD yyddd MJD
# 05    non-eclipsing:         #
# 06-10 non-eclipsing (SIGMA): sRAD, sALO, sOUT, sPOS, sVEL
# 11-13 non-eclipsing (MEAN):  RAD, ALO, OUT
# 14    eclipsing:             #
# 15-19 eclipsing (SIGMA):     sRAD, sALO, sOUT, sPOS, sVEL
# 20-22 eclipsing (MEAN):      RAD, ALO, OUT

    if($gps) {
      print G sprintf("%010d %8d %05d %f ".
                      "%4d %6.0f %6.0f %6.0f %6.0f %6.0f %6.0f %6.0f %6.0f ".
                      "%4d %6.0f %6.0f %6.0f %6.0f %6.0f %6.0f %6.0f %6.0f\n",
                &gps_date("-mjd $mjd -o %U %Y%m%d %y%j %J"),
                @gps[1,9..13],@meg[9..11],@gps[15,23..27],@meg[21..23]);
    }

    if($glo) {
      print R sprintf("%010d %8d %05d %f ".
                      "%4d %6.0f %6.0f %6.0f %6.0f %6.0f %6.0f %6.0f %6.0f ".
                      "%4d %6.0f %6.0f %6.0f %6.0f %6.0f %6.0f %6.0f %6.0f\n",
                &gps_date("-mjd $mjd -o %U %Y%m%d %y%j %J"),
                @glo[1,9..13],@mer[33..35],@glo[15,23..27],@mer[45..47]);
    }
  }

  close(G);
  close(R);
}

print localtime(time)." Script finished\n";

__END__
