//************************************************************************
// A C++ program that provides an example of reading in data from a 
// comma delimited file
//
// Author: Sherry Towers
//         smtowers@asu.edu
// Created: Feb 13th, 2013
//
// Copyright Sherry Towers, 2013
//
// This program is not guaranteed to be free of bugs and/or errors.
//
// This program can be freely used and shared, as long as the author information
// and copyright in the header remains intact.
//************************************************************************

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int main(){
  ifstream infile("ozone_2000.csv");

  int icount = 0;
  vector<double> vozone;
  vector<int> vmonth;
  vector<int> vday;
  vector<int> vyear;
  while (infile){
    string myline;
    if (!getline(infile,myline)) break;
    //cout << myline << endl;
    istringstream ss(myline);

    int ifield = 0;
    while (ss){
      string myelement;
      if (!getline(ss,myelement, ',' )) break;
      ifield++;
      if (ifield==1&icount>0){
        //cout << "The date string is " << myelement << endl;
        //cout << "The length of the date string (including quotes) " 
        //     << myelement.length() << endl; 

        string mymonth = myelement.substr(1,2);
        string myday = myelement.substr(4,2);
        string myyear = myelement.substr(7,4);
        vmonth.push_back(atof(mymonth.c_str()));
        vday.push_back(atof(myday.c_str()));
        vyear.push_back(atof(myyear.c_str()));
      }
      if (ifield==4&icount>0){
        //cout << "The ozone reading string is " << myelement << endl;
        // remove the quotes from the string
        myelement.erase(remove(myelement.begin()
                              ,myelement.end(),'\"' )
                       ,myelement.end()); 
        double ozone = atof(myelement.c_str());
        vozone.push_back(ozone);

        //cout << "The ozone reading string (without qoutes) is " 
        //     << myelement << endl;
        //cout << "The ozone reading is " << ozone << endl;
      }
    }

    icount++;
  }// end loop over lines in file


  for (int i=0;i<vozone.size();i++){
     cout << i+1       << "," 
          << vmonth[i] << ","
          << vday[i]   << ","
          << vyear[i]  << ","
          << vozone[i] << endl;
  }

  if (!infile.eof()){
    cerr << "File not found!\n";
  }

  return 0;
}


