Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Ds a29 #114

Merged
merged 9 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions pipelines/A29/A29.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# -------------------------------------------------------------------------
# Step 0: Get and aggregate the data
# -------------------------------------------------------------------------

ds <- statbotData::create_dataset("A29")

# The dataset is split into 1 csv file per year, the URL remains the same
# with only the year changing

agg_month <- function(df) {
# Only measurements from Stampfenbachstrasse are kept
# (only station available for all years)
# Exclude parameters that represent a threshold/max value
# Widen table to have 1 column per parameter
# Truncate timestamps to month (yyyy-mm)
# Compute monthly mean for each parameter
agg <- df %>%
janitor::clean_names() %>%
dplyr::filter(
standort == "Zch_Stampfenbachstrasse",
!parameter %in% c("O3_nb_h1>120", "O3_max_h1")
) %>%
dplyr::select(-intervall, -status) %>%
tidyr::pivot_wider(
names_from = c(parameter, einheit),
values_from = wert,
names_glue = "{parameter}_{einheit}"
) %>%
janitor::clean_names(replace = janitor:::mu_to_u) %>%
dplyr::mutate(datum = substr(datum, 1, 7)) %>%
dplyr::group_by(datum) %>%
dplyr::summarise(across(is.numeric, ~ mean(.x, na.rm = T))) %>%
dplyr::mutate(across(everything(), ~ ifelse(is.nan(.), NA, .))) %>%
dplyr::rename(nox_ppb = "n_ox_ppb")
return(agg)
}

# Download + aggregate data from each year
# and collect all years in a single tibble
full_data <- tibble::tibble()
for (year in 1983:2023) {
# Year is substituted in URL
ds$download_url <- stringr::str_replace(
ds$download_url,
"[0-9]{4}",
as.character(year)
)
# Daily data for a single year aggregated by month
temp <- statbotData::download_data(ds)$data
temp <- agg_month(temp)
# Monthly data for a single year appended to the full dataset
full_data <- dplyr::bind_rows(full_data, temp)
}
# Overwrite ds$data with the full dataset
ds$data <- full_data

# -------------------------------------------------------------------------
# Step 1: Prepare rename columns
# -------------------------------------------------------------------------

ds$postgres_export <- ds$data %>%
dplyr::mutate(
jahr = as.integer(substr(datum, 1, 4)),
monat = as.integer(substr(datum, 6, 7))
) %>%
dplyr::select(-datum)

# -------------------------------------------------------------------------
# Step 2 map to spatial units
# -------------------------------------------------------------------------

# All data comes from Stadt Zürich. Explicitely adding UID
ds$postgres_export <- ds$postgres_export %>%
dplyr::mutate(spatialunit_uid = "253_A.ADM3")

# -------------------------------------------------------------------------
# Step: Upload dataset to postgres, test run queries, generate a sample and
# metadata files.
# input: ds$postgres_export, ds$dir/queries.sql
# output: ds$dir/queries.log
# -------------------------------------------------------------------------

# create the table in postgres
statbotData::create_postgres_table(ds)
# copy the metadata templates to the metadata files and then complete them
statbotData::update_pipeline_last_run_date(ds)
statbotData::update_metadata_in_postgres(ds)
# generate sample data for the dataset from the local tibble
statbotData::dataset_sample(ds)
statbotData::testrun_queries(ds)
11 changes: 11 additions & 0 deletions pipelines/A29/metadata_table_columns.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name;table_name;data_type;title;title_en;example_values
co_mg_m3;stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983;numeric;"Kohlenmonoxid (CO) in mg/m3";"Carbon monoxide (CO) in mg/m3";"3.69741935483871,2.3325,2.45451612903226"
no2_ug_m3;stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983;numeric;"Stickstoffdioxid (N02) in µg/m3";"Nitrogen dioxide (NO2) in µg/m3";"38.5848387096774,47.8067857142857,48.0387096774194"
no_ug_m3;stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983;numeric;"Stickstoffmonoxid (NO) in µg/m3";"Nitrogen monoxide (NO) in µg/m3";"162.903870967742,157.969285714286,171.871290322581"
nox_ppb;stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983;numeric;"Stickstoffoxide (NOx) in ppb";"Nitrogen oxides (NOx) in ppb";"150.781612903226,151.648214285714,162.916774193548"
so2_ug_m3;stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983;numeric;"Schwefeldioxid (SO2) in µg/m3";"Sulfur dioxid (SO2) in µg/m3";"83.6127586206897,100.517142857143,69.5396666666667"
o3_ug_m3;stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983;numeric;"Ozon (O3) in µg/m3";"Ozone (O3) in µ3/m3";"11.4648148148148,6.99464285714286"
pm10_ug_m3;stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983;numeric;"Schwebestaub PM10 (Durchmesser 10µm oder weniger) in µg/m3";"Airborne dust PM10 (diameter 10µm or less) in µg/m3";"33.9512,30.7521428571429"
pn_1_cm3;stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983;numeric;"Partikelanzahl pro cm3";"Particle count per cm3";"23403.0509090909,12632.1964516129"
pm2_5_ug_m3;stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983;numeric;"Schwebestaub PM2.5 (Durchmesser 2,5µm oder weniger) in µg/m3";"Airborne dust PM2.5 (diameter 2.5µm or less) in µg/m3";"26.6368181818182,16.9007407407407"
monat;stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983;numeric;"Monat";"Month";"1,2,3"
2 changes: 2 additions & 0 deletions pipelines/A29/metadata_tables.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name;language;last_pipeline_run;data_source_url;title;title_en;description;description_en;temporal_coverage;update_frequency;spatial_coverage
stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983;de;2023-10-12;https://data.stadt-zuerich.ch/dataset/ugz_luftschadstoffmessung_tageswerte;Täglich aktualisierte Luftqualitätsmessungen, seit 1983;Daily updated air quality measurements, since 1983;Dieser Datensatz enthält monatliche Werte seit 1983 bis zum Oktober 2023. Er wurde aus dem von der Stadt Zürich bereitgestellten vorgelagerten Datensatz abgeleitet, der in Jahresdateien aufgeteilte Tagesdaten enthält. In diesem aggregierten Datensatz enthalten wir Tagesmittelwerte für Schwefeldioxid (SO2), Kohlenmonoxid (CO), Stickstoffoxid (NOx), Stickstoffmonoxid (NO), Stickstoffdioxid (NO2), Feinstaub (PM10 und PM2.5) und Partikelanzahl (PN), sofern in ausreichender Qualität verfügbar. Alle Werte werden pro Monat gemittelt. Von den 4 Standorten im stromaufwärts gelegenen Datensatz wurden nur die Messungen der Zch_Stampfenbachstraße berücksichtigt, da sie über den gesamten Zeitraum verfügbar waren.;This dataset is includes monthly values since 1983 up to October 2023. It is derived from the upstream dataset provided by Stadt Zürich, which contains daily data divided into annual files. In this aggregated dataset, we include daily means for sulphur dioxide (SO2), carbon monoxide (CO), nitrogen oxide (NOx), nitrogen monoxide (NO), nitrogen dioxide (NO2), particulate matter (PM10 and PM2.5) and particle number (PN), if available in sufficient quality. All values are averaged per month. From the 4 location in the upstream dataset, only measurements from Zch_Stampfenbachstrasse were taken into account, as it was available over the whole time range.;1983-2023;MONTHLY;Gemeinde
143 changes: 143 additions & 0 deletions pipelines/A29/queries.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
[1] "Environment: REMOTE, Time: 2023-10-13 11:04:34"

Query Nr. 1
-- Wann erreichte die Kohlenmonoxidkonzentration in der Luft in der Stadt Zürich ihr Maximum? Geben Sie auch die Konzentration in mg/m3 an.
SELECT T.jahr, T.monat, T.co_mg_m3 FROM stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983 AS T JOIN spatial_unit AS S ON T.spatialunit_uid = S.spatialunit_uid WHERE S.municipal = TRUE AND S.name = 'Zürich' AND T.co_mg_m3 IS NOT NULL ORDER BY T.co_mg_m3 DESC LIMIT 1;
jahr monat co_mg_m3
1984 11 4.651667

Query Nr. 2
-- In welchem Monat gibt es in der Stadt Zürich generell am wenigsten Partikel in der Luft?
SELECT T.monat, SUM(T.pn_1_cm3) / COUNT(T.pn_1_cm3) AS monat_avg_pn_1_cm3 FROM stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983 AS T JOIN spatial_unit AS S ON T.spatialunit_uid = S.spatialunit_uid WHERE S.municipal = TRUE AND S.name = 'Zürich' AND T.pn_1_cm3 IS NOT NULL GROUP BY T.monat ORDER BY SUM(T.pn_1_cm3) / COUNT(T.pn_1_cm3) ASC LIMIT 1;
monat monat_avg_pn_1_cm3
12 13083.66

Query Nr. 3
-- Wie hoch war die monatliche Stickoxidkonzentration (in Teilen pro Milliarde) in der Stadt Zürich im Jahr 2019?
SELECT T.monat, T.nox_ppb FROM stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983 AS T JOIN spatial_unit AS S ON T.spatialunit_uid = S.spatialunit_uid WHERE S.municipal = TRUE AND S.name = 'Zürich' AND T.jahr = 2019 ORDER BY T.monat ASC;
monat nox_ppb
1 23.38516
2 43.83250
3 21.13839
4 19.90567
5 16.73968
6 16.13467
7 14.69742
8 15.56903
9 18.94267
10 25.81323
11 28.44233
12 34.41323

Query Nr. 4
-- Wie hoch war die jährliche Durchschnittskonzentration von Stickstoffmonoxid und Stickstoffdioxid in der Stadt Zürich in den Jahren 1993, 2003, 2013 und 2023?
SELECT T.jahr, SUM(T.no_ug_m3) / COUNT(T.no_ug_m3) AS jahr_avg_no_ug_m3, SUM(T.no2_ug_m3) / COUNT(T.no2_ug_m3) AS avg_no2_ug_m3 FROM stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983 AS T JOIN spatial_unit AS S ON T.spatialunit_uid = S.spatialunit_uid WHERE S.municipal = TRUE AND S.name = 'Zürich' AND T.jahr IN (1993, 2003, 2013, 2023) AND T.no_ug_m3 IS NOT NULL GROUP BY T.jahr;
jahr jahr_avg_no_ug_m3 avg_no2_ug_m3
1993 48.441402 45.86977
2003 29.552807 42.78025
2013 20.718149 33.79991
2023 6.532129 19.14540

Query Nr. 5
-- Welches war die niedrigste Feinstaubkonzentration (PM2.5), die je in Zürich gemessen wurde, und in welchem Jahr war dies?
SELECT T.jahr, T.pm2_5_ug_m3 FROM stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983 AS T JOIN spatial_unit AS S ON T.spatialunit_uid = S.spatialunit_uid WHERE S.municipal = TRUE AND S.name = 'Zürich' AND T.pm2_5_ug_m3 IS NOT NULL ORDER BY T.pm2_5_ug_m3 ASC LIMIT 1;
jahr pm2_5_ug_m3
2022 5.405667

Query Nr. 6
-- Wie hoch war die Schwefeldioxidkonzentration in der Luft in Zürich im Jahr 2019? Geben Sie die Konzentration in Milligramm pro Kubikmeter an.
SELECT T.monat, T.so2_ug_m3 / 1000 AS so2_mg_m3 FROM stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983 AS T JOIN spatial_unit AS S ON T.spatialunit_uid = S.spatialunit_uid WHERE S.municipal = TRUE AND S.name = 'Zürich' AND T.jahr = 2019 ORDER BY T.monat ASC;
monat so2_mg_m3
1 0.0010593548
2 0.0014789286
3 0.0012296774
4 0.0008716667
5 0.0006812903
6 0.0011473333
7 0.0014664516
8 0.0015093548
9 0.0013230000
10 0.0006209677
11 0.0014966667
12 0.0016651613

Query Nr. 7
-- Wie hoch war die CO- und SO2-Konzentration (beide in Mikrogramm/m3) in Zürich zwischen Oktober 1999 und Juli 2000?
SELECT T.jahr, T.monat, T.co_mg_m3 * 1000 AS co_ug_m3, T.so2_ug_m3 FROM stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983 AS T JOIN spatial_unit AS S ON T.spatialunit_uid = S.spatialunit_uid WHERE S.municipal = TRUE AND S.name = 'Zürich' AND ( (T.jahr = 1999 AND T.monat >= 10) OR (T.jahr = 2000 AND T.monat <= 7) ) AND T.co_mg_m3 IS NOT NULL ORDER BY T.jahr ASC, T.monat ASC;
jahr monat co_ug_m3 so2_ug_m3
1999 10 895.1613 9.278000
1999 11 955.3333 10.376667
1999 12 713.5484 9.607419
2000 1 844.4828 11.155806
2000 2 797.5000 9.929655
2000 3 675.4839 7.746774
2000 4 677.6667 7.812759
2000 5 549.3103 4.227097
2000 6 507.0000 3.376552
2000 7 424.8276 3.157097

Query Nr. 8
-- Zeigen Sie mir die mittlere PM10- und PM2.5-Konzentration in Zürich während jeder Jahreszeit des Jahres 2018.
SELECT CASE WHEN T.monat IN (12, 1, 2) THEN 'Winter' WHEN T.monat IN (3, 4, 5) THEN 'Frühling' WHEN T.monat IN (6, 7, 8) THEN 'Sommer' WHEN T.monat IN (9, 10, 11) THEN 'Herbst' END AS jahreszeit, SUM(T.pm10_ug_m3) / COUNT(T.pm10_ug_m3) AS avg_pm10_ug_m3, SUM(T.pm2_5_ug_m3) / COUNT(T.pm2_5_ug_m3) AS avg_pm2_5_ug_m3 FROM stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983 AS T JOIN spatial_unit AS S ON T.spatialunit_uid = S.spatialunit_uid WHERE S.municipal = TRUE AND S.name = 'Zürich' AND T.jahr = 2018 AND T.pm10_ug_m3 IS NOT NULL AND T.pm2_5_ug_m3 IS NOT NULL GROUP BY jahreszeit;
jahreszeit avg_pm10_ug_m3 avg_pm2_5_ug_m3
Frühling 18.37968 11.704186
Herbst 21.89971 14.096649
Sommer 15.11333 9.196935
Winter 16.50175 12.977110

Query Nr. 9
-- Wie hoch ist die relative Veränderung der PM10-Konzentration in Zürich von 2017 bis 2023 in Prozent?
SELECT ( 100.0 * SUM( CASE WHEN T.jahr = 2017 THEN T.pm10_ug_m3 WHEN T.jahr = 2023 THEN -T.pm10_ug_m3 END ) / SUM(CASE WHEN T.jahr = 2017 THEN T.pm10_ug_m3 END) ) AS pm10_anderung_2017_2023_prozent FROM stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983 AS T JOIN spatial_unit AS S ON T.spatialunit_uid = S.spatialunit_uid WHERE S.municipal = TRUE AND S.name = 'Zürich' AND T.jahr IN (2017, 2023) AND T.pm10_ug_m3 IS NOT NULL;
pm10_anderung_2017_2023_prozent
17.69004

Query Nr. 10
-- Nennen Sie mir die Konzentration von Stickoxiden in der Luft von Zürich, in Teilen pro Million (ppm), für den Monat Januar in den Jahren 2017 bis 2021.
SELECT T.jahr, T.nox_ppb / 1000 AS nox_ppm_januar FROM stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983 AS T JOIN spatial_unit AS S ON T.spatialunit_uid = S.spatialunit_uid WHERE S.municipal = TRUE AND S.name = 'Zürich' AND T.monat = 1 AND T.jahr >= 2017 AND T.jahr <= 2021 ORDER BY T.jahr ASC;
jahr nox_ppm_januar
2017 0.04320645
2018 0.02535710
2019 0.02338516
2020 0.03729032
2021 0.02151710

Query Nr. 11
-- Wie viel Ozon war im Jahr 2005 jeden Monat in der Luft der Stadt Zürich?
SELECT T.monat, T.o3_ug_m3 AS o3_ug_m3_2005 FROM stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983 AS T JOIN spatial_unit AS S ON T.spatialunit_uid = S.spatialunit_uid WHERE S.municipal = TRUE AND S.name = 'Zürich' AND T.jahr = 2005;
monat o3_ug_m3_2005
1 25.63968
2 38.21536
3 46.64742
4 52.46667
5 64.68516
6 75.32700
7 63.88387
8 53.73806
9 42.83467
10 13.54806
11 14.99933
12 21.38226

Query Nr. 12
-- Wie hoch war die durchschnittliche Kohlenmonoxid-Konzentration in der Zürcher Luft im Januar und im Juli, bezogen auf den Zeitraum 1990-2000?
SELECT T.monat, SUM(T.co_mg_m3) / COUNT(T.co_mg_m3) AS avg_co_mg_m3 FROM stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983 AS T JOIN spatial_unit AS S ON T.spatialunit_uid = S.spatialunit_uid WHERE S.municipal = TRUE AND S.name = 'Zürich' AND T.jahr >= 1990 AND T.jahr <= 2000 AND T.co_mg_m3 IS NOT NULL AND T.monat IN (1, 7) GROUP BY T.monat;
monat avg_co_mg_m3
1 1.3610142
7 0.6491916

Query Nr. 13
-- Zeigen Sie mir die Partikelzahl für jeden Monat in Zürich, im Durchschnitt aller verfügbaren Jahre.
SELECT T.monat, SUM(T.pn_1_cm3) / COUNT(T.pn_1_cm3) AS avg_pn_1_cm3 FROM stadt_zurich_monatlich_luftqualitatsmessungen_seit_1983 AS T JOIN spatial_unit AS S ON T.spatialunit_uid = S.spatialunit_uid WHERE S.municipal = TRUE AND S.name = 'Zürich' AND T.pn_1_cm3 IS NOT NULL GROUP BY T.monat;
monat avg_pn_1_cm3
1 13453.51
2 15193.94
3 14060.48
4 13478.18
5 13876.29
6 14353.45
7 13144.14
8 13545.95
9 15271.33
10 17149.51
11 14329.35
12 13083.66
Loading