-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathelexon.rb
494 lines (432 loc) · 13.3 KB
/
elexon.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# coding: utf-8
require 'faraday/retry'
require 'faraday/net_http_persistent'
require 'fastest_csv'
require 'chronic'
require 'business_time'
module Elexon
class Base
TZ = TZInfo::Timezone.get('UTC')
@@faraday = Faraday.new do |f|
f.adapter :net_http_persistent
# f.request :retry, {
# retry_statuses: [404, 500, 502, 504],
# interval: 2,
# backoff_factor: 2,
# max: 5
# }
# f.response :logger, Logger.new($stderr)
end
def self.source_id
"elexon"
end
DATE_FORMAT = '%Y-%m-%d'
TIME_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
end
class BaseCSV < Base
def initialize(date_or_io)
if date_or_io.is_a? Date
date = date_or_io
@options = {}
@options[:format] = 'csv'
else
@csv = FastestCSV.read(date_or_io, row_sep: "\r\n")
end
end
def fetch
return if @csv
logger.benchmark_info(@url) do
res = @@faraday.get(@url, @options)
@csv = FastestCSV.parse(res.body, row_sep: "\r\n")
end
end
end
# https://bmrs.elexon.co.uk/api-documentation/endpoint/datasets/FUELINST
class Fuelinst < BaseCSV
include SemanticLogger::Loggable
include Out::Generation
def self.cli(args)
if args.length == 1 && args.first.include?('.')
self.new(args.first).process
elsif args.length < 2
$stderr.puts "#{$0} <from> <to>"
exit 1
else
from = Chronic.parse(args.shift).to_date
to = Chronic.parse(args.shift).to_date
(from...to).each do |time|
e = self.new(time, time + 1.day)
e.process
rescue EmptyError
logger.warn "EmptyError #{time}"
end
end
end
def initialize(from_or_path, to = nil)
super(from_or_path)
@from = from_or_path
@to = to || @from + 1.day
@url = "https://data.elexon.co.uk/bmrs/api/v1/datasets/FUELINST"
@options[:settlementDateFrom] = @from.strftime('%Y-%m-%d')
@options[:settlementDateTo] = @to.strftime('%Y-%m-%d')
end
FUEL_MAP = {
'BIOMASS' => 'biomass',
'CCGT' => 'fossil_gas_ccgt',
'COAL' => 'fossil_hard_coal',
'NPSHYD' => 'hydro',
'NUCLEAR' => 'nuclear',
'OCGT' => 'fossil_gas_ocgt',
'OIL' => 'fossil_oil',
'OTHER' => 'other',
'PS' => 'hydro_pumped_storage',
'WIND' => 'wind'
}
TRAN_MAP = {
'INTELE' => 'FR',
'INTELEC' => 'FR',
'INTEW' => 'IE',
'INTFR' => 'FR',
'INTGRNL' => 'IE',
'INTIFA2' => 'FR',
'INTIRL' => 'IE',
'INTNED' => 'NL',
'INTNEM' => 'BE',
'INTNSL' => 'NO',
'INTVKL' => 'DK'
}
def process
r_gen = []
r_tran = {}
fetch
@csv.each do |row|
#0 Dataset
next unless row[0] == 'FUELINST'
#1 PublishTime
#2 StartTime
time = Time.strptime(row[2], TIME_FORMAT)
#3 SettlementDate
#4 SettlementPeriod
#5 FuelType
#6 Generation
value = row[6].to_i*1000
if production_type = FUEL_MAP[row[5]]
r_gen << {country: 'GB', time:, value:, production_type: FUEL_MAP[row[5]]}
elsif to_area = TRAN_MAP[row[5]]
k = [time,to_area]
r_tran[k] ||= {time:, from_area: 'GB', to_area:, value: 0}
r_tran[k][:value] += value
else
raise row.inspect
end
end
r_gen = Validate.validate_generation(r_gen, self.class.source_id)
Out2::Generation.run(r_gen, @from, @to, self.class.source_id)
Out2::Transmission.run(r_tran.values, @from, @to, self.class.source_id)
end
end
# https://bmrs.elexon.co.uk/api-documentation/endpoint/datasets/AGPT
class Generation < BaseCSV
include SemanticLogger::Loggable
include Out::Generation
def self.cli(args)
if args.length != 2
$stderr.puts "#{$0} <from> <to>"
exit 1
end
from = Chronic.parse(args.shift).to_date
to = Chronic.parse(args.shift).to_date
(from...to).each do |date|
e = Elexon::Generation.new(date)
e.process
end
end
DATETIME_FORMAT = '%Y-%m-%d %H:%M'
def initialize(date)
super
@url = 'https://data.elexon.co.uk/bmrs/api/v1/datasets/AGPT'
@from = date
@to = date + 1.day
@options[:publishDateTimeFrom] = @from.strftime(DATETIME_FORMAT)
@options[:publishDateTimeTo] = @to.strftime(DATETIME_FORMAT)
end
def points_generation
r = {}
fetch
@csv.each do |row|
#0 Dataset
next unless row[0] == 'AGPT'
#1 DocumentId
#2 DocumentRevisionNumber
#3 PublishTime
#4 BusinessType
#5 PsrType
production_type = row[5].downcase.tr_s(' ', '_')
#6 Quantity
value = row[6].to_f*1000
#7 StartTime
time = Time.strptime(row[7], TIME_FORMAT)
#8 SettlementDate
#9 SettlementPeriod
k=[time,production_type]
r[k] ||= {country: 'GB_B1620', production_type:, time:, value:}
end
Validate.validate_generation(r.values, self.class.source_id)
end
end
# https://bmrs.elexon.co.uk/api-documentation/endpoint/demand/actual/total
# https://bmrs.elexon.co.uk/api-documentation/endpoint/datasets/ATL
# maximum data output range of 7 days
class Load < BaseCSV
include SemanticLogger::Loggable
include Out::Load
def self.cli(args)
if args.length != 2
$stderr.puts "#{$0} <from> <to>"
exit 1
end
from = Chronic.parse(args.shift).to_date
to = Chronic.parse(args.shift).to_date
(from...to).each do |time|
e = Elexon::Load.new(time)
e.process
end
end
def initialize(date)
super
@url = 'https://data.elexon.co.uk/bmrs/api/v1/demand/actual/total'
@options[:from] = date.strftime(DATE_FORMAT)
@options[:to] = (date + 1.day).strftime(DATE_FORMAT)
end
def points_load
r = {}
fetch
@csv.shift #skip header
@csv.each do |row|
#0 PublishTime
#1 StartTime
time = Time.strptime(row[1], TIME_FORMAT)
#2 SettlementDate
#3 SettlementPeriod
#4 Quantity
value = (row[4].to_f*1000).to_i
r[time] ||= {time:, country: 'GB', value:}
end
#require 'pry' ; binding.pry
Validate.validate_load(r.values, self.class.source_id)
end
end
# https://bmrs.elexon.co.uk/api-documentation/endpoint/datasets/B1610
class Unit < BaseCSV
include SemanticLogger::Loggable
include Out::Unit
def self.cli(args)
if args.length != 2
$stderr.puts "#{$0} <from> <to>"
exit 1
end
from = Chronic.parse(args.shift).to_date
to = Chronic.parse(args.shift).to_date
(from...to).each do |date|
begin
(1..50).each do |period|
Elexon::Unit.new(date, period).process
end
rescue EmptyError
logger.warn "EmptyError #{time}"
end
end
end
def self.each
from =::GenerationUnit.joins(:unit => :area).where("area.source" => self.source_id).where("time > ?", 2.months.ago).maximum(:time)
from = from.to_date
(from..5.business_days.ago).each do |date|
(1..50).each do |period|
yield self.new(date, period)
end
rescue EmptyError
logger.warn "Empty response #{date}"
end
end
def initialize(date, period)
super(date)
@from = date
@to = date.tomorrow
@url = 'https://data.elexon.co.uk/bmrs/api/v1/datasets/B1610'
@options[:settlementDate] = date.strftime('%Y-%m-%d')
@options[:settlementPeriod] = period
#@options[:NGCBMUnitID] = unit
end
@@units = {}
def self.clear_cache!
@@units = {}
end
def points
fetch
#require 'pry' ; binding.pry
area = Area.find_by(code: 'GB', source: 'elexon')
default_production_type_id = ProductionType.where(name: 'other').pluck(:id).first
r = {}
@csv.each do |row|
#0 Dataset
next unless row[0] == 'B1610'
#1 PsrType
next unless row[1] == 'Generation'
#2 BmUnit
next unless row[3]
unit_internal_id = row[3]
unit = @@units[unit_internal_id] ||= area.units.
create_with(production_type_id: default_production_type_id).
find_or_create_by(internal_id: unit_internal_id)
#3 NationalGridBmUnitId
#4 SettlementDate
#5 SettlementPeriod
#6 HalfHourEndTime
time = (Time.strptime("#{row[4]} UTC", '%Y-%m-%d %Z') + (row[5].to_i * 30).minutes)
#7 Quantity
value = row[7].to_f*1000*2
k = [unit.id, time]
if r[k] && r[k][:value] != value
require 'pry' ; binding.pry
end
r[k] = {unit_id: unit.id, time:, value:}
end
#binding.irb
r.values
end
def process
Out2::Unit.run(points, @from, @to, self.class.source_id)
end
end
# https://bmrs.elexon.co.uk/api-documentation/endpoint/datasets/IGCA
class Capacity < BaseCSV
include SemanticLogger::Loggable
include CliMixin::Yearly
def initialize(date)
@url = 'https://data.elexon.co.uk/bmrs/api/v1/datasets/IGCA'
@from = date
@to = date + 1.year
super
end
def points_capacity
area = Area.find_by(code: 'GB_B1620', source: 'elexon')
area_id = area.id
fetch
5.times { @csv.shift }
r = []
@csv.each do |row|
#0:Document Type
next unless row[0] == 'Installed generation per type'
#1:Business Type
next unless row[1] == 'Installed generation'
#2:Process Type
next unless row[2] == 'Year ahead'
#3:Time Series ID
#4:Quantity
value = row[4].to_f*1000
#5:Resolution
next unless row[5] == 'P1Y'
#6:Year
time = Time.strptime(row[6], '%Y')
#7:Power System Resource Type
production_type = row[7].gsub(/ /,'_').downcase
# missing/no generation data:
next if production_type == 'other_renewable'
#8:Active Flag
unless row[8] == 'Y'
require 'pry' ; binding.pry
end
#9:Document ID
#10:Document RevNum
r << {area_id:, production_type:, time:, value:}
end
require 'pry' ; binding.pry
r
end
def process
Out2::Capacity.run(points_capacity, nil, nil, self.class.source_id)
end
end
# https://bmrs.elexon.co.uk/api-documentation/endpoint/datasets/IGCPU
class UnitCapacity < BaseCSV
include SemanticLogger::Loggable
include CliMixin::Yearly
def initialize(date)
@url = 'https://data.elexon.co.uk/bmrs/api/v1/datasets/IGCPU'
super
end
def points_unit_capacity
area = Area.find_by(code: 'GB', source: 'elexon')
fetch
@csv.shift
r = {}
#r = []
@csv.each do |row|
#Document Type
next unless row[0] == 'Configuration document'
#Business Type
next unless row[1] == 'Production unit'
#Process Type
#Time Series ID
#Power System Resource Type
production_type_name = row[4].gsub(/ /,'_').downcase
#Year
#time = Time.strptime(row[5], '%Y')
#BM Unit ID
#Registered Resource EIC Code
#Voltage limit
#9:Nominal
value = row[9].to_f*1000
#10:NGC BM Unit ID
next if row[10] == 'NA'
unit = area.units.find_by(internal_id: row[10])
unless unit
production_type = ProductionType.find_by! name: production_type_name
unless production_type
logger.warn "Unknown production_type: #{production_type_name}"
next
end
logger.info "New #{production_type_name} unit #{row[10]}"
unit = area.units.create!(internal_id: row[10], production_type:)
#require 'pry' ; binding.pry
end
if production_type_name != 'generation' && unit.production_type.name != production_type_name
production_type = ProductionType.find_by name: production_type_name
unit.production_type = production_type
unit.save!
puts "#{unit.name} #{unit.internal_id} current=#{unit.production_type.name} != api=#{production_type_name}"
end
#next unless unit
#create_with(production_type_id: default_production_type_id).
#Registered Resource Name
#12:Active Flag
unless row[12] == 'Y'
require 'pry' ; binding.pry
end
#Document ID
#Implementation Date
time = Time.strptime(row[14], '%Y-%m-%d')
#Decommissioning Date
k = [unit.id, time]
if r[k]
require 'pry' ; binding.pry
end
r[k] = {unit_id: unit.id, time:, value:}
end
#require 'pry' ; binding.pry
r.values
end
def process
Out2::UnitCapacity.run(points_unit_capacity, nil, nil, self.class.source_id)
end
end
# https://bmrs.elexon.co.uk/api-documentation/endpoint/temperature
class Temperature < BaseCSV
def initialize
super
@url = 'https://data.elexon.co.uk/bmrs/api/v1/temperature'
@options[:from] = date.strftime(DATE_FORMAT)
@options[:to] = (date + 1.day).strftime(DATE_FORMAT)
end
end
end