#!/usr/bin/perl -w
##
## tsc-tables.pl -- get adev and spectrum tables from TSC-5120A
## version 0.8 -- 10 March 2007
##
## Copyright 2007 by John R. Ackermann  N8UR (jra@febo.com)
##
## This program may be copied, modified, distributed and used for
## any legal purpose provided that (a) the copyright notice above as well
## as these terms are retained on all copies; (b) any modifications that
## correct bugs or errors, or increase the program's functionality, are
## sent via email to the author at the address above; and (c) such
## modifications are made subject to these license terms.

use strict;
use POSIX qw(setsid);
use Getopt::Std;
#use DateTime;
use Net::Telnet;
use n8ur qw(trim);

my $machine = '192.168.1.248';
my $port = '1299';
my $opt_string = 'hf:';

my $file;
my $t;
my @lines;
my $j;
my $k;
my @tmp;
my $current_tau;
my $tau_1ms_start;
my $tau_10ms_start;
my $tau_100ms_start;
my $tau_1000ms_start;
my @tau_range;
my @tau_1ms;
my @tau_10ms;
my @tau_100ms;
my @tau_1000ms;
my $num_tau;



sub usage() {
print STDERR << "EOF";

usage: $0 [-h ] -f filename

-h	: this help
-f	: output file base name using full pathname;
	  for output to console, use "-f -"; output files will have 
	  -adev.dat, etc. appended	

EOF
}

getopts( "$opt_string", \my %opt ) or usage() and exit;
usage() and exit if $opt{h};
usage() and exit if !$opt{f};
$file = $opt{f};

$t = new Net::Telnet (Timeout => 10,Port=>$port);
$t->open('femto.febo.com');
$t->waitfor("/=192.168.1.248 >/");

# Get phase noise data
open (LOG, ">$file-spectrum.dat") || die "Can't open $file-spectrum.dat!\n";
@lines = $t->cmd("show spectrum");
# swallow prompt at end
for ($j = 2; $j < @lines-1; $j++) {
	print LOG $lines[$j];
	}
close LOG;

# Get spur data
open (LOG, ">$file-spurs.dat") || die "Can't open $file-spurs.dat!\n";
@lines = $t->cmd("show spurs");
# swallow prompt at end
for ($j = 2; $j < @lines-1; $j++) {
	print LOG $lines[$j];
	}
close LOG;

# Get frequency difference
open (LOG, ">$file-freqdiff.dat") || die "Can't open $file-freqdiff.dat!\n";
@lines = $t->cmd("show freqdiff");
# swallow prompt at end
for ($j = 1; $j < @lines-1; $j++) {
	print LOG $lines[$j];
	}
close LOG;

# Get phase difference
open (LOG, ">$file-phasediff.dat") || die "Can't open $file-phasediff.dat!\n";
@lines = $t->cmd("show phasediff");
# swallow prompt at end
for ($j = 1; $j < @lines-1; $j++) {
	print LOG $lines[$j];
	}
close LOG;

# Get adev data
@lines = $t->cmd("show adev");
# scan to fine where each tau0 begins
for ($j = 0; $j < @lines;$j++) {
	if (trim($lines[$j]) =~ "^TAU0: 1E-3") { $tau_1ms_start = $j; }
	if (trim($lines[$j]) =~ "^TAU0: 1E-2") { $tau_10ms_start = $j; }
	if (trim($lines[$j]) =~ "^TAU0: 1E-1") { $tau_100ms_start = $j; }
	if (trim($lines[$j]) =~ "^TAU0: 1E0") { $tau_1000ms_start = $j; }
	# initialize arrays
	$tau_range[$j] = "";
	$tau_1ms[$j]  = 0;
	$tau_10ms[$j]  = 0;
	$tau_100ms[$j]  = 0;
	$tau_1000ms[$j]  = 0;
	}

# get tau range and 1ms values
$k = 0;
$num_tau = 0;
for ($j = $tau_1ms_start+1; $j < $tau_10ms_start-1;$j++) {
	@tmp =  split(" ",$lines[$j]);
	$tau_range[$k] = $tmp[1];
	$tau_1ms[$k] = $tmp[3];
	$num_tau++;
	$k++;
	}

# now get values for other ranges
$k = 3;
for ($j = $tau_10ms_start+1; $j < $tau_100ms_start; $j++) {
	@tmp =  split(" ",$lines[$j]);
	$tau_10ms[$k] = $tmp[3];
	$k++;	
	}

$k = 6;
for ($j = $tau_100ms_start+1; $j < $tau_1000ms_start; $j++) {
	@tmp =  split(" ",$lines[$j]);
	$tau_100ms[$k] = $tmp[3];
	$k++;	
	}

$k = 9;
for ($j = $tau_1000ms_start+1; $j < @lines-1; $j++) {
	@tmp =  split(" ",$lines[$j]);
	$tau_1000ms[$k] = $tmp[3];
	$k++;	
	}

open (LOG, ">$file-adev.dat") || die "Can't open $file-adev.dat!\n";
for ($j = 0; $j < $num_tau; $j++) {
	print LOG $tau_range[$j]," ",$tau_1ms[$j]," ",$tau_10ms[$j]," ",
		$tau_100ms[$j]," ",$tau_1000ms[$j],"\n";
	}
close LOG;

$t->print("exit");
$t->close;
