dodo.py 15 KB

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