test_conversion.py 23 KB

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