test_conversion.py 25 KB

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