%22%22%22Tutorial%202%3A%20read%20election%20results%20and%20hourly%20air%20measurements%2C%20steering%20each%20reader%20with%20options.%0A%0ARun%20with%3A%20uv%20run%20marimo%20edit%20tutorials%2F02_elections_and_air_quality.py%20(needs%20network%20access%2C%20no%20account).%0A%22%22%22%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%20Tutorial%202%3A%20elections%20and%20air%20quality%0A%0A%20%20%20%20**Goal%3A**%20read%20two%20more%20sources%2C%20election%20results%20and%20hourly%20air%20measurements%2C%20and%20use%20*options*%20to%0A%20%20%20%20tell%20each%20reader%20what%20to%20read.%0A%0A%20%20%20%20**Time%3A**%20about%2015%20minutes.%20**You%20need%3A**%20basic%20Python%20and%20a%20network%20connection.%20No%20account.%0A%0A%20%20%20%20**Setup%3A**%20clone%20the%20repository%20and%20run%20%60uv%20sync%20--all-extras%60%20(see%20the%20README)%2C%20then%0A%20%20%20%20%60uv%20run%20marimo%20edit%20tutorials%2F02_elections_and_air_quality.py%60.%0A%0A%20%20%20%20In%20%5Btutorial%201%5D(https%3A%2F%2Fgithub.com%2Fmloda-ai%2Fmloda-plugin-govdata%2Fblob%2Fmain%2Ftutorials%2F01_first_table.py)%0A%20%20%20%20the%20reader%20already%20knew%20how%20its%20file%20is%20laid%20out.%20Here%20you%20pass%20more%20options%3A%20for%20a%20file%2C%20how%20it%20is%20laid%20out%3B%20for%20a%20web%0A%20%20%20%20service%2C%20what%20to%20ask%20it.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20datetime%0A%0A%20%20%20%20import%20altair%20as%20alt%0A%20%20%20%20import%20pandas%20as%20pd%0A%0A%20%20%20%20from%20mloda.user%20import%20Feature%2C%20mloda%0A%20%20%20%20from%20mloda_plugin_govdata.feature_groups.govdata%20import%20(%0A%20%20%20%20%20%20%20%20OPTION_UBA_COMPONENT%2C%0A%20%20%20%20%20%20%20%20OPTION_UBA_DATE_FROM%2C%0A%20%20%20%20%20%20%20%20OPTION_UBA_DATE_TO%2C%0A%20%20%20%20%20%20%20%20OPTION_UBA_SCOPE%2C%0A%20%20%20%20%20%20%20%20OPTION_UBA_STATION%2C%0A%20%20%20%20%20%20%20%20OPTION_WAHL_HEADER_ROWS%2C%0A%20%20%20%20%20%20%20%20OPTION_WAHL_LABEL_COLUMNS%2C%0A%20%20%20%20%20%20%20%20OPTION_WAHL_SKIPROWS%2C%0A%20%20%20%20%20%20%20%20BundeswahlleiterinReader%2C%0A%20%20%20%20%20%20%20%20DownloadCache%2C%0A%20%20%20%20%20%20%20%20UbaAirReader%2C%0A%20%20%20%20%20%20%20%20build_client%2C%0A%20%20%20%20%20%20%20%20uba_measures_url%2C%0A%20%20%20%20)%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20BundeswahlleiterinReader%2C%0A%20%20%20%20%20%20%20%20DownloadCache%2C%0A%20%20%20%20%20%20%20%20Feature%2C%0A%20%20%20%20%20%20%20%20OPTION_UBA_COMPONENT%2C%0A%20%20%20%20%20%20%20%20OPTION_UBA_DATE_FROM%2C%0A%20%20%20%20%20%20%20%20OPTION_UBA_DATE_TO%2C%0A%20%20%20%20%20%20%20%20OPTION_UBA_SCOPE%2C%0A%20%20%20%20%20%20%20%20OPTION_UBA_STATION%2C%0A%20%20%20%20%20%20%20%20OPTION_WAHL_HEADER_ROWS%2C%0A%20%20%20%20%20%20%20%20OPTION_WAHL_LABEL_COLUMNS%2C%0A%20%20%20%20%20%20%20%20OPTION_WAHL_SKIPROWS%2C%0A%20%20%20%20%20%20%20%20UbaAirReader%2C%0A%20%20%20%20%20%20%20%20alt%2C%0A%20%20%20%20%20%20%20%20build_client%2C%0A%20%20%20%20%20%20%20%20datetime%2C%0A%20%20%20%20%20%20%20%20mloda%2C%0A%20%20%20%20%20%20%20%20pd%2C%0A%20%20%20%20%20%20%20%20uba_measures_url%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%20Part%201%3A%20election%20results%0A%0A%20%20%20%20Germany%20elected%20its%20federal%20parliament%2C%20the%20*Bundestag*%2C%20on%2023%20February%202025.%20The%20Federal%20Returning%0A%20%20%20%20Officer%20(*Bundeswahlleiterin*)%2C%20who%20runs%20federal%20elections%2C%20publishes%20the%20results%20as%20one%20CSV%20file%2C%0A%20%20%20%20%60kerg.csv%60.%20It%20has%20a%20row%20for%20each%20constituency%20(*Wahlkreis*)%2C%20for%20each%20of%20the%2016%20federal%20states%0A%20%20%20%20(*L%C3%A4nder*%2C%20one%20*Land*)%20and%20for%20the%20whole%20country.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20KERG%20%3D%20%22https%3A%2F%2Fwww.bundeswahlleiterin.de%2Fbundestagswahlen%2F2025%2Fergebnisse%2Fopendata%2Fbtw25%2Fcsv%2Fkerg.csv%22%0A%20%20%20%20return%20(KERG%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Step%201%3A%20look%20at%20the%20file%0A%0A%20%20%20%20Before%20you%20read%20a%20file%2C%20look%20at%20its%20first%20lines.%20This%20cell%20downloads%20it%2C%20keeps%20a%20copy%20on%20your%20disk%20and%0A%20%20%20%20shows%20the%20first%20nine%20lines%2C%20each%20cut%20to%20100%20characters.%20Values%20are%20separated%20by%20%60%3B%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(BundeswahlleiterinReader%2C%20DownloadCache%2C%20KERG%2C%20build_client%2C%20mo)%3A%0A%20%20%20%20with%20build_client()%20as%20_client%2C%20DownloadCache(BundeswahlleiterinReader.cache_dir%2C%20client%3D_client)%20as%20_cache%3A%0A%20%20%20%20%20%20%20%20_path%20%3D%20_cache.get_or_download(KERG).path%0A%20%20%20%20kerg_lines%20%3D%20_path.read_text(encoding%3D%22utf-8-sig%22).splitlines()%0A%20%20%20%20mo.plain_text(%22%5Cn%22.join(_line%5B%3A100%5D%20for%20_line%20in%20kerg_lines%5B%3A9%5D))%0A%20%20%20%20return%20(kerg_lines%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20The%20first%20five%20lines%20are%20not%20data%3A%20copyright%2C%20license%2C%20an%20empty%20line%2C%20a%20title%20and%20a%20subtitle%0A%20%20%20%20(*Amtliches%20Endergebnis*%2C%20official%20final%20result).%20Then%20comes%20a%20header%20spread%20over%20three%20rows.%20Read%20one%0A%20%20%20%20column%20top%20to%20bottom%3A%0A%0A%20%20%20%201.%20what%20is%20counted%3A%20*Wahlberechtigte*%20(eligible%20voters)%2C%20*W%C3%A4hlende*%20(people%20who%20voted)%2C%20*Ung%C3%BCltige*%20or%0A%20%20%20%20%20%20%20*G%C3%BCltige%20Stimmen*%20(invalid%20or%20valid%20votes)%2C%20or%20a%20party%3B%0A%20%20%20%202.%20which%20vote%3A%20each%20voter%20has%20two.%20*Erststimmen*%20(first%20votes)%20elect%20a%20local%20candidate%2C%0A%20%20%20%20%20%20%20*Zweitstimmen*%20(second%20votes)%20go%20to%20a%20party%3B%0A%20%20%20%203.%20which%20election%3A%20*Endg%C3%BCltig*%20is%20the%20final%202025%20result%2C%20*Vorperiode*%20the%20previous%20election.%0A%0A%20%20%20%20After%20the%20header%2C%20one%20row%20per%20area.%20The%20first%20four%20columns%20are%20text%3A%20*Nr*%20(the%20area's%20number)%2C%0A%20%20%20%20*Gebiet*%20(its%20name)%2C%20*geh%C3%B6rt%20zu*%20(%22belongs%20to%22)%20and%20*Gew%C3%A4hlt*%20(elected%3A%20the%20party%20of%20the%0A%20%20%20%20constituency's%20winner).%20The%20rest%20are%20counts.%20In%20*Gew%C3%A4hlt*%2C%20a%20dash%20means%20the%20winner%20did%20not%20get%20a%20seat%0A%20%20%20%20in%20the%20Bundestag%3A%20a%20marker%2C%20where%20you%20might%20expect%20a%20party.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Step%202%3A%20tell%20the%20reader%20the%20layout%2C%20then%20read%0A%0A%20%20%20%20Three%20options%20describe%20what%20you%20just%20saw%3A%20skip%205%20lines%2C%20join%203%20header%20rows%2C%20read%20the%20first%204%20columns%0A%20%20%20%20as%20text%20(the%20rest%20are%20whole%20numbers).%20The%20reader%20joins%20the%20three%20header%20rows%20with%20spaces%2C%20so%20the%20column%0A%20%20%20%20for%20eligible%20voters'%20second%20votes%20in%202025%20is%20%60Wahlberechtigte%20Zweitstimmen%20Endg%C3%BCltig%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20BundeswahlleiterinReader%2C%0A%20%20%20%20Feature%2C%0A%20%20%20%20KERG%2C%0A%20%20%20%20OPTION_WAHL_HEADER_ROWS%2C%0A%20%20%20%20OPTION_WAHL_LABEL_COLUMNS%2C%0A%20%20%20%20OPTION_WAHL_SKIPROWS%2C%0A%20%20%20%20mloda%2C%0A)%3A%0A%20%20%20%20kerg_options%20%3D%20%7B%0A%20%20%20%20%20%20%20%20BundeswahlleiterinReader%3A%20KERG%2C%0A%20%20%20%20%20%20%20%20OPTION_WAHL_SKIPROWS%3A%205%2C%0A%20%20%20%20%20%20%20%20OPTION_WAHL_HEADER_ROWS%3A%203%2C%0A%20%20%20%20%20%20%20%20OPTION_WAHL_LABEL_COLUMNS%3A%204%2C%0A%20%20%20%20%7D%0A%20%20%20%20_result%20%3D%20mloda.run_all(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20Feature(%22Gebiet%22%2C%20options%3Dkerg_options)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20Feature(%22geh%C3%B6rt%20zu%22%2C%20options%3Dkerg_options)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20Feature(%22Wahlberechtigte%20Zweitstimmen%20Endg%C3%BCltig%22%2C%20options%3Dkerg_options)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20Feature(%22W%C3%A4hlende%20Zweitstimmen%20Endg%C3%BCltig%22%2C%20options%3Dkerg_options)%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20compute_frameworks%3D%5B%22PyArrowTable%22%5D%2C%0A%20%20%20%20%20%20%20%20column_ordering%3D%22request_order%22%2C%0A%20%20%20%20)%0A%20%20%20%20election%20%3D%20_result%5B0%5D.to_pandas()%0A%20%20%20%20election%0A%20%20%20%20return%20election%2C%20kerg_options%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20The%20three%20layout%20values%20are%20the%20reader's%20defaults%2C%20made%20for%20this%20file%3B%20they%20are%20written%20out%20here%20so%20you%20can%20see%0A%20%20%20%20them.%20If%20the%20next%20election's%20file%20is%20laid%20out%20differently%2C%20you%20change%20the%20numbers.%0A%20%20%20%20%60BundeswahlleiterinReader.peek(KERG)%60%20lists%20all%20the%20columns%20you%20can%20ask%20for.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Step%203%3A%20keep%20one%20level%0A%0A%20%20%20%20The%20file%20mixes%20three%20levels%3A%20constituencies%2C%20*L%C3%A4nder*%20and%20the%20whole%20country%20(*Bundesgebiet*).%20The%0A%20%20%20%20column%20*geh%C3%B6rt%20zu*%20tells%20them%20apart%3A%20a%20constituency%20belongs%20to%20its%20Land's%20number%2C%20a%20Land%20belongs%20to%0A%20%20%20%2099%2C%20the%20Bundesgebiet%2C%20and%20the%20Bundesgebiet%20belongs%20to%20nothing.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(election%2C%20mo)%3A%0A%20%20%20%20_eligible%20%3D%20%22Wahlberechtigte%20Zweitstimmen%20Endg%C3%BCltig%22%0A%20%20%20%20_belongs_to%20%3D%20election%5B%22geh%C3%B6rt%20zu%22%5D%0A%20%20%20%20_country%20%3D%20election.loc%5B_belongs_to.isna()%2C%20_eligible%5D.iloc%5B0%5D%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20-%20%7B(_belongs_to.notna()%20%26%20(_belongs_to%20!%3D%20%2299%22)).sum()%7D%20constituencies%2C%0A%20%20%20%20%20%20%7B(_belongs_to%20%3D%3D%20%2299%22).sum()%7D%20L%C3%A4nder%2C%20%7B_belongs_to.isna().sum()%7D%20Bundesgebiet.%0A%20%20%20%20-%20Eligible%20voters%2C%20summed%20over%20every%20row%3A%20%7Belection%5B_eligible%5D.sum()%3A%2C%7D.%20In%20the%20Bundesgebiet%20row%3A%0A%20%20%20%20%20%20%7B_country%3A%2C%7D.%20Each%20voter%20is%20counted%20once%20per%20level%2C%20three%20times%20in%20all.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20So%20keep%20one%20level.%20Here%2C%20the%2016%20L%C3%A4nder%2C%20with%20their%20turnout%3A%20the%20share%20of%20eligible%20voters%20who%20voted.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(election)%3A%0A%20%20%20%20_laender%20%3D%20election%5Belection%5B%22geh%C3%B6rt%20zu%22%5D%20%3D%3D%20%2299%22%5D%0A%20%20%20%20turnout%20%3D%20_laender.assign(%0A%20%20%20%20%20%20%20%20turnout%3D_laender%5B%22W%C3%A4hlende%20Zweitstimmen%20Endg%C3%BCltig%22%5D%20%2F%20_laender%5B%22Wahlberechtigte%20Zweitstimmen%20Endg%C3%BCltig%22%5D%0A%20%20%20%20).sort_values(%22turnout%22%2C%20ascending%3DFalse)%5B%5B%22Gebiet%22%2C%20%22turnout%22%5D%5D%0A%20%20%20%20turnout.style.format(%7B%22turnout%22%3A%20%22%7B%3A.1%25%7D%22%7D).hide(axis%3D%22index%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Pitfall%3A%20an%20empty%20cell%20is%20not%20a%20zero%0A%0A%20%20%20%20The%20CDU%20(*Christlich%20Demokratische%20Union%20Deutschlands*%2C%20Christian%20Democratic%20Union)%20runs%20candidates%20in%0A%20%20%20%20every%20Land%20but%20Bavaria%20(*Bayern*%2C%20Land%20number%2009).%20In%20Bavaria%20its%20sister%20party%20runs%20instead%2C%20the%20CSU%0A%20%20%20%20(*Christlich-Soziale%20Union%20in%20Bayern*%2C%20Christian%20Social%20Union).%20So%20in%20Bavaria's%20constituencies%20the%20CDU%0A%20%20%20%20cells%20are%20empty.%20Here%20are%20both%20parties'%20first%20votes%20there%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Feature%2C%20kerg_options%2C%20mloda)%3A%0A%20%20%20%20_cdu%20%3D%20%22Christlich%20Demokratische%20Union%20Deutschlands%20Erststimmen%20Endg%C3%BCltig%22%0A%20%20%20%20_csu%20%3D%20%22Christlich-Soziale%20Union%20in%20Bayern%20e.V.%20Erststimmen%20Endg%C3%BCltig%22%0A%20%20%20%20_result%20%3D%20mloda.run_all(%0A%20%20%20%20%20%20%20%20%5BFeature(_name%2C%20options%3Dkerg_options)%20for%20_name%20in%20(%22Gebiet%22%2C%20%22geh%C3%B6rt%20zu%22%2C%20_cdu%2C%20_csu)%5D%2C%0A%20%20%20%20%20%20%20%20compute_frameworks%3D%5B%22PyArrowTable%22%5D%2C%0A%20%20%20%20%20%20%20%20column_ordering%3D%22request_order%22%2C%0A%20%20%20%20)%0A%20%20%20%20parties%20%3D%20_result%5B0%5D.to_pandas().rename(columns%3D%7B_cdu%3A%20%22CDU%22%2C%20_csu%3A%20%22CSU%22%7D)%0A%20%20%20%20parties%5Bparties%5B%22geh%C3%B6rt%20zu%22%5D%20%3D%3D%20%2209%22%5D%0A%20%20%20%20return%20(parties%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20The%20reader%20turns%20an%20empty%20cell%20into%20a%20missing%20value%2C%20which%20pandas%20shows%20as%20%60NaN%60.%20It%20does%20not%20turn%20it%0A%20%20%20%20into%200.%20That%20is%20right%3A%20the%20CDU%20did%20not%20get%20zero%20votes%0A%20%20%20%20in%20Bavaria%2C%20it%20had%20no%20candidate%20there.%20See%20what%20a%200%20would%20do%20to%20the%20CDU's%20average%20result%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20parties)%3A%0A%20%20%20%20_cdu%20%3D%20parties.loc%5Bparties%5B%22geh%C3%B6rt%20zu%22%5D.notna()%20%26%20(parties%5B%22geh%C3%B6rt%20zu%22%5D%20!%3D%20%2299%22)%2C%20%22CDU%22%5D%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20-%20Average%20CDU%20first%20votes%20per%20constituency%2C%20empty%20cells%20left%20out%3A%20%7B_cdu.mean()%3A%2C.0f%7D%0A%20%20%20%20-%20The%20same%20with%20empty%20cells%20as%200%3A%20%7B_cdu.fillna(0).mean()%3A%2C.0f%7D%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20Other%20German%20files%20have%20more%20traps%3A%20a%20decimal%20comma%20(%601%2C5%60%20means%201.5)%2C%20a%20%60-%60%20that%20means%20exactly%20zero%2C%0A%20%20%20%20a%20%60.%60%20that%20means%20the%20number%20is%20unknown.%20In%20columns%20read%20as%20numbers%2C%20this%20plugin's%20CSV%20readers%20handle%0A%20%20%20%20those%20too.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%20Part%202%3A%20air%20quality%0A%0A%20%20%20%20The%20federal%20environment%20agency%2C%20the%20*Umweltbundesamt*%20(UBA)%2C%20measures%20air%20quality%20at%20stations%20all%0A%20%20%20%20over%20Germany.%20It%20publishes%20the%20measurements%20through%20a%20web%20service.%20So%20the%20options%20are%20your%20question%3A%0A%0A%20%20%20%20-%20which%20station%3A%20143%20is%20Berlin%20Grunewald%2C%20in%20a%20forest%20on%20the%20edge%20of%20the%20city%3B%0A%20%20%20%20-%20which%20pollutant%3A%20component%203%20is%20ozone%2C%20measured%20in%20%C2%B5g%2Fm%C2%B3%20(millionths%20of%20a%20gram%20per%20cubic%20metre%20of%20air)%3B%0A%20%20%20%20-%20which%20average%3A%20scope%202%20is%20the%20average%20over%20one%20hour%3B%0A%20%20%20%20-%20which%20days%3A%20from%20and%20to%2C%20as%20%60YYYY-MM-DD%60.%0A%0A%20%20%20%20The%20ids%20come%20from%20the%20UBA's%20own%20lists%20(its%20%60stations%60%2C%20%60components%60%20and%20%60scopes%60%20endpoints).%0A%20%20%20%20%60UbaAirReader%3A%20True%60%20only%20picks%20the%20reader%3B%20the%20other%20options%20are%0A%20%20%20%20the%20question.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Step%204%3A%20read%20one%20day%20of%20ozone%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20Feature%2C%0A%20%20%20%20OPTION_UBA_COMPONENT%2C%0A%20%20%20%20OPTION_UBA_DATE_FROM%2C%0A%20%20%20%20OPTION_UBA_DATE_TO%2C%0A%20%20%20%20OPTION_UBA_SCOPE%2C%0A%20%20%20%20OPTION_UBA_STATION%2C%0A%20%20%20%20UbaAirReader%2C%0A%20%20%20%20mloda%2C%0A)%3A%0A%20%20%20%20STATION%2C%20OZONE%2C%20ONE_HOUR%2C%20DAY%20%3D%20143%2C%203%2C%202%2C%20%222025-07-01%22%0A%20%20%20%20ozone_options%20%3D%20%7B%0A%20%20%20%20%20%20%20%20UbaAirReader%3A%20True%2C%0A%20%20%20%20%20%20%20%20OPTION_UBA_STATION%3A%20STATION%2C%0A%20%20%20%20%20%20%20%20OPTION_UBA_COMPONENT%3A%20OZONE%2C%0A%20%20%20%20%20%20%20%20OPTION_UBA_SCOPE%3A%20ONE_HOUR%2C%0A%20%20%20%20%20%20%20%20OPTION_UBA_DATE_FROM%3A%20DAY%2C%0A%20%20%20%20%20%20%20%20OPTION_UBA_DATE_TO%3A%20DAY%2C%0A%20%20%20%20%7D%0A%20%20%20%20_result%20%3D%20mloda.run_all(%0A%20%20%20%20%20%20%20%20%5BFeature(%22date_start%22%2C%20options%3Dozone_options)%2C%20Feature(%22value%22%2C%20options%3Dozone_options)%5D%2C%0A%20%20%20%20%20%20%20%20compute_frameworks%3D%5B%22PyArrowTable%22%5D%2C%0A%20%20%20%20%20%20%20%20column_ordering%3D%22request_order%22%2C%0A%20%20%20%20)%0A%20%20%20%20_result%5B0%5D.to_pandas()%0A%20%20%20%20return%20DAY%2C%20ONE_HOUR%2C%20OZONE%2C%20STATION%2C%20ozone_options%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20One%20row%20per%20hour%3A%20when%20the%20hour%20starts%20and%20the%20average%20ozone%20over%20it.%20The%20UBA%20gives%20times%20%22in%20the%20time%0A%20%20%20%20zone%20valid%20at%20the%20measurement%20time%20(CET%20or%20CEST)%22%3B%20every%20day%20has%2024%20rows%2C%20even%20when%20the%20clocks%20change.%0A%20%20%20%20The%20reader%20also%20has%20a%0A%20%20%20%20%60date_end%60%20column%3B%20the%20day's%20last%20hour%20ends%20at%20%6024%3A00%60%2C%20which%20pandas%20cannot%20read%20as%20a%20time%2C%20so%20use%0A%20%20%20%20%60date_start%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Try%20it%3A%20pick%20a%20day%0A%0A%20%20%20%20Ozone%20forms%20in%20sunlight%2C%20so%20on%20a%20summer%20day%20it%20usually%20peaks%20in%20the%20afternoon.%20Pick%20a%20day%3B%20the%20chart%0A%20%20%20%20below%20shows%20that%20day's%20hourly%20ozone%20at%20the%20same%20station.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(datetime%2C%20mo)%3A%0A%20%20%20%20_yesterday%20%3D%20datetime.date.today()%20-%20datetime.timedelta(days%3D1)%0A%20%20%20%20day%20%3D%20mo.ui.date(start%3D%222016-01-01%22%2C%20stop%3D_yesterday.isoformat()%2C%20value%3D%222025-07-01%22%2C%20label%3D%22Day%22)%0A%20%20%20%20day%0A%20%20%20%20return%20(day%2C)%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20Feature%2C%0A%20%20%20%20OPTION_UBA_DATE_FROM%2C%0A%20%20%20%20OPTION_UBA_DATE_TO%2C%0A%20%20%20%20alt%2C%0A%20%20%20%20day%2C%0A%20%20%20%20mloda%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20ozone_options%2C%0A%20%20%20%20pd%2C%0A)%3A%0A%20%20%20%20%23%20Step%204's%20question%20for%20the%20picked%20day%2C%20written%20as%20%22YYYY-MM-DD%22.%0A%20%20%20%20_options%20%3D%20%7B**ozone_options%2C%20OPTION_UBA_DATE_FROM%3A%20day.value.isoformat()%2C%20OPTION_UBA_DATE_TO%3A%20day.value.isoformat()%7D%0A%20%20%20%20_result%20%3D%20mloda.run_all(%0A%20%20%20%20%20%20%20%20%5BFeature(%22date_start%22%2C%20options%3D_options)%2C%20Feature(%22value%22%2C%20options%3D_options)%5D%2C%0A%20%20%20%20%20%20%20%20compute_frameworks%3D%5B%22PyArrowTable%22%5D%2C%0A%20%20%20%20%20%20%20%20column_ordering%3D%22request_order%22%2C%0A%20%20%20%20)%0A%20%20%20%20_ozone%20%3D%20_result%5B0%5D.to_pandas()%0A%20%20%20%20mo.stop(_ozone.empty%2C%20mo.md(f%22No%20measurements%20for%20%7Bday.value%7D.%20Pick%20another%20day.%22))%0A%20%20%20%20alt.Chart(%0A%20%20%20%20%20%20%20%20_ozone.assign(hour%3Dpd.to_datetime(_ozone%5B%22date_start%22%5D).dt.hour)%2C%20title%3Df%22Ozone%20on%20%7Bday.value%7D%22%0A%20%20%20%20).mark_line(point%3DTrue).encode(%0A%20%20%20%20%20%20%20%20x%3Dalt.X(%22hour%3AQ%22%2C%20title%3D%22Hour%20the%20measurement%20starts%22)%2C%0A%20%20%20%20%20%20%20%20y%3Dalt.Y(%22value%3AQ%22%2C%20title%3D%22Ozone%20(%C2%B5g%2Fm%C2%B3)%22)%2C%0A%20%20%20%20%20%20%20%20tooltip%3D%5B%22date_start%22%2C%20%22value%22%5D%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Where%20the%20data%20came%20from%0A%0A%20%20%20%20The%20election%20file%20names%20its%20publisher%20and%20license%20in%20its%20own%20first%20two%20lines.%20For%20both%20sources%2C%20the%0A%20%20%20%20copy%20on%20your%20disk%20records%20when%20you%20downloaded%20it.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20BundeswahlleiterinReader%2C%0A%20%20%20%20DAY%2C%0A%20%20%20%20DownloadCache%2C%0A%20%20%20%20KERG%2C%0A%20%20%20%20ONE_HOUR%2C%0A%20%20%20%20OZONE%2C%0A%20%20%20%20STATION%2C%0A%20%20%20%20UbaAirReader%2C%0A%20%20%20%20build_client%2C%0A%20%20%20%20kerg_lines%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20uba_measures_url%2C%0A)%3A%0A%20%20%20%20%23%20The%20same%20address%20the%20reader%20built%20in%20Step%204%2C%20so%20the%20cache%20finds%20that%20copy.%0A%20%20%20%20_ozone_url%20%3D%20uba_measures_url(station%3DSTATION%2C%20component%3DOZONE%2C%20scope%3DONE_HOUR%2C%20date_from%3DDAY%2C%20date_to%3DDAY)%0A%20%20%20%20with%20build_client()%20as%20_client%3A%0A%20%20%20%20%20%20%20%20with%20DownloadCache(BundeswahlleiterinReader.cache_dir%2C%20client%3D_client)%20as%20_cache%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_kerg_retrieved%20%3D%20_cache.get_or_download(KERG%2C%20revalidate%3DFalse).retrieved_at%0A%20%20%20%20%20%20%20%20with%20DownloadCache(UbaAirReader.cache_dir%2C%20client%3D_client)%20as%20_cache%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_ozone_retrieved%20%3D%20_cache.get_or_download(_ozone_url%2C%20revalidate%3DFalse).retrieved_at%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20**Election%20results**%0A%0A%20%20%20%20-%20**Publisher%3A**%20%60%7Bkerg_lines%5B0%5D.strip(%22%3B%22)%7D%60%0A%20%20%20%20-%20**License%3A**%20%60%7Bkerg_lines%5B1%5D.strip(%22%3B%22)%7D%60%0A%20%20%20%20-%20**File%3A**%20%7BKERG%7D%0A%20%20%20%20-%20**Retrieved%3A**%20%7B_kerg_retrieved%3A%25Y-%25m-%25d%7D%0A%0A%20%20%20%20**Ozone%20(Step%204)**%0A%0A%20%20%20%20-%20**Source%3A**%20Umweltbundesamt%20(German%20Federal%20Environment%20Agency)%2C%0A%20%20%20%20%20%20https%3A%2F%2Fwww.umweltbundesamt.de%2Fdaten%2Fluft%2Fluftdaten%0A%20%20%20%20-%20**License%3A**%20%60%7BUbaAirReader.LICENSE%7D%60%2C%20the%20id%20the%20reader%20declares%20for%20the%20UBA's%20terms%20(see%20Readers)%0A%20%20%20%20-%20**Query%3A**%20%7B_ozone_url%7D%0A%20%20%20%20-%20**Retrieved%3A**%20%7B_ozone_retrieved%3A%25Y-%25m-%25d%7D%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20Name%20the%20source%20when%20you%20reuse%20the%20data.%20The%20election%20license%20(*Datenlizenz%20Deutschland*%2C%20Data%0A%20%20%20%20License%20Germany%2C%20attribution%2C%20version%202.0)%20also%20asks%20you%20to%20say%20what%20you%20changed%3A%0A%20%20%20%20here%2C%20you%20kept%20only%20the%20L%C3%A4nder%20rows%20and%20computed%20the%20turnout.%20The%20ozone%20values%20are%20shown%20as%20read.%0A%0A%20%20%20%20%23%23%20Next%0A%0A%20%20%20%20%5BTutorial%203%5D(https%3A%2F%2Fgithub.com%2Fmloda-ai%2Fmloda-plugin-govdata%2Fblob%2Fmain%2Ftutorials%2F03_destatis_table.py)%0A%20%20%20%20reads%20a%20table%20from%20Destatis%2C%20the%20federal%20statistical%20office%20(it%20needs%20a%20free%20account).%0A%20%20%20%20%5BReaders%5D(https%3A%2F%2Fgithub.com%2Fmloda-ai%2Fmloda-plugin-govdata%2Fblob%2Fmain%2Fdocs%2Freaders.md)%0A%20%20%20%20describes%20the%20election%20and%20air%20quality%20readers%20and%20their%20options.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
ea660a9560fc46e8663a4b8d46460e80