test_conversion.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import primap2 as pm2
  2. import pytest
  3. from src.faostat_data_primap.helper.paths import (
  4. downloaded_data_path,
  5. extracted_data_path,
  6. )
  7. from src.faostat_data_primap.read import process, read_data
  8. # For development work on process function only
  9. def test_processed_output_remains_the_same():
  10. # get processed data
  11. release_name = "v2024-11-14"
  12. # release_name = "v2023-12-13"
  13. filename_processed_ds = f"FAOSTAT_Agrifood_system_emissions_{release_name}"
  14. filepath = extracted_data_path / release_name / (filename_processed_ds + ".nc")
  15. ds_processed = pm2.open_dataset(filepath)
  16. # get raw data
  17. filename_raw_ds = (
  18. extracted_data_path
  19. / f"{release_name}/FAOSTAT_Agrifood_system_emissions_{release_name}_raw.nc"
  20. )
  21. ds_raw = pm2.open_dataset(filename_raw_ds)
  22. # process raw data
  23. ds_processed_new = process(ds=ds_raw)
  24. # compare
  25. assert ds_processed.broadcast_equals(ds_processed_new)
  26. @pytest.mark.parametrize(
  27. "domains_and_releases_to_read",
  28. [
  29. pytest.param(
  30. [
  31. ("farm_gate_agriculture_energy", "2023-12-13"),
  32. ("farm_gate_emissions_crops", "2023-11-09"),
  33. ("farm_gate_livestock", "2023-11-09"),
  34. ("land_use_drained_organic_soils", "2023-11-09"),
  35. ("land_use_fires", "2023-11-09"),
  36. ("land_use_forests", "2023-11-09"),
  37. ("pre_post_agricultural_production", "2023-11-09"),
  38. ],
  39. id="2023 release",
  40. ),
  41. pytest.param(
  42. [
  43. ("farm_gate_agriculture_energy", "2024-11-14"),
  44. ("farm_gate_emissions_crops", "2024-11-14"),
  45. ("farm_gate_livestock", "2024-11-14"),
  46. ("land_use_drained_organic_soils", "2024-11-14"),
  47. ("land_use_fires", "2024-11-14"),
  48. ("land_use_forests", "2024-11-14"),
  49. ("pre_post_agricultural_production", "2024-11-14"),
  50. ],
  51. id="2024 release",
  52. ),
  53. ],
  54. )
  55. def test_read(tmp_path, domains_and_releases_to_read):
  56. read_data(
  57. domains_and_releases_to_read=domains_and_releases_to_read,
  58. read_path=downloaded_data_path,
  59. save_path=tmp_path,
  60. )
  61. # TODO delete everything below here when data set is final
  62. # def test_conversion_from_FAO_to_IPCC2006_PRIMAP():
  63. # release_name = "v2024-11-14"
  64. # # release_name = "v2023-12-13"
  65. #
  66. # # get raw data
  67. # filename_raw_ds = (
  68. # extracted_data_path
  69. # / f"{release_name}/FAOSTAT_Agrifood_system_emissions_{release_name}_raw.nc"
  70. # )
  71. # ds_raw = pm2.open_dataset(filename_raw_ds)
  72. #
  73. # # process raw data
  74. # result_proc = process(ds=ds_raw)
  75. #
  76. # result_proc_if = result_proc.pr.to_interchange_format()
  77. #
  78. # # save processed data
  79. # output_filename = f"FAOSTAT_Agrifood_system_emissions_{release_name}"
  80. # output_folder = extracted_data_path / release_name
  81. #
  82. # if not output_folder.exists():
  83. # output_folder.mkdir()
  84. #
  85. # filepath = output_folder / (output_filename + ".csv")
  86. # print(f"Writing processed primap2 file to {filepath}")
  87. # pm2.pm2io.write_interchange_format(
  88. # filepath,
  89. # result_proc_if,
  90. # )
  91. #
  92. # compression = dict(zlib=True, complevel=9)
  93. # encoding = {var: compression for var in result_proc.data_vars}
  94. # filepath = output_folder / (output_filename + ".nc")
  95. # print(f"Writing netcdf file to {filepath}")
  96. # result_proc.pr.to_netcdf(filepath, encoding=encoding)
  97. #
  98. # def test_read_2023():
  99. # domains_and_releases_to_read = [
  100. # # ("farm_gate_agriculture_energy", "2023-12-13"),
  101. # # ("farm_gate_emissions_crops", "2023-11-09"),
  102. # # ("farm_gate_livestock", "2023-11-09"),
  103. # # ("land_use_drained_organic_soils", "2023-11-09"),
  104. # # ("land_use_fires", "2023-11-09"),
  105. # # ("land_use_forests", "2023-11-09"),
  106. # # ("pre_post_agricultural_production", "2023-11-09"),
  107. # ("farm_gate_agriculture_energy", "2024-11-14"),
  108. # ("farm_gate_emissions_crops", "2024-11-14"),
  109. # ("farm_gate_livestock", "2024-11-14"),
  110. # ("land_use_drained_organic_soils", "2024-11-14"),
  111. # ("land_use_fires", "2024-11-14"),
  112. # ("land_use_forests", "2024-11-14"),
  113. # ("pre_post_agricultural_production", "2024-11-14"),
  114. # ]
  115. #
  116. # read_data(
  117. # domains_and_releases_to_read=domains_and_releases_to_read,
  118. # read_path=downloaded_data_path,
  119. # save_path=extracted_data_path,
  120. # )