pyresample icon indicating copy to clipboard operation
pyresample copied to clipboard

`SwathDef.aggregate` bugs and enhancements

Open ghiggi opened this issue 3 years ago • 0 comments

Problem description

SwathDef.aggregate currently works only if SwathDefinition is defined by passing lons/lats xr.DataArray with lons and lats dimensions and dask arrays.

Code Sample

import numpy as np
import dask.array as da
import xarray as xr
from pyresample.geometry import SwathDefinition

lons = np.arange(-179.75, 179.75 + 0.5, 0.5)
lats = np.arange(-89.75,  0.5,  0.5)
lons, lats = np.meshgrid(lons, lats)

# BUG: This does not work. It expects an xr.DataArray  
swath_def = SwathDefinition(lons=lons, lats=lats)
swath_def.aggregate(x=2, y=1)      # Downsample by factor of 2 along x

# BUG: This does not work. It expects a chunked dask array.
lons_da = xr.DataArray(lons, dims=("lats","lons"))
lats_da = xr.DataArray(lats, dims=("lats","lons"))
swath_def = SwathDefinition(lons=lons_da, lats=lats_da)
swath_def.aggregate(x=2, y=1)      # Downsample by factor of 2 along x
 
# This works to coarse the data
lons = da.from_array(lons)
lats = da.from_array(lats) 
lons_da = xr.DataArray(lons, dims=("lats","lons"))
lats_da = xr.DataArray(lats, dims=("lats","lons"))
swath_def = SwathDefinition(lons=lons_da, lats=lats_da)
swath_def.aggregate(x=2, y=1)      # Downsample by factor of 2 along x

Limitations & Consistency

AreaDef.aggregate enables to also upsampling (downscaling) the area by providing x, y dimensions < 1

swath_def.aggregate(x=1/2, y=1/2)  # Split each pixel in 4 pixels

In contrast, SwathDef.aggregate is able to just coarse the grid by averaging geocentric x, y, z coordinates, as dask.array coarsen accepts only accepts only x, y integers >= 1.

swath_def.aggregate(x=1/2, y=1)   
swath_def.aggregate(x=1/2, y=1)   

Suggested improvements

  • [x] Enable swath_def.aggregate to work also with lons/lats defined in numpy arrays
  • [x] Add method area_def.upsample (straighforward)
  • [x] Add method swath_def.upsample
  • [x] Switch name from aggregate to downsample?

This would address also issue https://github.com/pytroll/pyresample/issues/28

Implementation idea for swath_def.upsample

  1. Conversion of lons/lats to geocentric x,y,z
  2. Infer current "pixel" corners in x,y,z
  3. Define new pixel centroids in x,y,z
  4. Backconversion to lats/lons

ghiggi avatar Jan 04 '22 10:01 ghiggi