Created
August 31, 2023 10:33
-
-
Save Codelaby/53c25454672dc2c358cdf124fcba2b02 to your computer and use it in GitHub Desktop.
Energy parser Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
//https://www.omie.es/sites/default/files/dados/AGNO_2023/MES_07/TXT/INT_PBC_TECNOLOGIAS_H_1_17_07_2023_17_07_2023.TXT | |
let csvContent = """ | |
Fecha;Hora;CARBÓN;FUEL-GAS;AUTOPRODUCTOR;NUCLEAR;HIDRÁULICA;CICLO COMBINADO;EÓLICA;SOLAR TÉRMICA;SOLAR FOTOVOLTAICA;COGENERACIÓN/RESIDUOS/MINI HIDRA;IMPORTACIÓN INTER.;IMPORTACIÓN INTER. SIN MIBEL; | |
17/07/2023;1;292,0;;;6.880,9;1.442,2;4.358,2;8.524,2;655,6;3,8;3.200,0;;1.821,7; | |
17/07/2023;2;291,0;;;6.893,9;945,2;3.319,2;8.966,1;647,4;3,8;3.141,7;;1.939,8; | |
17/07/2023;3;180,0;;;6.910,9;746,5;3.277,2;8.996,6;643,9;3,7;3.137,9;;2.419,9; | |
17/07/2023;4;;;;6.913,9;653,3;3.021,4;8.767,3;638,8;3,7;3.140,8;;2.913,0; | |
17/07/2023;5;;;;6.915,9;646,5;2.840,7;8.237,4;638,8;3,7;3.180,1;;3.145,0; | |
17/07/2023;6;;;;6.918,9;659,3;3.446,3;7.729,2;619,8;3,7;3.200,0;;3.019,9; | |
17/07/2023;7;;;;6.921,9;1.558,8;4.505,3;7.154,3;494,3;72,5;3.285,4;;1.859,3; | |
17/07/2023;8;;;;6.922,9;2.391,6;5.842,3;6.137,9;203,2;1.098,4;3.408,2;;1.585,1; | |
17/07/2023;9;;;;6.918,9;2.455,9;5.841,1;5.212,6;221,9;5.003,7;3.428,1;;404,4; | |
17/07/2023;10;;;;6.912,9;1.213,4;4.717,5;4.458,3;1.069,8;9.903,0;3.402,9;;774,6; | |
17/07/2023;11;;;;6.907,9;813,1;3.926,3;3.896,6;1.741,3;12.794,6;3.307,2;;1.771,8; | |
17/07/2023;12;;;;6.903,9;817,2;3.780,5;3.818,6;1.884,5;14.316,9;3.267,6;;2.050,0; | |
17/07/2023;13;;;;6.895,9;778,4;4.054,6;4.007,6;1.901,4;15.272,5;3.280,5;;555,7; | |
17/07/2023;14;;;;6.893,9;835,5;3.777,9;4.215,6;1.909,2;15.586,4;3.223,3;;2.050,0; | |
17/07/2023;15;;;;6.886,9;843,5;3.762,2;4.746,4;1.905,7;15.630,5;3.179,7;;2.050,0; | |
17/07/2023;16;;;;6.872,9;832,8;3.509,8;5.274,6;1.906,3;15.394,4;3.171,2;;2.050,0; | |
17/07/2023;17;;;;6.875,9;809,2;3.524,0;5.774,2;1.902,3;14.770,1;3.223,6;;2.050,0; | |
17/07/2023;18;;;;6.873,9;772,8;4.215,3;6.331,3;1.885,5;13.598,7;3.227,8;;1.109,6; | |
17/07/2023;19;;;;6.872,9;740,3;5.406,7;7.025,7;1.844,3;11.781,2;3.298,4;;0; | |
17/07/2023;20;;;;6.878,9;2.490,4;6.718,1;7.356,6;1.814,8;8.366,1;3.332,8;;0; | |
17/07/2023;21;;;;6.880,9;4.597,1;8.103,5;7.656,1;1.511,9;3.624,1;3.429,1;;0; | |
17/07/2023;22;;;;6.881,9;5.860,8;8.259,6;8.099,6;931,7;514,3;3.541,7;;0; | |
17/07/2023;23;;;;6.885,9;5.884,3;8.293,7;8.160,5;668,8;8,0;3.540,4;;0; | |
17/07/2023;24;;;;6.886,9;4.416,3;8.310,8;7.895,0;652,1;7,2;3.520,5;;247,4; | |
;;;;;;;;;;;;;; | |
""" | |
import Foundation | |
struct CSVData { | |
var fecha: Date | |
var hora: Int | |
var carbon: Decimal | |
var fuelGas: Decimal | |
var autoproductor: Decimal | |
var nuclear: Decimal | |
var hidraulica: Decimal | |
var cicloCombinado: Decimal | |
var eolica: Decimal | |
var solarTermica: Decimal | |
var solarFotovoltaica: Decimal | |
var cogeneracionResiduosMiniHidra: Decimal | |
var importacionInter: Decimal | |
var importacionInterSinMIBEL: Decimal | |
} | |
let dateFormatter = DateFormatter() | |
dateFormatter.dateFormat = "dd/MM/yyyy" | |
dateFormatter.locale = Locale(identifier: "en_US_POSIX") | |
var csvDataArray: [CSVData] = [] | |
let lines = csvContent.components(separatedBy: "\n") | |
for line in lines { | |
let fields = line.components(separatedBy: ";") | |
if fields.count >= 2, let date = dateFormatter.date(from: fields[0]) { | |
let csvData = CSVData( | |
fecha: date, | |
hora: Int(fields[1]) ?? 0, | |
carbon: Decimal(string: fields[2]) ?? 0, | |
fuelGas: Decimal(string: fields[3]) ?? 0, | |
autoproductor: Decimal(string: fields[4]) ?? 0, | |
nuclear: Decimal(string: fields[5]) ?? 0, | |
hidraulica: Decimal(string: fields[6]) ?? 0, | |
cicloCombinado: Decimal(string: fields[7]) ?? 0, | |
eolica: Decimal(string: fields[8]) ?? 0, | |
solarTermica: Decimal(string: fields[9]) ?? 0, | |
solarFotovoltaica: Decimal(string: fields[10]) ?? 0, | |
cogeneracionResiduosMiniHidra: Decimal(string: fields[11]) ?? 0, | |
importacionInter: Decimal(string: fields[12]) ?? 0, | |
importacionInterSinMIBEL: Decimal(string: fields[13]) ?? 0 | |
) | |
csvDataArray.append(csvData) | |
} | |
} | |
for data in csvDataArray { | |
print("Fecha: \(data.fecha), Hora: \(data.hora), Carbon: \(data.carbon), solarFotovoltaica: \(data.solarFotovoltaica)") | |
// Print the other fields similarly | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment