test_conversion.py 24 KB

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