def create_yearly_statistics(fromDate, toDate, siteId): """ Create yearly stat :param fromDate: start date of the calculation :param toDate: end date of the calculation :param siteId: site :return: None """ logger = logging.getLogger(__name__) logger.error("create yearly stat from {} to {}".format(fromDate, toDate)) fromDate = fromDate.replace(month=1, day=1, hour=0, minute=0, second=0) toDate = toDate.replace(month=12, day=31, hour=23, minute=59, second=59) f = fromDate while f < toDate: manual_data_set = RawManualData.objects.filter(siteId=siteId).filter(year=f.year) start_of_current_year = datetime.datetime(year=f.year, month=1, day=1, hour=0, minute=0) start_of_next_year = datetime.datetime(year=f.year+1, month=1, day=1) automatic_data_set = RawData.objects.filter(siteId=siteId).filter( createdDate__range=(start_of_current_year, start_of_next_year)) if len(manual_data_set) or len(automatic_data_set): d, created = YearlyStatistics.objects.update_or_create(siteId=siteId, year=f.year) significants = {} for day in manual_data_set: significants = Climate.count_significants(significants, day.weatherCode) d.significants = significants d.save() f = f.replace(year=f.year + 1) logger.error("create yearly stat finished")
def test_valid(self): for p in self.parametrize: with self.subTest(): significant = Climate.count_significants(significant={}, daily=p[0]) for k in significant.keys(): expected_value = p[1].get(k, 0) self.assertEqual(significant[k], expected_value) self.assertIsNotNone(Weather.get_weather_code_text(k))
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)