Skip to main content

Making it easier to navigate and clean station data

Project description

<title>Python: module __init__</title>
 
 
__init__
index
c:\users\austin\desktop\agent\packages\filter_stations\filter_stations\__init__.py

 
Modules
       
matplotlib.animation
argparse
base64
datetime
dateutil
folium
gc
haversine
json
math
numpy
os
pandas
matplotlib.pyplot
requests
urllib

 
Classes
       
builtins.object
retreive_data
Filter
Interactive_maps

 
class Filter(retreive_data)
    Filter(apiKey, apiSecret)
 
# Move the functions to a class
 
 
Method resolution order:
Filter
retreive_data
builtins.object

Methods defined here:
__init__(self, apiKey, apiSecret)
Initialize self.  See help(type(self)) for accurate signature.
calculate_new_point(self, lat, lon, distance, bearing)
Calculate a new point given a starting point, distance, and bearing.
 
:param lat: starting latitude in degrees
:param lon: starting longitude in degrees
:param distance: distance to move in meters
:param bearing: bearing to move in degrees (0 is north)
:return: tuple containing the new latitude and longitude in degrees
compute_filter(self, lat, lon, distance)
Calculates the bounding box coordinates for a given location and distance.
 
Parameters:
lat (float): The latitude of the location.
lon (float): The longitude of the location.
distance (float): The distance from the location, in kilometers, to the edge of the bounding box.
 
Returns:
A tuple containing four floats representing the bounding box coordinates: (min_lat, min_lon, max_lat, max_lon).
filterStations(self, address, distance, startDate=None, endDate=None, csvfile='KEcheck3.csv')
This method filters weather station data within a certain distance from a given address.
 
Parameters:
address (str): Address to center the bounding box around.
distance (float): The distance (in kilometers) from the center to the edge of the bounding box.
startDate (str): The start date for filtering the weather station data in the format 'YYYY-MM-DD'.
endDate (str): The end date for filtering the weather station data in the format 'YYYY-MM-DD'.
csvfile (str): The name of the csv file containing the weather station data.
 
Returns:
pandas.DataFrame: The filtered weather station data within the bounding box.
filterStationsList(self, address, distance=100)
Filters stations based on their proximity to a given address and returns a list of station codes that fall within the specified distance.
 
Args:
    address (str): Address to filter stations by.
    distance (float, optional): Maximum distance (in kilometers) between the stations and the address. Default is 100 km.
 
Returns:
    List of station codes that fall within the specified distance from the given address.
getLocation(self, address)
This method retrieves the latitude and longitude coordinates of a given address using the Nominatim API.
 
Parameters:
-----------
address : str
    The address of the location you want to retrieve the coordinates for.
    
Returns:
--------
Tuple (float, float)
    The latitude and longitude coordinates of the location.
get_stations_info(self, station=None, multipleStations=[], countrycode=None)
Retrieves information about weather stations from an API endpoint and returns relevant information based on the parameters passed to it.
 
Args:
    station (str, optional): Code for a single station to retrieve information for. Defaults to None.
    multipleStations (list, optional): List of station codes to retrieve information for multiple stations. Defaults to [].
    countrycode (str, optional): Country code to retrieve information for all stations located in the country. Defaults to None.
 
Returns:
    pandas.DataFrame: DataFrame containing information about the requested weather stations.

Methods inherited from retreive_data:
aggregate_variables(self, dataframe)
Aggregates a pandas DataFrame of weather variables by summing values across each day.
 
Args:
    dataframe (pandas.DataFrame): DataFrame containing weather variable data.
 
Returns:
    pandas.DataFrame: DataFrame containing aggregated weather variable data, summed by day.
get_measurements(self, station, startDate=None, endDate=None, variables=None, dataset='controlled', aggregate=False)
Get measurements for a specified station and time period.
 
:param station: The station ID for which to retrieve measurements.
:type station: str
:param startDate: The start date of the time period for which to retrieve measurements.
:type startDate: datetime or str, optional
:param endDate: The end date of the time period for which to retrieve measurements.
:type endDate: datetime or str, optional
:param variables: A list of variable shortcodes to retrieve measurements for. If None, all variables are retrieved.
:type variables: list or None, optional
:param dataset: The dataset to retrieve measurements from. Default is 'controlled'.
:type dataset: str, optional
:param aggregate: Whether to aggregate variables by sensor ID. Default is False.
:type aggregate: bool, optional
:return: A Pandas DataFrame containing the requested measurements.
:rtype: pandas.DataFrame
get_variables(self)
Retrieves information about available weather variables from an API endpoint.
 
Returns:
    dict: Dictionary containing information about available weather variables, keyed by variable shortcode.
k_neighbours(self, station, number=5)
Returns a dictionary of the nearest neighbouring stations to the specified station.
 
Args:
    station (str): Code for the station to find neighbouring stations for.
    number (int, optional): Number of neighbouring stations to return. Defaults to 5.
 
Returns:
    dict: Dictionary containing the station codes and distances of the nearest neighbouring stations.
multiple_measurements(self, stations_list, csv_file, startDate, endDate, variables, dataset='controlled')
Retrieves measurements for multiple stations and saves the aggregated data to a CSV file.
 
Parameters:
    stations_list (list): A list of strings containing the names of the stations to retrieve data from.
    csv_file (str): The name of the CSV file to save the data to.
    startDate (str): The start date for the measurements, in the format 'yyyy-mm-dd'.
    endDate (str): The end date for the measurements, in the format 'yyyy-mm-dd'.
    variables (list): A list of strings containing the names of the variables to retrieve.
    dataset (str): The name of the dataset to retrieve the data from. Default is 'controlled'.
 
Returns:
    df (pandas.DataFrame): A DataFrame containing the aggregated data for all stations.
 
Raises:
    ValueError: If stations_list is not a list.
raw_measurements(self, station, startDate=None, endDate=None, variables=None)

Data descriptors inherited from retreive_data:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Interactive_maps(retreive_data)
    Interactive_maps(apiKey, apiSecret)
 
# A different class for visualisations
 
 
Method resolution order:
Interactive_maps
retreive_data
builtins.object

Methods defined here:
__init__(self, apiKey, apiSecret)
Initialize self.  See help(type(self)) for accurate signature.
create_animation(self, data, valid_sensors, day=100, T=10, interval=500)
Creates an animation of pollutant levels for a given range of days and valid sensors.
 
Parameters:
data (DataFrame): A pandas DataFrame containing pollution data.
valid_sensors (list): A list of valid sensor names.
day (int): The starting day of the animation (default is 100).
T (int): The range of days for the animation (default is 10).
interval (int): The interval between frames in milliseconds (default is 500).
 
Returns:
HTML: An HTML object containing the animation.
draw_map(self, map_center)
Creates a Folium map centered on the specified location and adds markers for each weather station in the area.
 
Args:
    map_center: a tuple with the latitude and longitude of the center of the map
 
Returns:
    A Folium map object
encode_image(self, ws, df_rainfall)
Encodes a station's rainfall data plot as a base64-encoded image.
 
Args:
- ws (str): the code for the station to encode the image for
- df_rainfall (pandas.DataFrame): a DataFrame containing the rainfall data for all stations
 
Returns:
- str: a string containing an HTML image tag with the encoded image data, or a message indicating no data is available for the given station
get_map(self, subset_list, start_date=None, end_date=None, data_values=False, csv_file='KEcheck3.csv', min_zoom=8, max_zoom=11, width=850, height=850, png_resolution=300)
Creates a Folium map showing the locations of the weather stations in the given subsets.
 
Parameters:
-----------
subset_list : list of lists of str
    List of subsets of weather stations, where each subset is a list of station codes.
start_date : str, optional
    Start date in the format YYYY-MM-DD, default is None.
end_date : str, optional
    End date in the format YYYY-MM-DD, default is None.
data_values : bool, optional
    If True, the map markers will display a plot of rainfall data, default is False.
csv_file : str, optional
    The name of the CSV file containing the rainfall data, default is 'KEcheck3.csv'.
min_zoom : int, optional
    The minimum zoom level of the map, default is 8.
max_zoom : int, optional
    The maximum zoom level of the map, default is 11.
width : int, optional
    The width of the map in pixels, default is 850.
height : int, optional
    The height of the map in pixels, default is 850.
png_resolution : int, optional
    The resolution of the PNG image if data_values is True, default is 300.
 
Returns:
--------
my_map : folium.folium.Map
    A Folium map object showing the locations of the weather stations in the given subsets.
plot_station(self, ws, df_rainfall)
Plot the rainfall data for a specific weather station.
 
Args:
- ws: string, the code of the weather station to plot
- df_rainfall: DataFrame, a pandas DataFrame with rainfall data
 
Returns:
- None if no data is available for the specified station
- a Matplotlib figure showing rainfall data for the specified station otherwise

Methods inherited from retreive_data:
aggregate_variables(self, dataframe)
Aggregates a pandas DataFrame of weather variables by summing values across each day.
 
Args:
    dataframe (pandas.DataFrame): DataFrame containing weather variable data.
 
Returns:
    pandas.DataFrame: DataFrame containing aggregated weather variable data, summed by day.
get_measurements(self, station, startDate=None, endDate=None, variables=None, dataset='controlled', aggregate=False)
Get measurements for a specified station and time period.
 
:param station: The station ID for which to retrieve measurements.
:type station: str
:param startDate: The start date of the time period for which to retrieve measurements.
:type startDate: datetime or str, optional
:param endDate: The end date of the time period for which to retrieve measurements.
:type endDate: datetime or str, optional
:param variables: A list of variable shortcodes to retrieve measurements for. If None, all variables are retrieved.
:type variables: list or None, optional
:param dataset: The dataset to retrieve measurements from. Default is 'controlled'.
:type dataset: str, optional
:param aggregate: Whether to aggregate variables by sensor ID. Default is False.
:type aggregate: bool, optional
:return: A Pandas DataFrame containing the requested measurements.
:rtype: pandas.DataFrame
get_stations_info(self, station=None, multipleStations=[], countrycode=None)
Retrieves information about weather stations from an API endpoint and returns relevant information based on the parameters passed to it.
 
Args:
    station (str, optional): Code for a single station to retrieve information for. Defaults to None.
    multipleStations (list, optional): List of station codes to retrieve information for multiple stations. Defaults to [].
    countrycode (str, optional): Country code to retrieve information for all stations located in the country. Defaults to None.
 
Returns:
    pandas.DataFrame: DataFrame containing information about the requested weather stations.
get_variables(self)
Retrieves information about available weather variables from an API endpoint.
 
Returns:
    dict: Dictionary containing information about available weather variables, keyed by variable shortcode.
k_neighbours(self, station, number=5)
Returns a dictionary of the nearest neighbouring stations to the specified station.
 
Args:
    station (str): Code for the station to find neighbouring stations for.
    number (int, optional): Number of neighbouring stations to return. Defaults to 5.
 
Returns:
    dict: Dictionary containing the station codes and distances of the nearest neighbouring stations.
multiple_measurements(self, stations_list, csv_file, startDate, endDate, variables, dataset='controlled')
Retrieves measurements for multiple stations and saves the aggregated data to a CSV file.
 
Parameters:
    stations_list (list): A list of strings containing the names of the stations to retrieve data from.
    csv_file (str): The name of the CSV file to save the data to.
    startDate (str): The start date for the measurements, in the format 'yyyy-mm-dd'.
    endDate (str): The end date for the measurements, in the format 'yyyy-mm-dd'.
    variables (list): A list of strings containing the names of the variables to retrieve.
    dataset (str): The name of the dataset to retrieve the data from. Default is 'controlled'.
 
Returns:
    df (pandas.DataFrame): A DataFrame containing the aggregated data for all stations.
 
Raises:
    ValueError: If stations_list is not a list.
raw_measurements(self, station, startDate=None, endDate=None, variables=None)

Data descriptors inherited from retreive_data:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class retreive_data(builtins.object)
    retreive_data(apiKey, apiSecret)
 
# Get data class
 
  Methods defined here:
__init__(self, apiKey, apiSecret)
Initialize self.  See help(type(self)) for accurate signature.
aggregate_variables(self, dataframe)
Aggregates a pandas DataFrame of weather variables by summing values across each day.
 
Args:
    dataframe (pandas.DataFrame): DataFrame containing weather variable data.
 
Returns:
    pandas.DataFrame: DataFrame containing aggregated weather variable data, summed by day.
get_measurements(self, station, startDate=None, endDate=None, variables=None, dataset='controlled', aggregate=False)
Get measurements for a specified station and time period.
 
:param station: The station ID for which to retrieve measurements.
:type station: str
:param startDate: The start date of the time period for which to retrieve measurements.
:type startDate: datetime or str, optional
:param endDate: The end date of the time period for which to retrieve measurements.
:type endDate: datetime or str, optional
:param variables: A list of variable shortcodes to retrieve measurements for. If None, all variables are retrieved.
:type variables: list or None, optional
:param dataset: The dataset to retrieve measurements from. Default is 'controlled'.
:type dataset: str, optional
:param aggregate: Whether to aggregate variables by sensor ID. Default is False.
:type aggregate: bool, optional
:return: A Pandas DataFrame containing the requested measurements.
:rtype: pandas.DataFrame
get_stations_info(self, station=None, multipleStations=[], countrycode=None)
Retrieves information about weather stations from an API endpoint and returns relevant information based on the parameters passed to it.
 
Args:
    station (str, optional): Code for a single station to retrieve information for. Defaults to None.
    multipleStations (list, optional): List of station codes to retrieve information for multiple stations. Defaults to [].
    countrycode (str, optional): Country code to retrieve information for all stations located in the country. Defaults to None.
 
Returns:
    pandas.DataFrame: DataFrame containing information about the requested weather stations.
get_variables(self)
Retrieves information about available weather variables from an API endpoint.
 
Returns:
    dict: Dictionary containing information about available weather variables, keyed by variable shortcode.
k_neighbours(self, station, number=5)
Returns a dictionary of the nearest neighbouring stations to the specified station.
 
Args:
    station (str): Code for the station to find neighbouring stations for.
    number (int, optional): Number of neighbouring stations to return. Defaults to 5.
 
Returns:
    dict: Dictionary containing the station codes and distances of the nearest neighbouring stations.
multiple_measurements(self, stations_list, csv_file, startDate, endDate, variables, dataset='controlled')
Retrieves measurements for multiple stations and saves the aggregated data to a CSV file.
 
Parameters:
    stations_list (list): A list of strings containing the names of the stations to retrieve data from.
    csv_file (str): The name of the CSV file to save the data to.
    startDate (str): The start date for the measurements, in the format 'yyyy-mm-dd'.
    endDate (str): The end date for the measurements, in the format 'yyyy-mm-dd'.
    variables (list): A list of strings containing the names of the variables to retrieve.
    dataset (str): The name of the dataset to retrieve the data from. Default is 'controlled'.
 
Returns:
    df (pandas.DataFrame): A DataFrame containing the aggregated data for all stations.
 
Raises:
    ValueError: If stations_list is not a list.
raw_measurements(self, station, startDate=None, endDate=None, variables=None)

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
Functions
       
parse_args()

 
Data
        API_BASE_URL = 'https://datahub.tahmo.org'
API_MAX_PERIOD = '365D'
endpoints = {'DATA_COMPLETE': 'custom/sensordx/latestmeasurements', 'STATION_INFO': 'services/assets/v2/stations', 'STATION_STATUS': 'custom/stations/status', 'VARIABLES': 'services/assets/v2/variables', 'WEATHER_DATA': 'services/measurements/v2/stations'}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

filter_stations-0.3.1.tar.gz (21.9 kB view hashes)

Uploaded Source

Built Distribution

filter_stations-0.3.1-py3-none-any.whl (14.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page