def calculate_climate_index_days(self): """ | Calculate climate index days :return: {'frostDays': fd, 'winterDays': wd, 'coldDays': cd, 'warmNights': wn, 'summerDays': sd, 'warmDays': wwd, 'hotDays': hd} """ return ({ 'frostDays': Climate.get_nr_frost_days(self.tempMins), 'winterDays': Climate.get_nr_winter_days(self.tempMaxs), 'coldDays': Climate.get_nr_cold_days(self.tempMins), 'warmNights': Climate.get_nr_warm_nights(self.tempMins), 'summerDays': Climate.get_nr_summer_days(self.tempMins), 'warmDays': Climate.get_nr_warm_days(self.tempMins), 'hotDays': Climate.get_nr_hot_days(self.tempMins) })
def create_monthly_statistics(fromDate, toDate, siteId): """ | Create monthly stat :param fromDate: start date of calculation :param toDate: end date of the calculation :param siteId: site :return: None """ logger = logging.getLogger(__name__) logger.error("create monthly stat from {} to {}".format(fromDate, toDate)) fromDate = fromDate.replace(hour=0, minute=0, second=0, day=1) if (toDate.month < 12): toDate = toDate.replace(month=toDate.month + 1, day=1, hour=0, minute=0, second=0) else: toDate = toDate.replace(year=toDate.year + 1, month=1, day=1, hour=0, minute=0, second=0) f = fromDate while f < toDate: rawDataSet = DailyStatistics.objects.filter(year=f.year, month=f.month).filter(siteId=siteId) if rawDataSet.count(): d, created = MonthlyStatistics.objects.update_or_create(siteId=siteId, month=f.month, year=f.year) d.dataAvailable = rawDataSet.count() tempmins = [] tempmaxs = [] tempavgs = [] precipitation = decimal.Decimal(0.0) for j in rawDataSet: if j.tempMin is not None: tempmins.append(j.tempMin) if j.tempMax is not None: tempmaxs.append(j.tempMax) if j.tempAvg is not None: tempavgs.append(j.tempAvg) if j.precipitation is not None: precipitation = precipitation + j.precipitation d.dataAvailable = rawDataSet.count() if len(tempmins) > 0: d.tempMin = min(tempmins) d.tempMinAvg = sum(tempmins) / len(tempmins) if len(tempmaxs) > 0: d.tempMax = max(tempmaxs) d.tempMaxAvg = sum(tempmaxs) / len(tempmaxs) if len(tempavgs) > 0: d.tempAvg = sum(tempavgs) / len(tempavgs) start_of_current_month = datetime.datetime( year=f.year, month=f.month, day=1, hour=0, minute=0) start_of_next_month = start_of_current_month + relativedelta(months=1) rawDataSet = RawData.objects\ .filter(createdDate__range=(start_of_current_month, start_of_next_month))\ .filter(siteId=siteId) temps = [] rhs = [] winds = [] for j in rawDataSet: if j.temperature is not None: temps.append(j.temperature) if j.humidity is not None: rhs.append(j.humidity) if j.windDir is not None: winds.append(j.windDir) tempDistribution = Climate.calculate_temperature_distribution(temps) d.tempDistribution = ''.join(str(e) + ',' for e in tempDistribution)[:-1] rhDistribution = Climate.calculate_rh_distribution(rhs) d.rhDistribution = ''.join(str(e) + ',' for e in rhDistribution)[:-1] windDistribution = Climate.calculate_wind_distribution(winds) d.windDistribution = ''.join(str(e) + ',' for e in windDistribution)[:-1] d.precipitation = precipitation d.summerDays = Climate.get_nr_summer_days(tempmaxs) d.frostDays = Climate.get_nr_frost_days(tempmins) d.winterDays = Climate.get_nr_winter_days(tempmaxs) d.coldDays = Climate.get_nr_cold_days(tempmins) d.warmNights = Climate.get_nr_warm_nights(tempmins) d.warmDays = Climate.get_nr_warm_days(tempmaxs) d.hotDays = Climate.get_nr_hot_days(tempmaxs) manualDataSet = RawManualData.objects.filter(siteId=siteId).filter(year=f.year).filter(month=f.month) significants = {} for day in manualDataSet: significants = Climate.count_significants(significants, day.weatherCode) d.significants = significants d.save() if (f.month == 12): f = f.replace(year=f.year + 1, month=1) else: f = f.replace(month=f.month + 1)
def test_winter_days_around(self): self.assertEqual(Climate.get_nr_winter_days([-0.1, 0, 0.1]), 2)
def test_winter_days_below(self): self.assertEqual(Climate.get_nr_winter_days([-0.1, -10, -99]), 3)
def test_winter_days_above(self): self.assertEqual(Climate.get_nr_winter_days([0.1, 3, 99]), 0)
def test_winter_days_empty(self): self.assertEqual(Climate.get_nr_winter_days([]), 0)
def test_winter_days_with_null(self): self.assertEqual(Climate.get_nr_winter_days([None, 1, 2]), 0)
def test_winter_days_only_null(self): self.assertEqual(Climate.get_nr_winter_days([None, None]), 0)