# ------------------------------------------------------------------------------
# program: legge le tabelle di Dabata
#
# input: pagine html in Dabata
#
# output: file csv che possono essere letti da excel
# 
# history: Sep 2025 Ferrario
#
# ------------------------------------------------------------------------------

# librerie
library("XML")
library("rvest")

#period0 storico di interesse - relativa pagina su Dabat@
pagina = "2030"

# sito internet
sitoweb = paste("https://www.uni-met.it/trombedaria/", pagina, "_TA.html", sep ="")

# associazione dell'URL
your_url <- read_html(sitoweb)
    
#option 2
line = html_nodes(your_url, xpath = '//table//tr') %>% html_text() 
lr = length(line) -1
line_row = line [4:lr]

dd = length(line_row)

# spacchettamento della riga
Result <- strsplit(line_row, split = "\n       ")

# inserimento dei dati in una matrice
dabata_m = matrix(unlist(Result), nrow=dd, byrow=T)

# esporta in csv
write.table(x = dabata_m, file = paste("C:/Users/ferra/Downloads/R_project/Leggi_Dabata/output/", pagina, ".csv", sep="")       , sep = ',', row.names = FALSE, col.names = FALSE)
