test_conversion.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. import climate_categories as cc
  2. import primap2 as pm2
  3. import xarray as xr
  4. from src.faostat_data_primap.helper.category_aggregation import (
  5. agg_info_fao,
  6. agg_info_ipcc2006_primap_CH4,
  7. agg_info_ipcc2006_primap_CO2,
  8. agg_info_ipcc2006_primap_N2O,
  9. )
  10. from src.faostat_data_primap.helper.paths import (
  11. downloaded_data_path,
  12. extracted_data_path,
  13. )
  14. from src.faostat_data_primap.read import process, read_data
  15. def test_process_output_remains_the_same():
  16. # get processed data
  17. # release_name = "v2024-11-14"
  18. release_name = "v2023-12-13"
  19. filename_processed_ds = f"FAOSTAT_Agrifood_system_emissions_{release_name}"
  20. filepath = extracted_data_path / release_name / (filename_processed_ds + ".nc")
  21. ds_processed = pm2.open_dataset(filepath)
  22. # get raw data
  23. filename_raw_ds = (
  24. extracted_data_path
  25. / f"{release_name}/FAOSTAT_Agrifood_system_emissions_{release_name}_raw.nc"
  26. )
  27. ds_raw = pm2.open_dataset(filename_raw_ds)
  28. # process raw data
  29. ds_processed_new = process(ds=ds_raw, year="2023")
  30. # compare
  31. assert ds_processed.broadcast_equals(ds_processed_new)
  32. def test_read(tmp_path):
  33. domains_and_releases_to_read = [
  34. ("farm_gate_agriculture_energy", "2024-11-14"),
  35. ("farm_gate_emissions_crops", "2024-11-14"),
  36. ("farm_gate_livestock", "2024-11-14"),
  37. ("land_use_drained_organic_soils", "2024-11-14"),
  38. ("land_use_fires", "2023-11-09"),
  39. ("land_use_forests", "2024-11-14"),
  40. ("pre_post_agricultural_production", "2024-11-14"),
  41. ]
  42. read_data(
  43. domains_and_releases_to_read=domains_and_releases_to_read,
  44. read_path=downloaded_data_path,
  45. save_path=tmp_path,
  46. )
  47. def test_conversion_from_FAO_to_IPCC2006_PRIMAP():
  48. # make categorisation A from yaml
  49. categorisation_a = cc.FAO
  50. # make categorisation B from yaml
  51. categorisation_b = cc.IPCC2006_PRIMAP
  52. # category FAOSTAT not yet part of climate categories, so we need to add it manually
  53. cats = {
  54. "FAO": categorisation_a,
  55. "IPCC2006_PRIMAP": categorisation_b,
  56. }
  57. # release_name = "v2024-11-14"
  58. release_name = "v2023-12-13"
  59. # reproduce 2023 data set
  60. reproduce23 = True
  61. ds_fao = (
  62. extracted_data_path
  63. # / "v2024-11-14/FAOSTAT_Agrifood_system_emissions_v2024-11-14_raw.nc"
  64. / f"{release_name}/FAOSTAT_Agrifood_system_emissions_{release_name}_raw.nc"
  65. )
  66. ds = pm2.open_dataset(ds_fao)
  67. # drop UNFCCC data
  68. ds = ds.drop_sel(source="UNFCCC")
  69. # consistency check in original categorisation
  70. ds_checked = ds.pr.add_aggregates_coordinates(agg_info=agg_info_fao) # noqa: F841
  71. # ds_checked_if = ds_checked.pr.to_interchange_format()
  72. # We need a conversion CSV file for each entity
  73. # That's a temporary workaround until convert function can filter for data variables (entities)
  74. conv = {}
  75. gases = ["CO2", "CH4", "N2O"]
  76. if reproduce23:
  77. reproduce23_filename = "_reproduce23"
  78. else:
  79. reproduce23_filename = ""
  80. for var in gases:
  81. conv[var] = cc.Conversion.from_csv(
  82. f"../../conversion_FAO_IPPCC2006_PRIMAP_{var}{reproduce23_filename}.csv",
  83. cats=cats,
  84. )
  85. # convert for each entity
  86. da_dict = {}
  87. for var in gases:
  88. da_dict[var] = ds[var].pr.convert(
  89. dim="category (FAO)",
  90. conversion=conv[var],
  91. )
  92. result = xr.Dataset(da_dict)
  93. result.attrs = ds.attrs
  94. result.attrs["cat"] = "category (IPCC2006_PRIMAP)"
  95. # convert to interchange format and back to get rid of empty categories
  96. # TODO there may be a better way to do this
  97. result_if = result.pr.to_interchange_format()
  98. result = pm2.pm2io.from_interchange_format(result_if)
  99. # aggregation for each gas for better understanding
  100. # TODO creates some duplicate code, we can combine maybe
  101. result_proc = result.pr.add_aggregates_coordinates(
  102. agg_info=agg_info_ipcc2006_primap_N2O
  103. )
  104. result_proc = result_proc.pr.add_aggregates_coordinates(
  105. agg_info=agg_info_ipcc2006_primap_CO2
  106. )
  107. result_proc = result_proc.pr.add_aggregates_coordinates(
  108. agg_info=agg_info_ipcc2006_primap_CH4
  109. )
  110. result_proc_if = result_proc.pr.to_interchange_format()
  111. # save processed data
  112. output_filename = f"FAOSTAT_Agrifood_system_emissions_{release_name}"
  113. output_folder = extracted_data_path / release_name
  114. if not output_folder.exists():
  115. output_folder.mkdir()
  116. filepath = output_folder / (output_filename + ".csv")
  117. print(f"Writing processed primap2 file to {filepath}")
  118. pm2.pm2io.write_interchange_format(
  119. filepath,
  120. result_proc_if,
  121. )
  122. compression = dict(zlib=True, complevel=9)
  123. encoding = {var: compression for var in result_proc.data_vars}
  124. filepath = output_folder / (output_filename + ".nc")
  125. print(f"Writing netcdf file to {filepath}")
  126. result_proc.pr.to_netcdf(filepath, encoding=encoding)
  127. # def test_read(tmp_path):
  128. # domains_and_releases_to_read = [
  129. # # ("farm_gate_agriculture_energy", "2024-11-14"),
  130. # # ("farm_gate_emissions_crops", "2024-11-14"),
  131. # # ("farm_gate_livestock", "2024-11-14"),
  132. # # ("land_use_drained_organic_soils", "2024-11-14"),
  133. # ("land_use_fires", "2023-11-09"),
  134. # # ("land_use_forests", "2024-11-14"),
  135. # # ("pre_post_agricultural_production", "2024-11-14"),
  136. # ]
  137. #
  138. # read_data(
  139. # domains_and_releases_to_read=domains_and_releases_to_read,
  140. # read_path=downloaded_data_path,
  141. # save_path=tmp_path,
  142. # )
  143. def test_read_2023():
  144. domains_and_releases_to_read = [
  145. ("farm_gate_agriculture_energy", "2023-12-13"),
  146. ("farm_gate_emissions_crops", "2023-11-09"),
  147. ("farm_gate_livestock", "2023-11-09"),
  148. ("land_use_drained_organic_soils", "2023-11-09"),
  149. ("land_use_fires", "2023-11-09"),
  150. ("land_use_forests", "2023-11-09"),
  151. ("pre_post_agricultural_production", "2023-11-09"),
  152. # ("farm_gate_agriculture_energy", "2024-11-14"),
  153. # ("farm_gate_emissions_crops", "2024-11-14"),
  154. # ("farm_gate_livestock", "2024-11-14"),
  155. # ("land_use_drained_organic_soils", "2024-11-14"),
  156. # ("land_use_fires", "2024-11-14"),
  157. # ("land_use_forests", "2024-11-14"),
  158. # ("pre_post_agricultural_production", "2024-11-14"),
  159. ]
  160. read_data(
  161. domains_and_releases_to_read=domains_and_releases_to_read,
  162. read_path=downloaded_data_path,
  163. save_path=extracted_data_path,
  164. )
  165. def test_yaml_to_python():
  166. cat = cc.from_yaml("FAO.yaml")
  167. cat.to_python("FAO.py")
  168. def test_python_to_yaml():
  169. from FAO import spec
  170. cat = cc.from_spec(spec)
  171. assert cat
  172. def test_fao_categorisation(): # noqa: PLR0912 PLR0915
  173. spec = {
  174. "name": "FAOSTAT",
  175. "title": (
  176. "Food and Agriculture Organization of the United Nations (FAO) "
  177. "FAOSTAT data set categorisation"
  178. ),
  179. "comment": "Needed to add FAOSTAT data to PRIMAP-hist",
  180. "references": "",
  181. "institution": "FAO",
  182. "hierarchical": True,
  183. "last_update": "2024-12-10",
  184. "version": "2024",
  185. "total_sum": True,
  186. "canonical_top_level_category": "0",
  187. }
  188. categories = {}
  189. # 0. main categories
  190. categories["0"] = {
  191. "title": "Total",
  192. "comment": "All emissions and removals",
  193. "children": [["1", "2", "3", "4", "5", "6", "7"]],
  194. }
  195. children_1 = ["1.A", "1.B"]
  196. children_2 = ["2.A", "2.B", "2.C", "2.D", "2.E"]
  197. children_3 = [f"3.{i}" for i in "ABCDEFGHIJKLMNOPQR"]
  198. main_categories = (
  199. # category code, name and comment, gases, children
  200. ("1", "Crops", ["CH4", "N2O"], children_1),
  201. (
  202. "2",
  203. "Energy use in agriculture",
  204. ["CH4", "N2O", "CO2"],
  205. children_2,
  206. ),
  207. ("3", "Livestock", ["CH4", "N2O"], children_3),
  208. # ("4", "Forest", ["CO2"], children_4),
  209. # (
  210. # "5",
  211. # "Drained organic soils",
  212. # ["N2O", "CO2"],
  213. # children_5,
  214. # ),
  215. # ("6", "Fires", ["CH4", "N2O", "CO2"], children_6),
  216. # (
  217. # "7",
  218. # "Pre and post agricultural production",
  219. # ["CH4", "N2O", "CO2"],
  220. # children_7,
  221. # ),
  222. )
  223. for code, name, gases, children in main_categories:
  224. categories[code] = {
  225. "title": name,
  226. "comment": name,
  227. # "alternative_codes": code.replace(".", ""),
  228. "children": [children],
  229. "info": {"gases": gases},
  230. }
  231. # 1. crops
  232. # all crops category
  233. code_all_crops = "1.A"
  234. codes_crops = [f"1.A.{i}" for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]]
  235. categories[code_all_crops] = {
  236. "title": "All crops",
  237. "comment": "All crops",
  238. # "alternative_codes": code_all_crops.replace(".", ""),
  239. "children": [codes_crops],
  240. "info": {"gases": ["CH4", "N2O"]},
  241. }
  242. crops = [
  243. "Wheat",
  244. "Rice",
  245. "Potatoes",
  246. "Millet",
  247. "Barley",
  248. "Maize (corn)",
  249. "Sugar cane",
  250. "Beans, dry",
  251. "Oats",
  252. "Rye",
  253. "Sorghum",
  254. "Soya beans",
  255. ]
  256. crop_burnings = [
  257. True,
  258. True,
  259. False,
  260. False,
  261. False,
  262. True,
  263. True,
  264. False,
  265. False,
  266. False,
  267. False,
  268. False,
  269. ]
  270. rice_cultivations = [
  271. False,
  272. True,
  273. False,
  274. False,
  275. False,
  276. False,
  277. False,
  278. False,
  279. False,
  280. False,
  281. False,
  282. False,
  283. ]
  284. for crop, code, crop_burning, rice_cultivation in zip(
  285. crops, codes_crops, crop_burnings, rice_cultivations
  286. ):
  287. # all crops have at least N2O emissions
  288. gases_main = "N2O"
  289. if crop_burning or rice_cultivation:
  290. gases_main = ["CH4", "N2O"]
  291. # all crops have at least crop residues as child
  292. children_main = [f"{code}.a"]
  293. if crop_burning:
  294. children_main.append(f"{code}.b")
  295. if rice_cultivation:
  296. children_main.append(f"{code}.c")
  297. categories[f"{code}"] = {
  298. "title": f"{crop}",
  299. "comment": f"{crop}",
  300. # "alternative_codes": [f"{code}".replace(".", "")],
  301. "info": {"gases": gases_main},
  302. "children": [children_main],
  303. }
  304. # crop residues (every crop has it)
  305. categories[f"{code}.a.i"] = {
  306. "title": f"{crop} crop residues direct emissions",
  307. "comment": f"{crop} crop residues direct emissions",
  308. # "alternative_codes": [f"{code}.a".replace(".", "")],
  309. "info": {"gases": ["N2O"]},
  310. }
  311. categories[f"{code}.a.ii"] = {
  312. "title": f"{crop} crop residues indirect emissions",
  313. "comment": f"{crop} crop residues indirect emissions",
  314. # "alternative_codes": [f"{code}.a.i".replace(".", "")],
  315. "info": {"gases": ["N2O"]},
  316. }
  317. categories[f"{code}.a"] = {
  318. "title": f"{crop} crop residues",
  319. "comment": f"{crop} crop residues",
  320. # "alternative_codes": [f"{code}.a".replace(".", "")],
  321. "info": {"gases": ["N2O"]},
  322. "children": [[f"{code}.a.ii", f"{code}.a.i"]],
  323. }
  324. if crop_burning:
  325. categories[f"{code}.b"] = {
  326. "title": f"{crop} burning crop residues",
  327. "comment": f"{crop} burning crop residues",
  328. # "alternative_codes": [f"{code}.b".replace(".", "")],
  329. "info": {"gases": ["CH4", "N2O"]},
  330. }
  331. if rice_cultivation:
  332. categories[f"{code}.c"] = {
  333. "title": "Rice cultivation",
  334. "comment": "Rice cultivation",
  335. # "alternative_codes": [f"{code}.c".replace(".", "")],
  336. "info": {"gases": ["CH4"]},
  337. }
  338. # synthetic fertilisers
  339. codes_synthetic_fertilisers = ["1.B", "1.B.1", "1.B.2", "1.B.2.a", "1.B.2.b"]
  340. names = [
  341. "Synthetic fertilisers",
  342. "Direct emissions",
  343. "Indirect emissions",
  344. "Indirect emissions that volatilise",
  345. "Indirect emissions that leach",
  346. ]
  347. children_cats = [["1.B.1", "1.B.2"], None, ["1.B.2.a", "1.B.2.b"], None, None]
  348. for code, name, child_cat in zip(codes_synthetic_fertilisers, names, children_cats):
  349. categories[code] = {
  350. "title": name,
  351. "comment": name,
  352. # "alternative_codes": [code.replace(".", "")],
  353. "info": {"gases": ["N2O"]},
  354. }
  355. if child_cat:
  356. categories[code]["children"] = [child_cat]
  357. # 2. energy use
  358. names = [
  359. "Natural gas",
  360. "Electricity",
  361. "Coal",
  362. "Heat",
  363. "Petroleum",
  364. ]
  365. codes = children_2
  366. for name, code in zip(names, codes):
  367. categories[code] = {
  368. "title": name,
  369. "comment": name,
  370. # "alternative_codes": code.replace(".", ""),
  371. "info": {"gases": ["CH4", "N2O", "CO2"]},
  372. }
  373. # 3 livestock
  374. animals = [
  375. "Asses",
  376. "Camels",
  377. "Cattle, dairy",
  378. "Cattle, non-dairy",
  379. "Chickens, broilers",
  380. "Chickens, layers",
  381. "Goats",
  382. "Horses",
  383. "Mules and hinnies",
  384. "Sheep",
  385. "Llamas",
  386. "Chickens",
  387. "Poultry Birds",
  388. "Buffalo",
  389. "Ducks",
  390. "Swine, breeding",
  391. "Swine, market",
  392. "Turkeys",
  393. ]
  394. codes_animals = [f"3.{i}" for i in "ABCDEFGHIJKLMNOPQR"]
  395. enteric_fermentation = [
  396. "Asses",
  397. "Camels",
  398. "Cattle, dairy",
  399. "Cattle, non-dairy",
  400. "Goats",
  401. "Horses",
  402. "Sheep",
  403. "Mules and hinnies",
  404. "Buffalo",
  405. "Swine, breeding",
  406. "Swine, market",
  407. "Llamas",
  408. ]
  409. for animal, code in zip(animals, codes_animals):
  410. if animal in enteric_fermentation:
  411. gases = ["CH4"]
  412. animal_children = [f"{code}.{i}" for i in "1234"]
  413. categories[f"{code}.4"] = {
  414. "title": f"{animal} enteric fermentation",
  415. "comment": f"{animal} enteric fermentation",
  416. # "alternative_codes" : code.replace(".", ""),
  417. "info": {"gases": gases},
  418. }
  419. else:
  420. gases = ["N2O"]
  421. animal_children = [f"{code}.{i}" for i in "123"]
  422. categories[code] = {
  423. "title": animal,
  424. "comment": animal,
  425. # "alternative_codes" : code.replace(".", ""),
  426. "info": {"gases": gases},
  427. "children": [animal_children],
  428. }
  429. # manure management branch
  430. manure_management_children = [f"{code}.1.{i}" for i in "abc"]
  431. categories[f"{code}.1"] = {
  432. "title": f"{animal} manure management",
  433. "comment": f"{animal} manure management",
  434. # "alternative_codes" : code.replace(".", ""),
  435. "info": {"gases": gases},
  436. "children": [manure_management_children],
  437. }
  438. categories[f"{code}.1.a"] = {
  439. "title": f"{animal} decomposition of organic matter",
  440. "comment": f"{animal} decomposition of organic matter",
  441. # "alternative_codes" : code.replace(".", ""),
  442. "info": {"gases": "CH4"},
  443. }
  444. categories[f"{code}.1.b"] = {
  445. "title": f"{animal} manure management (Direct emissions N2O)",
  446. "comment": f"{animal} manure management (Direct emissions N2O)",
  447. # "alternative_codes" : code.replace(".", ""),
  448. "info": {"gases": "N2O"},
  449. }
  450. categories[f"{code}.1.c"] = {
  451. "title": f"{animal} manure management (Indirect emissions N2O)",
  452. "comment": f"{animal} manure management (Indirect emissions N2O)",
  453. # "alternative_codes" : code.replace(".", ""),
  454. "info": {"gases": "N2O"},
  455. }
  456. # manure left on pasture branch
  457. manure_left_on_pasture_children = [f"{code}.2.{i}" for i in "ab"]
  458. categories[f"{code}.2"] = {
  459. "title": f"{animal} manure left on pasture",
  460. "comment": f"{animal} manure left on pasture",
  461. # "alternative_codes" : code.replace(".", ""),
  462. "info": {"gases": "N2O"},
  463. "children": [manure_left_on_pasture_children],
  464. }
  465. categories[f"{code}.2.a"] = {
  466. "title": f"{animal} manure left on pasture (direct emissions N2O)",
  467. "comment": f"{animal} manure left on pasture (direct emissions N2O)",
  468. # "alternative_codes" : code.replace(".", ""),
  469. "info": {"gases": "N2O"},
  470. }
  471. categories[f"{code}.2.b"] = {
  472. "title": f"{animal} manure left on pasture (indirect emissions N2O)",
  473. "comment": f"{animal} manure left on pasture (indirect emissions N2O)",
  474. # "alternative_codes" : code.replace(".", ""),
  475. "info": {"gases": "N2O"},
  476. "children": [[f"{code}.2.b.i", f"{code}.2.b.ii"]],
  477. }
  478. categories[f"{code}.2.b.i"] = {
  479. "title": (
  480. f"{animal} manure left on pasture "
  481. f"(indirect emissions, N2O that leaches)"
  482. ),
  483. "comment": (
  484. f"{animal} manure left on pasture (indirect "
  485. f"emissions, N2O that leaches)"
  486. ),
  487. # "alternative_codes" : code.replace(".", ""),
  488. "info": {"gases": "N2O"},
  489. }
  490. categories[f"{code}.2.b.ii"] = {
  491. "title": (
  492. f"{animal} manure left on pasture "
  493. f"(indirect emissions, N2O that volatilises)"
  494. ),
  495. "comment": (
  496. f"{animal} manure left on pasture (indirect "
  497. f"emissions, N2O that volatilises)"
  498. ),
  499. # "alternative_codes" : code.replace(".", ""),
  500. "info": {"gases": "N2O"},
  501. }
  502. # manure applied branch
  503. manure_applied_children = [f"{code}.3.{i}" for i in "ab"]
  504. categories[f"{code}.3"] = {
  505. "title": f"{animal} manure applied",
  506. "comment": f"{animal} manure applied",
  507. # "alternative_codes" : code.replace(".", ""),
  508. "info": {"gases": "N2O"},
  509. "children": [manure_applied_children],
  510. }
  511. categories[f"{code}.3.a"] = {
  512. "title": f"{animal} manure applied (direct emissions N2O)",
  513. "comment": f"{animal} manure applied (direct emissions N2O)",
  514. # "alternative_codes" : code.replace(".", ""),
  515. "info": {"gases": "N2O"},
  516. }
  517. categories[f"{code}.3.b"] = {
  518. "title": f"{animal} manure applied (indirect emissions N2O)",
  519. "comment": f"{animal} manure applied (indirect emissions N2O)",
  520. # "alternative_codes" : code.replace(".", ""),
  521. "info": {"gases": "N2O"},
  522. "children": [[f"{code}.3.b.i", f"{code}.3.b.ii"]],
  523. }
  524. categories[f"{code}.3.b.i"] = {
  525. "title": (
  526. f"{animal} manure applied " f"(indirect emissions, N2O that leaches)"
  527. ),
  528. "comment": (
  529. f"{animal} manure applied (indirect " f"emissions, N2O that leaches)"
  530. ),
  531. # "alternative_codes" : code.replace(".", ""),
  532. "info": {"gases": "N2O"},
  533. }
  534. categories[f"{code}.3.b.ii"] = {
  535. "title": (
  536. f"{animal} manure applied "
  537. f"(indirect emissions, N2O that volatilises)"
  538. ),
  539. "comment": (
  540. f"{animal} manure applied (indirect "
  541. f"emissions, N2O that volatilises)"
  542. ),
  543. # "alternative_codes" : code.replace(".", ""),
  544. "info": {"gases": "N2O"},
  545. }
  546. # forests
  547. categories["4"] = {
  548. "title": "Carbon stock change in forests",
  549. "comment": "Carbon stock change in forests",
  550. "info": {"gases": "CO2"},
  551. "children": [["4.A", "4.B"]],
  552. }
  553. categories["4.A"] = {
  554. "title": "Forest land",
  555. "comment": "Forest land",
  556. "info": {"gases": "CO2"},
  557. }
  558. categories["4.B"] = {
  559. "title": "Net Forest conversion",
  560. "comment": "Net Forest conversion",
  561. "info": {"gases": "CO2"},
  562. }
  563. # drained organic soils
  564. categories["5"] = {
  565. "title": "Drained organic soils",
  566. "comment": "Drained organic soils",
  567. "info": {"gases": "CO2"},
  568. "children": [["5.A", "5.B"]],
  569. }
  570. categories["5.A"] = {
  571. "title": "Drained grassland",
  572. "comment": "Drained grassland",
  573. "info": {"gases": ["CO2", "N2O"]},
  574. }
  575. categories["5.B"] = {
  576. "title": "Drained cropland",
  577. "comment": "Drained cropland",
  578. "info": {"gases": ["CO2", "N2O"]},
  579. }
  580. # 6 Fires
  581. # Forest fires
  582. forest_fires_children = ["Humid tropical forests", "Other forests"]
  583. forest_fires_children_codes = ["6.A.1", "6.A.2"]
  584. for cat_name, code in zip(forest_fires_children, forest_fires_children_codes):
  585. categories[code] = {
  586. "title": cat_name,
  587. "comment": cat_name,
  588. "info": {"gases": ["CO2", "N2O", "CH4"]},
  589. }
  590. categories["6.A"] = {
  591. "title": "Forest fires",
  592. "comment": "Forest fires",
  593. "info": {"gases": ["CO2", "N2O", "CH4"]},
  594. "children": [forest_fires_children_codes],
  595. }
  596. # Savanna fires
  597. savanna_fires_children = [
  598. "Closed shrubland",
  599. "Grassland",
  600. "Open shrubland",
  601. "Savanna",
  602. "Woody savanna",
  603. ]
  604. savanna_fires_children_codes = ["6.B.1", "6.B.2", "6.B.3", "6.B.4", "6.B.5"]
  605. for cat_name, code in zip(savanna_fires_children, savanna_fires_children_codes):
  606. categories[code] = {
  607. "title": cat_name,
  608. "comment": cat_name,
  609. "info": {"gases": ["CO2", "N2O", "CH4"]},
  610. }
  611. categories["6.B"] = {
  612. "title": "Savanna fires",
  613. "comment": "Savanna fires",
  614. "info": {"gases": ["CO2", "N2O", "CH4"]},
  615. "children": [savanna_fires_children_codes],
  616. }
  617. # fires in organic soils
  618. categories["6.C"] = {
  619. "title": "Fires in organic soils",
  620. "comment": "Fires in organic soils",
  621. "info": {"gases": ["CO2", "N2O", "CH4"]},
  622. }
  623. # 6 fires
  624. categories["6"] = {
  625. "title": "Fires",
  626. "comment": "Fires",
  627. "info": {"gases": ["CO2", "N2O", "CH4"]},
  628. "children": [["6.A", "6.B", "6.C"]],
  629. }
  630. # 7 pre and post production
  631. pre_post_production_categories = [
  632. "Fertilizers Manufacturing",
  633. "Food Transport",
  634. "Food Retail",
  635. "Food Household Consumption",
  636. "Solid Food Waste",
  637. "Domestic Wastewater",
  638. "Industrial Wastewater",
  639. "Incineration",
  640. "Pre- and Post- Production",
  641. "Energy Use (Pre- and Post-Production)",
  642. "Agrifood Systems Waste Disposal",
  643. "Cold Chain F-Gas",
  644. "Pesticides Manufacturing",
  645. "Food Processing",
  646. "Food Packaging",
  647. ]
  648. pre_post_production_categories_codes = ["7." + i for i in "ABCDEFGHIJKLMNO"]
  649. pre_post_production_categories_gases = [
  650. ["CO2", "N2O", "KYOTOGHG (AR5GWP100)"],
  651. ["CO2", "CH4", "N2O", "KYOTOGHG (AR5GWP100)", "FGASES (AR5GWP100)"],
  652. ["CO2", "CH4", "N2O", "KYOTOGHG (AR5GWP100)", "FGASES (AR5GWP100)"],
  653. ["CO2", "CH4", "N2O", "KYOTOGHG (AR5GWP100)", "FGASES (AR5GWP100)"],
  654. ["KYOTOGHG (AR5GWP100)", "CH4"],
  655. ["KYOTOGHG (AR5GWP100)", "CH4", "N2O"],
  656. ["KYOTOGHG (AR5GWP100)", "CH4", "N2O"],
  657. ["CO2", "KYOTOGHG (AR5GWP100)"], # incineration
  658. ["CO2", "CH4", "N2O", "KYOTOGHG (AR5GWP100)", "FGASES (AR5GWP100)"],
  659. ["CO2", "CH4", "N2O", "KYOTOGHG (AR5GWP100)"],
  660. ["CO2", "CH4", "N2O", "KYOTOGHG (AR5GWP100)"],
  661. ["FGASES (AR5GWP100)"],
  662. ["CO2", "CH4", "N2O", "KYOTOGHG (AR5GWP100)"],
  663. ["CO2", "CH4", "N2O", "KYOTOGHG (AR5GWP100)", "FGASES (AR5GWP100)"],
  664. ["CO2", "CH4", "N2O", "KYOTOGHG (AR5GWP100)"],
  665. ]
  666. for cat_name, code, gases in zip(
  667. pre_post_production_categories,
  668. pre_post_production_categories_codes,
  669. pre_post_production_categories_gases,
  670. ):
  671. categories[code] = {
  672. "title": cat_name,
  673. "comment": cat_name,
  674. "info": {"gases": gases},
  675. }
  676. categories["7"] = {
  677. "title": "Pre and post agricultural production",
  678. "comment": "Pre and post agricultural production",
  679. "info": {
  680. "gases": [
  681. "CO2",
  682. "CH4",
  683. "N2O",
  684. "KYOTOGHG (AR5GWP100)",
  685. "FGASES (AR5GWP100)",
  686. ],
  687. },
  688. "children": [pre_post_production_categories_codes],
  689. }
  690. spec["categories"] = categories
  691. fao_cats = cc.HierarchicalCategorization.from_spec(spec.copy())
  692. # run print(fao_cats.show_as_tree())
  693. fao_cats.to_python("FAO.py")
  694. fao_cats.to_yaml("FAO.yaml")