#!/usr/bin/perl
#
# Usage:   string.pl   datafile   min_period  max_period  num_period
#
# Calculate the string-length statistic for a variable star light curve.
# Expect data in the form
#
#              JD      mag
#              JD      mag
#              JD      mag
#
# Iterate between the min and max supplied periods, taking num_period
# steps.  For each candidate period, calculate the phases for each
# datum, then sort by phase and calculate the string length in
# phase-mag space.
#
#
# MWR 10/23/2003

use POSIX;

$debug = 1;

# we hold original data in these arrays
$ndata = 0;
$jd[0] = 0.0;
$mag[0] = 0.0;

# and we use these arrays for trial calculations
$trial_jd[0] = 0.0;
$trial_mag[0] = 0.0;
$trial_phase{0} = 0.0;


if ($#ARGV != 3) {
  printf stderr "usage: string.pl datafile min max num\n";
  exit(1);
}

$datafile = $ARGV[0];
$min_period = $ARGV[1];
$max_period = $ARGV[2];
$num_period = $ARGV[3];

open(DATAFILE, "$datafile") || die("can't open datafile $datafile");
while (<DATAFILE>) {
  $line = $_;
  if (substr($line, 0, 1) eq "#") {
    next;
  }
  @words = split(/\s+/, " " . $line);
  $jd[$ndata] = $words[1];
  $mag[$ndata] = $words[2];
  if ($debug > 0) {
    printf " jd %12.5f  mag %6.3f \n", $jd, $mag;
  }
  $ndata++;

}
close(DATAFILE);
  

# now, we pick a period

  $period = $min_period;

  # figure out the phases
  for ($i = 0; $i < $ndata; $i++) {
    $this_phase = ($jd[$i]/$period) - floor($jd[$i]/$period);
    if ($debug > 0) {
      printf "   jd %12.5f -> phase %5.3f   mag %6.3f \n", 
                       $jd[$i], $this_phase, $mag[$i];
    }

    $phase[$i] = ($this_phase, $mag[$i]);
  }





exit 0;


