#! /usr/local/bin/perl

#use HEACORE::HEAINIT;
#exit headas_main(\&hxdmkgainhist_pin);

###############################################
#  dummy script for PIN Gain history
#         version 0.6, by Y.Terada, 2005-02-07
#         version 1.0, by Y.Terada, 2005-06-14, use HEACORE
#         version 1.1, by Y.Terada, 2005-06-17, 
###############################################

#sub input{
#  use HEACORE::HEAUTILS;
#  use HEACORE::PIL;
#  my $tname = "hxdmkgainhist_pin";
#  my $tvers = "1.0.1";
#  my $status = 0;
#
#  ($status = PILGetFname('input_name', $fffile)) == 0 
#      || die "error getting infile name";
#  ($status = PILGetFname('hk_name', $hkfile)) == 0 
#      || die "error getting hkfile name";
#  ($status = PILGetFname('pin_fitlog_name', $fitresults)) == 0 
#      || die "error getting fitresults name";
#  ($status = PILGetString('process_id', $fff)) == 0 
#      || die "error getting process id (aeYYYYMMDD_HHMM_HHMM)";
#
#  set_toolname($tname);
#  set_toolversion($tvers);
#
#  return $status;
#}
sub input {
    my $status = 0;

    $fffile     = `pquery2 hxdmkgainhist_pin input_name $invokestring`;
    chop($fffile);
    $hkfile     = `pquery2 hxdmkgainhist_pin hk_name $invokestring`;
    chop($hkfile);
    $fitresults = `pquery2 hxdmkgainhist_pin pin_fitlog_name $invokestring`;
    chop($fitresults);
    $fff        = `pquery2 hxdmkgainhist_pin process_id $invokestring`;
    chop($fff);

    return $status;
}

sub get_gti{
    $fdump_log="fdump_pin.log";

    $fdump_exe = "fdump infile=\"${fffile}+2\" outfile=\"${fdump_log}\" columns=- rows=- prhead=no pagewidth=100\n";
    system($fdump_exe);

    open(LOG, "$fdump_log") || die "Cannot open fdump.log (${log}): $!\n";
    while(<LOG>){
	if(/^\s+\d+/){
	    s/^\s+//g;
	    chomp;
	    ($row,$tstart,$tstop) = split(" +",$_);
	}
    }
    close LOG;

    unlink "$fdump_log" || die "Cannot unlink $fdump_log\n";
}

sub make_dummy_pinghf{
    open (PINGHF, "> ${fitresults}") || die "Cannot open ${fitresults}: $!\n";
    
    print PINGHF "$tstart $tstop 0 2.919260 0.00 -3.167270 0.00\n";
    print PINGHF "$tstart $tstop 1 2.952150 0.00 -2.155260 0.00\n";
    print PINGHF "$tstart $tstop 2 2.713080 0.00 4.915760 0.00\n";
    print PINGHF "$tstart $tstop 3 2.778740 0.00 -2.143690 0.00\n";
    print PINGHF "$tstart $tstop 4 2.904000 0.00 -2.700120 0.00\n";
    print PINGHF "$tstart $tstop 5 2.695650 0.00 -0.691585 0.00\n";
    print PINGHF "$tstart $tstop 6 2.698540 0.00 5.163530 0.00\n";
    print PINGHF "$tstart $tstop 7 2.500540 0.00 14.846800 0.00\n";
    print PINGHF "$tstart $tstop 8 2.858860 0.00 -2.898470 0.00\n";
    print PINGHF "$tstart $tstop 9 2.773050 0.00 -0.250535 0.00\n";
    print PINGHF "$tstart $tstop 10 2.932720 0.00 -6.706640 0.00\n";
    print PINGHF "$tstart $tstop 11 2.931160 0.00 -6.031760 0.00\n";
    print PINGHF "$tstart $tstop 12 2.666060 0.00 -9.349350 0.00\n";
    print PINGHF "$tstart $tstop 13 2.979600 0.00 -4.351440 0.00\n";
    print PINGHF "$tstart $tstop 14 2.950640 0.00 -3.162000 0.00\n";
    print PINGHF "$tstart $tstop 15 2.903500 0.00 -4.682400 0.00\n";
    print PINGHF "$tstart $tstop 16 2.819940 0.00 -0.175214 0.00\n";
    print PINGHF "$tstart $tstop 17 2.972840 0.00 -0.145552 0.00\n";
    print PINGHF "$tstart $tstop 18 2.945410 0.00 -0.457310 0.00\n";
    print PINGHF "$tstart $tstop 19 2.819330 0.00 -5.716340 0.00\n";
    print PINGHF "$tstart $tstop 20 2.941720 0.00 -3.090150 0.00\n";
    print PINGHF "$tstart $tstop 21 3.180420 0.00 -5.495460 0.00\n";
    print PINGHF "$tstart $tstop 22 2.985010 0.00 2.783490 0.00\n";
    print PINGHF "$tstart $tstop 23 3.162880 0.00 -4.710860 0.00\n";
    print PINGHF "$tstart $tstop 24 2.820830 0.00 0.711879 0.00\n";
    print PINGHF "$tstart $tstop 25 2.897150 0.00 1.257610 0.00\n";
    print PINGHF "$tstart $tstop 26 3.025780 0.00 5.512800 0.00\n";
    print PINGHF "$tstart $tstop 27 3.239760 0.00 -5.657870 0.00\n";
    print PINGHF "$tstart $tstop 28 2.868400 0.00 -5.807470 0.00\n";
    print PINGHF "$tstart $tstop 29 3.285500 0.00 -6.177850 0.00\n";
    print PINGHF "$tstart $tstop 30 3.090790 0.00 -4.080350 0.00\n";
    print PINGHF "$tstart $tstop 31 3.007240 0.00 -0.195744 0.00\n";
    print PINGHF "$tstart $tstop 32 2.698460 0.00 -2.884740 0.00\n";
    print PINGHF "$tstart $tstop 33 2.917900 0.00 -0.072596 0.00\n";
    print PINGHF "$tstart $tstop 34 2.788380 0.00 -4.816080 0.00\n";
    print PINGHF "$tstart $tstop 35 2.765630 0.00 -5.878670 0.00\n";
    print PINGHF "$tstart $tstop 36 2.741050 0.00 -1.670340 0.00\n";
    print PINGHF "$tstart $tstop 37 3.058510 0.00 2.417110 0.00\n";
    print PINGHF "$tstart $tstop 38 2.745570 0.00 -7.814840 0.00\n";
    print PINGHF "$tstart $tstop 39 2.740420 0.00 -1.049250 0.00\n";
    print PINGHF "$tstart $tstop 40 2.941710 0.00 2.214100 0.00\n";
    print PINGHF "$tstart $tstop 41 2.703580 0.00 -0.012281 0.00\n";
    print PINGHF "$tstart $tstop 42 2.633910 0.00 1.210940 0.00\n";
    print PINGHF "$tstart $tstop 43 2.635190 0.00 -4.311470 0.00\n";
    print PINGHF "$tstart $tstop 44 2.918090 0.00 -8.116280 0.00\n";
    print PINGHF "$tstart $tstop 45 2.772180 0.00 -7.908510 0.00\n";
    print PINGHF "$tstart $tstop 46 2.896130 0.00 -9.125910 0.00\n";
    print PINGHF "$tstart $tstop 47 2.847620 0.00 -2.740030 0.00\n";
    print PINGHF "$tstart $tstop 48 2.806410 0.00 0.268396 0.00\n";
    print PINGHF "$tstart $tstop 49 2.979980 0.00 -0.278472 0.00\n";
    print PINGHF "$tstart $tstop 50 2.642660 0.00 -3.909660 0.00\n";
    print PINGHF "$tstart $tstop 51 2.667530 0.00 -9.477960 0.00\n";
    print PINGHF "$tstart $tstop 52 2.809120 0.00 -4.100410 0.00\n";
    print PINGHF "$tstart $tstop 53 2.867070 0.00 -7.903470 0.00\n";
    print PINGHF "$tstart $tstop 54 3.193670 0.00 1.689610 0.00\n";
    print PINGHF "$tstart $tstop 55 3.311910 0.00 -7.542700 0.00\n";
    print PINGHF "$tstart $tstop 56 2.752360 0.00 3.454670 0.00\n";
    print PINGHF "$tstart $tstop 57 2.758250 0.00 1.213050 0.00\n";
    print PINGHF "$tstart $tstop 58 2.755790 0.00 -0.963522 0.00\n";
    print PINGHF "$tstart $tstop 59 2.844730 0.00 -3.525310 0.00\n";
    print PINGHF "$tstart $tstop 60 3.034070 0.00 -3.661290 0.00\n";
    print PINGHF "$tstart $tstop 61 2.831420 0.00 -2.955800 0.00\n";
    print PINGHF "$tstart $tstop 62 2.990050 0.00 -6.837610 0.00\n";
    print PINGHF "$tstart $tstop 63 2.953740 0.00 1.032300 0.00\n";

    close(PINGHF);
}

##########
## MAIN ##
##########
##sub hxdmkgainhist_pin{
##  my $status = 0;

if(defined(@ARGV)) {
    foreach $i (0 .. $#ARGV) {
	$ARGV[$i] = "\"".$ARGV[$i]."\"";
    }
    $invokestring = join(' ',@ARGV);
}

$status = &input($ARGV[0]);  print "FFF= ${fffile}, HK= ${hkfile}\n";
&get_gti;          print "TSTART ${tstart}, TSOP ${tstop}\n";
&make_dummy_pinghf;

##  return $status;
##}
