Skip to content

Instantly share code, notes, and snippets.

@virtualdj
Created June 28, 2023 21:16
Show Gist options
  • Save virtualdj/e5ffea98f79b92c51b310e409e1982b8 to your computer and use it in GitHub Desktop.
Save virtualdj/e5ffea98f79b92c51b310e409e1982b8 to your computer and use it in GitHub Desktop.
Calcolo PSV da ZIP con XML di GME
from datetime import date, timedelta
from statistics import mean
import zipfile
import xml.etree.ElementTree as et
# Percorso del file ZIP scaricato dal sito
# https://www.mercatoelettrico.org/It/download/DownloadDati.aspx?val=MGPGAS_SintesiScambio
archive = zipfile.ZipFile('MGPGAS_SintesiScambio2023043020230530.zip')
# Esamina ogni file XML nello ZIP (ordinandoli prima)
psv = []
for fn in sorted(archive.namelist()):
# Scompatta il file XML in memoria
xml_tree = et.parse(archive.open(fn))
# Parsing dell'XML (1 file = 3 giorni = D+1, D+2, D+3)
xml_root = xml_tree.getroot()
# Estrae il primo elemento 'negoziazione_continua'
xml_first_element = xml_root.find('negoziazione_continua')
# Estrae i campi dall'elemento
datasessione_string = xml_first_element.find('DataSessione').text #yyyymmdd
nomeprodotto_string = xml_first_element.find('NomeProdotto').text #MGP-yyyy-mm-dd[+1]
# Verifica che le date siano corrette
datasessione = date(int(datasessione_string[0:4]), int(datasessione_string[4:6]), int(datasessione_string[6:8]))
dataprezzo = date(int(nomeprodotto_string[4:8]), int(nomeprodotto_string[9:11]), int(nomeprodotto_string[12:14]))
if dataprezzo == (datasessione + timedelta(days=1)):
# Estrae il prezzo dall'elemento
prezzo_string = xml_first_element.find('UltimoPrezzo').text
prezzo_string = prezzo_string.replace(',','.')
# Converte il prezzo in €/Smc (= €/MWh / 93.54)
prezzo = float(prezzo_string) / 93.54
# Calcola le statistiche
psv.append(prezzo)
print(f'{dataprezzo} = {prezzo} €/Smc')
else:
print('Errore nelle date!')
# Mostra i risultati
print(f'PSV = {mean(psv):.6g} €/Smc')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment