#!/usr/bin/perl
#
# create a simple multi-column ASCII text file with
#   photometry of stars in instrumental mag units
#   (using data on V404 Cyg from jun23_2015)
#
#        my label           APASS9 id
#       ----------------------------------------
#            A               13926112
#            B               13926087
#            C                 V404 Cyg
#            D               13926130 
#
# MWR 2/17/2022

use POSIX;
$debug = 1;

$infile = "./intensity.dat";
$outfile = "./instr_mag.dat";


open(INFILE, "$infile") || die("can't open file $infile");
open(OUTFILE, ">$outfile") || die("can't create file $outfile");

printf OUTFILE " %12s  %6s %6s %6s %6s \n",
       "Julian_Day", "A", "B", "C", "D";

while (<INFILE>) {

  $line = $_;
  if ($line =~ /^#/) {
    next;
  }
  @words = split(/\s+/, " " . $line);
  $jd = $words[1];
  $inten_a = $words[2];
  $inten_b = $words[3];
  $inten_c = $words[4];
  $inten_d = $words[5];

  if ($debug > 0) {
    printf " %12.5f  %9.1f %9.1f %9.1f %9.1f \n",
        $jd, $inten_a, $inten_b, $inten_c, $inten_d;
  }

  # convert from intensity to differtial mag
  #  using star "A" as reference

  $mag_a = 0.0 - 2.5*log10($inten_a / $inten_a);
  $mag_b = 0.0 - 2.5*log10($inten_b / $inten_a);
  $mag_c = 0.0 - 2.5*log10($inten_c / $inten_a);
  $mag_d = 0.0 - 2.5*log10($inten_d / $inten_a);

    printf OUTFILE " %12.5f  %6.3f %6.3f %6.3f %6.3f \n",
        $jd, $mag_a, $mag_b, $mag_c, $mag_d;



}


close(OUTFILE);
close(INFILE);






exit 0;
