Ejemplo n.º 1
0
def test_calculate_mean_intensity_round_down():
    """ Calculates mean intensity and rounds result down because decimal is below x.8 """
    daily1 = StationDaily(code=1, date=datetime.now(), intensity_group=1)
    daily2 = StationDaily(code=2, date=datetime.now(), intensity_group=3)
    daily3 = StationDaily(code=3, date=datetime.now(), intensity_group=4)
    # mean is 2.66666667
    result = calculate_mean_intensity([daily1, daily2, daily3])
    assert result == 2
Ejemplo n.º 2
0
def test_calculate_mean_intensity_round_up():
    """ Calculates mean intensity and rounds result up because decimal is at x.8 """
    daily1 = StationDaily(code=1, date=datetime.now(), intensity_group=2)
    daily2 = StationDaily(code=2, date=datetime.now(), intensity_group=4)
    daily3 = StationDaily(code=3, date=datetime.now(), intensity_group=4)
    daily4 = StationDaily(code=4, date=datetime.now(), intensity_group=4)
    daily5 = StationDaily(code=5, date=datetime.now(), intensity_group=5)
    # mean is 3.8
    result = calculate_mean_intensity([daily1, daily2, daily3, daily4, daily5])
    assert result == 4
Ejemplo n.º 3
0
def test_calculate_mean_intensity_basic():
    """ Calculates mean intensity """
    daily1 = StationDaily(
        code=1,
        date=datetime.now(),
        intensity_group=1
    )

    daily2 = StationDaily(
        code=2,
        date=datetime.now(),
        intensity_group=1
    )
    result = calculate_mean_intensity([daily1, daily2])
    assert result == 1
Ejemplo n.º 4
0
def test_valid_daily():
    """ Daily missing any required field is invalid """
    base_daily = StationDaily(
        code=1,
        date=datetime.now(),
        temperature=1,
        relative_humidity=1,
        wind_speed=1,
        wind_direction=1,
        precipitation=1,
        intensity_group=1
    )
    for field in required_daily_fields:
        daily = StationDaily(**base_daily.__dict__)
        setattr(daily, field, None)
        result = validate_station_daily(daily)
        assert result.valid == False
Ejemplo n.º 5
0
def test_valid_daily():
    """ Daily with all required fields is valid """
    daily = StationDaily(
        code=1,
        date=datetime.now(),
        temperature=1,
        relative_humidity=1,
        wind_speed=1,
        wind_direction=1,
        precipitation=1,
        intensity_group=1
    )
    result = validate_station_daily(daily)
    assert result.valid == True
Ejemplo n.º 6
0
def test_requested_fire_starts_unaltered(mocker: MockerFixture):
    """ Fire starts from user request remain unchanged """

    start_date = datetime.now()
    daily = StationDaily(
        code=1,
        date=start_date,
        intensity_group=1
    )

    station = hfi_calc_models.PlanningWeatherStation(id=1, planning_area_id=1, station_code=1)

    result = calculate_hfi_results(planning_area_fire_starts={
        kamloops_fc.planning_areas[0].id: [highest_fire_starts]},
        dailies=[daily],
        num_prep_days=5,
        selected_station_codes=[1, 2],
        area_station_map={kamloops_fc.planning_areas[0].id: [station]},
        start_date=start_date)
    assert result[0].daily_results[0].fire_starts == highest_fire_starts
Ejemplo n.º 7
0
def test_calculate_mean_intensity_perfect_divisor():
    """ Calculates mean intensity, shouldn't need to round """
    daily1 = StationDaily(code=1, date=datetime.now(), intensity_group=2)
    daily2 = StationDaily(code=2, date=datetime.now(), intensity_group=2)
    result = calculate_mean_intensity([daily1, daily2])
    assert result == 2
Ejemplo n.º 8
0
def generate_station_daily(raw_daily,  # pylint: disable=too-many-locals
                           station: WFWXWeatherStation,
                           fuel_type: FuelType) -> StationDaily:
    """ Transform from the raw daily json object returned by wf1, to our daily object.
    """
    # pylint: disable=invalid-name
    # we use the fuel type lookup to get default values.
    pc = fuel_type.percentage_conifer if fuel_type.percentage_conifer is not None\
        else FUEL_TYPE_DEFAULTS[fuel_type.fuel_type_code]["PC"]
    pdf = fuel_type.percentage_dead_fir if fuel_type.percentage_dead_fir is not None\
        else FUEL_TYPE_DEFAULTS[fuel_type.fuel_type_code]["PDF"]
    cbh = FUEL_TYPE_DEFAULTS[fuel_type.fuel_type_code]["CBH"]
    cfl = FUEL_TYPE_DEFAULTS[fuel_type.fuel_type_code]["CFL"]
    date = raw_daily.get('weatherTimestamp', None)
    isi = raw_daily.get('initialSpreadIndex', None)
    bui = raw_daily.get('buildUpIndex', None)
    ffmc = raw_daily.get('fineFuelMoistureCode', None)
    cc = raw_daily.get('grasslandCuring', None)
    wind_speed = raw_daily.get('windSpeed', None)

    try:
        fire_behaviour_prediction = calculate_fire_behaviour_prediction(
            latitude=station.lat,
            longitude=station.long,
            elevation=station.elevation,
            fuel_type=FuelTypeEnum[fuel_type.fuel_type_code],
            bui=bui,
            ffmc=ffmc,
            wind_speed=wind_speed,
            cc=cc,
            pc=pc,
            isi=isi,
            pdf=pdf,
            cbh=cbh,
            cfl=cfl)
    # pylint: disable=broad-except
    except Exception as exc:
        logger.error('Encountered error while generating StationDaily for station %s', station.code)
        logger.error(exc, exc_info=True)
        # prediction calculation failed, so we set the values to None
        fire_behaviour_prediction = FireBehaviourPrediction(None, None, None, None, None)

    return StationDaily(
        code=station.code,
        date=date,
        status=raw_daily.get('recordType', '').get('id', None),
        temperature=raw_daily.get('temperature', None),
        relative_humidity=raw_daily.get('relativeHumidity', None),
        wind_speed=raw_daily.get('windSpeed', None),
        wind_direction=raw_daily.get('windDirection', None),
        precipitation=raw_daily.get('precipitation', None),
        grass_cure_percentage=raw_daily.get('grasslandCuring', None),
        ffmc=ffmc,
        dmc=raw_daily.get('duffMoistureCode', None),
        dc=raw_daily.get('droughtCode', None),
        fwi=raw_daily.get('fireWeatherIndex', None),
        danger_class=raw_daily.get('dailySeverityRating', None),
        isi=isi,
        bui=bui,
        rate_of_spread=fire_behaviour_prediction.ros,
        hfi=fire_behaviour_prediction.hfi,
        observation_valid=raw_daily.get('observationValidInd', None),
        observation_valid_comment=raw_daily.get(
            'observationValidComment', None),
        intensity_group=fire_behaviour_prediction.intensity_group,
        sixty_minute_fire_size=fire_behaviour_prediction.sixty_minute_fire_size,
        fire_type=fire_behaviour_prediction.fire_type,
        error=raw_daily.get('observationValidInd', None),
        error_message=raw_daily.get('observationValidComment', None),
        last_updated=datetime.fromtimestamp(raw_daily.get(
            'lastEntityUpdateTimestamp') / 1000, tz=timezone.utc)
    )