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 run(): stations = build_station_list() update_water_levels(stations) bad_stations = stations_level_over_threshold(stations, 0.8) for i in bad_stations: print(i[0].name, i[1])
def run(): # Build list of stations stations = build_station_list() update_water_levels(stations) risky_stations = stations_highest_rel_level(stations, 10) for station in risky_stations: print(station.name, station.relative_water_level())
def run(): """Requirement for Task 2E""" #Initialise variables data = build_station_list() update_water_levels(data) ls = [] ID = [] #Number of days in past taken data from dt = 7 #How many graphs per window limit = 4 #How many stations number = 6 #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_levels(ID, dt, limit, s)
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 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(): #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()
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 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(): # 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(): # 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 stations_highest_rel_level(stations, N): '''This function creates a sorted list of all stations in order of relative water level it then returns the highest N values from that list''' if N >= len(stations): l = [("list is shorter than range", "soz")] return l else: update_water_levels(stations) l = [] for i in stations: x = i.relative_water_level() name = str(i.name) if x != 0: if i.typical_range_consistent() == True: if i.latest_level is not None: y = (name, x) l.append(y) l = sorted_by_key(l, 1, reverse=True) highest_rel_range = [] for i in range(N): highest_rel_range.append(l[i]) return highest_rel_range
def run(): stations = build_station_list() update_water_levels(stations) deliverables = stations_level_over_threshold(stations, tol=0.8) for a in deliverables: print(a[0].name)
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(): #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()
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(): # 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 Task 2B""" stations = build_station_list() update_water_levels(stations) tuplist = stations_level_over_threshold(stations, 0.8) for i in tuplist: print(i[0].name, " :", i[1])
def run(): # Build list of stations stations = build_station_list() update_water_levels(stations) # Station names with the current relative level of 0.8 for s in stations_level_over_threshold(stations, 0.9): print("{} {}".format(s[0].name, s[1]))
def run(): """ Requirement for Task 1C""" stations = build_station_list() update_water_levels(stations) dangerous_stations = stations_highest_rel_level(stations, 10) for i in dangerous_stations: print(i.name, " :", i.relative_water_level())
def test_towns_most_at_risk_returns_N_towns(): exptected_length = 5 stations = build_station_list()[:exptected_length * 5] update_water_levels(stations) towns = towns_most_at_risk(stations, exptected_length) assert len(towns) == exptected_length
def run(): stations = build_station_list() update_water_levels(stations) N_stations_at_risk = stations_highest_rel_level( stations, 10) #create a list of N stations at risk for a in N_stations_at_risk: print(a[0].name + ' ' + str(a[1])) #print name and station level
def test_stations_level_over_thresholds(): stations=build_station_list() update_water_levels(stations) stations = stations[:10] #Make sure no none types here stations_over = stations_level_over_threshold(stations,-1000) assert len(stations_over) == len(stations) station_over = stations_level_over_threshold(stations,1000) assert len(station_over) == 0
def run(): #Requirements for Task2B #we build a list of stations stations = build_station_list() #update them with the latest water levels update_water_levels(stations) #a list of stations for which the current relative level is over 0.8 a= stations_level_over_threshold(stations, 0.8) print(a)
def test_towns_most_at_risk_returns_towns_sorted_by_risk_level(): exptected_length = 5 stations = build_station_list()[:exptected_length * 5] update_water_levels(stations) towns = towns_most_at_risk(stations, exptected_length) for i in range(exptected_length - 1): assert towns[i][2] >= towns[i + 1][2]
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(): stations = build_station_list() update_water_levels(stations) first_N_stations = stations_highest_rel_level(stations, 10) for i in range(len(first_N_stations)): print(first_N_stations[i][0].name, first_N_stations[i][1])
def run(): """Requirements for task 2G""" # a severity of moderate includes all currently active flood warnings # severity.low includes warnings which were in force in the past 24 hours stations = build_station_list() update_water_levels(stations) severity = SeverityLevel.moderate print("Building warning list of severity {}...".format(severity.value)) warnings = build_warning_list(severity.value) if len(warnings) == 0: print("No warnings of this severity") return print("Simplifying geometry...") simplification_params = get_recommended_simplification_params(len(warnings)) for warning in warnings: # if the simplification parameters used for the cached file were # incorrect, resimplify the geometry if warning.is_poly_simplified != simplification_params: print('resimplifying') warning.simplify_geojson(tol=simplification_params['tol'], buf=simplification_params['buf']) warning.is_poly_simplified = simplification_params print("Making datasets...") geojson = build_regions_geojson(warnings) df = build_severity_dataframe(warnings) df2 = build_station_dataframe(stations) # we want the most severe warnings first - given that the list will be long sorted_warnings = FloodWarning.order_warning_list_with_severity(warnings) for warning in sorted_warnings: print(warning) print("\n") print("\n") print("Mapping warnings...") map_flood_warnings(geojson, warning_df=df, min_severity=severity.value, station_df=df2) print("Checking for warnings in Jesus College, Cambridge ...") jc_coords = (52.20527, 0.120705) warnings_here = FloodWarning.check_warnings_at_location(warnings, jc_coords) if len(warnings_here) == 0: print("No flood warnings in this location") else: print("The following warnings apply to this location") for warning in warnings_here: print(warning) print("\n") print("Saving caches...") update_poly_area_caches(warnings)
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) """if got time, try using polynomial p = 4 for n in stations_with_values_for_water_levels: #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) #extrapolate the polynomial to see if it passes high or severe in 1 day""" Low_risk = [] Moderate_risk = [] High_risk = [] Severe_risk = [] for n in stations_with_values_for_water_levels: if n[0] < 0.25: Low_risk.append((n[0], n[1])) elif 0.25 <= n[0] < 0.9: Moderate_risk.append((n[0], n[1])) elif 0.9 <= n[0] < 1.5: High_risk.append((n[0], n[1])) elif n[0] >= 1.5: Severe_risk.append((n[0], n[1])) Low_risk.sort(reverse=True) Moderate_risk.sort(reverse=True) High_risk.sort(reverse=True) Severe_risk.sort(reverse=True) #print ("Low risk") #print (Low_risk[:5]) #print ("Moderate risk") #print (Moderate_risk[:5]) print("High risk") print(High_risk[:5]) print("Severe risk") print(Severe_risk[:5])
def test_stations_highest_rel_level(): stations = build_station_list() update_water_levels(stations) Aurimas = MonitoringStation(1234, 1234, "Aurimas", (1234, 1234), (0.5, 1), "Sarva", "Vilnius", 1997 - 11 - 12, 1234) Aurimas.latest_level = 3 stations.append(Aurimas) x = stations_highest_rel_level(stations, 10) assert Aurimas in x