Skip to content

Commit

Permalink
Wind speed and wind speed informed variables use cffdrs (#3123)
Browse files Browse the repository at this point in the history
  • Loading branch information
conbrad authored Sep 25, 2023
1 parent 71be9dc commit c0e3e49
Show file tree
Hide file tree
Showing 105 changed files with 2,178 additions and 978 deletions.
3 changes: 3 additions & 0 deletions .sonarcloud.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ sonar.exclusions=**/Makefile, **/*.Dockerfile, api/app/data/**, api/alembic/vers
sonar.test.exclusions=*.feature
sonar.tests.inclusions=**/*.test.tsx

# Exclude duplication in fba tests due to many similar calculation numbers
sonar.cpd.exclusions=api/app/tests/fba_calc/*.py

# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
33 changes: 33 additions & 0 deletions api/app/fire_behaviour/wind_speed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@


from app.fire_behaviour import cffdrs
from app.schemas.fba_calc import StationRequest, WindResult

"""
If user has not specified wind speed, use the values retrieved from WFWX, always re-calculate FFMC & ISI
"""


def calculate_wind_speed_result(requested_station: StationRequest, yesterday: dict, raw_daily: dict) -> WindResult:
# extract variable from wf1 that we need to calculate the fire behaviour advisory.
bui = cffdrs.bui_calc(raw_daily.get('duffMoistureCode', None), raw_daily.get('droughtCode', None))
temperature = raw_daily.get('temperature', None)
relative_humidity = raw_daily.get('relativeHumidity', None)
precipitation = raw_daily.get('precipitation', None)

wind_speed = raw_daily.get('windSpeed', None)
status = raw_daily.get('recordType').get('id')

if requested_station.wind_speed is not None:
wind_speed = requested_station.wind_speed
status = 'ADJUSTED'

ffmc = cffdrs.fine_fuel_moisture_code(
yesterday.get('fineFuelMoistureCode', None),
temperature,
relative_humidity,
precipitation,
wind_speed)
isi = cffdrs.initial_spread_index(ffmc, wind_speed)
fwi = cffdrs.fire_weather_index(isi, bui)
return WindResult(ffmc=ffmc, isi=isi, bui=bui, wind_speed=wind_speed, fwi=fwi, status=status)
38 changes: 8 additions & 30 deletions api/app/routers/fba_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from app.auth import authentication_required, audit
from app.fire_behaviour.advisory import (FBACalculatorWeatherStation, FireBehaviourAdvisory,
calculate_fire_behaviour_advisory)
from app.fire_behaviour.wind_speed import calculate_wind_speed_result
from app.hourlies import get_hourly_readings_in_time_interval
from app.schemas.fba_calc import (StationListRequest, StationRequest,
StationsListResponse, StationResponse)
from app.fire_behaviour import cffdrs
from app.utils.time import get_hour_20_from_date
from app.wildfire_one.schema_parsers import WFWXWeatherStation
from app.wildfire_one.wfwx_api import (get_auth_header,
Expand Down Expand Up @@ -102,41 +102,19 @@ async def process_request(
last_observed_morning_rh_values = build_hourly_rh_dict(raw_observations.values)

# extract variable from wf1 that we need to calculate the fire behaviour advisory.
bui = raw_daily.get('buildUpIndex', None)
temperature = raw_daily.get('temperature', None)
relative_humidity = raw_daily.get('relativeHumidity', None)
precipitation = raw_daily.get('precipitation', None)
wind_direction = raw_daily.get('windDirection', None)
# if user has not specified wind speed as part of StationRequest, use the
# values retrieved from WFWX in raw_daily
if requested_station.wind_speed is None:
ffmc = raw_daily.get('fineFuelMoistureCode', None)
isi = raw_daily.get('initialSpreadIndex', None)
wind_speed = raw_daily.get('windSpeed', None)
fwi = raw_daily.get('fireWeatherIndex', None)
status = raw_daily.get('recordType').get('id')
# if user has specified wind speed as part of StationRequest, will need to
# re-calculate FFMC & ISI with modified value of wind speed
else:
wind_speed = requested_station.wind_speed
status = 'ADJUSTED'

ffmc = cffdrs.fine_fuel_moisture_code(
yesterday.get('fineFuelMoistureCode', None),
temperature,
relative_humidity,
precipitation,
wind_speed)
isi = cffdrs.initial_spread_index(ffmc, wind_speed)
fwi = cffdrs.fire_weather_index(isi, bui)
wind_result = calculate_wind_speed_result(requested_station, yesterday, raw_daily)

# Prepare the inputs for the fire behaviour advisory calculation.
# This is a combination of inputs from the front end, information about the station from wf1
# and the daily values (observations/forecasts).
fba_station = FBACalculatorWeatherStation(
elevation=wfwx_station.elevation,
fuel_type=requested_station.fuel_type,
status=status,
status=wind_result.status,
time_of_interest=time_of_interest,
percentage_conifer=requested_station.percentage_conifer,
percentage_dead_balsam_fir=requested_station.percentage_dead_balsam_fir,
Expand All @@ -145,12 +123,12 @@ async def process_request(
crown_fuel_load=requested_station.crown_fuel_load,
lat=wfwx_station.lat,
long=wfwx_station.long,
bui=bui,
ffmc=ffmc,
isi=isi,
fwi=fwi,
bui=wind_result.bui,
ffmc=wind_result.ffmc,
isi=wind_result.isi,
fwi=wind_result.fwi,
prev_day_daily_ffmc=yesterday.get('fineFuelMoistureCode', None),
wind_speed=wind_speed,
wind_speed=wind_result.wind_speed,
wind_direction=wind_direction,
temperature=temperature,
relative_humidity=relative_humidity,
Expand Down
9 changes: 9 additions & 0 deletions api/app/schemas/fba_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,12 @@ class StationsListResponse(BaseModel):
""" Response for all weather stations, in a list """
date: date
stations: List[StationResponse]


class WindResult(BaseModel):
ffmc: float
bui: float
isi: float
wind_speed: float
fwi: float
status: str
12 changes: 0 additions & 12 deletions api/app/tests/fba_calc/c1_request.json

This file was deleted.

11 changes: 0 additions & 11 deletions api/app/tests/fba_calc/c1_request_forecast.json

This file was deleted.

23 changes: 0 additions & 23 deletions api/app/tests/fba_calc/c1_request_multiple.json

This file was deleted.

11 changes: 0 additions & 11 deletions api/app/tests/fba_calc/c1_request_no_daily_data.json

This file was deleted.

13 changes: 0 additions & 13 deletions api/app/tests/fba_calc/c1_request_ws_override.json

This file was deleted.

35 changes: 0 additions & 35 deletions api/app/tests/fba_calc/c1_response.json

This file was deleted.

35 changes: 0 additions & 35 deletions api/app/tests/fba_calc/c1_response_forecast.json

This file was deleted.

101 changes: 0 additions & 101 deletions api/app/tests/fba_calc/c1_response_multiple.json

This file was deleted.

Loading

0 comments on commit c0e3e49

Please sign in to comment.