test_conversion.py 24 KB

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