pytroll / pytroll/pyresample

When using the resample_nearest function in pyresample for interpolation, it sometimes produces NaN values.

Open
#698 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
385
Forks
102
Avg merge
4d 2h
Merged PRs (30d)
9

Description

When I use the following code to interpolate the data, I find that the results occasionally contain NaN values after multiple runs.

import numpy as np
from pyresample.geometry import SwathDefinition, AreaDefinition
from pyresample.kd_tree import resample_nearest
import os
def resample_wgs84_to_grid(lat2d, lon2d, data2d,
                           lons_1d, lats_1d,
                           radius_of_influence=50000,
                           fill_value=np.nan):
    np.random.seed(42)

    src_swath = SwathDefinition(lons=np.asarray(lon2d),
                                lats=np.asarray(lat2d))


    proj_dict = {'proj': 'longlat', 'datum': 'WGS84'}
    area_extent = (lons_1d[0], lats_1d[0],
                   lons_1d[-1], lats_1d[-1])
    dst_area = AreaDefinition(
        'dst_grid', 'WGS84 regular', proj_dict,
        proj_dict,
        width=lons_1d.size,
        height=lats_1d.size,
        area_extent=area_extent
    )


    data_resampled = resample_nearest(
        src_swath, np.asarray(data2d),
        dst_area,
        radius_of_influence=radius_of_influence,
        fill_value=fill_value,nprocs=1

    )
    lon2d_dst, lat2d_dst = np.meshgrid(lons_1d, lats_1d)
    return data_resampled, lon2d_dst, lat2d_dst
fy4_data = np.load(r'H:\Yan\DATA\冰雹云\FY4\FY4B\AGRI\L1\FDI\DISK\4000M\2023\20230323\FY4B-_AGRI--_N_DISK_1330E_L1-_FDI-_MULT_NOM_20230323164500_20230323165959_4000M_V0001.npz')
ir = fy4_data['ir']
lat_f = fy4_data['lat']
lon_f = fy4_data['lon']
DeterministicInterpolator
target_lons = np.linspace(110,120,256)
target_lats = np.linspace(20,30,256)
for i in range(ir.shape[0]):
    d = copy.deepcopy(ir)
    grid_product, grid_x, grid_y = resample_wgs84_to_grid(
                    lat_f.astype(np.float64), lon_f.astype(np.float64), d[i].astype(np.float64),
                    lons_1d=target_lons.astype(np.float64),  # 目标经度轴
                    lats_1d=target_lats.astype(np.float64),  # 目标纬度轴
                    radius_of_influence=15000
            )
    print(np.nanmin(grid_product))

The expected result under normal operation should be

217.77999877929688
199.92999267578125
210.5399932861328
211.10000610351562
167.19000244140625
214.86000061035156
214.22999572753906
212.9199981689453
213.42999267578125

However, after repeated runs, a value of NaN may randomly appear in a certain channel, resulting in the following output:

217.77999877929688
nan
G:\py_project\radar_code\dor_2_nc\123.py:19: RuntimeWarning: All-NaN slice encountered
  print(np.nanmin(grid_product))
210.5399932861328
211.10000610351562
167.19000244140625
214.86000061035156
214.22999572753906
212.9199981689453
213.42999267578125

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided reproducer with the stated NumPy data and repeated resample_nearest calls. Read the resample_nearest entry point and the relevant pyresample kd-tree resampling path to determine why one channel can become entirely NaN despite nearby source data. Done means repeated runs produce deterministic results without the unexpected all-NaN output, with a regression test if the project’s existing test structure supports it.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.