108 lines
3.5 KiB
Python
108 lines
3.5 KiB
Python
from django.db import models
|
|
|
|
|
|
class WeatherParameter(models.Model):
|
|
"""
|
|
تعریف پارامترهای هواشناسی (مثلاً دما، بارش، تبخیر-تعرق، ...).
|
|
"""
|
|
|
|
code = models.CharField(
|
|
max_length=64,
|
|
unique=True,
|
|
db_index=True,
|
|
help_text="کد یکتا (مثلاً temperature_max)",
|
|
)
|
|
name_fa = models.CharField(max_length=128, help_text="نام فارسی")
|
|
unit = models.CharField(max_length=32, blank=True, help_text="واحد اندازهگیری")
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
class Meta:
|
|
ordering = ["code"]
|
|
verbose_name = "پارامتر هواشناسی"
|
|
verbose_name_plural = "پارامترهای هواشناسی"
|
|
|
|
def __str__(self):
|
|
return f"{self.code} ({self.name_fa})"
|
|
|
|
|
|
class WeatherForecast(models.Model):
|
|
"""
|
|
پیشبینی هواشناسی روزانه (تا ۷ روز آینده) برای یک SoilLocation.
|
|
دادهها شامل دما، بارش، رطوبت، باد و تبخیر-تعرق مرجع (ET0) هستند.
|
|
"""
|
|
|
|
location = models.ForeignKey(
|
|
"location_data.SoilLocation",
|
|
on_delete=models.CASCADE,
|
|
related_name="weather_forecasts",
|
|
help_text="موقعیت مکانی مرتبط از جدول SoilLocation",
|
|
)
|
|
forecast_date = models.DateField(
|
|
db_index=True,
|
|
help_text="تاریخ پیشبینی",
|
|
)
|
|
|
|
temperature_min = models.FloatField(
|
|
null=True, blank=True, help_text="حداقل دمای هوا (°C)"
|
|
)
|
|
temperature_max = models.FloatField(
|
|
null=True, blank=True, help_text="حداکثر دمای هوا (°C)"
|
|
)
|
|
temperature_mean = models.FloatField(
|
|
null=True, blank=True, help_text="میانگین دمای هوا (°C)"
|
|
)
|
|
|
|
precipitation = models.FloatField(
|
|
null=True, blank=True, help_text="مجموع بارش (mm)"
|
|
)
|
|
precipitation_probability = models.FloatField(
|
|
null=True, blank=True, help_text="احتمال بارش (%)"
|
|
)
|
|
|
|
humidity_mean = models.FloatField(
|
|
null=True, blank=True, help_text="میانگین رطوبت نسبی (%)"
|
|
)
|
|
|
|
wind_speed_max = models.FloatField(
|
|
null=True, blank=True, help_text="حداکثر سرعت باد (km/h)"
|
|
)
|
|
|
|
et0 = models.FloatField(
|
|
null=True,
|
|
blank=True,
|
|
help_text="تبخیر-تعرق مرجع (ET₀) — mm/day",
|
|
)
|
|
|
|
weather_code = models.IntegerField(
|
|
null=True,
|
|
blank=True,
|
|
help_text="کد وضعیت آبوهوا (WMO code)",
|
|
)
|
|
|
|
fetched_at = models.DateTimeField(
|
|
auto_now=True,
|
|
help_text="آخرین زمان واکشی از API",
|
|
)
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
class Meta:
|
|
constraints = [
|
|
models.UniqueConstraint(
|
|
fields=["location", "forecast_date"],
|
|
name="weather_unique_location_date",
|
|
)
|
|
]
|
|
ordering = ["location", "forecast_date"]
|
|
verbose_name = "پیشبینی هواشناسی"
|
|
verbose_name_plural = "پیشبینیهای هواشناسی"
|
|
|
|
def __str__(self):
|
|
return f"WeatherForecast({self.location_id}, {self.forecast_date})"
|
|
|
|
@property
|
|
def will_rain(self):
|
|
"""آیا بارندگی پیشبینی شده است؟"""
|
|
if self.precipitation is not None:
|
|
return self.precipitation > 0.0
|
|
return None
|