#!/usr/bin/env python3

#extractCounty

#Author: Nikolas Szejna

#Purpose: get a county out of epa dataset, assumes final directory has CSVs for all desired years
#Years 1990-2015

import urllib.request,os

from os import listdir
from os.path import isfile, join
import sys

if(len(sys.argv) > 1):
	COUNTY = str(sys.argv[1]).lower().title()
else:
	COUNTY = "Trinity"

global found_county
found_county = False

FINAL_DIRECTORY = "hourly_88101"

OUTFILE_NAME = "hourly_88101_"+COUNTY+".csv"




def listOfDirectoryFiles(directory):
    onlyfiles = [f for f in listdir(directory) if isfile(join(directory, f))]
    return onlyfiles

def concatenate_all_files_county(directory,outfilePath,county):
    fileList = listOfDirectoryFiles(directory)
    with open(outfilePath, 'w') as outfile:
        for fname in fileList:
            with open(FINAL_DIRECTORY+"/"+fname) as infile:
                for line in infile:
                    if county in line: #does not work for Hawaii because county === state name
                        found_county = True
                        print(line)
                        outfile.write(line)


    





concatenate_all_files_county(FINAL_DIRECTORY,FINAL_DIRECTORY+"/"+OUTFILE_NAME,COUNTY)
if(found_county):
	print("finished concatenating all files containing "+ COUNTY)
else:
	print("Did not find county:" + COUNTY)
