test_conversion.py 23 KB

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