|
@@ -4,9 +4,9 @@ import xarray as xr
|
|
|
|
|
|
from src.faostat_data_primap.helper.category_aggregation import (
|
|
|
agg_info_fao,
|
|
|
- agg_info_ipcc2006_primap,
|
|
|
agg_info_ipcc2006_primap_CH4,
|
|
|
agg_info_ipcc2006_primap_CO2,
|
|
|
+ agg_info_ipcc2006_primap_N2O,
|
|
|
)
|
|
|
from src.faostat_data_primap.helper.paths import (
|
|
|
downloaded_data_path,
|
|
@@ -15,6 +15,131 @@ from src.faostat_data_primap.helper.paths import (
|
|
|
from src.faostat_data_primap.read import read_data
|
|
|
|
|
|
|
|
|
+def test_conversion_from_FAO_to_IPCC2006_PRIMAP_output_equal():
|
|
|
+
|
|
|
+ categorisation_a = cc.FAO
|
|
|
+
|
|
|
+ categorisation_b = cc.IPCC2006_PRIMAP
|
|
|
+
|
|
|
+
|
|
|
+ cats = {
|
|
|
+ "FAO": categorisation_a,
|
|
|
+ "IPCC2006_PRIMAP": categorisation_b,
|
|
|
+ }
|
|
|
+
|
|
|
+ release_name = "v2023-12-13"
|
|
|
+
|
|
|
+
|
|
|
+ reproduce23 = True
|
|
|
+
|
|
|
+ ds_fao = (
|
|
|
+ extracted_data_path
|
|
|
+
|
|
|
+ / f"{release_name}/FAOSTAT_Agrifood_system_emissions_{release_name}_raw.nc"
|
|
|
+ )
|
|
|
+ ds = pm2.open_dataset(ds_fao)
|
|
|
+
|
|
|
+
|
|
|
+ ds = ds.drop_sel(source="UNFCCC")
|
|
|
+
|
|
|
+
|
|
|
+ ds_checked = ds.pr.add_aggregates_coordinates(agg_info=agg_info_fao)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ conv = {}
|
|
|
+ gases = ["CO2", "CH4", "N2O"]
|
|
|
+
|
|
|
+ if reproduce23:
|
|
|
+ reproduce23_filename = "_reproduce23"
|
|
|
+ else:
|
|
|
+ reproduce23_filename = ""
|
|
|
+
|
|
|
+ for var in gases:
|
|
|
+ conv[var] = cc.Conversion.from_csv(
|
|
|
+ f"../../conversion_FAO_IPPCC2006_PRIMAP_{var}{reproduce23_filename}.csv",
|
|
|
+ cats=cats,
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+ da_dict = {}
|
|
|
+ for var in gases:
|
|
|
+ da_dict[var] = ds[var].pr.convert(
|
|
|
+ dim="category (FAO)",
|
|
|
+ conversion=conv[var],
|
|
|
+ )
|
|
|
+ result = xr.Dataset(da_dict)
|
|
|
+ result.attrs = ds.attrs
|
|
|
+ result.attrs["cat"] = "category (IPCC2006_PRIMAP)"
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ result_if = result.pr.to_interchange_format()
|
|
|
+ result = pm2.pm2io.from_interchange_format(result_if)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ result_proc = result.pr.add_aggregates_coordinates(
|
|
|
+ agg_info=agg_info_ipcc2006_primap_N2O
|
|
|
+ )
|
|
|
+
|
|
|
+ result_proc = result_proc.pr.add_aggregates_coordinates(
|
|
|
+ agg_info=agg_info_ipcc2006_primap_CO2
|
|
|
+ )
|
|
|
+
|
|
|
+ result_proc = result_proc.pr.add_aggregates_coordinates(
|
|
|
+ agg_info=agg_info_ipcc2006_primap_CH4
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+ output_filename = f"FAOSTAT_Agrifood_system_emissions_{release_name}"
|
|
|
+ output_folder = extracted_data_path / release_name
|
|
|
+ filepath = output_folder / (output_filename + ".nc")
|
|
|
+ ds_original = pm2.open_dataset(filepath)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ assert ds_original.broadcast_equals(result_proc)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+def test_read(tmp_path):
|
|
|
+ domains_and_releases_to_read = [
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ ("land_use_fires", "2023-11-09"),
|
|
|
+
|
|
|
+
|
|
|
+ ]
|
|
|
+
|
|
|
+ read_data(
|
|
|
+ domains_and_releases_to_read=domains_and_releases_to_read,
|
|
|
+ read_path=downloaded_data_path,
|
|
|
+ save_path=tmp_path,
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
def test_conversion_from_FAO_to_IPCC2006_PRIMAP():
|
|
|
|
|
|
categorisation_a = cc.FAO
|
|
@@ -79,8 +204,9 @@ def test_conversion_from_FAO_to_IPCC2006_PRIMAP():
|
|
|
result = pm2.pm2io.from_interchange_format(result_if)
|
|
|
|
|
|
|
|
|
+
|
|
|
result_proc = result.pr.add_aggregates_coordinates(
|
|
|
- agg_info=agg_info_ipcc2006_primap
|
|
|
+ agg_info=agg_info_ipcc2006_primap_N2O
|
|
|
)
|
|
|
|
|
|
result_proc = result_proc.pr.add_aggregates_coordinates(
|
|
@@ -114,22 +240,22 @@ def test_conversion_from_FAO_to_IPCC2006_PRIMAP():
|
|
|
result_proc.pr.to_netcdf(filepath, encoding=encoding)
|
|
|
|
|
|
|
|
|
-def test_read(tmp_path):
|
|
|
- domains_and_releases_to_read = [
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- ("land_use_fires", "2023-11-09"),
|
|
|
-
|
|
|
-
|
|
|
- ]
|
|
|
-
|
|
|
- read_data(
|
|
|
- domains_and_releases_to_read=domains_and_releases_to_read,
|
|
|
- read_path=downloaded_data_path,
|
|
|
- save_path=tmp_path,
|
|
|
- )
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
def test_read_2023():
|