138 lines
3.9 KiB
Python
138 lines
3.9 KiB
Python
|
|
# Seed migration: populate sample 7-day weather forecasts for existing SoilLocations.
|
||
|
|
|
||
|
|
from datetime import timedelta
|
||
|
|
|
||
|
|
from django.db import migrations
|
||
|
|
from django.utils import timezone
|
||
|
|
|
||
|
|
|
||
|
|
SAMPLE_DAILY_DATA = [
|
||
|
|
{
|
||
|
|
"day_offset": 0,
|
||
|
|
"temperature_min": 18.5,
|
||
|
|
"temperature_max": 33.2,
|
||
|
|
"temperature_mean": 25.8,
|
||
|
|
"precipitation": 0.0,
|
||
|
|
"precipitation_probability": 5.0,
|
||
|
|
"humidity_mean": 28.0,
|
||
|
|
"wind_speed_max": 12.0,
|
||
|
|
"et0": 6.8,
|
||
|
|
"weather_code": 0,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"day_offset": 1,
|
||
|
|
"temperature_min": 19.0,
|
||
|
|
"temperature_max": 34.5,
|
||
|
|
"temperature_mean": 26.7,
|
||
|
|
"precipitation": 0.0,
|
||
|
|
"precipitation_probability": 10.0,
|
||
|
|
"humidity_mean": 30.0,
|
||
|
|
"wind_speed_max": 14.0,
|
||
|
|
"et0": 7.1,
|
||
|
|
"weather_code": 1,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"day_offset": 2,
|
||
|
|
"temperature_min": 20.2,
|
||
|
|
"temperature_max": 32.0,
|
||
|
|
"temperature_mean": 26.1,
|
||
|
|
"precipitation": 3.5,
|
||
|
|
"precipitation_probability": 65.0,
|
||
|
|
"humidity_mean": 52.0,
|
||
|
|
"wind_speed_max": 18.0,
|
||
|
|
"et0": 5.2,
|
||
|
|
"weather_code": 61,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"day_offset": 3,
|
||
|
|
"temperature_min": 17.8,
|
||
|
|
"temperature_max": 28.5,
|
||
|
|
"temperature_mean": 23.1,
|
||
|
|
"precipitation": 12.0,
|
||
|
|
"precipitation_probability": 85.0,
|
||
|
|
"humidity_mean": 70.0,
|
||
|
|
"wind_speed_max": 22.0,
|
||
|
|
"et0": 3.8,
|
||
|
|
"weather_code": 63,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"day_offset": 4,
|
||
|
|
"temperature_min": 16.5,
|
||
|
|
"temperature_max": 27.0,
|
||
|
|
"temperature_mean": 21.7,
|
||
|
|
"precipitation": 5.0,
|
||
|
|
"precipitation_probability": 55.0,
|
||
|
|
"humidity_mean": 60.0,
|
||
|
|
"wind_speed_max": 16.0,
|
||
|
|
"et0": 4.5,
|
||
|
|
"weather_code": 61,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"day_offset": 5,
|
||
|
|
"temperature_min": 18.0,
|
||
|
|
"temperature_max": 31.0,
|
||
|
|
"temperature_mean": 24.5,
|
||
|
|
"precipitation": 0.0,
|
||
|
|
"precipitation_probability": 8.0,
|
||
|
|
"humidity_mean": 35.0,
|
||
|
|
"wind_speed_max": 10.0,
|
||
|
|
"et0": 6.2,
|
||
|
|
"weather_code": 2,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"day_offset": 6,
|
||
|
|
"temperature_min": 19.5,
|
||
|
|
"temperature_max": 34.0,
|
||
|
|
"temperature_mean": 26.7,
|
||
|
|
"precipitation": 0.0,
|
||
|
|
"precipitation_probability": 3.0,
|
||
|
|
"humidity_mean": 25.0,
|
||
|
|
"wind_speed_max": 8.0,
|
||
|
|
"et0": 7.0,
|
||
|
|
"weather_code": 0,
|
||
|
|
},
|
||
|
|
]
|
||
|
|
|
||
|
|
|
||
|
|
def seed_forecasts(apps, schema_editor):
|
||
|
|
SoilLocation = apps.get_model("location_data", "SoilLocation")
|
||
|
|
WeatherForecast = apps.get_model("weather", "WeatherForecast")
|
||
|
|
|
||
|
|
today = timezone.now().date()
|
||
|
|
|
||
|
|
for location in SoilLocation.objects.all():
|
||
|
|
for daily in SAMPLE_DAILY_DATA:
|
||
|
|
forecast_date = today + timedelta(days=daily["day_offset"])
|
||
|
|
WeatherForecast.objects.get_or_create(
|
||
|
|
location=location,
|
||
|
|
forecast_date=forecast_date,
|
||
|
|
defaults={
|
||
|
|
"temperature_min": daily["temperature_min"],
|
||
|
|
"temperature_max": daily["temperature_max"],
|
||
|
|
"temperature_mean": daily["temperature_mean"],
|
||
|
|
"precipitation": daily["precipitation"],
|
||
|
|
"precipitation_probability": daily["precipitation_probability"],
|
||
|
|
"humidity_mean": daily["humidity_mean"],
|
||
|
|
"wind_speed_max": daily["wind_speed_max"],
|
||
|
|
"et0": daily["et0"],
|
||
|
|
"weather_code": daily["weather_code"],
|
||
|
|
},
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def unseed_forecasts(apps, schema_editor):
|
||
|
|
WeatherForecast = apps.get_model("weather", "WeatherForecast")
|
||
|
|
WeatherForecast.objects.all().delete()
|
||
|
|
|
||
|
|
|
||
|
|
class Migration(migrations.Migration):
|
||
|
|
|
||
|
|
dependencies = [
|
||
|
|
("weather", "0002_seed_weather_parameters"),
|
||
|
|
("location_data", "0002_soildepthdata_refactor"),
|
||
|
|
]
|
||
|
|
|
||
|
|
operations = [
|
||
|
|
migrations.RunPython(seed_forecasts, unseed_forecasts),
|
||
|
|
]
|