#include "libcpp.h"
using namespace liberr;
using namespace tools;
using namespace libcpp;
using namespace std;

int main()
{
  
  ifstream raw("cty_raw.dat");
  if (!raw) liberr::error("Error, Cannot open cty_raw.dat");

  ofstream sub("cty_sub.dat");
  if (!sub) liberr::error("Error, Cannot open cty_sub.dat");

  INT_32BIT seed = 770726;

  REAL u;

  string line;

  getline(raw,line);

  sub << line << '\n'; 

  INTEGER yes, yes_sub, no, no_sub, total, total_sub;
  yes = yes_sub = no = no_sub = total = total_sub = 0;

  while(getline(raw,line)) {

    INTEGER j=0;
    char c;
    for(INTEGER i=1; i<=470; i++) {
      while(line[j++]!=',');
    }

    ++total;

    c = line[j];
    
    u = ran(&seed);
    
    if (c=='1') {
      ++yes;
      if (u < .2) {
        sub << line << '\n';
	++yes_sub;
	++total_sub;
      }
    }
    else {
      ++no;
      if (u < .044835) {
        sub << line << '\n';
	++no_sub;
	++total_sub;
      }
    }

  }

  cout << "total = " << total << '\n';
  cout << "total_sub = " << total_sub << '\n';
  cout << "yes = " << yes << '\n';
  cout << "yes_sub = " << yes_sub << '\n';
  cout << "no = " << no << '\n';
  cout << "no_sub = " << no_sub << '\n';
  REAL freq = REAL(yes)/REAL(total);
  cout << "freq = " << freq << '\n';
  REAL freq_sub = REAL(yes_sub)/REAL(total_sub);
  cout << "freq_sub = " << freq_sub << '\n';
  cout << "OSR = " << freq_sub/freq << '\n';

  return 0;
}
