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()
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)
def run(): """Requirements for Task 2F""" # 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 = 2 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_with_fit(station, dates, levels, 4) else: pass
def run(): """Requirement for Task 2F""" #Initialise variables data = build_station_list() update_water_levels(data) ls = [] ID = [] #Number of days in past taken data from dt = 9 #Degree of polynomial line of best fit p = 1 #How many stations number = 6 #How many graphs per window limit = 7 #Create list of measuring_id's sorted by water level for station in data: if station.typical_range_consistent( ) == True and station.relative_water_level() != None: ls.append((station, station.relative_water_level())) ls = sorted_by_key(ls, 1) for station in ls: ID.append(station[0]) s = count_inconsistent_sets(ID[:number], dt) ID = ID[:number + s] plot_water_level_with_fit(ID, dt, limit, s, p)
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()
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()
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)
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)
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 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)
def run(): """Requirements for Task 2F""" # Build a list of the top 5 stations by current water level stations = build_station_list() update_water_levels(stations) top_5_stations = sorted( [s for s in stations if s.latest_level is not None], key=lambda x: x.latest_level, reverse=True)[:5] # Plot the station levels against time for those stations, for the past 10 days dt = datetime.timedelta(days=2) for station in top_5_stations: dates, levels = fetch_measure_levels(station.measure_id, dt) plot_water_level_with_fit(station, dates, levels, 3)
def run(): stations = build_station_list() update_water_levels(stations) top_stations = stations_highest_rel_level(stations, N=5) stations_top_stations = [] for entry in top_stations: for station in stations: if entry.name == station.name: stations_top_stations.append(station) for station in stations_top_stations: dt = 2 dates, levels = fetch_measure_levels(station.measure_id, dt=datetime.timedelta(days=dt)) plot_water_level_with_fit(station, dates, levels, 4)
def run(): # Build list of stations stations = build_station_list() # Update latest level data for all stations update_water_levels(stations) #Initiate empty list levellist = [] #initiate temperory storage for water level data for error processing templevel = 0 # iterate for all stations for station in stations: templevel = station.latest_level # Change NonType to zero for data analysis if templevel == None: templevel = 0 # Change negative error data to zero if templevel < 0: templevel = 0 # append to a list levellist.append((station.name, templevel)) # Sorted after iteration levellist = sorted_by_key(levellist, 1) # get the greatest five station levellist = levellist[-5:] # Get the name of the 5 stations (first entry of the tumple) stationname = [] for llist in levellist: stationname.append(llist[0]) print(stationname) for station_name in stationname: station_temp = None for station in stations: if station.name == station_name: station_temp = station break dt = 12 dates, levels = fetch_measure_levels(station_temp.measure_id, dt=datetime.timedelta(days=dt)) plot_water_level_with_fit(station_name, dates, levels, 4)
def run(): stations = build_station_list() highest_levels_stations = stations_highest_rel_level(stations, 5) dt = 10 #returns a list of top 5 stations i = 0 stations_to_plot = [] for n in range(0, len(highest_levels_stations)): for station in stations: if station.name == highest_levels_stations[i][0]: print(highest_levels_stations[i][0]) stations_to_plot.append(station) n += 1 i += 1 for station in stations_to_plot: dates, levels = datafetcher.fetch_measure_levels( station.measure_id, datetime.timedelta(days=dt)) plot_water_level_with_fit(station, dates, levels, 4)
def run(): stations = build_station_list() update_water_levels(stations) #Update water levels list_of_top_stations = stations_highest_rel_level( stations, 5) #Find a list of five stations with largest relative heights list_of_information = [] #Create an empty list for i in list_of_top_stations: for j in stations: if i[0] == j.name: list_of_information.append( j) #Add top 5 stations with full information to the list for x in list_of_information: dt = 2 #For last two days dates, levels = fetch_measure_levels( x.measure_id, dt=datetime.timedelta( days=dt)) #Create a tuple with time and height plot_water_level_with_fit(x, dates, levels, 4) #plot graphs return
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 station = stations_highest_rel_level( stations, 1)[0] # NB Graylingwell does not give any date data color_list = ['b', 'g', 'r', 'c', 'y'] * 100 for weeks in range(3, 5): dates, levels = fetch_recent_data(station.measure_id, weeks) if len(dates) == 0: print("This station:\n", station, "\ndoes not give any date data") continue plt.figure(weeks) plot_water_level_with_fit(station, dates, levels, 10, color_list[weeks]) plt.show()
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')
def test_plot_water_level_with_fit_does_not_crash_if_levels_and_dates_are_not_the_same_length( ): # Create a station s_id = "test-s-id" m_id = "test-m-id" label = "some station" coord = (-2.0, 4.0) trange = (-2.3, 3.4445) river = "River X" town = "My Town" station = MonitoringStation(s_id, m_id, label, coord, trange, river, town) dates = [ dt(2025, 4, 3, 2, 1, 0), dt(2025, 4, 3, 2, 1, 1), dt(2025, 4, 3, 2, 1, 2), dt(2025, 4, 3, 2, 1, 3), dt(2025, 4, 3, 2, 1, 4) ] levels = [0, 1, 2, 3, 4, 5] plot_water_level_with_fit(station, dates, levels, 4)
def run(): # Get station data and update it stations = build_station_list() update_water_levels(stations) # Getting stations with highest water levels most_at_risk_stations = stations_highest_rel_level(stations, 5) # Setting the time interval to 2 days dt = 10 # Run curve fitting with degree of 4 p = 4 for station in most_at_risk_stations: dates, levels = fetch_measure_levels(station.measure_id, dt=datetime.timedelta(days=dt)) # print('station.measure_id =', station.measure_id) # print('len(dates) =', len(dates)) # print('len(levels) =', len(levels)) if len(dates) < 1 or len(levels) < 1: print('Faulty data:', station.name) continue plot_water_level_with_fit(station, dates, levels, p)
def run(): dates_input = (datetime.datetime(2017, 2, 24), datetime.datetime(2017, 3, 3)) # Build list of stations stations = build_station_list() # Compiles list of 5 most at risk stations list_of_stations = [0] * 5 for i in range (5): list_of_stations[i] = stations_highest_rel_level(stations, 5)[i][0] p = 4 return plot_water_level_with_fit(list_of_stations, dates_input, p)
# -*- coding: utf-8 -*- """ Created on Mon Feb 27 21:07:04 2017 @author: Minh """ import datetime from floodsystem.stationdata import build_station_list, update_water_levels from floodsystem.datafetcher import fetch_measure_levels import floodsystem.plot as plot from floodsystem.analysis import polyfit from floodsystem.flood import stations_highest_rel_level stations = build_station_list() update_water_levels(stations) #Getting the 5 stations with the highest water level at_risk = stations_highest_rel_level(stations, 5) dt = 5 #Finding dates and levels data for the 5 stations and plotting them with polyfit function for item in at_risk: for station in stations: if station.name == item[0]: dates, levels = fetch_measure_levels( station.measure_id, dt=datetime.timedelta(days=dt)) print(type(polyfit(dates, levels, 4))) plot.plot_water_level_with_fit(station.name, dates, levels, 4)
from floodsystem.stationdata import build_station_list from floodsystem.stationdata import update_water_levels from floodsystem.utils import sorted_by_key from floodsystem.datafetcher import fetch_measure_levels import datetime from floodsystem.plot import plot_water_levels from floodsystem.plot import plot_water_level_with_fit from floodsystem.analysis import polyfit import matplotlib.pyplot as plt from floodsystem.flood import stations_highest_rel_level stations = build_station_list() update_water_levels(stations) highest_stations = stations_highest_rel_level(stations, 5) for i in highest_stations: station_name = i[0] station_0 = None for station in stations: if station.name == station_name: station_0 = station break dt = 2 dates0, levs0 = fetch_measure_levels(station_0.measure_id, dt=datetime.timedelta(days=dt)) plot_water_level_with_fit(station_0, dates0, levs0, 4)
from floodsystem.stationdata import update_water_levels from floodsystem.flood import stations_highest_rel_level from floodsystem.plot import plot_water_level_with_fit from floodsystem.datafetcher import fetch_measure_levels stations = build_station_list() update_water_levels(stations) dt = 2 Number_of_stations = 5 #Set the degree of the polynomial to p p = 4 station_names = [] for i in stations_highest_rel_level(stations, Number_of_stations)[:Number_of_stations]: station_names.append(i[0]) greatest_relative_water_level_stations = [] for station in stations: if station.name in station_names: greatest_relative_water_level_stations.append(station) #call the function to plot the data and the best fit for station in greatest_relative_water_level_stations: dates, levels = fetch_measure_levels(station.measure_id, dt=datetime.timedelta(days=dt)) plot_water_level_with_fit(station, dates, levels, p)