#! /usr/bin/gawk -f
# Last edited on 2013-12-04 12:06:40 by stolfilocal

BEGIN { 
  # Reads the hand-entered blink location file, outputs list of 
  # runs which are not flagged for weirdens and where the blinks 
  # do not enter a specific interval. 
  
  # Input file has one line per run, with fields 
  #   
  #   {SUBJID} {RUNID} {FLAG}  {TINI[0]} {TFIN[0]}  {TINI[1]} {TFIN[1]} ... {TINI[nb-1]} {TFIN[nb-1]} 
  #
  # where {FLAG} is "-" (normal run) or "*" (weird run), {TINI[k]} and
  # {TFIN[k]} are starting and ending times of blink number {k} in the run
  # (in seconds, relative to the start of the run). 
  
  # Output has one line per "safe" run
  #   
  #   {SUBJID} {RUNID}
  #   
  
  # Arguments {stini,stfin} define the sensitive interval (in seconds from start of run file) :
  if (stini == "") { arg_error(("must define {stini}")); }
  if (stfin == "") { arg_error(("must define {stfin}")); }
  
  abort = -1;
  
  nruns = 0;   # Number of runs processed.
  nsafe = 0;   # Number of safe runs found.
  
  split("", nruns_sb);  # {nruns_sb[sid]} counts total runs of subject {sid}. */
  split("", nsafe_sb);  # {nsafe_sb[sid]} counts safe runs of subject {sid}. */
}

(abort >= 0) { exit(abort); }

# Remove comments: 
// {
  gsub(/[ ]*[#].*$/, "", $0);
}

# Skip blank lines:
/^[ ]*$/ { 
  next;
}

# process data lines:
/^[ ]*[0-9]+[ ]+[0-9]+[ ]+[-*]([ ]+|$)/ {
  if (NF < 3) { data_error(("wrong number of fields")); }
  sid = $1;
  rid = $2;
  flag = $3;
  nb = int((NF-3)/2); # Number of blinks in run.
  if (NF != 2*nb + 3) { data_error(("invalid number of fields")); }
  nbad = 0; # Number of bad blinks in run.
  for (k = 0; k < nb; k++) 
    { tini = $(2*k+4) + 0.0;
      tfin = $(2*k+5) + 0.0;
      if ((tini <= stfin) && (tfin >= stini)) { nbad++; }
    }
  if ((flag == "-") && (nbad == 0)) 
    { printf "%03d %05d\n", sid, rid; 
      nsafe++;
      nsafe_sb[sid]++;
    }
  nruns++;
  nruns_sb[sid]++;
  next;
}

# Anything else:
// { 
  data_error(("invalid line format"));
}

END {
  if (abort >= 0) { exit(abort); }
  printf "%d safe runs in %d runs (%.2f%%)\n", nsafe, nruns, 100*nsafe/nruns > "/dev/stderr";
  for (sid in nruns_sb)
    { ns = nsafe_sb[sid];
      nr = nruns_sb[sid];
      printf "subject %s: %d safe runs in %d runs (%.2f%%)\n", sid, ns, nr, 100*ns/nr > "/dev/stderr";
    }
}

function arg_error(msg) {
  printf "** %s\n", msg > "/dev/stderr";
  abort = 1;
  exit(1);
}

function data_error(msg) {
  printf "  «%s»\n", $0 > "/dev/stderr";
  printf "%s:%d: ** %s\n", FILENAME, FNR, msg > "/dev/stderr";
  abort = 1;
  exit(1);
}