C2SM-RCM / emiproc

Preprocessing of emissions: from any inventory to any model
BSD 3-Clause "New" or "Revised" License
4 stars 9 forks source link

nan values in inventory after speciation from 0 values on -99 countries #58

Closed lionel42 closed 1 month ago

lionel42 commented 1 month ago

Nan values appear in the inventory after speciation on the 2 following conditions.

Expected behaviour:

Example

# %%
import geopandas as gpd
import numpy as np
import xarray as xr

from emiproc.grids import RegularGrid
from emiproc.inventories import Inventory
from emiproc.plots import explore_inventory
from emiproc.speciation import speciate
from emiproc.utilities import get_country_mask

# %%
# Grid has cells in sea, in kosovo and in the surrounding countries
kosovian_grid = RegularGrid(
    xmin=18.0,
    ymin=41.8,
    xmax=21.0,
    ymax=43.0,
    nx=10,
    ny=10,
)

# %%
kosovian_grid.gdf.explore()
# %%

# %%
fractions = get_country_mask(kosovian_grid, return_fractions=True)
country_mask = get_country_mask(kosovian_grid, return_fractions=False)
# %%
gdf = kosovian_grid.gdf.copy()
gdf["country"] = country_mask.reshape(-1)
gdf.explore("country")

# %%

da_speciation = xr.DataArray(
    data=[
        [0.5, 0.5],
        [0.3, 0.7],
        [0.7, 0.3],
        [0.1, 0.9],
        [0.0, 1.0],
        [1.0, 0.0],
        [0.9, 0.1],
    ],
    dims=["speciation", "substance"],
    coords={
        "country": (("speciation"), ["ALB", "HRV", "BIH", "MKD", "SRB", "MNE", "-99"]),
        "substance": ["A", "B"],
        "speciation": range(7),
    },
)
da_speciation
# %%
gdf = kosovian_grid.gdf.copy()
ranges = np.arange(len(gdf), dtype=float)
ranges_0 = ranges.copy()
ranges_0[[50, 89, 98]] = 0.0
inv = Inventory.from_gdf(
    gpd.GeoDataFrame(
        {
            ("catrange", "sub"): ranges,
            ("catrange_0", "sub"): ranges_0,
        },
        geometry=gdf.geometry,
    )
)

# %%
explore_inventory(inv, category="catrange_0", substance="sub")
# %%
speciated = speciate(inv, speciation_ratios=da_speciation, substance="sub")

# %%
explore_inventory(speciated, category="catrange_0", substance="A")