dodo.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. # TODO: task for folder mapping
  10. # create virtual environment
  11. # def task_setup_venv():
  12. # """Create virtual environment"""
  13. # return {
  14. # 'file_dep': ['requirements_dev.txt', 'setup.cfg', 'pyproject.toml'],
  15. # 'actions': ['python3 -m venv venv',
  16. # './venv/bin/pip install --upgrade pip wheel',
  17. # #'./venv/bin/pip install -Ur unfccc_ghg_data/requirements.txt',
  18. # './venv/bin/pip install --upgrade --upgrade-strategy '
  19. # 'eager -e .[dev]',
  20. # 'touch venv',],
  21. # 'targets': ['venv'],
  22. # 'verbosity': 2,
  23. # }
  24. def task_in_venv():
  25. """
  26. Check if code run from virtual environment and throw an error is not.
  27. Returns
  28. -------
  29. Nothing
  30. """
  31. def in_venv():
  32. if sys.prefix == sys.base_prefix:
  33. raise ValueError( # noqa: TRY003
  34. "You need to run the code from the virtual environment."
  35. )
  36. return {
  37. "actions": [in_venv],
  38. }
  39. # set UNFCCC_GHG_ROOT_PATH environment variable
  40. def task_set_env():
  41. """
  42. Set the environment variable for the module so data is stored in the correct folders
  43. """
  44. def set_root_path():
  45. os.environ["UNFCCC_GHG_ROOT_PATH"] = "."
  46. return {
  47. "actions": [set_root_path],
  48. }
  49. # Task to create the mapping files which map folder names to ISO 3-letter country codes
  50. read_config_folder = {
  51. "folder": get_var("folder", None),
  52. }
  53. def task_map_folders():
  54. """
  55. Create or update the folder mapping in the given folder
  56. """
  57. return {
  58. "actions": [
  59. f"python src/unfccc_ghg_data/helper/folder_mapping.py "
  60. f"--folder={read_config_folder['folder']}"
  61. ],
  62. "task_dep": ["set_env"],
  63. "verbosity": 2,
  64. "setup": ["in_venv"],
  65. }
  66. # Tasks for getting submissions and downloading them
  67. def task_update_bur():
  68. """Update list of BUR submissions"""
  69. return {
  70. "targets": ["downloaded_data/UNFCCC/submissions-bur.csv"],
  71. # "actions": [
  72. # 'datalad run -m "Fetch BUR submissions" '
  73. # "-o downloaded_data/UNFCCC/submissions-bur.csv "
  74. # "python src/unfccc_ghg_data/unfccc_downloader/fetch_submissions_bur.py"
  75. # ],
  76. "actions": [
  77. datalad.api.run(
  78. cmd="python3 src/unfccc_ghg_data/unfccc_downloader/"
  79. "fetch_submissions_bur.py",
  80. dataset=root_path,
  81. message="Fetch BUR submissions",
  82. outputs="downloaded_data/UNFCCC/submissions-bur.csv",
  83. dry_run=None,
  84. explicit=True,
  85. )
  86. ],
  87. "task_dep": ["set_env"],
  88. "verbosity": 2,
  89. "setup": ["in_venv"],
  90. }
  91. def task_download_bur():
  92. """Download BUR submissions"""
  93. return {
  94. #'file_dep': ['downloaded_data/UNFCCC/submissions-bur.csv'],
  95. # deactivate file_dep fow now as it will always run fetch submissions
  96. # before download
  97. "actions": [
  98. 'datalad run -m "Download BUR submissions" '
  99. "-i downloaded_data/UNFCCC/submissions-bur.csv "
  100. "python src/unfccc_ghg_data/unfccc_downloader/download_nonannexI.py --category=BUR",
  101. "python src/unfccc_ghg_data/helper/folder_mapping.py "
  102. "--folder=downloaded_data/UNFCCC",
  103. ],
  104. "task_dep": ["set_env"],
  105. "verbosity": 2,
  106. "setup": ["in_venv"],
  107. }
  108. def task_update_nc():
  109. """Update list of NC submissions"""
  110. return {
  111. "targets": ["downloaded_data/UNFCCC/submissions-nc.csv"],
  112. "actions": [
  113. 'datalad run -m "Fetch NC submissions" '
  114. "-o downloaded_data/UNFCCC/submissions-nc.csv "
  115. "python src/unfccc_ghg_data/unfccc_downloader/fetch_submissions_nc.py"
  116. ],
  117. "task_dep": ["set_env"],
  118. "verbosity": 2,
  119. "setup": ["in_venv"],
  120. }
  121. def task_download_nc():
  122. """Download NC submissions"""
  123. return {
  124. #'file_dep': ['downloaded_data/UNFCCC/submissions-nc.csv'],
  125. # deactivate file_dep fow now as it will always run fetch submissions
  126. # before download
  127. "actions": [
  128. 'datalad run -m "Download NC submissions" '
  129. "-i downloaded_data/UNFCCC/submissions-nc.csv "
  130. "python src/unfccc_ghg_data/unfccc_downloader/download_nonannexI.py --category=NC",
  131. "python src/unfccc_ghg_data/helper/folder_mapping.py "
  132. "--folder=downloaded_data/UNFCCC",
  133. ],
  134. "task_dep": ["set_env"],
  135. "verbosity": 2,
  136. "setup": ["in_venv"],
  137. }
  138. # annexI data: one update call for all data types (as they are on one page)
  139. # but for each year separately.
  140. # downloading is per year and
  141. update_aI_config = {
  142. "year": get_var("year", None),
  143. "category": get_var("category", None),
  144. }
  145. def task_update_annexi():
  146. """Update list of AnnexI submissions"""
  147. return {
  148. "targets": [
  149. f"downloaded_data/UNFCCC/submissions-annexI_{update_aI_config['year']}.csv"
  150. ],
  151. "actions": [
  152. f"datalad run -m 'Fetch AnnexI submissions for {update_aI_config['year']}' "
  153. "--explicit "
  154. f"-o downloaded_data/UNFCCC/submissions-annexI_{update_aI_config['year']}.csv "
  155. f"python src/unfccc_ghg_data/unfccc_downloader/fetch_submissions_annexI.py "
  156. f"--year={update_aI_config['year']}"
  157. ],
  158. "task_dep": ["set_env"],
  159. "verbosity": 2,
  160. "setup": ["in_venv"],
  161. }
  162. def task_download_annexi():
  163. """Download AnnexI submissions"""
  164. return {
  165. #'file_dep': ['downloaded_data/UNFCCC/submissions-nc.csv'],
  166. # deactivate file_dep fow now as it will always run fetch submissions
  167. # before download
  168. "actions": [
  169. f"datalad run -m 'Download AnnexI submissions for "
  170. f"{update_aI_config['category']}{update_aI_config['year']}' "
  171. f"-i downloaded_data/UNFCCC/submissions-annexI_{update_aI_config['year']}.csv "
  172. f"python src/unfccc_ghg_data/unfccc_downloader/download_annexI.py "
  173. f"--category={update_aI_config['category']} --year={update_aI_config['year']}",
  174. "python src/unfccc_ghg_data/helper/folder_mapping.py "
  175. "--folder=downloaded_data/UNFCCC",
  176. ],
  177. "task_dep": ["set_env"],
  178. "verbosity": 2,
  179. "setup": ["in_venv"],
  180. }
  181. # annexI data: one update call for all data types (as they are on one page)
  182. # but for each year separately.
  183. # downloading is per year and
  184. update_btr_config = {
  185. "round": get_var("round", None),
  186. }
  187. def task_update_btr():
  188. """Update list of BTR submissions"""
  189. return {
  190. "targets": [
  191. f"downloaded_data/UNFCCC/submissions-BTR{update_btr_config['round']}.csv"
  192. ],
  193. "actions": [
  194. f"datalad run -m 'Fetch Biannial Transparency Report submissions for "
  195. f"BTR{update_btr_config['round']}' "
  196. "--explicit "
  197. f"-o downloaded_data/UNFCCC/submissions-BTR{update_btr_config['round']}.csv "
  198. f"./venv/bin/python UNFCCC_GHG_data/UNFCCC_downloader/fetch_submissions_btr.py "
  199. f"--round={update_btr_config['round']}"
  200. ],
  201. "task_dep": ["set_env"],
  202. "verbosity": 2,
  203. "setup": ["in_venv"],
  204. }
  205. def task_download_btr():
  206. """Download BTR submissions"""
  207. return {
  208. #'file_dep': ['downloaded_data/UNFCCC/submissions-nc.csv'],
  209. # deactivate file_dep fow now as it will always run fetch submissions
  210. # before download
  211. "actions": [
  212. f"datalad run -m 'Download BTR submissions for "
  213. f"BTR{update_btr_config['round']}' "
  214. f"-i downloaded_data/UNFCCC/submissions-BTR{update_btr_config['round']}.csv "
  215. f"./venv/bin/python UNFCCC_GHG_data/UNFCCC_downloader/download_btr.py "
  216. f"--round={update_btr_config['round']}",
  217. "./venv/bin/python UNFCCC_GHG_data/helper/folder_mapping.py "
  218. "--folder=downloaded_data/UNFCCC",
  219. ],
  220. "task_dep": ["set_env"],
  221. "verbosity": 2,
  222. "setup": ["in_venv"],
  223. }
  224. def task_download_ndc():
  225. """Download NDC submissions"""
  226. return {
  227. "actions": [
  228. 'datalad run -m "Download NDC submissions" '
  229. "python src/unfccc_ghg_data/unfccc_downloader/download_ndc.py",
  230. "python src/unfccc_ghg_data/helper/folder_mapping.py "
  231. "--folder=downloaded_data/UNFCCC",
  232. ],
  233. "task_dep": ["set_env"],
  234. "verbosity": 2,
  235. "setup": ["in_venv"],
  236. }
  237. # read UNFCCC submissions.
  238. # datalad run is called from within the read_UNFCCC_submission.py script
  239. read_config = {
  240. "country": get_var("country", None),
  241. "submission": get_var("submission", None),
  242. }
  243. # TODO: make individual task for non-UNFCCC submissions
  244. def task_read_unfccc_submission():
  245. """Read submission for a country (if code exists) (not for CRF)"""
  246. return {
  247. "actions": [
  248. f"python src/unfccc_ghg_data/unfccc_reader/read_UNFCCC_submission.py "
  249. f"--country={read_config['country']} --submission={read_config['submission']}",
  250. "python src/unfccc_ghg_data/helper/folder_mapping.py "
  251. "--folder=extracted_data/UNFCCC",
  252. ],
  253. "task_dep": ["set_env"],
  254. "verbosity": 2,
  255. "setup": ["in_venv"],
  256. }
  257. # read UNFCCC submissions.
  258. # datalad run is called from within the read_UNFCCC_submission.py script
  259. read_config_crf = {
  260. "country": get_var("country", None),
  261. "submission_year": get_var("submission_year", None),
  262. "submission_date": get_var("submission_date", None),
  263. "re_read": get_var("re_read", False),
  264. "countries": get_var("countries", None),
  265. "data_year": get_var("data_year", None),
  266. "totest": get_var("totest", None),
  267. }
  268. def task_read_unfccc_crf_submission():
  269. """Read CRF submission for a country"""
  270. actions = [
  271. f"python src/unfccc_ghg_data/unfccc_crf_reader"
  272. f"/read_unfccc_crf_submission_datalad.py "
  273. f"--country={read_config_crf['country']} "
  274. f"--submission_year={read_config_crf['submission_year']} "
  275. f"--submission_date={read_config_crf['submission_date']} ",
  276. "python src/unfccc_ghg_data/helper/folder_mapping.py "
  277. "--folder=extracted_data/UNFCCC",
  278. ]
  279. if read_config_crf["re_read"] == "True":
  280. actions[0] = actions[0] + " --re_read"
  281. return {
  282. "actions": actions,
  283. "task_dep": ["set_env"],
  284. "verbosity": 2,
  285. "setup": ["in_venv"],
  286. }
  287. def task_read_new_unfccc_crf_for_year():
  288. """
  289. Read CRF submission for all countries for given submission year.
  290. By default only reads data not present yet. Only reads the latest updated
  291. submission for each country.
  292. """
  293. actions = [
  294. f"python src/unfccc_ghg_data/unfccc_crf_reader"
  295. f"/read_new_unfccc_crf_for_year_datalad.py "
  296. f"--submission_year={read_config_crf['submission_year']} ",
  297. "python src/unfccc_ghg_data/helper/folder_mapping.py "
  298. "--folder=extracted_data/UNFCCC",
  299. ]
  300. # specifying countries is currently disabled duo to problems with command line
  301. # list arguments
  302. # if read_config_crf["countries"] is not None:
  303. # actions[0] = actions[0] + f"--countries={read_config_crf['countries']} "
  304. if read_config_crf["re_read"] == "True":
  305. actions[0] = actions[0] + " --re_read"
  306. return {
  307. #'basename': "Read_CRF_year",
  308. "actions": actions,
  309. "task_dep": ["set_env"],
  310. "verbosity": 2,
  311. "setup": ["in_venv"],
  312. }
  313. def task_test_read_unfccc_crf_for_year():
  314. """
  315. Test CRF reading.
  316. Test CRF with a single year only for speed and logging to extend specifications
  317. if necessary.
  318. """
  319. actions = [
  320. f"python "
  321. f"src/unfccc_ghg_data/unfccc_crf_reader"
  322. f"/test_read_unfccc_crf_for_year.py "
  323. f"--submission_year={read_config_crf['submission_year']} "
  324. f"--country={read_config_crf['country']} "
  325. ]
  326. if read_config_crf["totest"] == "True":
  327. actions[0] = actions[0] + " --totest"
  328. if read_config_crf["data_year"] is not None:
  329. actions[0] = actions[0] + f"--data_year={read_config_crf['data_year']} "
  330. return {
  331. #'basename': "Read_CRF_year",
  332. "actions": actions,
  333. "task_dep": ["set_env"],
  334. "verbosity": 2,
  335. "setup": ["in_venv"],
  336. }
  337. def task_compile_raw_unfccc_crf_for_year():
  338. """
  339. Collect all latest CRF submissions for a given year
  340. Reads the latest data fromt he extracted data folder for each country.
  341. Notifies the user if new data are available in the downloaded_data folder
  342. which have not yet been read.
  343. Data are saved in the datasets/UNFCCC/CRFYYYY folder.
  344. """
  345. actions = [
  346. f"python "
  347. f"src/unfccc_ghg_data/unfccc_crf_reader/crf_raw_for_year.py "
  348. f"--submission_year={read_config_crf['submission_year']} "
  349. ]
  350. return {
  351. "actions": actions,
  352. "task_dep": ["set_env"],
  353. "verbosity": 2,
  354. "setup": ["in_venv"],
  355. }
  356. # tasks for DI reader
  357. # datalad run is called from within the read_unfccc_di_for_country.py script
  358. read_config_di = {
  359. "country": get_var("country", None),
  360. "date": get_var("date", None),
  361. "annexI": get_var("annexI", False),
  362. # "countries": get_var('countries', None),
  363. }
  364. def task_read_unfccc_di_for_country():
  365. """Read DI data for a country"""
  366. actions = [
  367. f"python "
  368. f"src/unfccc_ghg_data/unfccc_di_reader/read_unfccc_di_for_country_datalad.py "
  369. f"--country={read_config_di['country']}",
  370. "python src/unfccc_ghg_data/helper/folder_mapping.py "
  371. "--folder=extracted_data/UNFCCC",
  372. ]
  373. return {
  374. "actions": actions,
  375. "task_dep": ["set_env"],
  376. "verbosity": 2,
  377. "setup": ["in_venv"],
  378. }
  379. def task_process_unfccc_di_for_country():
  380. """Process DI data for a country"""
  381. actions = [
  382. f"python "
  383. f"src/unfccc_ghg_data/unfccc_di_reader/process_unfccc_di_for_country_datalad"
  384. f".py "
  385. f"--country={read_config_di['country']} --date={read_config_di['date']}",
  386. "python src/unfccc_ghg_data/helper/folder_mapping.py "
  387. "--folder=extracted_data/UNFCCC",
  388. ]
  389. return {
  390. "actions": actions,
  391. "task_dep": ["set_env"],
  392. "verbosity": 2,
  393. "setup": ["in_venv"],
  394. }
  395. def task_read_unfccc_di_for_country_group():
  396. """Read DI data for a country group"""
  397. actions = [
  398. "python "
  399. "src/unfccc_ghg_data/unfccc_di_reader/read_unfccc_di_for_country_group_datalad"
  400. ".py",
  401. "python src/unfccc_ghg_data/helper/folder_mapping.py "
  402. "--folder=extracted_data/UNFCCC",
  403. ]
  404. if read_config_di["annexI"] == "True":
  405. actions[0] = actions[0] + " --annexI"
  406. return {
  407. "actions": actions,
  408. "task_dep": ["set_env"],
  409. "verbosity": 2,
  410. "setup": ["in_venv"],
  411. }
  412. def task_process_unfccc_di_for_country_group():
  413. """Process DI data for a country group"""
  414. actions = [
  415. "python "
  416. "src/unfccc_ghg_data/unfccc_di_reader"
  417. "/process_unfccc_di_for_country_group_datalad"
  418. ".py",
  419. ]
  420. if read_config_di["annexI"] == "True":
  421. actions[0] = actions[0] + " --annexI"
  422. if read_config_di["date"] is not None:
  423. actions[0] = actions[0] + f" --date={read_config_di['date']}"
  424. return {
  425. "actions": actions,
  426. "task_dep": ["set_env"],
  427. "verbosity": 2,
  428. "setup": ["in_venv"],
  429. }
  430. # general tasks
  431. def task_country_info():
  432. """
  433. Print information on submissions and datasets available for given country
  434. """
  435. return {
  436. "actions": [
  437. f"python src/unfccc_ghg_data/helper/country_info.py "
  438. f"--country={read_config['country']}"
  439. ],
  440. "task_dep": ["set_env"],
  441. "verbosity": 2,
  442. "setup": ["in_venv"],
  443. }