def test_stations_by_distance(): result = stations_by_distance(build_station_list(), (52.2053, 0.1218)) result_1 = result[0] result_2 = result[-1] assert len(stations_by_distance(build_station_list(), (52.2053, 0.1218))) > 0 assert result_1[1] == 0.840237595667494 assert result_2[1] == 467.53431870130544 assert type(result) == list assert type(result_1) == tuple assert type(result_2) == tuple assert type(result_1[1]) == float assert type(result_2[1]) == float
def test_inconsistent_typical_range_stations(): stations = build_station_list() x = inconsistent_typical_range_stations(stations) assert type(x) == list assert len(x) > 0 for item in x: assert type(item) == str
def run(): '''Requrements for Task 1D''' # Builds Stations List stations = build_station_list() # Compiles a list of rivers. prints the length and first ten riversWithStation = rivers_with_station(stations) print(len(riversWithStation)) sortedList = sorted(riversWithStation) print(sortedList[:10]) # Compiles a list of stations sorted by river and prints a list of stations on three rivers in particular stationsByRiver = stations_by_river(stations) riverAire = list() riverCam = list() riverThames = list() for station in stationsByRiver['River Aire']: riverAire.append(station.name) riverAire.sort() for station in stationsByRiver['River Cam']: riverCam.append(station.name) riverCam.sort() for station in stationsByRiver['River Thames']: riverThames.append(station.name) riverThames.sort() print(riverAire, '\n', riverCam, '\n', riverThames)
def run(): #build station list stations = build_station_list() #creates sorted set of rivers monitored rivers_monitored = rivers_with_station(stations) #prints the first 10 entries in the set print("\n", "First 10 stations with Monitoring Stations:") for i in range(10): print(rivers_monitored[i]) #generates dictionary stations_by_river_dict = stations_by_river(stations) #prints dictionary entries for 3 rivers print("\n", "Stations on the River Aire:") riveraire = stations_by_river_dict['River Aire'] for i in riveraire: print(i) print("\n", "Stations on the River Cam:") rivercam = stations_by_river_dict['River Cam'] for i in rivercam: print(i) print("\n", "Stations on the Thames:") thames = stations_by_river_dict['Thames'] for i in thames: print(i)
def run(): # Build list of stations stations = build_station_list() update_water_levels(stations) list_of_rel_moving_average = issuing_warning(stations) print(list_of_rel_moving_average)
def test_geo_3(): stations = build_station_list() rivers_with_station_list = sorted(rivers_with_station(stations)) assert rivers_with_station_list[0] == 'Addlestone Bourne' for i in range(100, 200): assert rivers_with_station_list[i] != rivers_with_station_list[i + 1]
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()
def run(): """Requirements for Task 1B""" cam_coord = (52.2053, 0.1218) #coordinates of Cambridge city centre #build station list stations = build_station_list() #calculate the distances to coordinates distances_to_cam = stations_by_distance(stations, cam_coord) #10 closest will be the first 10 items in the list close10 = distances_to_cam[:10] #reverse the order and take the first 10 items of the re-orderd list distances_to_cam = sorted_by_key(distances_to_cam, 1, reverse=True) far10 = distances_to_cam[:10] #format outputs close10formatted = [] far10formatted = [] for i in close10: close10formatted.append((i[0].name, i[0].town, i[1])) for i in far10: far10formatted.append((i[0].name, i[0].town, i[1])) print("Closest 10 stations to Cambride:") for i in close10formatted: print(i) print("") print("Furthest 10 stations from Cambridge:") for i in far10formatted: print(i)
def run(): #builds a list of stations stations = stationdata.build_station_list(use_cache=True) #builds a list of rivers that have at least one monitoring station rivers = geo.rivers_with_station(stations) #builds a dictionary of rivers matched with their monitoring stations rivers_stations = geo.stations_by_river(stations) #counts the number of rivers that have at least one monitoring station number_rivers = len(rivers) print(number_rivers) #orders the list of rivers with monitoring stations alphabetically order_rivers = sorted(rivers) #creates an empty list rivers_first = list() #adds the first 10 rivers alphabetically to the empty list for i in range(0, 10): rivers_first.append(order_rivers[i]) print(rivers_first) #returns the monitoring stations for River Aire in alphabetical order river_aire = rivers_stations["River Aire"] print("RIVER AIRE:", sorted(river_aire)) #returns the monitoring stations for River Cam in alphabetical order river_cam = rivers_stations["River Cam"] print("RIVER CAM:", sorted(river_cam)) #returns the monitoring stations for River Thames in alphabetical order river_thames = rivers_stations["River Thames"] print("RIVER THAMES", sorted(river_thames))
def test_plot(): # Build list of stations stations = build_station_list() # Find 5 stations at which the current level is the highest stations_highest_rel_level_list = [] stations_highest_rel_level_list_b = [] 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]) # Do the same again for 6 stations for i in range(len(stations_highest_rel_level(stations, N + 1))): stations_highest_rel_level_list_b.append( stations_highest_rel_level(stations, N + 1)[i][0]) # Check these stations have the highest water level relative_level_one = None relative_level_two = None for station in stations: if station.name == stations_highest_rel_level_list[4]: relative_level_one = station.relative_water_level() if station.name == stations_highest_rel_level_list_b[5]: relative_level_two = station.relative_water_level() else: pass assert relative_level_one > relative_level_two
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)
def run(): # Build list of stations stations = build_station_list() update_water_levels(stations) stations_over_limit = stations_level_over_threshold(stations, 0.8) for station in stations_over_limit: print(station)
def run(): """Requirement for Task 2G""" #Initialise variables data = build_station_list() risk = assess_risk(data[:50]) print(risk)
def run(): # Build list of stations stations = build_station_list() update_water_levels(stations) output_list = stations_highest_rel_level(stations, 10) print(output_list)
def run(): """Requirement for extension""" #Build station list data = build_station_list() #Run present_on_map(data)
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])
def test_stations_within_radius(): # Build list of stations stations = build_station_list() centre=(52.2053, 0.1218) r=10 x=stations_within_radius(stations, centre, r) assert len(x)==10
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
def run(): #Test sample of some station stations = build_station_list() # Running the function to get a list called result result = stations_within_radius(stations, (52.2053, 0.1218), 10) # Printing the result print(result)
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 test_inconsistent_typical_range_stations(): """Checks for inconsistencies in the reported typical ranges of the monitoring stations""" stations = build_station_list() x = inconsistent_typical_range_stations(stations) y = sorted(x) assert 'Addlestone' in y
def run(): """Requrement for Task 1D""" stations = build_station_list() rivers=rivers_with_station(stations) def test_first_x_rivers_with_station(x): """Calls the <<rivers_with_station>> function and prints the total number of rivers and the first x rivers with a station""" #Prints the total number of rivers print("Total rivers with stations: {}\n".format(str(len(rivers)))) #Converts the set into a sorted list rivers_in_order=sorted(rivers) #Creates empty list of first x rivers test_rivers=[] #Adds first x rivers to list for river in rivers_in_order[:x]: test_rivers.append(river) print("First {} rivers with stations: {}\n".format(x, test_rivers)) def test_stations_by_river(rivers): """Calls the <<stations_by_river>> function and prints a list of stations for each river in a list""" stationsbyriver=stations_by_river(stations) for river in rivers: #Calls list of stations on a river test_stations=stationsbyriver.get(river) #Sorts list of stations stations_in_order=sorted(test_stations) print("Stations on {}: {}\n".format(river, stations_in_order)) test_first_x_rivers_with_station(10) test_stations_by_river(["River Aire", "River Cam", "Thames"])
def test_geo_4(): stations = build_station_list() stations_by_river_dict = stations_by_river(stations) assert stations_by_river_dict['River Wallington'] == [ 'Denmead', 'North Fareham weir' ]
def test_rivers_with_station(): # Build list of stations stations = build_station_list() ans = geo.rivers_with_station(stations) assert len(ans) == len(set(ans)) # All elements should be unique
def run(): """Requirements for Task 1D""" print("*** Task 1D: CUED Part IA Flood Warning System ***") #Build list of stations stations = build_station_list() rivers_with_station_list = rivers_with_station(stations) # Count number of unique rivers print("Number of unique rivers:") print(len(rivers_with_station_list)) #List the first ten alphabetically print("First ten rivers alphabetically:") print(rivers_with_station_list[:10]) #List the stations on the specified rivers stations_by_river_dict = stations_by_river(stations) print("Stations on the River Aire:") print(stations_by_river_dict['River Aire']) print("Stations on the River Cam:") print(stations_by_river_dict['River Cam']) print("Stations on the River Thames:") print(stations_by_river_dict['River Thames'])
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
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(): # Build list of stations stations = build_station_list() update_water_levels(stations) output_list = stations_level_over_threshold(stations, 0.8) print(output_list)
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 test_rivers_with_station(): stations = build_station_list() stations = stations[15:35] rivers = rivers_with_station(stations) assert len(rivers) == 19 assert isinstance(rivers, set) == True