Пример #1
0
def test_build_station_list():

    # Build list of stations
    stations = build_station_list()

    # Find station 'Cam'
    for station in stations:
        if station.name == 'Cam':
            station_cam = station
            break

    # Assert that station is found
    assert station_cam

    # Fetch data over past 2 days
    dt = 2
    dates2, levels2 = fetch_measure_levels(station_cam.measure_id,
                                           dt=datetime.timedelta(days=dt))
    assert len(dates2) == len(levels2)

    # Fetch data over past 10 days
    dt = 10
    dates10, levels10 = fetch_measure_levels(station_cam.measure_id,
                                             dt=datetime.timedelta(days=dt))
    assert len(dates10) == len(levels10)
    assert len(dates10) > len(levels2)
def run():

    #Building list of stations to fetch data for
    stations = build_station_list()
    update_water_levels(stations)
    N_stations_at_risk = stations_highest_rel_level(stations, 5)

    #Variables
    dt = 2  # Fetch data over past 2 days
    p = 4  #Polynomial degree
    i = 0  #Counter

    for s in N_stations_at_risk:
        i += 1
        dates, levels = fetch_measure_levels(
            s[0].measure_id,
            dt=datetime.timedelta(days=dt))  #Compiling dates and water levels
        if not dates:
            print('Insufficient Data')
        else:
            d0, poly = polyfit(dates, levels, p)  #Creating polynomial
            #Plotting data, polynomial and typical high/low
            plt.subplot(3, 3, i + 1)
            plt.plot(dates, levels)
            plt.plot(dates, poly((mpl.dates.date2num(dates) - d0)))
            plt.axhline(y=s[0].typical_range[0], color='y')
            plt.axhline(y=s[0].typical_range[1], color='r')
            plt.xlabel('date')
            plt.ylabel('water level (m)')
            plt.xticks(rotation=45)
            plt.title(s[0].name)
    plt.show()
Пример #3
0
def run():
    N = 5
    DT = 2
    P = 4

    stations = build_station_list()
    update_water_levels(stations)
    at_risk_stations = stations_highest_rel_level(stations, N)

    for station in at_risk_stations:
        dates, levels = fetch_measure_levels(station.measure_id,
                                             dt=datetime.timedelta(days=DT))

        # plot real data
        date_nums = date2num(dates) - date2num(dates[0])
        plt.plot(date_nums, levels, color="orange")

        # plot line of best fit
        plot_water_level_with_fit(station, dates, levels, P)

        # plot high/low
        plt.axhline(station.typical_range[0],
                    linestyle="dashed",
                    color="green")
        plt.axhline(station.typical_range[1], linestyle="dashed", color="red")

        plt.legend(("Real values", "Best fit", "Typical low", "Typical high"))

        plt.show()
Пример #4
0
def run():
    #Requirements for Task2E
    n = 6
    dt = 10
    stations = build_station_list()
    update_water_levels(stations)
    #build list of tuples of stations
    b_tuples = stations_highest_rel_level(stations, n)

    #Let"s convert the list of tuples into a list of stations!
    b = []
    for station in stations:  #maybe change the order of these two functions?
        for station_tuple in b_tuples:

            if station.name == station_tuple[0]:
                b.append(station)
                break

    print(b)

    # plot data for each station
    for station in b:
        dates, levels = fetch_measure_levels(station.measure_id,
                                             dt=datetime.timedelta(days=dt))
        if len(dates) == 0 or len(levels) == 0:
            continue  # Test for the stations with incorrect datafetcher responses
        plot_water_levels(station, dates, levels)
        plt.show()
Пример #5
0
def run():
    #get the stations data and sorted by latest water level
    stations = build_station_list()
    
    #get the six stations with the largest water level
    highest_six = stations_highest_rel_level(stations, 6)
    
    #set the length of time for wanted water level
    dt = 10
    
    #plotting water level against time of those six stations
    i = 0
    station_name = []
    date_and_level = []
    for station in highest_six:
        dates, levels = fetch_measure_levels(station.measure_id, dt=datetime.timedelta(days=dt))
        plt.subplots()
        plot_water_levels(station, dates, levels)
        station_name.append(station.name)
        date_and_level.append((dates, levels))
        i += 1
    
    plt.show()    
    print(station_name)
    return station_name, date_and_level
Пример #6
0
def test_polyfit():
    # Build list of stations
    stations = build_station_list()
    update_water_levels(stations)
    station_and_relative_water_levels = []

    for station in stations:
        station_and_relative_water_levels.append(
            (station.relative_water_level(), station.name, station))

    p = 2

    dt = 2
    dates, levels = fetch_measure_levels(stations[3].measure_id,
                                         dt=datetime.timedelta(days=dt))
    #fit data to a polynomial
    x = matplotlib.dates.date2num(dates)
    y = levels

    #shift axis
    d0 = x - x[0]

    # Find coefficients of best-fit polynomial f(x) of degree p
    p_coeff = np.polyfit(d0, y, p)
    # Convert coefficient into a polynomial that can be evaluated,
    # e.g. poly(0.3)
    poly = np.poly1d(p_coeff)

    assert len(poly(d0)) == len(d0)
Пример #7
0
def run():
    """Requirements for Task 2E"""

    # Build list of stations
    stations = build_station_list()

    # Find 5 stations at which the current level is the highest
    stations_highest_rel_level_list = []
    N = 5
    for i in range(len(stations_highest_rel_level(stations, N))):
        stations_highest_rel_level_list.append(
            stations_highest_rel_level(stations, N)[i][0])

    # Plot the water level for each of these stations over the past 10 days

    # First fetch the time history for a station
    for station in stations:
        if station.name in stations_highest_rel_level_list:

            dt = 10
            dates, levels = fetch_measure_levels(
                station.measure_id, dt=datetime.timedelta(days=dt))
            # This gives list of dates and levels to be passed into a plot
            plot_water_level(station, dates, levels)
        else:
            pass
Пример #8
0
def run():
    """Requirement for task 2G"""

    #build a list of stations
    stations = build_station_list()
    update_water_levels(stations)
    stations_at_risk = stations_highest_rel_level(stations, 10)

    # Fetch data over past 2 days
    dt = 0.5
    danger_stations = []
    for station in stations_at_risk:
        dates, levels = fetch_measure_levels(station.measure_id,
                                             dt=datetime.timedelta(days=dt))

        try:
            prediction = predicted_value(dates, levels)
            danger_level = danger_assigning(station, prediction)
            if danger_level is not None:
                if danger_level[1] == "severe" or danger_level[1] == "high":
                    danger_stations.append(danger_level)

        except:
            pass
    sorted_stations = sorted_by_key(danger_stations, 1, reverse=True)
    for i in sorted_stations:
        print(i[0] + ":" + i[1])
Пример #9
0
def run():
    # Build list of stations
    stations = build_station_list()

    # Update latest level data for all stations
    update_water_levels(stations)
    
    #create list of names stations to be plotted with their measure_id
    highest_levels=stations_highest_rel_level(stations, 5)
    stations_to_plot = []

    #retrieve the rest of station data for the high
    for j in highest_levels:
        for i in stations:
            if j[0] == i.name:
                stations_to_plot.append(i)
                       
    dt = 10    
    #plot data for each station
    for i in stations_to_plot:
        
        dates, levels = fetch_measure_levels(i.measure_id,
                                             dt=timedelta(days=dt))
        plot_water_levels(i.name,dates,levels)
        plt.show()
Пример #10
0
def update_text_select(attr, old, new):
    """Function that updates the selected plot according to the name text provided."""
    global current_selection
    logger.info('Current Selection: {}'.format(current_selection))
    input_text = select_input.value
    if input_text == current_selection[0]:  # without fuzzy match, the new station is equal to the current station
        return
    if input_text != '':
        selected_station_name = process.extractOne(input_text, source.data['name'])[0]
        if selected_station_name == current_selection[0]:
            # after fuzzy match, the new station is equal to the current station
            select_input.value = selected_station_name
            return
        logger.info('Input: {}, Matched: {}'.format(input_text, selected_station_name))
        indx = name_to_indx[selected_station_name]
        current_selection = [selected_station_name, indx]  # update the current selection cache
        r.data_source.selected.indices = [indx]  # update the selection on map
        select_input.value = selected_station_name  # update the displayed text in the text input box
        # TODO recenter map
        dates, levels = fetch_measure_levels(source.data['measure_id'][indx],
                                             dt=timedelta(days=30))  # get the data for the newly selected station
        selected_plot_source.data.update(dict(dates=dates, levels=levels))
    else:
        current_selection = [None, None]  # update the current selection
        r.data_source.selected.indices = []
        selected_plot_source.data.update(dict(dates=[], levels=[]))
Пример #11
0
def run(N=5):
    
    #get the stations data and sorted by latest water level
    stations = build_station_list()
    
    #get the six stations with the largest water level
    highest_five = stations_highest_rel_level(stations, N) # NB Graylingwell does not give any date data
    
    #set the length of time for wanted water level
    dt = 365
    
    i = 0

    
    for station in highest_five:
        dates, levels = fetch_measure_levels(station.measure_id, dt=datetime.timedelta(days=dt))
        if len(dates) == 0:
            print("This station:\n", station, "\ndoes not give any date data")
            continue
        
        plt.figure(i)
        plot_water_level_with_fit(station, dates, levels, 10)
        
        i += 1
        
    print(i, ' out of ', N, ' were printed')
 
    plt.show()
Пример #12
0
def assess_risk(stations):
    """A function that given a list of MonitoringStations, calculates the risk
    of each station and places it in severe, high, moderate or low category"""
    ls = []
    
    for station in stations:
        #Get data
        dates, levels = fetch_measure_levels(station.measure_id,
                                                 dt=datetime.timedelta(days=5))        
        #Pass inconsistent data
        if levels == []:
            pass
        
        else:
            poly, d0 = polyfit(dates, levels, 1)
            
            if poly[1] < 0.01:
                status = 'Low'
            elif poly[1] < 0.05:
                status = 'Moderate'
            elif poly[1] < 0.1:
                status = 'High'
            else:
                status = 'Severe'
            ls.append(status)
    return ls
Пример #13
0
def run():

    # Build list of stations
    stations = build_station_list()
    update_water_levels(stations)
    station_and_relative_water_levels = []

    for station in stations:
        station_and_relative_water_levels.append(
            (station.relative_water_level(), station.name, station))

    stations_with_values_for_water_levels = []
    for x in station_and_relative_water_levels:
        if x[0] is None:
            pass
        else:
            stations_with_values_for_water_levels.append(x)

    stations_with_values_for_water_levels.sort(reverse=True)
    greatest_5 = stations_with_values_for_water_levels[1:6]

    p = 4
    for n in greatest_5:
        #Fetch data over past 2 days
        dt = 2
        dates, levels = fetch_measure_levels(n[2].measure_id,
                                             dt=datetime.timedelta(days=dt))
        #fit data to a polynomial
        k = polyfit(dates, levels, p)
        #plot graph with data and also polynomial
        plot_water_level_with_fit(n[2], dates, levels, k)
        #plot lines of typical high and low
        plt.axhline(n[2].typical_range[0], linewidth=2, color='r')
        plt.axhline(n[2].typical_range[1], linewidth=2, color='r')
        plt.show()
Пример #14
0
def run():
    stations = stationdata.build_station_list()
    stationdata.update_water_levels(stations)
    stations = station.consistant_typical_range_stations(stations)

    top10Stations = flood.stations_highest_rel_level(stations, 10)

    topStations = []
    topStationsDates = []
    topStationsLevels = []
    counter = 0
    NUMBER_PLOTS = 5

    for st in top10Stations:
        stationDates, stationLevels = datafetcher.fetch_measure_levels(
            st.measure_id, timedelta(days=2))

        # only consistant ones get saved
        if all(isinstance(x, (int, float))
               for x in stationLevels) and stationDates != []:
            counter += 1
            topStations.append(st)
            topStationsDates.append(stationDates)
            topStationsLevels.append(stationLevels)
        if counter == NUMBER_PLOTS:
            break

    # single object input

    # plot.plot_water_level_with_fit(topStations[0], topStationsDates[0], topStationsLevels[0], 4)

    # list of object input
    plot.plot_water_level_with_fit(topStations, topStationsDates,
                                   topStationsLevels, 4)
Пример #15
0
def test_polyfit():
    stations = build_station_list()
    station = stations[0]
    dt = 2
    dates, levels = fetch_measure_levels(station.measure_id,
                                         dt=datetime.timedelta(days=dt))
    N = randint(1, 5)

    assert type(polyfit(dates, levels, N)) == tuple
def run():
    stations = build_station_list()
    update_water_levels(stations)
    key = stations[0].measure_id
    station = stations[0]
    dt = 5
    dates, levels = fetch_measure_levels(key, dt=datetime.timedelta(days=dt))
    p = 4
    plot_water_level_with_fit(station, dates, levels, p)
Пример #17
0
def average_of_latest_level(station, p, dt, dt_future):
    '''
    This function is to get average of the water levels in recent 2 days and the predicting water level of 
    the following 2 days
    Input: 
        station: a MonitoringStaiion object
        p : degree of the best fit polynomial
        dt : the number of sample past days

    '''
    dates, levels = fetch_measure_levels(station.measure_id,
                                         dt=datetime.timedelta(days=dt))
    if len(levels) == 0:
        return None
    poly, d0 = polyfit(dates, levels, p)

    # get dates float
    dates_f = matplotlib.dates.date2num(dates)

    future_dates_float = dates_f + (dates_f[0] - dates_f[-1])

    # get future date from future date float
    future_dates = []
    for date in future_dates_float:
        future_dates.append(matplotlib.dates.num2date(date))

    future_dates_float_c = future_dates_float - dates_f[0]

    # predict future water level by poly
    #    x = np.linspace(date_f[0], date_f[-1], len(date_f))
    #    print(type(x))
    future_level = list(poly(future_dates_float_c))

    #Cuz the predicted level at the end of the future 2 days are too large to be exaxt, so only
    #fetch part of the future predicted levels
    a = int(float(dt_future) / float(dt) * len(future_level))

    # prepare to calculate average value of past and future levels
    merge_water_level = future_level[-a:-1] + levels
    merge_dates = future_dates[-a:-1] + dates
    '''
    for date in future_dates[-a:-1]:
        print(date)
        
    print('\n')
    for date in dates:
        print(date)
    
    print('a station')
    '''
    average_level = future_level[-a:-1] + levels[0:a]

    #calculate average level
    return sum(average_level) / float(
        len(average_level)), merge_water_level, merge_dates
Пример #18
0
def run():
    # Build list of stations
    stations = build_station_list()
    N = 5
    update_water_levels(stations)
    list_of_5_stations_greatest_level = stations_highest_rel_level(stations, N)
    dt = 10
    for station in list_of_5_stations_greatest_level:
        dates, levels = fetch_measure_levels(station.measure_id,
                                             dt=datetime.timedelta(days=dt))
        plot_water_levels(station, dates, levels)
Пример #19
0
def run():
    stations = build_station_list()
    update_water_levels(stations)
    stations_relative = stations_highest_rel_level(stations, 5)

    dt = 10

    for station in stations_relative:
        dates, levels = fetch_measure_levels(station.measure_id,
                                             datetime.timedelta(days=dt))
        plot_water_levels(station, dates, levels)
Пример #20
0
def test_polyfit():

    dt = 10
    #create time and level data to be testerd
    times, levels = fetch_measure_levels(stations[0].measure_id,
                                         dt=timedelta(days=dt))
    x = dates.date2num(times)

    #run polyfit
    poly, d0 = polyfit(times, levels, 2)
    assert d0 == x[0]
    assert isinstance(poly, np.poly1d)
Пример #21
0
def run():
    # Build list of stations
    stations = build_station_list()
    N=5 #number of stations we consider of having highest risk of flood
    update_water_levels(stations)
    list_of_5_stations_greatest_level=stations_highest_rel_level(stations , N)
    dt=2
    p=4 #degree 4 against time
    for station in list_of_5_stations_greatest_level:
        dates, levels = fetch_measure_levels(station.measure_id, dt=datetime.timedelta(days=dt))
        poly, d0 = polyfit(dates, levels, p)
        plot_water_level_with_fit(station, dates, levels, p)
Пример #22
0
def run():
    stations = build_station_list()
    update_water_levels(stations)
    high_risk_stations = stations_highest_rel_level(stations, 5)

    for station in high_risk_stations:
        dates, levels = fetch_measure_levels(station.measure_id,
                                             dt=timedelta(days=2))
        try:
            graph = plot_water_level_with_fit(station, dates, levels, 4)
            show(graph)
        except TypeError:
            print('No data')
Пример #23
0
def run():

    stations = build_station_list()
    dt = 10
    update_water_levels(stations)
    greatest_relative_level_stations = stations_highest_rel_level(stations, 5)

    for station in greatest_relative_level_stations:
        dates, levels = fetch_measure_levels(station.measure_id,
                                             dt=datetime.timedelta(days=dt))
        plot_water_levels(station, dates, levels)

        plt.show()
def test_analyse():
    stations = build_station_list()
    dt = 10
    stations=sorted(stations, key=lambda x: x.name)


    for station in stations[:10]:
        dates, levels = fetch_measure_levels(
        station.measure_id, dt=datetime.timedelta(days=dt))
        p=4
        poly, d0 = polyfit(dates,levels,p)
        dydx=np.polyder(poly)
        assert dydx==np.poly1d.deriv(poly)
def run():

    # Build list of stations
    stations = build_station_list()
    update_water_levels(stations)
    # Fetch data over past 2 days
    dt = 10
    x=stations_highest_rel_level(stations,5)
    for station, ratio in x:
        dates, levels = fetch_measure_levels(
        station.measure_id, dt=datetime.timedelta(days=dt))
        print(station.name)
        plot_water_levels(station, dates, levels)
Пример #26
0
def plot_graph(risk_level):
    for sample in risk_level:
        for station in stations:
            if station.name == sample:
                dt = 5
                dates, levels = fetch_measure_levels(
                    station.measure_id, dt=datetime.timedelta(days=dt))
                if len(dates) == 0:
                    print("NO AVAILABLE DATA for:", station.name)
                else:
                    plot_water_level_with_fit(station, dates, levels, 4)
                    #print ("Gradient of curve:", grad(dates,levels,4))
                    grad_risk.append((station.name, grad(dates, levels, 4)))
def run():
    """Requirement for task 2F"""

    #build a list of stations
    stations = build_station_list()
    update_water_levels(stations)
    stations_at_risk = stations_highest_rel_level(stations, 5)

    # Fetch data over past 2 days
    dt = 2
    for station in stations_at_risk:
        dates, levels = fetch_measure_levels(station.measure_id,
                                             dt=datetime.timedelta(days=dt))
        plot_water_level_with_polyfit(station, dates, levels)
def run():

    # Build list of stations
    stations = build_station_list()
    update_water_levels(stations)
    stations_at_risk = stations_highest_rel_level(stations, 5)
    print(stations_at_risk)

    # Fetch data over past 10 days
    dt = 10
    for station in stations_at_risk:
        dates, levels = fetch_measure_levels(station.measure_id,
                                             dt=datetime.timedelta(days=dt))
        plot_water_levels(station, dates, levels)
def test_plot_water_level_with_fit():
    stations = build_station_list()
    update_water_levels(stations)

    p = 4
    dt = 10
    x = random.randrange(0, len(stations) - 1, 1)
    y = random.randrange(0, len(stations) - 1, 1)

    for i in [x, y]:
        station = stations[i]
        dates, levels = fetch_measure_levels(station.measure_id,
                                             datetime.timedelta(days=dt))
        plot_water_level_with_fit(station, dates, levels, p)
Пример #30
0
def run():
    stations = stationdata.build_station_list()
    stationdata.update_water_levels(stations)
    stations = station.consistant_typical_range_stations(stations)

    top5Stations = flood.stations_highest_rel_level(stations, 5)
    top5StationsDates = []
    top5StationsLevels = []
    for top5station in top5Stations:
        stationDates, stationLevels = datafetcher.fetch_measure_levels(
            top5station.measure_id, timedelta(days=10))
        top5StationsDates.append(stationDates)
        top5StationsLevels.append(stationLevels)

    plot.plot_water_levels(top5Stations, top5StationsDates, top5StationsLevels)