|
@@ -1,11 +1,12 @@
|
|
|
+import numpy as np
|
|
|
+import primap2 as pm
|
|
|
import pytest
|
|
|
+import xarray as xr
|
|
|
|
|
|
from src.unfccc_ghg_data.unfccc_crf_reader.crf_raw_for_year_sparse_arrays import (
|
|
|
- crf_raw_for_year_datatree
|
|
|
+ crf_raw_for_year_datatree,
|
|
|
)
|
|
|
-import primap2 as pm
|
|
|
-import xarray as xr
|
|
|
-import numpy as np
|
|
|
+
|
|
|
|
|
|
@pytest.fixture
|
|
|
def crt_dt(tmp_path):
|
|
@@ -20,76 +21,120 @@ def crt_dt(tmp_path):
|
|
|
|
|
|
return crt_dt
|
|
|
|
|
|
-def test_country_aggregation(crt_dt):
|
|
|
|
|
|
+def test_country_aggregation(crt_dt):
|
|
|
|
|
|
expected_NGA = 123840.3389332373
|
|
|
- assert np.squeeze(crt_dt["/NGA"]["CO2"].sel(
|
|
|
- {"category (CRT1)" : "1", "class" : "Total", "time" : "2022-01-01"}).to_numpy()).item() == expected_NGA
|
|
|
+ assert (
|
|
|
+ np.squeeze(
|
|
|
+ crt_dt["/NGA"]["CO2"]
|
|
|
+ .sel({"category (CRT1)": "1", "class": "Total", "time": "2022-01-01"})
|
|
|
+ .to_numpy()
|
|
|
+ ).item()
|
|
|
+ == expected_NGA
|
|
|
+ )
|
|
|
|
|
|
assert len(crt_dt["/NGA"].time) == 23
|
|
|
|
|
|
|
|
|
expected_GEO = 10954.899111969342
|
|
|
- assert np.squeeze(crt_dt["/GEO"]["CO2"].sel(
|
|
|
- {"category (CRT1)" : "1", "class" : "Total", "time" : "2022-01-01"}).to_numpy()).item() == expected_GEO
|
|
|
+ assert (
|
|
|
+ np.squeeze(
|
|
|
+ crt_dt["/GEO"]["CO2"]
|
|
|
+ .sel({"category (CRT1)": "1", "class": "Total", "time": "2022-01-01"})
|
|
|
+ .to_numpy()
|
|
|
+ ).item()
|
|
|
+ == expected_GEO
|
|
|
+ )
|
|
|
|
|
|
assert len(crt_dt["/GEO"].time) == 33
|
|
|
|
|
|
- assert np.squeeze(crt_dt["/GEO"]["CO2"].sel(
|
|
|
- {"category (CRT1)" : "1", "class" : "Total", "time" : "1990-01-01"}).to_numpy()).item()
|
|
|
+ assert np.squeeze(
|
|
|
+ crt_dt["/GEO"]["CO2"]
|
|
|
+ .sel({"category (CRT1)": "1", "class": "Total", "time": "1990-01-01"})
|
|
|
+ .to_numpy()
|
|
|
+ ).item()
|
|
|
|
|
|
|
|
|
- crt_dt["/NGA_GEO"] = crt_dt["/NGA"].to_dataset().pr.loc[{"area (ISO3)" : "NGA"}] + crt_dt["/GEO"].to_dataset().pr.loc[{"area (ISO3)" : "GEO"}]
|
|
|
+ crt_dt["/NGA_GEO"] = (
|
|
|
+ crt_dt["/NGA"].to_dataset().pr.loc[{"area (ISO3)": "NGA"}]
|
|
|
+ + crt_dt["/GEO"].to_dataset().pr.loc[{"area (ISO3)": "GEO"}]
|
|
|
+ )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- assert np.squeeze(crt_dt["/NGA_GEO"]["CO2"].sel(
|
|
|
- {"category (CRT1)" : "1", "class" : "Total", "time" : "2022-01-01"}).to_numpy()).item() == expected_NGA + expected_GEO
|
|
|
+ assert (
|
|
|
+ np.squeeze(
|
|
|
+ crt_dt["/NGA_GEO"]["CO2"]
|
|
|
+ .sel({"category (CRT1)": "1", "class": "Total", "time": "2022-01-01"})
|
|
|
+ .to_numpy()
|
|
|
+ ).item()
|
|
|
+ == expected_NGA + expected_GEO
|
|
|
+ )
|
|
|
assert len(crt_dt["/NGA_GEO"].time) == 23
|
|
|
|
|
|
|
|
|
|
|
|
with xr.set_options(arithmetic_join="outer"):
|
|
|
- crt_dt["/NGA_GEO_outer"] = crt_dt["/NGA"].to_dataset().pr.loc[{"area (ISO3)" : "NGA"}] + crt_dt["/GEO"].to_dataset().pr.loc[{"area (ISO3)" : "GEO"}]
|
|
|
+ crt_dt["/NGA_GEO_outer"] = (
|
|
|
+ crt_dt["/NGA"].to_dataset().pr.loc[{"area (ISO3)": "NGA"}]
|
|
|
+ + crt_dt["/GEO"].to_dataset().pr.loc[{"area (ISO3)": "GEO"}]
|
|
|
+ )
|
|
|
|
|
|
|
|
|
- assert np.squeeze(crt_dt["/NGA_GEO_outer"]["CO2"].sel(
|
|
|
- {"category (CRT1)" : "1", "class" : "Total", "time" : "2022-01-01"}).to_numpy()).item() == expected_NGA + expected_GEO
|
|
|
+ assert (
|
|
|
+ np.squeeze(
|
|
|
+ crt_dt["/NGA_GEO_outer"]["CO2"]
|
|
|
+ .sel({"category (CRT1)": "1", "class": "Total", "time": "2022-01-01"})
|
|
|
+ .to_numpy()
|
|
|
+ ).item()
|
|
|
+ == expected_NGA + expected_GEO
|
|
|
+ )
|
|
|
|
|
|
|
|
|
- assert np.isnan(np.squeeze(crt_dt["/NGA_GEO_outer"]["CO2"].sel(
|
|
|
- {"category (CRT1)" : "1", "class" : "Total", "time" : "1990-01-01"}).to_numpy()).item())
|
|
|
+ assert np.isnan(
|
|
|
+ np.squeeze(
|
|
|
+ crt_dt["/NGA_GEO_outer"]["CO2"]
|
|
|
+ .sel({"category (CRT1)": "1", "class": "Total", "time": "1990-01-01"})
|
|
|
+ .to_numpy()
|
|
|
+ ).item()
|
|
|
+ )
|
|
|
|
|
|
assert len(crt_dt["/NGA_GEO_outer"].time) == 33
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- dt_geo_nga = crt_dt.filter(lambda x: x.name in ["NGA", "GEO"])
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+ dt_geo_nga = crt_dt.filter(lambda x: x.name in ["NGA", "GEO"])
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
def test_crf_for_year_original_version(crt_dt):
|
|
|
-
|
|
|
print(crt_dt.groups)
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
- primap_hist = pm.open_dataset("../../data/Guetschow_et_al_2025-PRIMAP-hist_v2.6.1_final_13-Mar-2025.nc")
|
|
|
+ primap_hist = pm.open_dataset(
|
|
|
+ "../../data/Guetschow_et_al_2025-PRIMAP-hist_v2.6.1_final_13-Mar-2025.nc"
|
|
|
+ )
|
|
|
|
|
|
crt_dt["primap_hist"] = primap_hist
|
|
|
print(crt_dt.groups)
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
@@ -101,7 +146,9 @@ def test_crf_for_year_original_version(crt_dt):
|
|
|
|
|
|
|
|
|
|
|
|
- crt_dt["primap_hist"] = primap_hist.rename({"category (IPCC2006_PRIMAP)" : "category (CRT1)"})
|
|
|
+ crt_dt["primap_hist"] = primap_hist.rename(
|
|
|
+ {"category (IPCC2006_PRIMAP)": "category (CRT1)"}
|
|
|
+ )
|
|
|
|
|
|
|
|
|
- dt_cat1 = crt_dt.sel({"category (CRT1)" : "1"})
|
|
|
+ dt_cat1 = crt_dt.sel({"category (CRT1)": "1"})
|