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