admin管理员组

文章数量:1419223

so i want to calculate the SPEI with the Climate Indices package but i get the following error code:

numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)

Untyped global name '_norm_fitdict': Cannot determine Numba type of <class 'function'>

File "Lib\site-packages\climate_indices\indices.py", line 262:

def spei(

<source elided>

# Normalize fitting param keys

fitting_params = _norm_fitdict(fitting_params)

^

I read that you can just deactivate numba but that lead to the same error.

Also tried to adjust the code in Climate Indices with this:

# Normalize fitting param keys
    def _norm_fitdict(fitting_params):
    if fitting_params is None:
        return {}
    # Ensure all keys are strings (Numba doesn't handle dict comprehensions well)
    normalized = {}
    for key, value in fitting_params.items():
        normalized[str(key).lower()] = value
    return normalized

which also leads to the same issue. Right know i only see downgrading to python 3.10 as an option but i am worried that this will corrupt my current work. Has anyone an idea what else i could try?

Except manually doing the calculation?

Could i just delete my current environment and start with a new one? I read that climate indices could have issues with older environments.

The Code i am trying to run:

import xarray as xr
import numpy as np
from climate_indices import indices


precdata = xr.open_dataset(r"file") 
pevdata = xr.open_dataset(r"dfile")


precip = precdata["tp"]  
pet = pevdata["pev"]     


precip_np = precip.values
pet_np = pet.values
time = precip["time"].values
lat = precip["lat"].values  
lon = precip["lon"].values    


spei_values = np.full_like(precip_np, np.nan)


scale = 1 
distribution = "gamma"  #
calibration_start_year = 1994  
calibration_end_year = 2024    
data_start_year = 1994        
data_start_month = 1           

# Loop through each grid point to calculate SPEI
for i in range(len(lat)):
    for j in range(len(lon)):
        precip_series = precip_np[:, i, j]  # Time series of precipitation
        pet_series = pet_np[:, i, j]       # Time series of PET

        if np.all(np.isnan(precip_series)) or np.all(np.isnan(pet_series)):
            continue  

        # Calculate SPEI for this grid point
        spei_values[:, i, j] = indices.spei(
            precips_mm=precip_series,
            pet_mm=pet_series,
            scale=scale,
            distribution=distribution,
            periodicity="monthly",
            data_start_year=data_start_year,
            calibration_year_initial=calibration_start_year,
            calibration_year_final=calibration_end_year
        )

本文标签: Climate Indices nopythontypeinference with numbaStack Overflow