#!/usr/bin/perl
#
# Convert a datafile of variable-star measurements 
#    FROM  the format I use for CBA and VSNet
#    TO    a format for AAVSO
# 
# Follow the instructions at
#
#     http://www.aavso.org/aavso-extended-file-format
#
# The user must supply the name of the variable, in a form recognized
#    by the AAVSO, as the long command-line argument.  That may require
#    using single-quote characters if the name incudes a space,
#    as in the example below
#
# Usage:     perl to_aavso.pl  starid  filter kname kmag chartid < infile > outfile
#
#    Here,
#           kname           is the label of the star used to shift the
#                               ensemble magnitudes to the standard scale,
#                               as shown on the AAVSO chart for the field
#                               (or a name from UCAC4, I guess)
#           kmag            is the magnitude on the standard system of
#                               this star
#           chartid         is the AAVSO chart ID from which 'kname' and 
#                               'kmag' were taken
#
#    For example,
#       perl to_aavso.pl  'V404 Cyg' I 128 10.432 15088BFS  < v404cyg_jun20ut.dat > aavso.dat
#
# MWR 6/21/2015

$debug = 0;

# use this character as field delimiter
$delim = ",";

# default values for some quantities on the AAVSO data lines
#
# TRANS should be "NO" for most RIT Obs measurements; or "YES"
$trans = "NO";
# MTYPE should always be "STD"
$mtype = "STD";
# CNAME should always be "ENSEMBLE"
$cname = "ENSEMBLE";
# CMAG should always be "na", since we use ENSEMBLE
$cmag = "na";
# AIRMASS should always be "na", because I'm lazy
$airmass = "na";
# GROUP should always be "na", because I do simple time series in one filter
$group = "na";
# NOTES should always be "na".  If comments need to be added, I'll do so by hand
$notes = "na";

# parse the command line arguments
if ($#ARGV != 4) {
  printf "usage: to_aavso.pl starid filter kname kmag chartid < v404cyg.dat > aavso.dat \n";
  exit(1);
}
$varname = $ARGV[0];
$filter = $ARGV[1];
$kname = $ARGV[2];
$kmag = $ARGV[3];
$chartid = $ARGV[4];
if ($debug > 0) {
  printf " variable name is ..%s.. \n", $varname;
  printf " filter is ..%s.. \n", $filter;
  printf " kname is ..%s.. \n", $kname;
  printf " kmag is ..%s.. \n", $kmag;
  printf " chartid is ..%s.. \n", $chartid;
}


# read in all data from stdin and save for later use
$nline = 0;
$ndata = 0;
while (<STDIN>) {
  $line = $_;
  chomp($line);
  $line_array[$nline++] = $line;
  if ($line =~ /^#/) {
    # not a data line
  }
  else {
    $ndata++;
  }
}
if ($debug > 0) {
  printf "read in %5d lines, of which %5d were header and %5d data \n",
       $nline, $nline - $ndata, $ndata;
}
  
  

# first, we write the required AAVSO header information
printf "#TYPE=EXTENDED \n";
printf "#OBSCODE=RHM \n";
printf "#SOFTWARE=XVista \n";
printf "#DELIM=$delim \n";
printf "#DATE=HJD \n";
printf "#OBSTYPE=CCD \n";

# next, we print header lines from the original RIT data file,
#    which may contain useful information on weather, comp stars, etc.
if ($nline - $ndata > 0) {
  printf "#  \n";
  printf "# - Comments from original data file follow  \n";
  for ($i = 0; $i < $nline; $i++) {
     if ($line_array[$i] =~ /^#/) {
       printf "#   %s \n", $line_array[$i];
     }
  }
  printf "# - End of comments from original data file \n";
  printf "#  \n";
}

# Now, we grab data from each line, re-format for AAVSO, and print it out
for ($i = 0; $i < $nline; $i++) {
  if ($line_array[$i] =~ /^#/) {
    next;
  }

  # this line contains data, so pick out the important stuff
  @words = split(/\s+/, $line_array[$i]);
  $utday = $words[0];
  $jd = $words[1];
  $hjd = $words[2];
  $mag = $words[3];
  $magsig = $words[4];

  # build the new line for AAVSO
  #    starID
  $newline = sprintf "%s%s", $varname, $delim;
  if ($debug > 0) {
    printf "line is ..%s.. \n", $newline;
  }
  #    date
  $newline .= sprintf "%s%s", $hjd, $delim;
  if ($debug > 0) {
    printf "line is ..%s.. \n", $newline;
  }
  #    magnitude
  $newline .= sprintf "%s%s", $mag, $delim;
  if ($debug > 0) {
    printf "line is ..%s.. \n", $newline;
  }
  #    magerr
  $newline .= sprintf "%s%s", $magsig, $delim;
  if ($debug > 0) {
    printf "line is ..%s.. \n", $newline;
  }
  #    filter
  $newline .= sprintf "%s%s", $filter, $delim;
  if ($debug > 0) {
    printf "line is ..%s.. \n", $newline;
  }
  #    trans
  $newline .= sprintf "%s%s", $trans, $delim;
  if ($debug > 0) {
    printf "line is ..%s.. \n", $newline;
  }
  #    mtype
  $newline .= sprintf "%s%s", $mtype, $delim;
  if ($debug > 0) {
    printf "line is ..%s.. \n", $newline;
  }
  #    cname
  $newline .= sprintf "%s%s", $cname, $delim;
  if ($debug > 0) {
    printf "line is ..%s.. \n", $newline;
  }
  #    cmag
  $newline .= sprintf "%s%s", $cmag, $delim;
  if ($debug > 0) {
    printf "line is ..%s.. \n", $newline;
  }
  #    kname
  $newline .= sprintf "%s%s", $kname, $delim;
  if ($debug > 0) {
    printf "line is ..%s.. \n", $newline;
  }
  #    kmag
  $newline .= sprintf "%s%s", $kmag, $delim;
  if ($debug > 0) {
    printf "line is ..%s.. \n", $newline;
  }
  #    airmass
  $newline .= sprintf "%s%s", $airmass, $delim;
  if ($debug > 0) {
    printf "line is ..%s.. \n", $newline;
  }
  #    group
  $newline .= sprintf "%s%s", $group, $delim;
  if ($debug > 0) {
    printf "line is ..%s.. \n", $newline;
  }
  #    chart
  $newline .= sprintf "%s%s", $chartid, $delim;
  if ($debug > 0) {
    printf "line is ..%s.. \n", $newline;
  }
  #    notes.  
  #    Since this is the final field, do NOT add the delimiter afterwards
  $newline .= sprintf "%s", $notes;
  if ($debug > 0) {
    printf "line is ..%s.. \n", $newline;
  }


  if ($debug > 0) {
    exit(0);
  }

  printf "%s \n", $newline;
}


exit 0;
