Example #1
0
def augment_weather_instance(weather, datetime_, ipp):
    start_date = datetime_.date()
    end_date = start_date + datetime.timedelta(1)
    fields = ['surfaceTemperatureCelsius', 'windSpeedKph',
              'precipitationPreviousHourCentimeters', 'downwardSolarRadiation']

    history = wsi.fetch_history(lat=ipp.latitude, long=ipp.longitude,
                                startDate=start_date, endDate=end_date,
                                fields=fields)

    series = history['weatherData']['hourly']['hours']

    scrub = lambda sequence: filter(lambda item: item is not None, sequence)
    collect = lambda field: map(lambda hourly: hourly.get(field, None), series)

    temperatures = list(scrub(collect('surfaceTemperatureCelsius')))
    if len(temperatures) > 0:
        if weather.low_temp is None:
            weather.low_temp = min(temperatures)
        if weather.high_temp is None:
            weather.high_temp = max(temperatures)

    wind_speeds = list(scrub(collect('windSpeedKph')))
    if len(wind_speeds) > 0 and weather.wind_speed is None:
        weather.wind_speed = round(sum(wind_speeds)/len(wind_speeds), 3)

    solar_radiation = list(scrub(collect('downwardSolarRadiation')))
    if len(solar_radiation) > 0 and weather.solar_radiation is None:
        mean = sum(solar_radiation)/len(solar_radiation)
        weather.solar_radiation = round(mean, 3)

    snow, rain = 0, 0
    for hourly in series:
        temperature = hourly.get('surfaceTemperatureCelsius', None)
        prcp = hourly.get('precipitationPreviousHourCentimeters', None)

        if temperature is not None and prcp is not None:
            if temperature <= Weather.WATER_FREEZING_POINT:
                snow += prcp
            else:
                rain += prcp

    if weather.snow is None:
        weather.snow = round(snow, 3)
    if weather.rain is None:
        weather.rain = round(rain, 3)
Example #2
0
def augment_weather_instance(weather, datetime_, ipp):
    """
    Pull historical weather data from the online WSI database and attempt to
    fill in the `Weather` instance's missing fields.

    The data pulled are for the first day of the incident in hourly intervals.
    Wind speed and downward solar radiation are averaged and rounded to three
    decimal places. To determine the total snowfall and rainfall over the day,
    we check the temperature at each hour. If the surface temperature is less
    than zero degrees Celsius, we add the amount of precipitation for that hour
    to the total amount of snowfall. Otherwise, we consider the precipitation
    as rainfall.

    Arguments:
        weather: An instance of the `Weather` database model with at least one
                 missing field.
        datetime_: The date and time of the incident (named with the trailing
                   underscore to avoid clashing with the `datetime` module).
        ipp: The initial planning point (that is, coordinates) of the incident,
             represented as a `Point` instance.
    """
    start_date = datetime_.date()
    end_date = start_date + datetime.timedelta(1)
    fields = ['surfaceTemperatureCelsius', 'windSpeedKph',
              'precipitationPreviousHourCentimeters', 'downwardSolarRadiation']

    history = wsi.fetch_history(lat=ipp.latitude, long=ipp.longitude,
                                startDate=start_date, endDate=end_date,
                                fields=fields)

    series = history['weatherData']['hourly']['hours']

    scrub = lambda sequence: filter(lambda item: item is not None, sequence)
    collect = lambda field: map(lambda hourly: hourly.get(field, None), series)

    temperatures = list(scrub(collect('surfaceTemperatureCelsius')))
    if len(temperatures) > 0:
        if weather.low_temp is None:
            weather.low_temp = min(temperatures)
        if weather.high_temp is None:
            weather.high_temp = max(temperatures)

    wind_speeds = list(scrub(collect('windSpeedKph')))
    if len(wind_speeds) > 0 and weather.wind_speed is None:
        weather.wind_speed = round(sum(wind_speeds)/len(wind_speeds), 3)

    solar_radiation = list(scrub(collect('downwardSolarRadiation')))
    if len(solar_radiation) > 0 and weather.solar_radiation is None:
        mean = sum(solar_radiation)/len(solar_radiation)
        weather.solar_radiation = round(mean, 3)

    snow, rain = 0, 0
    for hourly in series:
        temperature = hourly.get('surfaceTemperatureCelsius', None)
        prcp = hourly.get('precipitationPreviousHourCentimeters', None)

        if temperature is not None and prcp is not None:
            if temperature <= Weather.WATER_FREEZING_POINT:
                snow += prcp
            else:
                rain += prcp

    if weather.snow is None:
        weather.snow = round(snow, 3)
    if weather.rain is None:
        weather.rain = round(rain, 3)