verde icon indicating copy to clipboard operation
verde copied to clipboard

Prepare project_grid for the future grid method in Verde gridders

Open santisoler opened this issue 2 years ago • 2 comments

Description

Since Verde v2.0.0 the grid method of every gridder in Verde will not take the spacing, shape or region arguments. They will not create the desired grid, but will be able to take the coordinates of a predefined regular grid (see #326 for more details).

Currently the verde.project_grid makes use of the Chain.grid method to produce the projected and interpolated grid. During this step, the output grid is generated by passing the soon-to-be-deprecated region and spacing arguments. This raises a FutureWarning when calling project_grid that isn't due to any misuse from the user.

Here I copy a working example that reproduces this problem:

import pyproj
import ensaio
import xarray as xr
import verde as vd

# Download the data using ensaio
fname = ensaio.fetch_earth_topography(version=1)

# Load it with xarray
grid = xr.load_dataarray(fname)

# Crop it to a smaller region
region = (-70, -67, -45, -40)
grid = grid.sel(longitude=slice(*region[:2]), latitude=slice(*region[2:]))

# Define a projection
projection = pyproj.Proj(proj="merc", lat_ts=grid.latitude.values.mean())

# Project the grid
grid_proj = vd.project_grid(grid, projection)

/home/santi/git/verde/verde/base/base_classes.py:463: FutureWarning: The 'spacing', 'shape' and 'region' arguments will be removed in Verde v2.0.0. Please use the 'verde.grid_coordinates' function to define grid coordinates and pass them as the 'coordinates' argument.
  warnings.warn(

How to solve this?

We need verde.project_grid to predefine the output grid through the verde.grid_coordinates and use the Chain.grid method by passing the coordinates argument with the coordinates of this predefined grid.

This change wouldn't need significant tests, just one that checks that no FutureWarning is raised after using it. We can do so with pytest.warns (see Warnings Capture for further details and examples).

santisoler avatar Apr 23 '22 20:04 santisoler

@santisoler good catch! We probably don't even need to call make_xarray_grid and just pass the coordinates from grid_coordinates straight to interpolator.grid.

leouieda avatar Apr 24 '22 16:04 leouieda

You're right! The interpolator.grid method creates the xarray object, so grid coordinates can be 2D arrays obtained directly from grid_coordinates. Editing the issue description...

santisoler avatar Apr 24 '22 18:04 santisoler

Actually, we backtracked on this decision to remove those arguments from grid. But still, the ScipyGridder is going away and this function uses it by default. It should be replaced with the new Linear, Cubic, and KNearest gridders instead.

leouieda avatar Oct 28 '22 08:10 leouieda

@leouieda Would you mind opening a new Issue for the new changes and closing this one. It might be better than editing this Issue to solve a different thing. I will remove the good-first-issue label so we don't get any contributor tackling this issue.

santisoler avatar Nov 02 '22 17:11 santisoler

👍🏽 will do

leouieda avatar Nov 03 '22 19:11 leouieda