#! /bin/bash 
# Last edited on 2011-05-29 17:56:39 by stolfi

# Recomputes the time in hours (column 3 not counting the "|"s) 
# from columns 1 (date yyyy-mm-dd) and 2 (time of day HH:MM:SS)
# Then realigns columns.

gawk \
  -v FS='|' \
  ' /^[ ]*([\#]|$)/ { print; next; }
    // { 
      gsub(/[ ]/, "", $0); 
      dt=$1; hr=$2; 
      ht = ehours(dt,hr);
      printf "%s | %s | %8.3f", dt, hr, ht; 
      for (i=4; i<=NF;i++) { printf " | %s", $(i); } printf "\n"; 
    }
    function ehours(dt,hr,  dtx,yr,dy,n,hf,ht) {
      dtx=(dt " 00 00 00"); gsub(/[\/-]/, " ", dtx);
      yr=substr(dt,1,4); dy=(yr-2011)*365 + strftime("%j", mktime(dtx));
      n=split(hr,hf,":");
      if (hf[1] == "99")
        { ht = 9999.999; }
      else
        { ht = 24*dy - 1680 + hf[1]+(1.0/60.0)*hf[2]; 
          if (n == 3) { ht = ht + (1.0/3600.0)*hf[3]; }
        }
      return ht;
    }
  ' \
  | txtable-reformat
  
