test_conversion.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. assert result_proc_if
  118. # df_all = pd.concat([ds_if, result_if], axis=0, join="outer", ignore_index=True)
  119. #
  120. # compare = df_all.loc[
  121. # df_all["entity"] == "CO2"
  122. # # (df_all["category (IPCC2006_PRIMAP)"] == "3.A")
  123. # # | (df_all["category (FAOSTAT)"] == "3")
  124. # ].sort_values(by="area (ISO3)")
  125. #
  126. # compare_short = compare[
  127. # [
  128. # "source",
  129. # "scenario (FAO)",
  130. # "area (ISO3)",
  131. # "entity",
  132. # "unit",
  133. # "category (FAOSTAT)",
  134. # "2021",
  135. # "2022",
  136. # "2023",
  137. # "category (IPCC2006_PRIMAP)",
  138. # ]
  139. # ]
  140. #
  141. # assert compare_short
  142. def test_read(tmp_path):
  143. domains_and_releases_to_read = [
  144. ("farm_gate_agriculture_energy", "2024-11-14"),
  145. ("farm_gate_emissions_crops", "2024-11-14"),
  146. ("farm_gate_livestock", "2024-11-14"),
  147. ("land_use_drained_organic_soils", "2024-11-14"),
  148. ("land_use_fires", "2024-11-14"),
  149. ("land_use_forests", "2024-11-14"),
  150. ("pre_post_agricultural_production", "2024-11-14"),
  151. ]
  152. read_data(
  153. domains_and_releases_to_read=domains_and_releases_to_read,
  154. read_path=downloaded_data_path,
  155. save_path=tmp_path,
  156. )
  157. def test_yaml_to_python():
  158. cat = cc.from_yaml("FAO.yaml")
  159. cat.to_python("FAO.py")
  160. def test_python_to_yaml():
  161. from FAO import spec
  162. cat = cc.from_spec(spec)
  163. assert cat
  164. def test_make_dict_comprehension_for_faster_typing(): # noqa: PLR0912 PLR0915
  165. spec = {
  166. "name": "FAOSTAT",
  167. "title": (
  168. "Food and Agriculture Organization of the United Nations (FAO) "
  169. "FAOSTAT data set categorisation"
  170. ),
  171. "comment": "Needed to add FAOSTAT data to PRIMAP-hist",
  172. "references": "",
  173. "institution": "FAO",
  174. "hierarchical": True,
  175. "last_update": "2024-12-10",
  176. "version": "2024",
  177. "total_sum": True,
  178. "canonical_top_level_category": "0",
  179. }
  180. categories = {}
  181. # 0. main categories
  182. categories["0"] = {
  183. "title": "Total",
  184. "comment": "All emissions and removals",
  185. "children": [["1", "2", "3", "4", "5", "6", "7"]],
  186. }
  187. children_1 = ["1.A", "1.B"]
  188. children_2 = ["2.A", "2.B", "2.C", "2.D", "2.E"]
  189. children_3 = [f"3.{i}" for i in "ABCDEFGHIJKLMNOPQR"]
  190. # children_4 = ["4.A"]
  191. # children_5 = ["5.A", "5.B"]
  192. # children_6 = ["6.A", "6.B", "6.C"]
  193. # children_7 = [f"3.{i}" for i in "ABCDEFGHIJKLM"]
  194. main_categories = (
  195. # category code, name and comment, gases, children
  196. ("1", "Crops", ["CH4", "N2O"], children_1),
  197. (
  198. "2",
  199. "Energy use in agriculture",
  200. ["CH4", "N2O", "CO2"],
  201. children_2,
  202. ),
  203. ("3", "Livestock", ["CH4", "N2O"], children_3),
  204. # ("4", "Forest", ["CO2"], children_4),
  205. # (
  206. # "5",
  207. # "Drained organic soils",
  208. # ["N2O", "CO2"],
  209. # children_5,
  210. # ),
  211. # ("6", "Fires", ["CH4", "N2O", "CO2"], children_6),
  212. # (
  213. # "7",
  214. # "Pre and post agricultural production",
  215. # ["CH4", "N2O", "CO2"],
  216. # children_7,
  217. # ),
  218. )
  219. for code, name, gases, children in main_categories:
  220. categories[code] = {
  221. "title": name,
  222. "comment": name,
  223. # "alternative_codes": code.replace(".", ""),
  224. "children": [children],
  225. "info": {"gases": gases},
  226. }
  227. # 1. crops
  228. # all crops category
  229. code_all_crops = "1.A"
  230. codes_crops = [f"1.A.{i}" for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]]
  231. categories[code_all_crops] = {
  232. "title": "All crops",
  233. "comment": "All crops",
  234. # "alternative_codes": code_all_crops.replace(".", ""),
  235. "children": [codes_crops],
  236. "info": {"gases": ["CH4", "N2O"]},
  237. }
  238. crops = [
  239. "Wheat",
  240. "Rice",
  241. "Potatoes",
  242. "Millet",
  243. "Barley",
  244. "Maize (corn)",
  245. "Sugar cane",
  246. "Beans, dry",
  247. "Oats",
  248. "Rye",
  249. "Sorghum",
  250. "Soya beans",
  251. ]
  252. crop_burnings = [
  253. True,
  254. True,
  255. False,
  256. False,
  257. False,
  258. True,
  259. True,
  260. False,
  261. False,
  262. False,
  263. False,
  264. False,
  265. ]
  266. rice_cultivations = [
  267. False,
  268. True,
  269. False,
  270. False,
  271. False,
  272. False,
  273. False,
  274. False,
  275. False,
  276. False,
  277. False,
  278. False,
  279. ]
  280. for crop, code, crop_burning, rice_cultivation in zip(
  281. crops, codes_crops, crop_burnings, rice_cultivations
  282. ):
  283. # all crops have at least N2O emissions
  284. gases_main = "N2O"
  285. if crop_burning or rice_cultivation:
  286. gases_main = ["CH4", "N2O"]
  287. # all crops have at least crop residues as child
  288. children_main = [f"{code}.a"]
  289. if crop_burning:
  290. children_main.append(f"{code}.b")
  291. if rice_cultivation:
  292. children_main.append(f"{code}.c")
  293. categories[f"{code}"] = {
  294. "title": f"{crop}",
  295. "comment": f"{crop}",
  296. # "alternative_codes": [f"{code}".replace(".", "")],
  297. "info": {"gases": gases_main},
  298. "children": [children_main],
  299. }
  300. # crop residues (every crop has it)
  301. categories[f"{code}.a.i"] = {
  302. "title": f"{crop} crop residues direct emissions",
  303. "comment": f"{crop} crop residues direct emissions",
  304. # "alternative_codes": [f"{code}.a".replace(".", "")],
  305. "info": {"gases": ["N2O"]},
  306. }
  307. categories[f"{code}.a.ii"] = {
  308. "title": f"{crop} crop residues indirect emissions",
  309. "comment": f"{crop} crop residues indirect emissions",
  310. # "alternative_codes": [f"{code}.a.i".replace(".", "")],
  311. "info": {"gases": ["N2O"]},
  312. }
  313. categories[f"{code}.a"] = {
  314. "title": f"{crop} crop residues",
  315. "comment": f"{crop} crop residues",
  316. # "alternative_codes": [f"{code}.a".replace(".", "")],
  317. "info": {"gases": ["N2O"]},
  318. "children": [[f"{code}.a.ii", f"{code}.a.i"]],
  319. }
  320. if crop_burning:
  321. categories[f"{code}.b"] = {
  322. "title": f"{crop} burning crop residues",
  323. "comment": f"{crop} burning crop residues",
  324. # "alternative_codes": [f"{code}.b".replace(".", "")],
  325. "info": {"gases": ["CH4", "N2O"]},
  326. }
  327. if rice_cultivation:
  328. categories[f"{code}.c"] = {
  329. "title": "Rice cultivation",
  330. "comment": "Rice cultivation",
  331. # "alternative_codes": [f"{code}.c".replace(".", "")],
  332. "info": {"gases": ["CH4"]},
  333. }
  334. # synthetic fertilisers
  335. codes_synthetic_fertilisers = ["1.B", "1.B.1", "1.B.2", "1.B.2.a", "1.B.2.b"]
  336. names = [
  337. "Synthetic fertilisers",
  338. "Direct emissions",
  339. "Indirect emissions",
  340. "Indirect emissions that volatilise",
  341. "Indirect emissions that leach",
  342. ]
  343. children_cats = [["1.B.1", "1.B.2"], None, ["1.B.2.a", "1.B.2.b"], None, None]
  344. for code, name, child_cat in zip(codes_synthetic_fertilisers, names, children_cats):
  345. categories[code] = {
  346. "title": name,
  347. "comment": name,
  348. # "alternative_codes": [code.replace(".", "")],
  349. "info": {"gases": ["N2O"]},
  350. }
  351. if child_cat:
  352. categories[code]["children"] = [child_cat]
  353. # 2. energy use
  354. names = [
  355. "Natural gas",
  356. "Electricity",
  357. "Coal",
  358. "Heat",
  359. "Petroleum",
  360. ]
  361. codes = children_2
  362. for name, code in zip(names, codes):
  363. categories[code] = {
  364. "title": name,
  365. "comment": name,
  366. # "alternative_codes": code.replace(".", ""),
  367. "info": {"gases": ["CH4", "N2O", "CO2"]},
  368. }
  369. # 3 livestock
  370. animals = [
  371. "Asses",
  372. "Camels",
  373. "Cattle, dairy",
  374. "Cattle, non-dairy",
  375. "Chickens, broilers",
  376. "Chickens, layers",
  377. "Goats",
  378. "Horses",
  379. "Mules and hinnies",
  380. "Sheep",
  381. "Llamas",
  382. "Chickens",
  383. "Poultry Birds",
  384. "Buffalo",
  385. "Ducks",
  386. "Swine, breeding",
  387. "Swine, market",
  388. "Turkeys",
  389. ]
  390. codes_animals = [f"3.{i}" for i in "ABCDEFGHIJKLMNOPQR"]
  391. enteric_fermentation = [
  392. "Asses",
  393. "Camels",
  394. "Cattle, dairy",
  395. "Cattle, non-dairy",
  396. "Goats",
  397. "Horses",
  398. "Sheep",
  399. "Mules and hinnies",
  400. "Buffalo",
  401. "Swine, breeding",
  402. "Swine, market",
  403. "Llamas",
  404. ]
  405. for animal, code in zip(animals, codes_animals):
  406. if animal in enteric_fermentation:
  407. gases = ["CH4"]
  408. animal_children = [f"{code}.{i}" for i in "1234"]
  409. categories[f"{code}.4"] = {
  410. "title": f"{animal} enteric fermentation",
  411. "comment": f"{animal} enteric fermentation",
  412. # "alternative_codes" : code.replace(".", ""),
  413. "info": {"gases": gases},
  414. }
  415. else:
  416. gases = ["N2O"]
  417. animal_children = [f"{code}.{i}" for i in "123"]
  418. categories[code] = {
  419. "title": animal,
  420. "comment": animal,
  421. # "alternative_codes" : code.replace(".", ""),
  422. "info": {"gases": gases},
  423. "children": [animal_children],
  424. }
  425. # manure management branch
  426. manure_management_children = [f"{code}.1.{i}" for i in "abc"]
  427. categories[f"{code}.1"] = {
  428. "title": f"{animal} manure management",
  429. "comment": f"{animal} manure management",
  430. # "alternative_codes" : code.replace(".", ""),
  431. "info": {"gases": gases},
  432. "children": [manure_management_children],
  433. }
  434. categories[f"{code}.1.a"] = {
  435. "title": f"{animal} decomposition of organic matter",
  436. "comment": f"{animal} decomposition of organic matter",
  437. # "alternative_codes" : code.replace(".", ""),
  438. "info": {"gases": "CH4"},
  439. }
  440. categories[f"{code}.1.b"] = {
  441. "title": f"{animal} manure management (Direct emissions N2O)",
  442. "comment": f"{animal} manure management (Direct emissions N2O)",
  443. # "alternative_codes" : code.replace(".", ""),
  444. "info": {"gases": "N2O"},
  445. }
  446. categories[f"{code}.1.c"] = {
  447. "title": f"{animal} manure management (Indirect emissions N2O)",
  448. "comment": f"{animal} manure management (Indirect emissions N2O)",
  449. # "alternative_codes" : code.replace(".", ""),
  450. "info": {"gases": "N2O"},
  451. }
  452. # manure left on pasture branch
  453. manure_left_on_pasture_children = [f"{code}.2.{i}" for i in "ab"]
  454. categories[f"{code}.2"] = {
  455. "title": f"{animal} manure left on pasture",
  456. "comment": f"{animal} manure left on pasture",
  457. # "alternative_codes" : code.replace(".", ""),
  458. "info": {"gases": "N2O"},
  459. "children": [manure_left_on_pasture_children],
  460. }
  461. categories[f"{code}.2.a"] = {
  462. "title": f"{animal} manure left on pasture (direct emissions N2O)",
  463. "comment": f"{animal} manure left on pasture (direct emissions N2O)",
  464. # "alternative_codes" : code.replace(".", ""),
  465. "info": {"gases": "N2O"},
  466. }
  467. categories[f"{code}.2.b"] = {
  468. "title": f"{animal} manure left on pasture (indirect emissions N2O)",
  469. "comment": f"{animal} manure left on pasture (indirect emissions N2O)",
  470. # "alternative_codes" : code.replace(".", ""),
  471. "info": {"gases": "N2O"},
  472. "children": [[f"{code}.2.b.i", f"{code}.2.b.ii"]],
  473. }
  474. categories[f"{code}.2.b.i"] = {
  475. "title": (
  476. f"{animal} manure left on pasture "
  477. f"(indirect emissions, N2O that leaches)"
  478. ),
  479. "comment": (
  480. f"{animal} manure left on pasture (indirect "
  481. f"emissions, N2O that leaches)"
  482. ),
  483. # "alternative_codes" : code.replace(".", ""),
  484. "info": {"gases": "N2O"},
  485. }
  486. categories[f"{code}.2.b.ii"] = {
  487. "title": (
  488. f"{animal} manure left on pasture "
  489. f"(indirect emissions, N2O that volatilises)"
  490. ),
  491. "comment": (
  492. f"{animal} manure left on pasture (indirect "
  493. f"emissions, N2O that volatilises)"
  494. ),
  495. # "alternative_codes" : code.replace(".", ""),
  496. "info": {"gases": "N2O"},
  497. }
  498. # manure applied branch
  499. manure_applied_children = [f"{code}.3.{i}" for i in "ab"]
  500. categories[f"{code}.3"] = {
  501. "title": f"{animal} manure applied",
  502. "comment": f"{animal} manure applied",
  503. # "alternative_codes" : code.replace(".", ""),
  504. "info": {"gases": "N2O"},
  505. "children": [manure_applied_children],
  506. }
  507. categories[f"{code}.3.a"] = {
  508. "title": f"{animal} manure applied (direct emissions N2O)",
  509. "comment": f"{animal} manure applied (direct emissions N2O)",
  510. # "alternative_codes" : code.replace(".", ""),
  511. "info": {"gases": "N2O"},
  512. }
  513. categories[f"{code}.3.b"] = {
  514. "title": f"{animal} manure applied (indirect emissions N2O)",
  515. "comment": f"{animal} manure applied (indirect emissions N2O)",
  516. # "alternative_codes" : code.replace(".", ""),
  517. "info": {"gases": "N2O"},
  518. "children": [[f"{code}.3.b.i", f"{code}.3.b.ii"]],
  519. }
  520. categories[f"{code}.3.b.i"] = {
  521. "title": (
  522. f"{animal} manure applied " f"(indirect emissions, N2O that leaches)"
  523. ),
  524. "comment": (
  525. f"{animal} manure applied (indirect " f"emissions, N2O that leaches)"
  526. ),
  527. # "alternative_codes" : code.replace(".", ""),
  528. "info": {"gases": "N2O"},
  529. }
  530. categories[f"{code}.3.b.ii"] = {
  531. "title": (
  532. f"{animal} manure applied "
  533. f"(indirect emissions, N2O that volatilises)"
  534. ),
  535. "comment": (
  536. f"{animal} manure applied (indirect "
  537. f"emissions, N2O that volatilises)"
  538. ),
  539. # "alternative_codes" : code.replace(".", ""),
  540. "info": {"gases": "N2O"},
  541. }
  542. # forests
  543. categories["4"] = {
  544. "title": "Carbon stock change in forests",
  545. "comment": "Carbon stock change in forests",
  546. "info": {"gases": "CO2"},
  547. "children": [["4.A", "4.B"]],
  548. }
  549. categories["4.A"] = {
  550. "title": "Forest land",
  551. "comment": "Forest land",
  552. "info": {"gases": "CO2"},
  553. }
  554. categories["4.B"] = {
  555. "title": "Net Forest conversion",
  556. "comment": "Net Forest conversion",
  557. "info": {"gases": "CO2"},
  558. }
  559. # drained organic soils
  560. categories["5"] = {
  561. "title": "Drained organic soils",
  562. "comment": "Drained organic soils",
  563. "info": {"gases": "CO2"},
  564. "children": [["5.A", "5.B"]],
  565. }
  566. categories["5.A"] = {
  567. "title": "Drained grassland",
  568. "comment": "Drained grassland",
  569. "info": {"gases": ["CO2", "N2O"]},
  570. }
  571. categories["5.B"] = {
  572. "title": "Drained cropland",
  573. "comment": "Drained cropland",
  574. "info": {"gases": ["CO2", "N2O"]},
  575. }
  576. # 6 Fires
  577. # Forest fires
  578. forest_fires_children = ["Humid tropical forests", "Other forests"]
  579. forest_fires_children_codes = ["6.A.1", "6.A.2"]
  580. for cat_name, code in zip(forest_fires_children, forest_fires_children_codes):
  581. categories[code] = {
  582. "title": cat_name,
  583. "comment": cat_name,
  584. "info": {"gases": ["CO2", "N2O", "CH4"]},
  585. }
  586. categories["6.A"] = {
  587. "title": "Forest fires",
  588. "comment": "Forest fires",
  589. "info": {"gases": ["CO2", "N2O", "CH4"]},
  590. "children": [forest_fires_children_codes],
  591. }
  592. # Savanna fires
  593. savanna_fires_children = [
  594. "Closed shrubland",
  595. "Grassland",
  596. "Open shrubland",
  597. "Savanna",
  598. "Woody savanna",
  599. ]
  600. savanna_fires_children_codes = ["6.B.1", "6.B.2", "6.B.3", "6.B.4", "6.B.5"]
  601. for cat_name, code in zip(savanna_fires_children, savanna_fires_children_codes):
  602. categories[code] = {
  603. "title": cat_name,
  604. "comment": cat_name,
  605. "info": {"gases": ["CO2", "N2O", "CH4"]},
  606. }
  607. categories["6.B"] = {
  608. "title": "Savanna fires",
  609. "comment": "Savanna fires",
  610. "info": {"gases": ["CO2", "N2O", "CH4"]},
  611. "children": [savanna_fires_children_codes],
  612. }
  613. # fires in organic soils
  614. categories["6.C"] = {
  615. "title": "Fires in organic soils",
  616. "comment": "Fires in organic soils",
  617. "info": {"gases": ["CO2", "N2O", "CH4"]},
  618. }
  619. # 6 fires
  620. categories["6"] = {
  621. "title": "Fires",
  622. "comment": "Fires",
  623. "info": {"gases": ["CO2", "N2O", "CH4"]},
  624. "children": [["6.A", "6.B", "6.C"]],
  625. }
  626. # 7 pre and post production
  627. pre_post_production_categories = [
  628. "Fertilizers Manufacturing",
  629. "Food Transport",
  630. "Food Retail",
  631. "Food Household Consumption",
  632. "Solid Food Waste",
  633. "Domestic Wastewater",
  634. "Industrial Wastewater",
  635. "Incineration",
  636. "Pre- and Post- Production",
  637. "Energy Use (Pre- and Post-Production)",
  638. "Agrifood Systems Waste Disposal",
  639. "Cold Chain F-Gas",
  640. "Pesticides Manufacturing",
  641. "Food Processing",
  642. "Food Packaging",
  643. ]
  644. pre_post_production_categories_codes = ["7." + i for i in "ABCDEFGHIJKLMNO"]
  645. pre_post_production_categories_gases = [
  646. ["CO2", "N2O", "KYOTOGHG (AR5GWP100)"],
  647. ["CO2", "CH4", "N2O", "KYOTOGHG (AR5GWP100)", "FGASES (AR5GWP100)"],
  648. ["CO2", "CH4", "N2O", "KYOTOGHG (AR5GWP100)", "FGASES (AR5GWP100)"],
  649. ["CO2", "CH4", "N2O", "KYOTOGHG (AR5GWP100)", "FGASES (AR5GWP100)"],
  650. ["KYOTOGHG (AR5GWP100)", "CH4"],
  651. ["KYOTOGHG (AR5GWP100)", "CH4", "N2O"],
  652. ["KYOTOGHG (AR5GWP100)", "CH4", "N2O"],
  653. ["CO2", "KYOTOGHG (AR5GWP100)"], # incineration
  654. ["CO2", "CH4", "N2O", "KYOTOGHG (AR5GWP100)", "FGASES (AR5GWP100)"],
  655. ["CO2", "CH4", "N2O", "KYOTOGHG (AR5GWP100)"],
  656. ["CO2", "CH4", "N2O", "KYOTOGHG (AR5GWP100)"],
  657. ["FGASES (AR5GWP100)"],
  658. ["CO2", "CH4", "N2O", "KYOTOGHG (AR5GWP100)"],
  659. ["CO2", "CH4", "N2O", "KYOTOGHG (AR5GWP100)", "FGASES (AR5GWP100)"],
  660. ["CO2", "CH4", "N2O", "KYOTOGHG (AR5GWP100)"],
  661. ]
  662. for cat_name, code, gases in zip(
  663. pre_post_production_categories,
  664. pre_post_production_categories_codes,
  665. pre_post_production_categories_gases,
  666. ):
  667. categories[code] = {
  668. "title": cat_name,
  669. "comment": cat_name,
  670. "info": {"gases": gases},
  671. }
  672. categories["7"] = {
  673. "title": "Pre and post agricultural production",
  674. "comment": "Pre and post agricultural production",
  675. "info": {
  676. "gases": [
  677. "CO2",
  678. "CH4",
  679. "N2O",
  680. "KYOTOGHG (AR5GWP100)",
  681. "FGASES (AR5GWP100)",
  682. ],
  683. },
  684. "children": [pre_post_production_categories_codes],
  685. }
  686. spec["categories"] = categories
  687. fao_cats = cc.HierarchicalCategorization.from_spec(spec.copy())
  688. # run print(fao_cats.show_as_tree())
  689. fao_cats.to_python("FAO.py")
  690. fao_cats.to_yaml("FAO.yaml")
  691. pass