import flood_adapt.objects.forcing as f
from pathlib import Path
from datetime import datetime
from flood_adapt import unit_system as us
from flood_adapt import FloodAdapt, Settings
from flood_adapt.objects import HistoricalEvent
from flood_adapt.objects.forcing.time_frame import TimeFrame
# Setup FloodAdapt
= Path("../../_data/examples/static-data").resolve()
STATIC_DATA_DIR = Settings(
settings =Path("../../_data/examples").resolve(),
DATABASE_ROOT="charleston_test",
DATABASE_NAME
)= FloodAdapt(database_path=settings.database_path) fa
💾 Event modifications in the database
This notebook demonstrates how to manipulate event objects in a FloodAdapt database, including loading, creating, editing, copying, and deleting events.
It is assumed that at the start of this notebook, you have a database and the events saved from the other event notebooks.
Analyze the effect of different rainfall forcings
Lets say we want to analyze the effect of rainfall, how would you approach that:
- Retrieve an event from the database
- Create the rainfall forcings you want to analyze
- Update the event’s name and rainfall forcings
- Save the updated event to the database
- Create scenarios using these events and run them (see this example) TODO
Of course, this approach also works for analyzing the difference between any other forcings.
# 1. Create an event or load it from the database
# event = fa.get_event(...)
="database_manipulation_event"
event_name= HistoricalEvent(
event =event_name,
name="Some event description",
description=TimeFrame(
time=datetime(2020, 1, 1),
start_time=datetime(2020, 1, 2),
end_time
),={
forcings
f.ForcingType.WATERLEVEL: [f.WaterlevelGauged()]
}
)=event)
fa.save_event(event
# 2. Create rainfall forcings
= f.RainfallConstant(
rainfall_constant =us.UnitfulIntensity(value=10, units=us.UnitTypesIntensity.mm_hr)
intensity
)
= f.RainfallSynthetic(
rainfall_synthetic =f.GaussianTimeseries(
timeseries=us.UnitfulTime(value=12, units=us.UnitTypesTime.hours),
duration# Choose the middle of the time frame for peak time
=us.UnitfulTime.from_timedelta(event.time.duration / 2),
peak_time=us.UnitfulIntensity(value=10, units=us.UnitTypesIntensity.mm_hr),
peak_value
)
)
= f.RainfallCSV(path=STATIC_DATA_DIR / "rainfall.csv")
rainfall_csv
= [
rainfall_forcings "constant"),
(rainfall_constant, "synthetic"),
(rainfall_synthetic, "csv")
(rainfall_csv,
]
for forcing, fsource in rainfall_forcings:
# 3. Update the event
= [forcing]
event.forcings[f.ForcingType.RAINFALL] = f"{event_name}_rainfall_{fsource}"
event.name
# 4. Save the event
=event)
fa.save_event(event
"name"]
fa.get_events()[
# 5. Now the events can be used as the components for scenario creation. Similar to how the rainfall forcings are used to create various events
# For more information on how to create scenarios, see the scenario creation example
['database_manipulation_event',
'database_manipulation_event_rainfall_constant',
'database_manipulation_event_rainfall_csv',
'database_manipulation_event_rainfall_synthetic',
'event_set',
'test_set']
Delete, Save, Copy, Edit
FloodAdapt supports deleting, editing, and copying objects by name, as shown below.
It is possible to achieve the exact same goal in multiple ways:
event = fa.get_event(event_name)
-> updateevent
->fa.delete_event(event_name)
->fa.save_event(event)
event = fa.get_event(event_name)
-> updateevent
->fa.edit_event(event)
print("Initial:", fa.get_events()["name"])
# Copy all events
for name in fa.get_events()["name"]:
= f"{name}_copy"
new_name =name, new_name=new_name, new_description=f"Copy of {name}")
fa.copy_event(old_nameprint("Names after `Copy`:", fa.get_events()["name"])
# Delete all copied events
for name in fa.get_events()["name"]:
if "copy" in name:
fa.delete_event(name)print("Names after `Delete`:", fa.get_events()["name"])
# Edit an existing event
= fa.get_event(event_name)
unedited_event print("Description before `Edit`:", unedited_event.description)
= f"Updated description"
unedited_event.description =unedited_event, overwrite=True)
fa.save_event(event
= fa.get_event(event_name)
edited_event print("Description after `Edit`:", edited_event.description)
Initial: ['database_manipulation_event', 'database_manipulation_event_rainfall_constant', 'database_manipulation_event_rainfall_csv', 'database_manipulation_event_rainfall_synthetic', 'event_set', 'test_set']
Names after `Copy`: ['database_manipulation_event', 'database_manipulation_event_copy', 'database_manipulation_event_rainfall_constant', 'database_manipulation_event_rainfall_constant_copy', 'database_manipulation_event_rainfall_csv', 'database_manipulation_event_rainfall_csv_copy', 'database_manipulation_event_rainfall_synthetic', 'database_manipulation_event_rainfall_synthetic_copy', 'event_set', 'event_set_copy', 'test_set', 'test_set_copy']
Names after `Delete`: ['database_manipulation_event', 'database_manipulation_event_rainfall_constant', 'database_manipulation_event_rainfall_csv', 'database_manipulation_event_rainfall_synthetic', 'event_set', 'test_set']
Description before `Edit`: Some event description
Description after `Edit`: Updated description