test_conversion.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import climate_categories as cc
  2. import primap2 as pm2
  3. import pytest
  4. def test_yaml_to_python():
  5. cat = cc.from_yaml("FAO.yaml")
  6. cat.to_python("FAO.py")
  7. def test_python_to_yaml():
  8. from FAO import spec
  9. cat = cc.from_spec(spec)
  10. assert cat
  11. def test_make_dict_comprehension_for_faster_typing():
  12. crops = [
  13. "Millet",
  14. "Barley",
  15. "Maize (corn)",
  16. "Sugar cane",
  17. "Beans, dry",
  18. "Oats",
  19. "Rye",
  20. "Sorghum",
  21. "Soya beans",
  22. ]
  23. letters = ["d", "e", "f", "g", "h", "i", "j", "k", "l"]
  24. dict_crops = {}
  25. for crop, letter in zip(crops, letters):
  26. main_code = f"1.A.1.{letter}"
  27. crop_residues_code = f"1.A.1.{letter}.i"
  28. crop_residues_indirect_code = f"1.A.1.{letter}.i.2"
  29. crop_residues_direct_code = f"1.A.1.{letter}.i.1"
  30. dict_crops[main_code] = {
  31. "title": crop,
  32. "comment": crop,
  33. "alternative_codes": main_code.replace(".", ""),
  34. "children": [crop_residues_code],
  35. "info": {"gases": ["CH4", "N2O"]},
  36. }
  37. dict_crops[crop_residues_direct_code] = {
  38. "title": f"{crop} crop residues direct emissions",
  39. "comment": f"{crop} crop residues direct emissions",
  40. "alternative_codes": [crop_residues_direct_code.replace(".", "")],
  41. "info": {"gases": ["CH4", "N2O"]},
  42. }
  43. dict_crops[crop_residues_indirect_code] = {
  44. "title": f"{crop} crop residues indirect emissions",
  45. "comment": f"{crop} crop residues indirect emissions",
  46. "alternative_codes": [crop_residues_direct_code.replace(".", "")],
  47. "info": {"gases": ["CH4", "N2O"]},
  48. }
  49. dict_crops[crop_residues_code] = {
  50. "title": f"{crop} crop residues",
  51. "comment": f"{crop} crop residues",
  52. "alternative_codes": [crop_residues_code.replace(".", "")],
  53. "info": {"gases": ["CH4", "N2O"]},
  54. "children": [[crop_residues_direct_code, crop_residues_indirect_code]],
  55. }
  56. pass
  57. @pytest.mark.xfail
  58. def test_conversion_from_FAO_to_IPCC2006_PRIMAP():
  59. # make categorisation A from yaml
  60. categorisation_a = cc.from_yaml("FAO.yaml")
  61. # make categorisation B from yaml
  62. categorisation_b = cc.IPCC2006_PRIMAP
  63. # categories not part of climate categories so we need to add them manually
  64. cats = {
  65. "A": categorisation_a,
  66. "B": categorisation_b,
  67. }
  68. # make conversion from csv
  69. conv = cc.Conversion.from_csv("conversion.FAO.IPPCC2006_PRIMAP.csv", cats=cats)
  70. ds = pm2.open_dataset(
  71. "extracted_data/v2024-11-14/FAOSTAT_Agrifood_system_emissions_v2024-11-14.nc"
  72. )
  73. result = ds.pr.convert(
  74. dim="category",
  75. conversion=conv,
  76. auxiliary_dimensions={"gas": "source (gas)"},
  77. )
  78. assert result