Created
August 11, 2023 09:44
-
-
Save jobonaf/8c3d63959e40bdbe147400a998892852 to your computer and use it in GitHub Desktop.
get air quality data from EEA
This file contains hidden or 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
library(readr) | |
library(glue) | |
library(RCurl) | |
library(lubridate) | |
library(dplyr) | |
library(futile.logger) | |
library(httr) | |
library(data.table) | |
# https://discomap.eea.europa.eu/map/fme/AirQualityExport.htm | |
poll_codes <- read_csv("https://dd.eionet.europa.eu/vocabulary/aq/pollutant/csv") | |
get_eea_pollutantcodes <- function(descr) { | |
i <- match(descr, poll_codes$Notation) | |
basename(poll_codes$URI)[i] | |
} | |
list_eea_pollutants <- function() { | |
sort(unique(poll_codes$Notation)) | |
} | |
get_eea_data <- function(CountryCode="IT", | |
CityName="", | |
Pollutant="", | |
Year_from="", | |
Year_to=Year_from, | |
Station="", | |
Samplingpoint="", | |
Source="E1a", | |
Output="TEXT", | |
UpdateDate="", | |
TimeCoverage="Year", | |
TimeZone="Etc/GMT-1") { | |
if(!is.numeric(Pollutant)) { | |
Pollutant <- get_eea_pollutantcodes(Pollutant) | |
} | |
address <- glue( | |
"https://fme.discomap.eea.europa.eu/fmedatastreaming/AirQualityDownload/AQData_Extract.fmw?", | |
"CountryCode={CountryCode}&CityName={CityName}&Pollutant={Pollutant}&", | |
"Year_from={Year_from}&Year_to={Year_to}&Station={Station}&", | |
"Samplingpoint={Samplingpoint}&Source={Source}&Output={Output}&", | |
"UpdateDate={UpdateDate}&TimeCoverage={TimeCoverage}") | |
uu <- gsub('[^\x20-\x7E]', '', strsplit(getURL(address), split="\r\n")[[1]]) | |
dd <- lapply(uu, function(x) read_csv(url(x))) | |
out <- data.table::rbindlist(dd) | |
out %>% | |
mutate(DatetimeBegin=as_datetime(gsub("\\:","",DatetimeBegin), | |
format = "%Y-%m-%d %H%M%S %z", tz=TimeZone), | |
DatetimeEnd=as_datetime(gsub("\\:","",DatetimeEnd), | |
format = "%Y-%m-%d %H%M%S %z", tz=TimeZone)) | |
} | |
get_eea_metadata <- function( | |
filein = "http://discomap.eea.europa.eu/map/fme/metadata/PanEuropean_metadata.csv") { | |
ana <- NULL | |
st <- HEAD(filein)$status | |
if(st==200) { | |
ana <- fread( | |
filein, sep="\t", header = T, stringsAsFactors = F, | |
na.strings = c("-9999","-999","-9900","-100","-99","nan"), dec=".", | |
encoding = "Latin-1") | |
} else { | |
flog.warn(glue("Problems with {filein} - code: {st}")) | |
} | |
if(nrow(ana)>0) { | |
ana %>% | |
filter(!is.na(Longitude), !is.na(Latitude)) %>% | |
distinct(Namespace,AirQualityStationEoICode,AirPollutantCode, | |
Sample, .keep_all=TRUE) -> ana | |
} else { | |
flog.warn(glue("File {filein} is empty")) | |
} | |
return(ana) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment