#!/usr/bin/perl
#
# Given a "good" data file, with photometric data like so:
#
#       55     272.24 336.11    8 12115.57264   0.000 -0.129 0.121
#
# where 
#          col 5             is JD - 2,440,000
#          col 7             is differential mag
#
# create a file which has three different times and one differential
#   magnitude, like so:
#
#     col 1   fractional UT date                      Jul_25.07264
#
#     col 2   Julian Date - 2,450,000                 2115.57264
#
#     col 3   Heliocentric Julian Date - 2,450,000    2115.57234
#
#     col 4   differential mag                          -0.129

#     col 5   uncertainty in diff mag                    0.012
#
# -----
# Subtract the magnitude of a comparison star from the magnitude in the
#   "good" data file before printing the result.  This allows us to
#   set the zeropoint as we wish.
# MWR 7/4/2003
#
# Modified to allow me to set the zeropoint by hand at a value
#    estimated from nearby star.  I had trouble with scattered
#    light and a rotating field :-(
# MWR 6/22/2004
#
# Modified to add a fixed "uncertainty" column, the same for all
#    magnitudes.
# MWR 6/23/2005
#
# Fixed some fundamental errors in calculation of dates
#    - got rid of all shortened versions of JD and HJD -- now
#          we print the whole thing, darnit
#    - fixed the UT_day by adding -0.5 day offset, which is necessary
#          for RIT Obs
#
# MWR 6/23/2009




# need this for "floor"
use POSIX;
$target = "J004527.52+503213.8";
$ra = "00:45:25.5";
$dec = "+52:32:14";
$utprefix = "Sep";
$utday = 19;

$JDBASE = 2455000.0;
$hjd_dir = "~/old/bait/";

# must change the following 3 items for each night
#  (get uncert_mag from datafile itself, words[7]
# $uncert_mag = 0.035;
# or, in this case, this is the amount by which to shift the differential
#   magnitudes to get a very very rough V-band mag
$aper = "4";
$add_to_mag =  0.00;

# these are 
#       a) the calibrated magnitude of a comparison star on standard scale
#            (star = AAVSO 000-BLD-546 	at 0:45:06.67   +50:37:14.5 
#                   has V=13.036 according to AAVSO chart 12683RZT
#       b) the mean magnitude of the comparison star in the 
#             ensemble solution
$true_comparison_mag = 13.036;
$ensemble_comparison_mag = 1.305;



# print the "header" information
print "# Measurements of $target made at RIT Obs, $utprefix $utday, 2013 UT, \n";
print "#    in good conditions, \n";
print "#    by Michael Richmond, using 12-inch Meade and SBIG ST-8E CCD. \n";
print "# Exposures 30 seconds long, no filter. \n";
print "# Tabulated times are midexposure (FITS header time - half exposure length) \n";
print "#    and accurate only to +/- 1 second (??). \n";
print "# 'mag' is a differential magnitude based on ensemble photometry \n";
print "#    using a circular aperture of radius 7.4 arcseconds.  \n";
print "#    which has been shifted so UCAC4 704-005807 has mag=13.036 \n"; 
print "#    which is its V-band magnitude according to AAVSO.  \n";
print "# \n";
print "# UT_day             JD            HJD        mag    uncert\n";
#print "# UT day      JD-2,450,000  HJD-2,450,000   mag    \n";


$file = "starJ0045_clear.dat" . $aper;
open(INFILE, "$file") || die ("can't open file $file");
while (<INFILE>) {
  @words = split(/\s+/);
  $jd = $words[4];
  $mag = $words[6] + ($true_comparison_mag - $ensemble_comparison_mag);
  #$mag = $words[6] + $add_to_mag;
  $uncert_mag = $words[7];

  # calculate the fractional UT day
  #   this section very different for SDSS data!
  $fraction = $jd - floor($jd);
  $fraction -= 0.5;
  $fraction += $utday;
  $utdate = sprintf("%s%08.5f", $utprefix, $fraction);

  # don't mess around with shortened values of the dates
  if (0 == 1) {
    # calculate the Julian Date - 2,450,000
    $full_julian_date = $jd + 2450000.0;
    #$full_julian_date = $jd;
    $short_jd = $full_julian_date - 2450000.0;
  }

  # calculate the heliocentric julian date
  $full_hjd = `$hjd_dir/hjd jd=$jd ra=$ra dec=$dec`;
  if (0 == 1) {
    $short_hjd = $full_hjd - 2450000.0;
  }

  # print out the results
  #   this version if I have a real uncertainty estimate
  # printf "%-15s %11.5f  %11.5f  %6.3f %6.3f \n", $utdate, $short_jd, $short_hjd, 
   #                  $mag, $uncert_mag;
  #   this version if I don't
  printf "%-15s %12.5f  %12.5f  %6.3f %6.3f \n", 
           $utdate, $jd, $full_hjd, $mag, $uncert_mag;

}

exit 0;


