https://pyresample.readthedocs.io/en/latest/
This package seems a bit more speciallized, and does not have as tight of integration with xarray like xESMF and Verde do. If working with satellite or swath data, this is not one to miss! This package integrates with Satpy https://satpy.readthedocs.io/en/stable/ .
This is on the to-do list: https://pyresample.readthedocs.io/en/latest/roadmap.html?highlight=xarray#xarray-and-geoxarray
Knowing your way around xarray, numpy is beneficial. This is not deisgned to be an introduction to any of those packages. Would do this notebook after doing the xESMF one!
import pandas as pd
import numpy as np
import xarray as xr
import matplotlib.pyplot as plt
from appdirs import *
import dask.array as da
import pyresample
from pyresample import image, geometry
from pyresample.bilinear import NumpyBilinearResampler
from xarray import DataArray
from pyresample.bilinear import XArrayBilinearResampler
import os
%load_ext watermark
%watermark --iversions
file = '../data/onestorm.nc'
Let's open this file with xarray:
ds = xr.open_dataset(file)
ds
Trying to do the same thing with pyresample:
from pyresample.utils import load_cf_area
area_def, cf_info = load_cf_area('data/onestorm.nc', variable='visible', x='x', y='y')
This is supposed to fail. Will chat about pro's and con's in the summary.
Link to this turtorial is here: https://pyresample.readthedocs.io/en/latest/swath.html#pyresample-bilinear
We will be deconstructing it a bit to get into the details, but all of the code is from the above link.
target_def = geometry.AreaDefinition('areaD',
'Europe (3km, HRV, VTC)',
'areaD',
{'a': '6378144.0', 'b': '6356759.0',
'lat_0': '50.00', 'lat_ts': '50.00',
'lon_0': '8.00', 'proj': 'stere'},
800, 800,
[-1370912.72, -909968.64,
1029087.28, 1490031.36])
Unlike using xESMF, this does not depend or work with xarray:
print('target def type', type(target_def))
data = DataArray(da.from_array(np.fromfunction(lambda y, x: y*x, (500, 100))), dims=('y', 'x'))
type(data)
lons = da.from_array(np.fromfunction(lambda y, x: 3 + x * 0.1, (500, 100)))
lats = da.from_array(np.fromfunction(lambda y, x: 75 - y * 0.1, (500, 100)))
source_def = geometry.SwathDefinition(lons=lons, lats=lats)
resampler = XArrayBilinearResampler(source_def, target_def, 30e3)
result = resampler.resample(data)
type(result)
result.to_dataset()
data.to_dataset()
Pyresample is a speciallist program, with strong functionality with satpy. Would reccomend if swath/sat image data is part of your normal workflow. For others, the requirement of the data being CF compliant and API is a hurdle.