#!/usr/bin/perl
#
# Compute the phase for a binary star, given a light-curve datafile
#  of the form
#
#          date   JD  HJD  mag
#
# We use the HJD and mag as the inputs for the phased light curve.
#
# Usage:   calc_phase.pl   lightcurve.dat
#
# MWR 5/3/2016
use POSIX;

$debug = 1;

# for ASAS 002511
$epoch = 3267.65;
#   the following is good for combination of Sep19 and Sep20 UT
#$period = 0.05398;
#   the following is good for Sep19 alone
$period = 0.05340;

# for sparse example
$epoch = 0;
$period = 6.280;


# for RR_Lyr
$epoch = 1685.05;
$period = 0.5668677600;

if ($#ARGV != 0) {
  printf STDERR "usage: calc_phase.pl  datafile  \n";
  exit(1);
}

$infile = $ARGV[0];
open(INFILE, "$infile") || die("can't open file $infile");
while (<INFILE>) {
  $line = $_;
  if ($line =~ /^#/) {
    next;
  }
  chomp($line);
  @words = split(/\s+/, " " . $line);

# for ASAS 
  $hjd = $words[3];
  $mag = $words[4];
# for sparse
  $hjd = $words[1];
  $mag = $words[3];
# for RR Lyr
  $hjd = $words[1];
  $mag = 25.0 - 2.5*log10($words[2]);

  if ($debug > 0) {
    printf " %12.5f  %6.3f \n", $hjd, $mag;
  }

  $x = ($hjd - $epoch) / $period;
  $phase = $x - floor($x);
  printf " %s %20.12f %20.12f %6.3f \n", $line, $phase, 1.0 + $phase, $mag;

}



exit 0;




