test_conversion.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import climate_categories as cc
  2. import primap2 as pm2
  3. import pytest
  4. from src.faostat_data_primap.helper.paths import downloaded_data_path
  5. from src.faostat_data_primap.read import read_data
  6. def test_read(tmp_path):
  7. domains_and_releases_to_read = [
  8. # ("farm_gate_agriculture_energy", "2024-11-14"),
  9. # ("farm_gate_emissions_crops", "2024-11-14"),
  10. ("farm_gate_livestock", "2024-11-14"),
  11. # ("land_use_drained_organic_soils", "2024-11-14"),
  12. # ("land_use_fires", "2024-11-14"),
  13. # ("land_use_forests", "2024-11-14"),
  14. # ("pre_post_agricultural_production", "2024-11-14"),
  15. ]
  16. read_data(
  17. domains_and_releases_to_read=domains_and_releases_to_read,
  18. read_path=downloaded_data_path,
  19. save_path=tmp_path,
  20. )
  21. def test_yaml_to_python():
  22. cat = cc.from_yaml("FAO.yaml")
  23. cat.to_python("FAO.py")
  24. def test_python_to_yaml():
  25. from FAO import spec
  26. cat = cc.from_spec(spec)
  27. assert cat
  28. def test_make_dict_comprehension_for_faster_typing():
  29. spec = {
  30. "name": "FAO",
  31. "title": (
  32. "Food and Agriculture Organization of the United Nations (FAO) "
  33. "FAOSTAT data set categorisation"
  34. ),
  35. "comment": "Needed to add FAOSTAT data to PRIMAP-hist",
  36. "references": "",
  37. "institution": "FAO",
  38. "hierarchical": True,
  39. "last_update": "2024-12-10",
  40. "version": "2024",
  41. "total_sum": True,
  42. "canonical_top_level_category": "0",
  43. }
  44. categories = {}
  45. # 0. main categories
  46. categories["0"] = {
  47. "title": "Total",
  48. "comment": "All emissions and removals",
  49. "children": [["1", "2"]], # , "3", "4", "5", "6", "7"]],
  50. }
  51. children_1 = ["1.A", "1.B"]
  52. children_2 = ["2.A", "2.B", "2.C", "2.D", "2.E"]
  53. children_3 = [f"3.{i}" for i in "ABCDEFGHIJKLMNOPQR"]
  54. children_4 = ["4.A"]
  55. children_5 = ["5.A", "5.B"]
  56. children_6 = ["6.A", "6.B", "6.C"]
  57. children_7 = [f"3.{i}" for i in "ABCDEFGHIJKLM"]
  58. main_categories = (
  59. # category code, name and comment, gases, children
  60. ("1", "Crops", ["CH4", "N2O"], children_1),
  61. (
  62. "2",
  63. "Energy use in agriculture",
  64. ["CH4", "N2O", "CO2"],
  65. children_2,
  66. ),
  67. # ("3", "Livestock", ["CH4", "N2O"], children_3),
  68. # ("4", "Forest", ["CO2"], children_4),
  69. # (
  70. # "5",
  71. # "Drained organic soils",
  72. # ["N2O", "CO2"],
  73. # children_5,
  74. # ),
  75. # ("6", "Fires", ["CH4", "N2O", "CO2"], children_6),
  76. # (
  77. # "7",
  78. # "Pre and post agricultural production",
  79. # ["CH4", "N2O", "CO2"],
  80. # children_7,
  81. # ),
  82. )
  83. for code, name, gases, children in main_categories:
  84. categories[code] = {
  85. "title": name,
  86. "comment": name,
  87. "alternative_codes": code.replace(".", ""),
  88. "children": [children],
  89. "info": {"gases": gases},
  90. }
  91. # 1. crops
  92. # all crops category
  93. code_all_crops = "1.A"
  94. codes_crops = [f"1.A.{i}" for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]]
  95. categories[code_all_crops] = {
  96. "title": "All crops",
  97. "comment": "All crops",
  98. "alternative_codes": code_all_crops.replace(".", ""),
  99. "children": [codes_crops],
  100. "info": {"gases": ["CH4", "N2O"]},
  101. }
  102. crops = [
  103. "Wheat",
  104. "Rice",
  105. "Potatoes",
  106. "Millet",
  107. "Barley",
  108. "Maize (corn)",
  109. "Sugar cane",
  110. "Beans, dry",
  111. "Oats",
  112. "Rye",
  113. "Sorghum",
  114. "Soya beans",
  115. ]
  116. crop_burnings = [
  117. True,
  118. True,
  119. False,
  120. False,
  121. False,
  122. True,
  123. True,
  124. False,
  125. False,
  126. False,
  127. False,
  128. False,
  129. ]
  130. rice_cultivations = [
  131. False,
  132. True,
  133. False,
  134. False,
  135. False,
  136. False,
  137. False,
  138. False,
  139. False,
  140. False,
  141. False,
  142. False,
  143. ]
  144. for crop, code, crop_burning, rice_cultivation in zip(
  145. crops, codes_crops, crop_burnings, rice_cultivations
  146. ):
  147. # all crops have at least N2O emissions
  148. gases_main = "N2O"
  149. if crop_burning or rice_cultivation:
  150. gases_main = ["CH4", "N2O"]
  151. # all crops have at least crop residues as child
  152. children_main = [f"{code}.a"]
  153. if crop_burning:
  154. children_main.append(f"{code}.b")
  155. if rice_cultivation:
  156. children_main.append(f"{code}.c")
  157. categories[f"{code}"] = {
  158. "title": f"{crop}",
  159. "comment": f"{crop}",
  160. "alternative_codes": [f"{code}".replace(".", "")],
  161. "info": {"gases": gases_main},
  162. "children": [children_main],
  163. }
  164. # crop residues (every crop has it)
  165. categories[f"{code}.a.i"] = {
  166. "title": f"{crop} crop residues direct emissions",
  167. "comment": f"{crop} crop residues direct emissions",
  168. "alternative_codes": [f"{code}.a".replace(".", "")],
  169. "info": {"gases": ["N2O"]},
  170. }
  171. categories[f"{code}.a.ii"] = {
  172. "title": f"{crop} crop residues indirect emissions",
  173. "comment": f"{crop} crop residues indirect emissions",
  174. "alternative_codes": [f"{code}.a.i".replace(".", "")],
  175. "info": {"gases": ["N2O"]},
  176. }
  177. categories[f"{code}.a"] = {
  178. "title": f"{crop} crop residues",
  179. "comment": f"{crop} crop residues",
  180. "alternative_codes": [f"{code}.a".replace(".", "")],
  181. "info": {"gases": ["N2O"]},
  182. "children": [[f"{code}.a.ii", f"{code}.a.i"]],
  183. }
  184. if crop_burning:
  185. categories[f"{code}.b"] = {
  186. "title": f"{crop} burning crop residues",
  187. "comment": f"{crop} burning crop residues",
  188. "alternative_codes": [f"{code}.b".replace(".", "")],
  189. "info": {"gases": ["CH4", "N2O"]},
  190. }
  191. if rice_cultivation:
  192. categories[f"{code}.c"] = {
  193. "title": "Rice cultivation",
  194. "comment": "Rice cultivation",
  195. "alternative_codes": [f"{code}.c".replace(".", "")],
  196. "info": {"gases": ["CH4"]},
  197. }
  198. # synthetic fertilisers
  199. codes_synthetic_fertilisers = ["1.B", "1.B.1", "1.B.2", "1.B.2.a", "1.B.2.b"]
  200. names = [
  201. "Synthetic fertilisers",
  202. "Direct emissions",
  203. "Indirect emissions",
  204. "Indirect emissions that volatilise",
  205. "Indirect emissions that leach",
  206. ]
  207. children_cats = [["1.B.1", "1.B.2"], None, ["1.B.2.a", "1.B.2.b"], None, None]
  208. for code, name, child_cat in zip(codes_synthetic_fertilisers, names, children_cats):
  209. categories[code] = {
  210. "title": name,
  211. "comment": name,
  212. "alternative_codes": [code.replace(".", "")],
  213. "info": {"gases": ["N2O"]},
  214. }
  215. if child_cat:
  216. categories[code]["children"] = [child_cat]
  217. # 2. energy use
  218. names = [
  219. "Natural gas",
  220. "Electricity",
  221. "Coal",
  222. "Heat",
  223. "Petroleum",
  224. ]
  225. codes = children_2
  226. for name, code in zip(names, codes):
  227. categories[code] = {
  228. "title": name,
  229. "comment": name,
  230. "alternative_codes": code.replace(".", ""),
  231. "info": {"gases": ["CH4", "N2O", "CO2"]},
  232. }
  233. spec["categories"] = categories
  234. cat = cc.HierarchicalCategorization.from_spec(spec.copy())
  235. pass
  236. @pytest.mark.xfail
  237. def test_conversion_from_FAO_to_IPCC2006_PRIMAP():
  238. # make categorisation A from yaml
  239. categorisation_a = cc.from_yaml("FAO.yaml")
  240. # make categorisation B from yaml
  241. categorisation_b = cc.IPCC2006_PRIMAP
  242. # categories not part of climate categories so we need to add them manually
  243. cats = {
  244. "A": categorisation_a,
  245. "B": categorisation_b,
  246. }
  247. # make conversion from csv
  248. conv = cc.Conversion.from_csv("conversion.FAO.IPPCC2006_PRIMAP.csv", cats=cats)
  249. ds = pm2.open_dataset(
  250. "extracted_data/v2024-11-14/FAOSTAT_Agrifood_system_emissions_v2024-11-14.nc"
  251. )
  252. result = ds.pr.convert(
  253. dim="category",
  254. conversion=conv,
  255. auxiliary_dimensions={"gas": "source (gas)"},
  256. )
  257. assert result