dodo.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. """
  2. Define the tasks for UNFCCC data repository
  3. """
  4. import os
  5. import sys
  6. import datalad.api
  7. from doit import get_var
  8. root_path = "."
  9. os.environ["UNFCCC_GHG_ROOT_PATH"] = root_path
  10. from unfccc_ghg_data.helper.functions import ( # noqa: E402
  11. get_country_datasets,
  12. get_country_submissions,
  13. )
  14. from unfccc_ghg_data.unfccc_crf_reader.unfccc_crf_reader_devel import ( # noqa: E402
  15. read_year_to_test_specs,
  16. )
  17. from unfccc_ghg_data.unfccc_crf_reader.unfccc_crf_reader_prod import ( # noqa: E402
  18. read_crf_for_country_datalad,
  19. read_new_crf_for_year_datalad,
  20. )
  21. from unfccc_ghg_data.unfccc_di_reader import ( # noqa: E402
  22. process_DI_for_country_datalad,
  23. process_DI_for_country_group_datalad,
  24. read_DI_for_country_datalad,
  25. read_DI_for_country_group_datalad,
  26. )
  27. def set_root_path():
  28. """Set the root folder for the repository"""
  29. os.environ["UNFCCC_GHG_ROOT_PATH"] = root_path
  30. def map_folders(parent_folder):
  31. """
  32. Create or update the folder mapping in the given folder
  33. Internal function
  34. """
  35. datalad.api.run(
  36. cmd="python3 src/unfccc_ghg_data/helper/folder_mapping.py "
  37. f"--folder={parent_folder}",
  38. dataset=root_path,
  39. message=f"Update folder mapping for {parent_folder}",
  40. outputs=f"{parent_folder}/folder_mapping.json",
  41. dry_run=None,
  42. explicit=True,
  43. )
  44. def task_in_venv():
  45. """
  46. Check if code run from virtual environment and throw an error is not.
  47. Returns
  48. -------
  49. Nothing
  50. """
  51. def in_venv():
  52. if sys.prefix == sys.base_prefix:
  53. raise ValueError( # noqa: TRY003
  54. "You need to run the code from the virtual environment."
  55. )
  56. return {
  57. "actions": [in_venv],
  58. }
  59. # Task to create the mapping files which map folder names to ISO 3-letter country codes
  60. read_config_folder = {
  61. "folder": get_var("folder", None),
  62. }
  63. def task_map_folders():
  64. """
  65. Create or update the folder mapping in the given folder
  66. """
  67. return {
  68. "actions": [(map_folders, [read_config_folder["folder"]])],
  69. "verbosity": 2,
  70. "setup": ["in_venv"],
  71. }
  72. # Tasks for getting submissions and downloading them
  73. def task_update_bur():
  74. """Update list of BUR submissions"""
  75. return {
  76. "targets": ["downloaded_data/UNFCCC/submissions-bur.csv"],
  77. "actions": [
  78. (
  79. datalad.api.run,
  80. [],
  81. {
  82. "cmd": "python3 src/unfccc_ghg_data/unfccc_downloader/"
  83. "fetch_submissions_bur.py",
  84. "dataset": root_path,
  85. "message": "Fetch BUR submissions",
  86. "outputs": "downloaded_data/UNFCCC/submissions-bur.csv",
  87. "dry_run": None,
  88. "explicit": True,
  89. },
  90. ),
  91. ],
  92. "verbosity": 2,
  93. "setup": ["in_venv"],
  94. }
  95. def task_download_bur():
  96. """Download BUR submissions"""
  97. return {
  98. #'file_dep': ['downloaded_data/UNFCCC/submissions-bur.csv'],
  99. # deactivate file_dep fow now as it will always run fetch submissions
  100. # before download
  101. "actions": [
  102. (
  103. datalad.api.run,
  104. [],
  105. {
  106. "cmd": "python3 src/unfccc_ghg_data/unfccc_downloader/"
  107. "download_nonannexI.py --category=BUR",
  108. "dataset": root_path,
  109. "message": "Download BUR submissions",
  110. "inputs": "downloaded_data/UNFCCC/submissions-bur.csv",
  111. "dry_run": None,
  112. "explicit": False,
  113. },
  114. ),
  115. (map_folders, ["downloaded_data/UNFCCC"]),
  116. ],
  117. "verbosity": 2,
  118. "setup": ["in_venv"],
  119. }
  120. def task_update_nc():
  121. """Update list of NC submissions"""
  122. def fetch_nc():
  123. datalad.api.run(
  124. cmd="python3 src/unfccc_ghg_data/unfccc_downloader/"
  125. "fetch_submissions_nc.py",
  126. dataset=root_path,
  127. message="Fetch NC submissions",
  128. outputs="downloaded_data/UNFCCC/submissions-nc.csv",
  129. dry_run=None,
  130. explicit=True,
  131. )
  132. return {
  133. "targets": ["downloaded_data/UNFCCC/submissions-nc.csv"],
  134. "actions": [
  135. (
  136. datalad.api.run,
  137. [],
  138. {
  139. "cmd": "python3 src/unfccc_ghg_data/unfccc_downloader/"
  140. "fetch_submissions_nc.py",
  141. "dataset": root_path,
  142. "message": "Fetch NC submissions",
  143. "outputs": "downloaded_data/UNFCCC/submissions-nc.csv",
  144. "dry_run": None,
  145. "explicit": True,
  146. },
  147. ),
  148. ],
  149. "verbosity": 2,
  150. "setup": ["in_venv"],
  151. }
  152. def task_download_nc():
  153. """Download BUR submissions"""
  154. return {
  155. #'file_dep': ['downloaded_data/UNFCCC/submissions-bur.csv'],
  156. # deactivate file_dep fow now as it will always run fetch submissions
  157. # before download
  158. "actions": [
  159. (
  160. datalad.api.run,
  161. [],
  162. {
  163. "cmd": "python3 src/unfccc_ghg_data/unfccc_downloader/"
  164. "download_nonannexI.py --category=NC",
  165. "dataset": root_path,
  166. "message": "Download NC submissions",
  167. "inputs": "downloaded_data/UNFCCC/submissions-nc.csv",
  168. "dry_run": None,
  169. "explicit": False,
  170. },
  171. ),
  172. (map_folders, ["downloaded_data/UNFCCC"]),
  173. ],
  174. "verbosity": 2,
  175. "setup": ["in_venv"],
  176. }
  177. # annexI data: one update call for all data types (as they are on one page)
  178. # but for each year separately.
  179. # downloading is per year and
  180. update_aI_config = {
  181. "year": get_var("year", None),
  182. "category": get_var("category", None),
  183. }
  184. def task_update_annexi():
  185. """Update list of AnnexI submissions"""
  186. return {
  187. "targets": [
  188. f"downloaded_data/UNFCCC/submissions-annexI_{update_aI_config['year']}.csv"
  189. ],
  190. "actions": [
  191. (
  192. datalad.api.run,
  193. [],
  194. {
  195. "cmd": "python src/unfccc_ghg_data/unfccc_downloader/"
  196. "fetch_submissions_annexI.py "
  197. f"--year={update_aI_config['year']}",
  198. "dataset": root_path,
  199. "message": f"Fetch AnnexI submissions for {update_aI_config['year']}",
  200. "outputs": f"downloaded_data/UNFCCC/submissions-annexI_"
  201. f"{update_aI_config['year']}.csv",
  202. "dry_run": None,
  203. "explicit": True,
  204. },
  205. ),
  206. ],
  207. "verbosity": 2,
  208. "setup": ["in_venv"],
  209. }
  210. def task_download_annexi():
  211. """Download AnnexI submissions"""
  212. return {
  213. # 'file_dep': [f"downloaded_data/UNFCCC/submissions-annex1_"
  214. # f"{update_aI_config['year']}.csv"],
  215. # deactivate file_dep fow now as it will always run fetch submissions
  216. # before download
  217. "actions": [
  218. (
  219. datalad.api.run,
  220. [],
  221. {
  222. "cmd": "python src/unfccc_ghg_data/unfccc_downloader/download_annexI.py "
  223. f"--category={update_aI_config['category']} "
  224. f"--year={update_aI_config['year']}",
  225. "dataset": root_path,
  226. "message": f"Download AnnexI submissions for "
  227. f"{update_aI_config['category']}"
  228. f"{update_aI_config['year']}",
  229. "inputs": f"downloaded_data/UNFCCC/submissions-annexI_"
  230. f"{update_aI_config['year']}.csv",
  231. "dry_run": None,
  232. "explicit": False,
  233. },
  234. ),
  235. (map_folders, ["downloaded_data/UNFCCC"]),
  236. ],
  237. "verbosity": 2,
  238. "setup": ["in_venv"],
  239. }
  240. # BTR data: one update call for all data types (as they are on one page)
  241. # but for each submission round separately.
  242. # downloading is per submission round
  243. update_btr_config = {
  244. "round": get_var("round", None),
  245. }
  246. def task_update_btr():
  247. """Update list of BTR submissions"""
  248. return {
  249. "targets": [
  250. f"downloaded_data/UNFCCC/submissions-BTR{update_btr_config['round']}.csv"
  251. ],
  252. "actions": [
  253. (
  254. datalad.api.run,
  255. [],
  256. {
  257. "cmd": "python src/unfccc_ghg_data/unfccc_downloader/"
  258. "fetch_submissions_btr.py "
  259. f"--round={update_btr_config['round']}",
  260. "dataset": root_path,
  261. "message": f"Fetch Biannial Transparency Report submissions for "
  262. f"BTR{update_btr_config['round']}",
  263. "outputs": f"downloaded_data/UNFCCC/submissions-BTR"
  264. f"{update_btr_config['round']}.csv",
  265. "dry_run": None,
  266. "explicit": True,
  267. },
  268. ),
  269. ],
  270. "verbosity": 2,
  271. "setup": ["in_venv"],
  272. }
  273. def task_download_btr():
  274. """Download BTR submissions"""
  275. return {
  276. # 'file_dep': [f"downloaded_data/UNFCCC/submissions-btr.csv "
  277. # f"{update_btr_config['round']}.csv"],
  278. # deactivate file_dep fow now as it will always run fetch submissions
  279. # before download
  280. "actions": [
  281. (
  282. datalad.api.run,
  283. [],
  284. {
  285. "cmd": "python src/unfccc_ghg_data/unfccc_downloader/download_btr.py "
  286. f"--round={update_btr_config['round']}",
  287. "dataset": root_path,
  288. "message": "Download BTR submissions for "
  289. f"BTR{update_btr_config['round']}",
  290. "inputs": f"downloaded_data/UNFCCC/submissions-BTR"
  291. f"{update_btr_config['round']}.csv",
  292. "dry_run": None,
  293. "explicit": False,
  294. },
  295. ),
  296. (map_folders, ["downloaded_data/UNFCCC"]),
  297. ],
  298. "verbosity": 2,
  299. "setup": ["in_venv"],
  300. }
  301. def task_download_ndc():
  302. """Download NDC submissions"""
  303. return {
  304. "actions": [
  305. (
  306. datalad.api.run,
  307. [],
  308. {
  309. "cmd": "src/unfccc_ghg_data/unfccc_downloader/download_ndc.py",
  310. "dataset": root_path,
  311. "message": "Download NDC submissions",
  312. "inputs": None,
  313. "dry_run": None,
  314. "explicit": False,
  315. },
  316. ),
  317. (map_folders, ["downloaded_data/UNFCCC"]),
  318. ],
  319. "verbosity": 2,
  320. "setup": ["in_venv"],
  321. }
  322. # read UNFCCC submissions.
  323. # datalad run is called from within the read_UNFCCC_submission.py script
  324. read_config = {
  325. "country": get_var("country", None),
  326. "submission": get_var("submission", None),
  327. }
  328. # TODO: make individual task for non-UNFCCC submissions
  329. def task_read_unfccc_submission():
  330. """Read submission for a country (if code exists) (not for CRF)
  331. Datalad is called from `read_UNFCCC_submission`, so we can just call this script
  332. here.
  333. TODO: check if it makes sense to convert script to function
  334. """
  335. return {
  336. "actions": [
  337. f"python src/unfccc_ghg_data/unfccc_reader/read_UNFCCC_submission.py "
  338. f"--country={read_config['country']} "
  339. f"--submission={read_config['submission']}",
  340. (map_folders, ["extracted_data/UNFCCC"]),
  341. ],
  342. "verbosity": 2,
  343. "setup": ["in_venv"],
  344. }
  345. # read UNFCCC CRF submissions.
  346. # datalad run is called from within the read_UNFCCC_submission.py script
  347. read_config_crf = {
  348. "country": get_var("country", None),
  349. "submission_year": get_var("submission_year", None),
  350. "submission_date": get_var("submission_date", None),
  351. "re_read": get_var("re_read", False),
  352. "countries": get_var("countries", None),
  353. "data_year": get_var("data_year", None),
  354. "totest": get_var("totest", None),
  355. "type": get_var("type", "CRF"),
  356. }
  357. def task_read_unfccc_crf_submission():
  358. """Read CRF submission for a country"""
  359. def read_CRF():
  360. if read_config_crf["re_read"] == "True":
  361. re_read = True
  362. else:
  363. re_read = False
  364. read_crf_for_country_datalad(
  365. read_config_crf["country"],
  366. submission_year=int(read_config_crf["submission_year"]),
  367. submission_date=read_config_crf["submission_date"],
  368. re_read=re_read,
  369. type=read_config_crf["type"],
  370. )
  371. return {
  372. "actions": [
  373. (read_CRF,),
  374. (map_folders, ["extracted_data/UNFCCC"]),
  375. ],
  376. "task_dep": ["set_env"],
  377. "verbosity": 2,
  378. "setup": ["in_venv"],
  379. }
  380. def task_read_new_unfccc_crf_for_year():
  381. """
  382. Read CRF/CRT submission for all countries for given submission year.
  383. By default only reads data not present yet. Only reads the latest updated
  384. submission for each country.
  385. """
  386. def read_new_CRF():
  387. if read_config_crf["re_read"] == "True":
  388. re_read = True
  389. else:
  390. re_read = False
  391. read_new_crf_for_year_datalad(
  392. submission_year=int(read_config_crf["submission_year"]),
  393. # countries=read_config_crf["countries"],
  394. re_read=re_read,
  395. type=read_config_crf["type"],
  396. )
  397. return {
  398. "actions": [
  399. (read_new_CRF,),
  400. (map_folders, ["extracted_data/UNFCCC"]),
  401. ],
  402. "verbosity": 2,
  403. "setup": ["in_venv"],
  404. }
  405. def task_test_read_unfccc_crf_for_year():
  406. """
  407. Test CRF/CRT reading.
  408. Test CRF/CRT with a single year only for speed and logging to extend specifications
  409. if necessary.
  410. """
  411. def read_CRF():
  412. if read_config_crf["totest"] == "True":
  413. totest = True
  414. else:
  415. totest = False
  416. if read_config_crf["data_year"] is not None:
  417. data_year = int(read_config_crf["data_year"])
  418. else:
  419. data_year = None
  420. read_year_to_test_specs(
  421. submission_year=int(read_config_crf["submission_year"]),
  422. data_year=data_year,
  423. totest=totest,
  424. country_code=read_config_crf["country"],
  425. type=read_config_crf["type"],
  426. )
  427. return {
  428. "actions": [
  429. (read_CRF,),
  430. (map_folders, ["extracted_data/UNFCCC"]),
  431. ],
  432. "verbosity": 2,
  433. "setup": ["in_venv"],
  434. }
  435. def task_compile_raw_unfccc_crf_for_year():
  436. """
  437. Collect all latest CRF/CRT submissions for a given year / submission round
  438. Reads the latest data from the extracted data folder for each country.
  439. Notifies the user if new data are available in the downloaded_data folder
  440. which have not yet been read.
  441. Data are saved in the datasets/UNFCCC/[CRFYYYY|CRTX] folder.
  442. TODO: could make a function from the script
  443. """
  444. actions = [
  445. f"python "
  446. f"src/unfccc_ghg_data/unfccc_crf_reader/crf_raw_for_year.py "
  447. f"--submission_year={read_config_crf['submission_year']} "
  448. f"--type={read_config_crf['type']} "
  449. ]
  450. return {
  451. "actions": actions,
  452. "task_dep": ["set_env"],
  453. "verbosity": 2,
  454. "setup": ["in_venv"],
  455. }
  456. # tasks for DI reader
  457. # TODO DI tasks need testing
  458. # datalad run is called from within the read_unfccc_di_for_country.py script
  459. read_config_di = {
  460. "country": get_var("country", None),
  461. "date": get_var("date", None),
  462. "annexI": get_var("annexI", False),
  463. # "countries": get_var('countries', None),
  464. }
  465. def task_read_unfccc_di_for_country():
  466. """Read DI data for a country"""
  467. return {
  468. "actions": [
  469. (read_DI_for_country_datalad, [read_config_di["country"]]),
  470. (map_folders, ["extracted_data/UNFCCC"]),
  471. ],
  472. "verbosity": 2,
  473. "setup": ["in_venv"],
  474. }
  475. def task_process_unfccc_di_for_country():
  476. """Process DI data for a country"""
  477. return {
  478. "actions": [
  479. (
  480. process_DI_for_country_datalad,
  481. [],
  482. {
  483. "country": read_config_di["country"],
  484. "date_str": read_config_di["date"],
  485. },
  486. ),
  487. (map_folders, ["extracted_data/UNFCCC"]),
  488. ],
  489. "verbosity": 2,
  490. "setup": ["in_venv"],
  491. }
  492. def task_read_unfccc_di_for_country_group():
  493. """Read DI data for a country group"""
  494. def read_DI():
  495. if read_config_di["annexI"] == "True":
  496. annexI = True
  497. else:
  498. annexI = False
  499. read_DI_for_country_group_datalad(annexI=annexI)
  500. return {
  501. "actions": [(read_DI,), (map_folders, ["extracted_data/UNFCCC"])],
  502. "verbosity": 2,
  503. "setup": ["in_venv"],
  504. }
  505. def task_process_unfccc_di_for_country_group():
  506. """Process DI data for a country group"""
  507. def proc_DI():
  508. if read_config_di["annexI"] == "True":
  509. annexI = True
  510. else:
  511. annexI = False
  512. process_DI_for_country_group_datalad(
  513. annexI=annexI,
  514. date_str=read_config_di["date"],
  515. )
  516. return {
  517. "actions": [(proc_DI,), (map_folders, ["extracted_data/UNFCCC"])],
  518. "verbosity": 2,
  519. "setup": ["in_venv"],
  520. }
  521. # general tasks
  522. def task_country_info():
  523. """
  524. Print information on submissions and datasets available for given country
  525. """
  526. def country_info(country):
  527. # print available submissions
  528. print("=" * 15 + " Available submissions " + "=" * 15)
  529. get_country_submissions(country, True)
  530. print("")
  531. # print available datasets
  532. print("=" * 15 + " Available datasets " + "=" * 15)
  533. get_country_datasets(country, True)
  534. return {
  535. "actions": [(country_info, [read_config["country"]])],
  536. "verbosity": 2,
  537. "setup": ["in_venv"],
  538. }