#!/usr/bin/perl
#
# combine information from the historical runs-per-game file,
#   and the year-by-year,team-by-team lineup files
#
# MWR 12/4/2006
#
# Modified to compute correlation between winning percentage of 
#     a team and several other statistics: runs scored, or 
#     strikeouts, or doubles, or whatever.
#
# Go to the line marked "STATCHOICE" to choose the statistic of interest
#
# MWR 3/22/2009


$debug = 0;

#$hist_file = "./all_teams.dat";
$hist_file = "./all_teams_bat.dat";
#$hist_file = "./foo.dat";
$team_glob = "./team_stats/*starters.dat";
#$team_glob = "./team_stats/CLE_1999_starters.dat";
$calc_out_file = "./calc_team_ops.out";


# read in all data from the summary starters' OPS file
$num_ops_lines = 0;
open(OPS_FILE, "$calc_out_file") || die("can't open file $calc_out_file");
while (<OPS_FILE>) {
  $line = $_;
  chomp($line);
  $ops_line_array[$num_ops_lines] = $line;
  $num_ops_lines++;
}
close(OPS_FILE);
if ($debug > 0) {
  printf "read %6d lines from $calc_out_file \n", $num_ops_lines;
}


# read in all data from the historical file
$num_hist_lines = 0;
open(HIST_FILE, "$hist_file") || die("can't open file $hist_file");
while (<HIST_FILE>) {
  $line = $_;
  chomp($line);
  $this_year = substr($line, 4, 4);
  if ($this_year < 1990) {
    next;
  }
  $hist_line_array[$num_hist_lines] = $line;
  $num_hist_lines++;
}
close(HIST_FILE);
if ($debug > 0) {
  printf "read %6d lines from $hist_file \n", $num_hist_lines;
}


@starter_files_list = glob("$team_glob");
foreach $starter_file (@starter_files_list) {

  if ($debug > 0) {
    printf " next file is $starter_file \n";
  }

  # get the year and team name from the file
  $name = substr($starter_file, 13, 3);
  $year = substr($starter_file, 17, 4);
  if ($debug > 0) {
    printf "  name ..$name..  year ..$year.. \n";
  }

  # find this team and year in the historical array
  $found = 0;
  for ($i = 0; $i < $num_hist_lines; $i++) {
    $this_year = substr($hist_line_array[$i], 4, 4);
    $this_team = substr($hist_line_array[$i], 0, 3);
    if ($debug > 0) {
      printf "  this_year ..$this_year..  this_team ..$this_team.. \n";
    }
    
    if (($this_year == $year) && ($this_team eq $name)) {
      $found = 1;
      last;
    }

  }
  if ($found == 0) {
    printf "error, can't find year ..$year.. and team ..$name.. \n";
    exit(0);
  }
  else {
    if ($debug > 0) {
      printf "  found team $name for year $year \n";
    }

    ($runs_scored_total, $runs_allowed_total, 
               $this_wins, $this_losses, $this_win_pct) = 
         get_score_information($year, $name);
    


    # now grab the relevant info from historical record for this team
    $this_games = substr($hist_line_array[$i], 41, 4);
    $this_runs = substr($hist_line_array[$i], 50, 4);
    $this_rpg = $this_runs / $this_games;
    $this_slg = substr($hist_line_array[$i], 98, 4);
    $this_obp = substr($hist_line_array[$i], 92, 4);
    $this_ops = $this_slg + $this_obp;

    $this_ab = substr($hist_line_array[$i], 45, 4);
    $this_hits = substr($hist_line_array[$i], 55, 4);
    $this_2b = substr($hist_line_array[$i], 60, 3);
    $this_3b = substr($hist_line_array[$i], 64, 3);
    $this_hr = substr($hist_line_array[$i], 67, 3);
    $this_rbi = substr($hist_line_array[$i], 71, 4);
    $this_bb = substr($hist_line_array[$i], 76, 3);
    $this_so = substr($hist_line_array[$i], 80, 4);
    $this_ba = substr($hist_line_array[$i], 86, 4);
    $this_sb = substr($hist_line_array[$i], 103, 3);
    $this_cs = substr($hist_line_array[$i], 107, 3);
    $this_gdp = substr($hist_line_array[$i], 111, 3);
    $this_hbp = substr($hist_line_array[$i], 115, 3);
    $this_sh = substr($hist_line_array[$i], 119, 3);
    $this_sf = substr($hist_line_array[$i], 123, 3);
    $this_ibb = substr($hist_line_array[$i], 127, 3);
    $this_opsplus = substr($hist_line_array[$i], 131, 4);

    if ($debug > 0) {
      printf "  this_rpg $this_rpg   this_runs $this_runs \n";
      printf "  this_slg $this_slg   this_obp  $this_obp \n";
      printf "  this_ops %7.4f    \n", $this_ops;
      printf "  this_hits $this_hits  this_2b $this_2b  this_3b $this_3b \n";
      printf "  this_hr   $this_hr  this_rbi $this_rbi  this_bb $this_bb \n";
      printf "  this_so   $this_so  this_ba  $this_ba  this_sb $this_sb \n";
      printf "  this_cs   $this_cs  this_gdp  $this_gdp  this_hbp $this_hbp \n";
      printf "  this_sh   $this_sh  this_sf  $this_sf  this_ibb $this_ibb \n";
      printf "  this_opsplus   $this_opsplus  \n";
    }

    # compute contextual runs for this team
    $this_lob = 0;

    $roe_oob = $this_runs + $this_lob - 
            ($this_hits + $this_bb + $this_hbp - $this_cs - $this_gdp);
    $outs = $this_ab - $this_hits + $this_sf + $this_sh + $this_cs 
                + $this_gdp - $roe_oob;
    $wrob = $this_hits - $this_hr + $this_bb + $this_hbp 
                - $this_cs - $this_gdb + $roe_oob;
    $cr = $this_hr + 
            $wrob*(0.038 + (0.514*$this_hits + 0.403*$this_2b +
                            1.413*$this_3b + 0.534*$this_hr +
                            0.627*$this_sb + 
                            0.093*($this_bb - $this_ibb + $this_hbp) + 
                            0.064*($this_ab - $this_hits - $this_so + 
                                       $this_sh + $this_sf) ) / $outs) ;
    $scaled_cr = 2.03*$cr;

    # and get the starters' info from the starters summary file
    $found_ops = 0;
    for ($j = 0; $j < $num_ops_lines; $j++) {
      $ops_year = substr($ops_line_array[$j], 17, 4);
      $ops_team = substr($ops_line_array[$j], 13, 3);
      if ($debug > 0) {
        printf "  ops_year ..$ops_year..  ops_team ..$ops_team.. \n";
      }
      
      if (($ops_year == $year) && ($ops_team eq $name)) {
        $found_ops = 1;
        last;
      }
  
    }
    if ($found_ops == 0) {
      printf "error, can't find ops record for year ..$year.. and team ..$team.. \n";
      exit(0);
    }
    else {
      if ($debug > 0) {
        printf "  found ops info for team $team for year $year \n";
      }

      # okay, get the mean and sigma for this team
      $this_mean_ops = substr($ops_line_array[$j], 29, 5);
      $this_sigma_ops = substr($ops_line_array[$j], 43, 5);
  
      if ($debug > 0) {
        printf "  this_mean_ops $this_mean_ops   this_sigma_ops $this_sigma_ops \n";
      }

      # finally, we print out one line per team with information on
      #     winning percentage and the stat of interest
      # STATCHOICE
      $stat_name = "bb";
      $stat_value = $this_bb;
      printf " %4d %4s  wins %3d loss %3d  winpct %6.3f   %s %d \n", 
         $year, $name, $this_wins, $this_losses, $this_win_pct,
         $stat_name, $stat_value;
  

    }


  }

#  exit(0);

  


}




exit 0;




######################################################################
# PROCEDURE: get_score_information
#
# DESCRIPTION: Given a year and a team name, we look up information
#              from some datafiles in a different directory.
#              Specifically, we want to find
#
#                    wins at end of season
#                    losses at end of season
#                    runs_allowed at end of season
#
#              So, we first have to devise the proper filename
#              and locate the file.  This is complicated because
#              some teams have names which change, and some have
#              abbreviations which differ from one data source
#              to the next.
#
#              We read through the file, one game per line, and
#              accumulate runs scored and runs allowed.  The final
#              line gives us the final team record.
#
#              Returns five values:
#
#                    runs_scored_total
#                    runs_allowed_total 
#                    this_wins 
#                    this_losses 
#                    this_win_pct
#
sub get_score_information {


    my($year);
    my($name, @score_names);
    my($found, $score_file);
    my($line, $substr, @words, $this_wins, $this_losses);
    my($this_win_pct);
    my($runs_scored_total, $runs_scored_game);
    my($runs_allowed_total, $runs_allowed_game);

    $year = $_[0];
    $name = $_[1];

    # the "score_file" below may use a different abbreviation for 
    #     some team names
    @score_names = ($name);
    if ($name eq "CHW") {
      @score_names = ($name, "CHA");
    }
    if ($name eq "TBD") {
      @score_names = ($name, "TBA");
    }
    if ($name eq "NYY") {
      @score_names = ($name, "NYA");
    }
    if ($name eq "KCR") {
      @score_names = ($name, "KCA");
    }
    if ($name eq "LAA") {
      @score_names = ($name, "CAL", "ANA");
    }
    # grab the final record for this team from the last line in a 
    #     datafile with game-by-game results of this team's season
    $found = 0;
    foreach $score_name (@score_names) {
      $score_file = sprintf "../record_corr/new_scores/data/AL_games_%04d/V%s0%04d_team.y.dat",
              $year, $score_name, $year;
      if (-e $score_file != 1) {
        if ($debug > 0) {
          printf " hmmm, can't find file ..%s.. \n", $score_file;
        }
      }
      else {
        $found = 1;
        last;
      }
    }
    if ($found == 0) {
      printf " error, can't find file for team %s \n", $name;
    }
    else {
      if ($debug > 0) {
        printf " about to look for record in ..%s.. \n", $score_file;
      }
    }
    # read to the end, accumulate runs scored and allowed each game,
    #     use the last line to find team's final record
    $runs_scored_total = 0;
    $runs_allowed_total = 0;
    open(SCORE_FILE, $score_file) || die("can't open file $score_file");
    while (<SCORE_FILE>) {
      $line = $_;

      # get the score of this game
      $this_substr = " " . substr($line, 118, 16);
      $this_substr =~ s/-/ /g;
      $this_substr =~ s/[A-Z]/ /g;
      @words = split(/\s+/, $this_substr);
      $runs_scored_game = $words[1];
      $runs_allowed_game = $words[2];
      
      $runs_scored_total += $runs_scored_game;
      $runs_allowed_total += $runs_allowed_game;
      if ($debug > 0) {
        printf " game had %3d %3d  so scored %4d allowed %4d \n",
              $runs_scored_game, $runs_allowed_game,
              $runs_scored_total, $runs_allowed_total;
      }
    }
    close(SCORE_FILE);
    $this_substr = " " . substr($line, 59, 10);
    @words = split(/\s+/, $this_substr);
    $this_wins = $words[1];
    $this_losses = $words[2];
    if ($debug > 0) {
      printf " final record was %3d wins and %3d losses \n", 
                 $this_wins, $this_losses;
    }
    $this_win_pct = ($this_wins) / ($this_wins + $this_losses);

    # now return the results
    return($runs_scored_total, $runs_allowed_total, 
               $this_wins, $this_losses, $this_win_pct);

}


if (0  == 0) {

    # the "score_file" below may use a different abbreviation for 
    #     some team names
    @score_names = ($name);
    if ($name eq "CHW") {
      @score_names = ($name, "CHA");
    }
    if ($name eq "TBD") {
      @score_names = ($name, "TBA");
    }
    if ($name eq "NYY") {
      @score_names = ($name, "NYA");
    }
    if ($name eq "KCR") {
      @score_names = ($name, "KCA");
    }
    if ($name eq "LAA") {
      @score_names = ($name, "CAL", "ANA");
    }
    # grab the final record for this team from the last line in a 
    #     datafile with game-by-game results of this team's season
    $found = 0;
    foreach $score_name (@score_names) {
      $score_file = sprintf "../record_corr/new_scores/data/AL_games_%04d/V%s0%04d_team.y.dat",
              $year, $score_name, $year;
      if (-e $score_file != 1) {
        if ($debug > 0) {
          printf " hmmm, can't find file ..%s.. \n", $score_file;
        }
      }
      else {
        $found = 1;
        last;
      }
    }
    if ($found == 0) {
      printf " error, can't find file for team %s \n", $name;
    }
    else {
      if ($debug > 0) {
        printf " about to look for record in ..%s.. \n", $score_file;
      }
    }
    # read to the end, use the last line to find team's final record
    open(SCORE_FILE, $score_file) || die("can't open file $score_file");
    while (<SCORE_FILE>) {
      $line = $_;
    }
    close(SCORE_FILE);
    $substr = " " . substr($line, 59, 10);
    @winloss_words = split(/\s+/, $substr);
    $this_wins = $winloss_words[1];
    $this_losses = $winloss_words[2];
    if ($debug > 0) {
      printf " final record was %3d wins and %3d losses \n", 
                 $this_wins, $this_losses;
    }
    $this_win_pct = ($this_wins) / ($this_wins + $this_losses);

}

