# L-6 MCS 275 Mon 23 Jan 2017 : earthquakes.py
"""
Selects from the file quakes.cvs the latitude and longitude
of those quakes of magnitude above a certain threshold.
"""

import turtle
from makedata import show_data

DATAFILE = "quakes.csv"

def select_data(mag):
    """
    Opens the file and returns list of
    longitudes and latitudes of quakes of
    magnitude larger than or equal to mag.
    Because latitude runs vertically,
    latitude is y and longitude is x.
    """
    result = []
    file = open(DATAFILE, 'r')
    line = file.readline()
    while True:
        line = file.readline()
        if line == '':
            break
        data = line.split(',')
        try:
            dtm = float(data[4])
        except:
            dtm = 0.0
        if dtm >= mag:
            pnt = (float(data[2]), float(data[1]))
            result.append(pnt)
    return result

def round_data(points):
    """
    Rounds all coordinates to the nearest integer.
    """
    result = []
    for pnt in points:
        point = (round(pnt[0]), round(pnt[1]))
        result.append(point)
    return result

def main():
    """
    Visualization of locations of largest earth quakes.
    """
    mag = float(input("give threshold magnitude : "))
    data = select_data(mag)
    intdata = [round_data(data)]
    turtle.hideturtle()
    show_data(intdata)
    input("hit enter to exit ...")

main()
