test_conversion.py 24 KB

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