-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
omie_dataset_gas.js
105 lines (93 loc) · 3.66 KB
/
omie_dataset_gas.js
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
import { writeJSON, readJSON } from 'https://deno.land/x/[email protected]/src/json.ts'
import { readTXT } from 'https://deno.land/x/[email protected]/src/txt.ts'
//Vamos a creer que los del OMIE respetan la estructura, a ñapear
const removeLines = (data, lines = []) => {
return data
.split('\n')
.filter((val, idx) => lines.indexOf(idx) === -1)
.join('\n');
}
/*https://stackoverflow.com/a/61474145*/
const parseCsv = csv => {
let lines = csv.split("\n");
const header = lines.shift().split(";")
return lines.map(line => {
const bits = line.split(",")
let obj = {};
header.forEach((h, i) => {
if(h) {
obj[h] = bits[i]
}
});
return obj;
})
};
const compensacion_gas = await readTXT('public/data/omie_prices_gas.json');
const remove_lines_compensacion_gas = removeLines(compensacion_gas, [0,1,2,4,5,6,7,8,9,10,11,12,13])
let remove_lines_compensacion_gas_replace = remove_lines_compensacion_gas.replaceAll(',', '.').replaceAll(';',',')
remove_lines_compensacion_gas_replace = remove_lines_compensacion_gas_replace.replace('Precio de ajuste en el sistema espa�ol (EUR/MWh), ', 'compensacion;\n').replaceAll(', ',',\n')
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
let get_date = tomorrow.getDate();
let get_month = tomorrow.getMonth() + 1;
const get_year = tomorrow.getFullYear();
get_month = get_month < 10 ? `0${get_month}` : get_month;
get_date = get_date < 10 ? `0${get_date}` : get_date;
const compensacion_csv_to_json = parseCsv(remove_lines_compensacion_gas_replace)
let omie_compensacion = compensacion_csv_to_json.map((element, index) => {
return {
precio: element.compensacion === "0" ? 0 : +((element.compensacion / 1000).toFixed(3)),
hora: index,
dia: `${get_date}/${get_month}/${get_year}`
};
});
const omie_compensacion_historic = await readJSON('public/data/historic_compensacion_gas.json');
const omie_compensacion_historic_update = [...omie_compensacion, ...omie_compensacion_historic]
const reduced = omie_compensacion_historic_update.reduce((m, d) => {
if (!m[d.dia]) {
m[d.dia] = { ...d, count: 1 };
return m;
}
m[d.dia].precio += d.precio;
m[d.dia].count += 1;
return m;
}, {});
const group_data_by_day = Object.keys(reduced).map((item_by_day) => {
const item = reduced[item_by_day];
return {
price: +(item.precio / item.count).toFixed(2),
day: item.dia.split('/')[0],
month: item.dia.split('/')[1],
year: item.dia.split('/')[2],
date: `${item.dia.split('/')[1]}/${item.dia.split('/')[0]}/${item.dia.split('/')[2]}`,
monthYear: `${item.dia.split('/')[1]}/${item.dia.split('/')[2]}`
}
})
//Similar al primer reduce, pero ahora
//vamos a sumar los precios agrupando por mes
const reduced_by_month = group_data_by_day.reduce((m, d) => {
if (!m[d.monthYear]) {
m[d.monthYear] = { ...d, count: 1 };
return m;
}
m[d.monthYear].price += d.price;
m[d.monthYear].count += 1;
return m;
}, {});
//Lo mismo, usamos el reduce para
//sacar el precio medio por mes, y
//aprovechamos las transformaciones anteriores
const group_prices_by_month = Object.keys(reduced_by_month).map((item_by_month) => {
const item = reduced_by_month[item_by_month];
return {
averagePrice: +(item.price / item.count).toFixed(2),
monthYear: item.monthYear,
month: item.month,
date: item.date,
year: item.year
}
})
await writeJSON('public/data/omie_compensacion_data_by_month.json', group_prices_by_month)
await writeJSON('public/data/omie_compensacion_data_by_day.json', group_data_by_day)
await writeJSON('public/data/omie_compensacion_data.json', omie_compensacion)
await writeJSON('public/data/historic_compensacion_gas.json', omie_compensacion_historic_update)