Skip to content

Commit

Permalink
Refactor xarray nearest neighbor
Browse files Browse the repository at this point in the history
  • Loading branch information
djhoese committed Nov 19, 2023
1 parent f2c7801 commit 9e549ab
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions pyresample/kd_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,26 +1083,9 @@ def get_sample_from_neighbour_info(self, data, fill_value=np.nan):
# Convert from multiple neighbor shape to 1 neighbor
ia = self.index_array[:, :, 0]
vii = self.valid_input_index
src_geo_dims, dst_geo_dims = self._get_valid_dims(data)

if isinstance(self.source_geo_def, geometry.SwathDefinition):
# could be 1D or 2D
src_geo_dims = self.source_geo_def.lons.dims
else:
# assume AreaDefinitions and everything else are 2D with 'y', 'x'
src_geo_dims = ('y', 'x')
dst_geo_dims = ('y', 'x')
# verify that source dims are the same between geo and data
data_geo_dims = tuple(d for d in data.dims if d in src_geo_dims)
if data_geo_dims != src_geo_dims:
raise ValueError("Data dimensions do not match source area dimensions")
# verify that the dims are next to each other
first_dim_idx = data.dims.index(src_geo_dims[0])
num_dims = len(src_geo_dims)
if data.dims[first_dim_idx:first_dim_idx + num_dims] != data_geo_dims:
raise ValueError("Data's geolocation dimensions are not consecutive.")

# FIXME: Can't include coordinates whose dimensions depend on the geo
# dims either
# FIXME: Can't include coordinates whose dimensions depend on the geo dims either

Check notice on line 1088 in pyresample/kd_tree.py

View check run for this annotation

codefactor.io / CodeFactor

pyresample/kd_tree.py#L1088

unresolved comment '# FIXME: Can't include coordinates whose dimensions depend on the geo dims either' (C100)
def contain_coords(var, coord_list):
return bool(set(coord_list).intersection(set(var.dims)))

Expand Down Expand Up @@ -1177,6 +1160,25 @@ def contain_coords(var, coord_list):

return res

def _get_valid_dims(self, data):
if isinstance(self.source_geo_def, geometry.SwathDefinition):
# could be 1D or 2D
src_geo_dims = self.source_geo_def.lons.dims
else:
# assume AreaDefinitions and everything else are 2D with 'y', 'x'
src_geo_dims = ('y', 'x')
dst_geo_dims = ('y', 'x')
# verify that source dims are the same between geo and data
data_geo_dims = tuple(d for d in data.dims if d in src_geo_dims)
if data_geo_dims != src_geo_dims:
raise ValueError("Data dimensions do not match source area dimensions")
# verify that the dims are next to each other
first_dim_idx = data.dims.index(src_geo_dims[0])
num_dims = len(src_geo_dims)
if data.dims[first_dim_idx:first_dim_idx + num_dims] != data_geo_dims:
raise ValueError("Data's geolocation dimensions are not consecutive.")
return src_geo_dims, dst_geo_dims


def _get_fill_mask_value(data_dtype):
"""Return the maximum value of dtype."""
Expand Down

0 comments on commit 9e549ab

Please sign in to comment.