enerdata 1.1.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,702 @@
1
+ # -*- coding: utf-8 -*-
2
+ from __future__ import division
3
+
4
+ import bisect
5
+ import logging
6
+ try:
7
+ from collections import namedtuple, Counter
8
+ except ImportError:
9
+ from backport_collections import namedtuple, Counter
10
+ from datetime import datetime, date, timedelta
11
+ from multiprocessing import Lock
12
+ try:
13
+ from StringIO import StringIO
14
+ except ImportError:
15
+ from io import StringIO
16
+ from dateutil.relativedelta import relativedelta
17
+ from decimal import Decimal
18
+ from six import string_types
19
+
20
+ from ..profiles import Dragger
21
+ from ..contracts.tariff import Tariff, T30A_one_period, T31A_one_period, T31A
22
+ from ..datetime.timezone import TIMEZONE
23
+ from ..metering.measure import Measure, EnergyMeasure
24
+ from ..datetime.solar_hour import convert_to_solar_hour
25
+
26
+ from os import path
27
+ from six import BytesIO
28
+ import pandas as pd
29
+ import bz2
30
+ import csv
31
+ import gzip
32
+ import requests
33
+
34
+ try:
35
+ import httplib
36
+ except ImportError:
37
+ import http.client as httplib
38
+
39
+
40
+ logger = logging.getLogger(__name__)
41
+
42
+
43
+ COEFFS = ['A', 'B', 'C', 'D']
44
+ EXTRA_COEFFS = ['2.0TD', '3.0TD', '3.0TDVE']
45
+
46
+
47
+ def get_tariff_coeffs_list(year, month):
48
+ assert isinstance(year, int)
49
+ assert isinstance(month, int)
50
+ if (year >= 2021 and month >= 6) or year >= 2022: # temporal solution, till REE publish new coeffs for 2022
51
+ return EXTRA_COEFFS
52
+ else:
53
+ return COEFFS
54
+
55
+
56
+ class Coefficent(namedtuple('Coefficient', ['hour', 'cof'])):
57
+ __slots__ = ()
58
+
59
+ def __lt__(self, other):
60
+ return self.hour < other.hour
61
+
62
+ def __le__(self, other):
63
+ return self.hour <= other.hour
64
+
65
+ def __gt__(self, other):
66
+ return self.hour > other.hour
67
+
68
+ def __ge__(self, other):
69
+ return self.hour >= other.hour
70
+
71
+
72
+ class Coefficients(object):
73
+ def __init__(self, coefs=None):
74
+ if coefs is None:
75
+ coefs = []
76
+ assert isinstance(coefs, list)
77
+ self.coefs = list(coefs)
78
+
79
+ def _check_pos(self, pos):
80
+ if pos == len(self.coefs):
81
+ raise ValueError('start date not found in coefficients')
82
+
83
+ def insert_coefs(self, coefs):
84
+ pos_0 = bisect.bisect_left(self.coefs, Coefficent(coefs[0][0], {}))
85
+ pos_1 = bisect.bisect_right(self.coefs, Coefficent(coefs[-1][0], {}))
86
+ logger.debug('Deleting from {start}({pos_0}) to {end}({pos_1})'.format(
87
+ start=coefs[0][0], end=coefs[-1][0], pos_0=pos_0, pos_1=pos_1
88
+ ))
89
+ del self.coefs[pos_0:pos_1]
90
+ for c in reversed(coefs):
91
+ logger.debug('Inserting {c} into {pos_0}'.format(
92
+ c=c, pos_0=pos_0
93
+ ))
94
+ self.coefs.insert(pos_0, c)
95
+
96
+ def get(self, dt):
97
+ assert isinstance(dt, datetime)
98
+ if dt.dst() is None:
99
+ dt = TIMEZONE.localize(dt)
100
+ dt = TIMEZONE.normalize(dt)
101
+ pos = bisect.bisect_left(self.coefs, Coefficent(dt, {}))
102
+ self._check_pos(pos)
103
+ return self.coefs[pos]
104
+
105
+ def get_range(self, start, end):
106
+ assert isinstance(start, date)
107
+ assert isinstance(end, date)
108
+ start = TIMEZONE.localize(datetime(
109
+ start.year, start.month, start.day, 1), is_dst=True
110
+ )
111
+ # Sum one day to get the hour 00:00 of the next day
112
+ end += timedelta(days=1)
113
+ end = TIMEZONE.localize(datetime(
114
+ end.year, end.month, end.day), is_dst=True
115
+ ) + timedelta(seconds=1)
116
+ pos = bisect.bisect_left(self.coefs, Coefficent(start, {}))
117
+ self._check_pos(pos)
118
+ end_pos = bisect.bisect_right(self.coefs, Coefficent(end, {}))
119
+ return self.coefs[pos:end_pos]
120
+
121
+ def get_coefs_by_tariff(self, tariff, start, end):
122
+ assert hasattr(tariff, 'get_period_by_date')
123
+ assert hasattr(tariff, 'energy_periods')
124
+ assert isinstance(start, date)
125
+ assert isinstance(end, date)
126
+ sum_cofs = dict.fromkeys(tariff.energy_periods.keys(), 0)
127
+ for hour, coef in self.get_range(start, end):
128
+ if len(sum_cofs) > 1:
129
+ dt = hour - timedelta(minutes=1)
130
+ period = tariff.get_period_by_date(dt)
131
+ p_name = period.code
132
+ else:
133
+ p_name = sum_cofs.keys()[0]
134
+ sum_cofs[p_name] += coef[tariff.cof]
135
+ return sum_cofs
136
+
137
+
138
+ class Profiler(object):
139
+ def __init__(self, coefficient):
140
+ self.coefficient = coefficient
141
+
142
+ def profile(self, tariff, measures, drag_method='hour'):
143
+ """
144
+ :param tariff:
145
+ :param measures:
146
+ :param drag_method: 'hour' means drag is passed to the next hour
147
+ 'period' means drag is passed to the next hour for
148
+ the same period
149
+ :return:
150
+ """
151
+ # {'PX': [(date(XXXX-XX-XX), 100), (date(XXXX-XX-XX), 110)]}
152
+ _measures = list(measures)
153
+ measures = {}
154
+ for m in sorted(_measures):
155
+ measures.setdefault(m.period.code, [])
156
+ measures[m.period.code].append(m)
157
+ measures_intervals = EnergyMeasure.intervals(_measures)
158
+ # Detect single day profiling case
159
+ if len(measures_intervals) == 1:
160
+ measures_intervals.append(measures_intervals[-1]) # duplicate measure date to do not skip loop below
161
+ logger.debug('Profiling {0} intervals'.format(len(measures_intervals)))
162
+ for idx, measure_date in enumerate(measures_intervals):
163
+ if idx + 1 == len(measures_intervals):
164
+ break
165
+ start = measure_date
166
+ if idx > 0:
167
+ start += timedelta(days=1)
168
+ end = measures_intervals[idx + 1]
169
+ logger.debug('Getting coeffs from {0} to {1}'.format(
170
+ start, end
171
+ ))
172
+ sum_cofs = self.coefficient.get_coefs_by_tariff(tariff, start, end)
173
+ dragger = Dragger()
174
+ for hour, cof in self.coefficient.get_range(start, end):
175
+ dt = hour - timedelta(minutes=1)
176
+ period = tariff.get_period_by_date(dt)
177
+ if drag_method == 'hour':
178
+ dp = 'hour'
179
+ else:
180
+ dp = period.code
181
+ d = hour.date()
182
+ if hour.hour == 0:
183
+ d -= timedelta(days=1)
184
+ # To take the first measure
185
+ if d == start and len(set(measures_intervals)) != 1: # if single day case, do not regress date
186
+ d += timedelta(days=1)
187
+ fake_m = Measure(d, period, 0)
188
+ pos = bisect.bisect_left(measures.get(period.code, []), fake_m)
189
+ pcode = period.code
190
+ if pcode not in measures or pos >= len(measures[period.code]):
191
+ consumption = 0
192
+ consumption_date = None
193
+ else:
194
+ consumption = measures[period.code][pos].consumption
195
+ consumption_date = measures[period.code][pos].date
196
+ logger.debug('Hour: {0} Period: {1} Consumption: {2}'.format(
197
+ hour, period.code, consumption
198
+ ))
199
+ cof = cof[tariff.cof]
200
+ hour_c = ((consumption * cof) / sum_cofs[period.code])
201
+ aprox = dragger.drag(hour_c, key=dp)
202
+ yield (
203
+ hour,
204
+ {
205
+ 'aprox': aprox,
206
+ 'drag': dragger[dp],
207
+ 'consumption': consumption,
208
+ 'consumption_date': consumption_date,
209
+ 'sum_cofs': sum_cofs[period.code],
210
+ 'cof': cof,
211
+ 'period': period.code
212
+ }
213
+ )
214
+
215
+
216
+ class REEProfile(object):
217
+ HOST = 'www.ree.es'
218
+ PATH = '/sites/default/files/simel/perff'
219
+ GISCE_URL = 'https://github.com/gisce/ree_monthly_profiles/blob/main/perff/'
220
+ down_lock = Lock()
221
+
222
+ _CACHE = {}
223
+
224
+ @classmethod
225
+ def get_range(cls, start, end):
226
+ cofs = []
227
+ start = datetime(start.year, start.month, 1)
228
+ end = datetime(end.year, end.month, 1)
229
+ while start <= end:
230
+ logger.debug('Downloading coefficients for {0}/{1}'.format(
231
+ start.month, start.year
232
+ ))
233
+ cofs.extend(REEProfile.get(start.year, start.month))
234
+ start += relativedelta(months=1)
235
+ return cofs
236
+
237
+ @classmethod
238
+ def get(cls, year, month):
239
+ try:
240
+ import ssl
241
+ try:
242
+ _create_unverified_https_context = ssl._create_unverified_context
243
+ except AttributeError:
244
+ pass
245
+ else:
246
+ ssl._create_default_https_context = _create_unverified_https_context
247
+ except ImportError:
248
+ pass
249
+ conn = None
250
+ try:
251
+ cls.down_lock.acquire()
252
+
253
+ key = '%(year)s%(month)02i' % locals()
254
+ if key in cls._CACHE:
255
+ logger.debug('Using CACHE for REEProfile {0}'.format(key))
256
+ return cls._CACHE[key]
257
+ perff_file = 'PERFF_%(key)s.gz' % locals()
258
+ conn = httplib.HTTPSConnection(cls.HOST)
259
+ conn.request('GET', '%s/%s' % (cls.PATH, perff_file))
260
+ logger.debug('Downloading REEProfile from {0}/{1}'.format(
261
+ cls.PATH, perff_file
262
+ ))
263
+ r = conn.getresponse()
264
+ if r.getheader('Content-Type') == 'application/x-gzip':
265
+ content = r.read()
266
+ try:
267
+ c = StringIO(content)
268
+ m = StringIO(gzip.GzipFile(fileobj=c).read())
269
+ except:
270
+ c = BytesIO(content)
271
+ n = BytesIO(gzip.GzipFile(fileobj=c).read())
272
+ content = n.read().decode('iso8859-15')
273
+ m = StringIO(content)
274
+ c.close()
275
+ reader = csv.reader(m, delimiter=';')
276
+ header = True
277
+ cofs = []
278
+ coeffs_list = get_tariff_coeffs_list(year, month)
279
+ for vals in reader:
280
+ if header:
281
+ header = False
282
+ continue
283
+ if int(vals[3]) == 1:
284
+ n_hour = 1
285
+ dt = datetime(
286
+ int(vals[0]), int(vals[1]), int(vals[2])
287
+ )
288
+ day = TIMEZONE.localize(dt, is_dst=bool(not int(vals[4])))
289
+ day += timedelta(hours=n_hour)
290
+ n_hour += 1
291
+ cofs.append(Coefficent(
292
+ TIMEZONE.normalize(day), dict(
293
+ (k, float(vals[i])) for i, k in enumerate(coeffs_list, 5)
294
+ ))
295
+ )
296
+ cls._CACHE[key] = cofs
297
+ return cofs
298
+ else:
299
+ try:
300
+ perff_file = 'PERFF_%(key)s.0.bz2' % locals()
301
+ r = requests.get(cls.GISCE_URL + perff_file, params={'raw': 'true'})
302
+ c = StringIO(r.content)
303
+ m = StringIO(bz2.decompress(c.read()))
304
+ reader = csv.reader(m, delimiter=';')
305
+ header = True
306
+ cofs = []
307
+ coeffs_list = get_tariff_coeffs_list(year, month)
308
+ for vals in reader:
309
+ if header:
310
+ header = False
311
+ continue
312
+ if int(vals[3]) == 1:
313
+ n_hour = 1
314
+ dt = datetime(
315
+ int(vals[0]), int(vals[1]), int(vals[2])
316
+ )
317
+ day = TIMEZONE.localize(dt, is_dst=bool(not int(vals[4])))
318
+ day += timedelta(hours=n_hour)
319
+ n_hour += 1
320
+ cofs.append(Coefficent(
321
+ TIMEZONE.normalize(day), dict(
322
+ (k, float(vals[i])) for i, k in enumerate(coeffs_list, 5)
323
+ ))
324
+ )
325
+ cls._CACHE[key] = cofs
326
+ return cofs
327
+ except:
328
+ raise Exception('Profiles from REE not found')
329
+ finally:
330
+ if conn is not None:
331
+ conn.close()
332
+ cls.down_lock.release()
333
+
334
+
335
+ class REProfile(object):
336
+ translate_month = {
337
+ 1: 'Enero',
338
+ 2: 'Febrero',
339
+ 3: 'Marzo',
340
+ 4: 'Abril',
341
+ 5: 'Mayo',
342
+ 6: 'Junio',
343
+ 7: 'Julio',
344
+ 8: 'Agosto',
345
+ 9: 'Septiembre',
346
+ 10: 'Octubre',
347
+ 11: 'Noviembre',
348
+ 12: 'Diciembre'
349
+ }
350
+
351
+ @classmethod
352
+ def get_range(cls, start, end):
353
+ sheet_name = 'zona_{}'.format(cls.climatic_zone)
354
+ filename = path.join(
355
+ path.dirname(path.realpath(__file__)), 'data/coefficients_RE.xlsx'
356
+ )
357
+ df = pd.read_excel(filename, sheet_name=sheet_name)
358
+ key = df.keys()[0]
359
+ cofs = []
360
+ while start <= end:
361
+ month = cls.translate_month[start.month]
362
+ solar_hour = convert_to_solar_hour(start)
363
+ if solar_hour.hour != 0:
364
+ hour = solar_hour.hour
365
+ else:
366
+ hour = 24
367
+ coff_value = float(df[df[key] == month][hour])
368
+ coff = Coefficent(start, {'A': coff_value})
369
+ cofs.append(coff)
370
+ start += relativedelta(hours=1)
371
+ return cofs
372
+
373
+ @classmethod
374
+ def validate_exported_energy(cls, measures):
375
+ """
376
+ Check if there are ProfileHour with energy != 0 in periods with RE coefficient == 0
377
+ :param measures: A list ProfileHour objects
378
+ :return valid: A boolean that indicates if all the profiles in measures are valid
379
+ :return invalid_profiles: A list containing the invalid profiles in measures
380
+ """
381
+ # Check if measures list is valid
382
+ valid = True
383
+ # Save invalid profiles
384
+ invalid_profiles = []
385
+ # get and iterate RE coefficients
386
+ coefficients = cls.get_range(min(measures).date, max(measures).date)
387
+ # iterate all RE measures
388
+ for measure in measures:
389
+ # if energy value is not zero
390
+ if measure[1] != 0:
391
+ # fetch coeff by profile date
392
+ cof = False
393
+ for c in coefficients:
394
+ if c.hour == measure[0]:
395
+ cof = c
396
+ break
397
+ if cof:
398
+ # if coeff is zero
399
+ if cof.cof['A'] == 0:
400
+ # replace energy value by 'warning'
401
+ invalid_profiles.append(measure)
402
+ # mark measures as invalid
403
+ valid = False
404
+ return valid, invalid_profiles
405
+
406
+
407
+ class REProfileZone1(REProfile):
408
+ climatic_zone = 1
409
+
410
+
411
+ class REProfileZone2(REProfile):
412
+ climatic_zone = 2
413
+
414
+
415
+ class REProfileZone3(REProfile):
416
+ climatic_zone = 3
417
+
418
+
419
+ class REProfileZone4(REProfile):
420
+ climatic_zone = 4
421
+
422
+
423
+ class REProfileZone5(REProfile):
424
+ climatic_zone = 5
425
+
426
+
427
+ class REProfileHydraulic(REProfile):
428
+ @classmethod
429
+ def get_range(cls, start, end):
430
+ filename = path.join(path.dirname(path.realpath(__file__)), 'data/coefficients_HIDRO_RE.csv')
431
+ df = pd.read_csv(filename)
432
+ cofs = []
433
+ while start <= end:
434
+ month = cls.translate_month[start.month]
435
+ coff_value = float(df[df['MES'] == month]['Factor de funcionamiento'])
436
+ coff = Coefficent(start, {'A': coff_value})
437
+ cofs.append(coff)
438
+ start += relativedelta(hours=1)
439
+ return cofs
440
+
441
+
442
+ class REProfileFlat(REProfile):
443
+ flat_cof = 0.85
444
+ @classmethod
445
+ def get_range(cls, start, end):
446
+ cofs = []
447
+ while start <= end:
448
+ cofs.append(Coefficent(start, {'A': cls.flat_cof}))
449
+ start += relativedelta(hours=1)
450
+ return cofs
451
+
452
+
453
+ class ProfileHour(namedtuple('ProfileHour', ['date', 'measure', 'valid', 'accumulated'])):
454
+
455
+ __slots__ = ()
456
+
457
+ def __lt__(self, other):
458
+ return self.date < other.date
459
+
460
+ def __le__(self, other):
461
+ return self.date <= other.date
462
+
463
+ def __gt__(self, other):
464
+ return self.date > other.date
465
+
466
+ def __ge__(self, other):
467
+ return self.date >= other.date
468
+
469
+
470
+ class Profile(object):
471
+ """A Profile object representing hours and consumption.
472
+ """
473
+
474
+ def __init__(self, start, end, measures, accumulated=None, drag_by_periods=True):
475
+ self.measures = measures[:]
476
+ self.gaps = [] # Containing the gaps and invalid measures
477
+ self.adjusted_periods = [] # If a period is adjusted
478
+ self.start_date = start
479
+ self.end_date = end
480
+ self.profile_class = REEProfile
481
+
482
+ assert type(drag_by_periods) == bool, "drag_by_periods must be a Boolean"
483
+ self.drag_by_periods = drag_by_periods
484
+
485
+ self.accumulated = Decimal(0)
486
+ if accumulated:
487
+ assert type(accumulated) == float or isinstance(accumulated, Decimal), "Provided accumulated must be a Decimal or a float"
488
+ assert accumulated < 1 and accumulated > -1, "Provided accumulated '{}' must be -1 < accumulated < 1".format(accumulated)
489
+ self.accumulated = accumulated
490
+
491
+ measures_by_date = dict(
492
+ [(m.date, m.measure) for m in measures if m.valid]
493
+ )
494
+ # End is included
495
+ while start <= end:
496
+ if measures_by_date.pop(TIMEZONE.normalize(start), None) is None:
497
+ self.gaps.append(start)
498
+ start += timedelta(hours=1)
499
+
500
+ @property
501
+ def n_hours(self):
502
+ # End date is included, we have to sum one hour
503
+ return int((self.end_date - self.start_date).total_seconds() / 3600) + 1
504
+
505
+ @property
506
+ def n_hours_measures(self):
507
+ return len(self.measures)
508
+
509
+ @property
510
+ def total_consumption(self):
511
+ return sum(x[1] for x in self.measures)
512
+
513
+ @property
514
+ def first_day_of_month(self):
515
+ return self.end_date.day == 1 and self.end_date.hour > 0
516
+
517
+ @staticmethod
518
+ def simple_dragger(measures):
519
+ dragger = Dragger()
520
+ for idx, measure in enumerate(measures):
521
+ values = measure._asdict()
522
+ consumption = dragger.drag(measure.measure)
523
+ values['measure'] = consumption
524
+ measures[idx] = measure._replace(**values)
525
+ return measures
526
+
527
+ def get_hours_per_period(self, tariff, only_valid=False):
528
+ assert isinstance(tariff, Tariff)
529
+ hours_per_period = Counter()
530
+ if only_valid:
531
+ for m in self.measures:
532
+ if m.valid:
533
+ dt = m.date - timedelta(minutes=1)
534
+ period = tariff.get_period_by_date(dt)
535
+ hours_per_period[period.code] += 1
536
+ else:
537
+ start_idx = self.start_date
538
+ end = self.end_date
539
+ while start_idx <= end:
540
+ dt = start_idx - timedelta(minutes=1)
541
+ period = tariff.get_period_by_date(dt)
542
+ hours_per_period[period.code] += 1
543
+ start_idx += timedelta(hours=1)
544
+ return hours_per_period
545
+
546
+ def get_consumption_per_period(self, tariff):
547
+ assert isinstance(tariff, Tariff)
548
+ consumption_per_period = Counter()
549
+ for period in tariff.energy_periods:
550
+ consumption_per_period[period] = 0
551
+ for m in self.measures:
552
+ if m.valid:
553
+ dt = m.date - timedelta(minutes=1)
554
+ period = tariff.get_period_by_date(dt)
555
+ consumption_per_period[period.code] += m.measure
556
+ return consumption_per_period
557
+
558
+ def get_estimable_hours(self, tariff):
559
+ assert isinstance(tariff, Tariff)
560
+ total_hours = self.get_hours_per_period(tariff)
561
+ valid_hours = self.get_hours_per_period(tariff, only_valid=True)
562
+ estimable_hours = {}
563
+ for period in total_hours.keys():
564
+ estimable_hours[period] = total_hours[period] - valid_hours[period]
565
+ return estimable_hours
566
+
567
+ def get_estimable_consumption(self, tariff, balance):
568
+ assert isinstance(tariff, Tariff)
569
+ consumption_per_period = self.get_consumption_per_period(tariff)
570
+ estimable = {}
571
+ for period in consumption_per_period:
572
+ estimable[period] = balance[period] - consumption_per_period[period]
573
+ return estimable
574
+
575
+ def estimate(self, tariff, balance):
576
+ assert isinstance(tariff, Tariff)
577
+ logger.debug('Estimating for tariff: {0}'.format(tariff.code))
578
+
579
+ # Adapt balance for simplified T30A with just one period
580
+ if isinstance(tariff, T30A_one_period) or isinstance(tariff, T31A_one_period):
581
+ balance = {"P1": sum([values for values in balance.values()])}
582
+ # Adapt T31A6P adding P4 to P1
583
+ if isinstance(tariff, T31A) and balance.get('P4', 0) > 0:
584
+ balance['P1'] += balance['P4']
585
+ balance['P4'] = 0
586
+
587
+ measures = [x for x in self.measures if x.valid]
588
+ start = self.start_date
589
+ end = self.end_date
590
+ # - REE cofs get from (year/month)
591
+ # - Simel cofs get from (year/month/day hour) - can't substract one day
592
+ if self.first_day_of_month or not issubclass(self.profile_class, REEProfile):
593
+ cofs = self.profile_class.get_range(start, end)
594
+ else:
595
+ cofs = self.profile_class.get_range(start, end - relativedelta(days=1))
596
+ cofs = Coefficients(cofs)
597
+ cofs_per_period = Counter()
598
+
599
+ for gap in self.gaps:
600
+ dt = gap - timedelta(minutes=1)
601
+ period = tariff.get_period_by_date(dt)
602
+ gap_cof = cofs.get(dt)
603
+ cofs_per_period[period.code] += gap_cof.cof[tariff.cof]
604
+
605
+ logger.debug('Coefficients per period calculated: {0}'.format(cofs_per_period))
606
+
607
+ energy_per_period = self.get_estimable_consumption(tariff, balance)
608
+ energy_per_period_rem = energy_per_period.copy()
609
+
610
+ dragger = Dragger()
611
+
612
+ # Initialize the Dragger with passed accumulated value
613
+ if len(self.gaps) > 0:
614
+ # Drag by_hours
615
+ if not self.drag_by_periods:
616
+ init_drag_key = "default"
617
+ else:
618
+ dt = self.gaps[0] - timedelta(minutes=1)
619
+ init_drag_key = tariff.get_period_by_date(dt).code
620
+
621
+ dragger.drag(self.accumulated, key=init_drag_key)
622
+
623
+ for idx, gap in enumerate(self.gaps):
624
+ logger.debug('Gap {0}/{1}'.format(idx + 1, len(self.gaps)))
625
+ dt = gap - timedelta(minutes=1)
626
+ period = tariff.get_period_by_date(dt)
627
+
628
+ drag_key = period.code if self.drag_by_periods else "default"
629
+
630
+ gap_cof = cofs.get(gap).cof[tariff.cof]
631
+ energy = energy_per_period[period.code]
632
+ # If the balance[period] < energy_profile[period] fill with 0
633
+ if energy < 0:
634
+ energy = 0
635
+
636
+ try:
637
+ gap_energy = (energy * gap_cof) / cofs_per_period[period.code]
638
+ except ZeroDivisionError as error:
639
+ gap_energy = 0
640
+ logger.debug(error)
641
+
642
+ aprox = dragger.drag(gap_energy, key=drag_key)
643
+ energy_per_period_rem[period.code] -= gap_energy
644
+
645
+ logger.debug(
646
+ 'Energy for hour {0} is {1}. {2} Energy {3}/{4}'.format(
647
+ gap, aprox, period.code,
648
+ energy_per_period_rem[period.code], energy
649
+ ))
650
+ pos = bisect.bisect_left(measures, ProfileHour(gap, 0, True, 0.0))
651
+ profile_hour = ProfileHour(TIMEZONE.normalize(gap), aprox, True, dragger[drag_key])
652
+
653
+ measures.insert(pos, profile_hour)
654
+
655
+ profile = Profile(self.start_date, self.end_date, measures)
656
+ return profile
657
+
658
+ def adjust(self, tariff, balance, diff=0):
659
+ # Adjust values
660
+ if self.gaps:
661
+ raise Exception('Is not possible to adjust a profile with gaps')
662
+ profile = Profile(self.start_date, self.end_date, self.measures)
663
+ dragger = Dragger()
664
+ energy_per_period = profile.get_consumption_per_period(tariff)
665
+ for period_name, period_balance in balance.items():
666
+ period_profile = energy_per_period[period_name]
667
+ margin_bottom = period_balance - diff
668
+ margin_top = period_balance + diff
669
+ if not margin_bottom <= period_profile <= margin_top:
670
+ profile.adjusted_periods.append(period_name)
671
+ for idx, measure in enumerate(profile.measures):
672
+ dt = measure.date - timedelta(minutes=1)
673
+ period = tariff.get_period_by_date(dt).code
674
+ if period != period_name:
675
+ continue
676
+ values = measure._asdict()
677
+ values['valid'] = True
678
+ if not energy_per_period[period]:
679
+ values['measure'] = dragger.drag(measure.measure * 0)
680
+ else:
681
+ values['measure'] = dragger.drag(measure.measure * (
682
+ balance[period] / energy_per_period[period]
683
+ ))
684
+ profile.measures[idx] = measure._replace(**values)
685
+ return profile
686
+
687
+ def fixit(self, tariff, balance, diff=0):
688
+ # Fill the gaps
689
+ profile = self.estimate(tariff, balance)
690
+ # Adjust to the balance
691
+ profile = profile.adjust(tariff, balance, diff)
692
+
693
+ if hasattr(tariff, 'low_voltage_measure') and getattr(tariff, 'low_voltage_measure'):
694
+ # Apply losses on new 6.XTD flag_low and old 3.1A LB
695
+ profile.measures = tariff.apply_curve_losses(profile.measures)
696
+ profile.measures = self.simple_dragger(profile.measures)
697
+ return profile
698
+
699
+ def __repr__(self):
700
+ return '<Profile ({0} - {1}) {2}h {3}kWh>'.format(
701
+ self.start_date, self.end_date, self.n_hours, self.total_consumption
702
+ )