Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

regridding yields 0 when there should be no value #63

Closed
pascalgross opened this issue Sep 11, 2019 · 4 comments
Closed

regridding yields 0 when there should be no value #63

pascalgross opened this issue Sep 11, 2019 · 4 comments

Comments

@pascalgross
Copy link

pascalgross commented Sep 11, 2019

I use the following code to:

  1. read a grib file with weather data
  2. regrid it using xesmf
  3. export it as GeoTIFF using rioxarray
import cfgrib
import rioxarray
import xarray as xr
import xesmf as xe
import numpy as np
from src.datapaths import get_grib_file
from src.mkdir_p import mkdir_p

year, month, day, hour = 2015, 5, 10, 12
grib_file = get_grib_file(year=year, month=month, day=day, hour=hour)
datasets = cfgrib.open_datasets(grib_file, backend_kwargs={'indexpath': ''})
ds = datasets[0]

ds = ds.rename(latitude='lat', longitude='lon')
ds_out = xr.Dataset({'lat': (['lat'], np.linspace(44.77, 56.14, 1000)),
                     'lon': (['lon'], np.linspace(2.976, 19.84, 1000))})
regridder = xe.Regridder(ds, ds_out, 'bilinear', reuse_weights=True)
ds_lonlat = regridder(ds)
ds_lonlat = ds_lonlat.rename(lat='y', lon='x')

for var in ds_lonlat.variables:
	if ds_lonlat[var].dims == ('y', 'x'):
		ds_lonlat[var].rio.set_nodata(9999).rio.set_crs(4326).rio.to_raster('{}.tif'.format(var))

This works fast as hell and is exactly what I needed.
With one problem: values which are not available in the source are set to 0.0

The results in "shadows" for values outside the source grid.
This is the actual result and this is what I expected.

Can I somehow define, which value is used, if no value can be interpolated?

@JiaweiZhuang
Copy link
Owner

values which are not available in the source are set to 0.0

Yes, this behavior is discussed in #15 #51

In short, "regridding" is just a sparse matrix multiply; the output matrix from a sparse matrix multiply is zero by default.

Can I somehow define, which value is used, if no value can be interpolated?

Filling unmapped points with non-zeros values will make the entire regridding operation non-linear (not like y = A*x anymore), so it is not possible to enable a non-zero default by just changing the regridding weights. One hack I can think of is to use #15 (comment) to convert unmapped points to nan and then convert nan to a user-specified value. Or, use masks (#22) instead of hacking nans. Welcome more clever suggestions...

A side question: how did you make the map plots? They look quite fancy...

@pascalgross
Copy link
Author

Okay, thank you. I'll have a look on that.

The shown maps are a combination of OpenStreetMaps and the image, which I generate like shown above. I use a tool called QGIS which allows me to combine different geo-referenced layers. QGIS allows me to manipulate these layers in color and transparancy etc.

@pascalgross
Copy link
Author

Setting the unmapped points to nan is solving the whole problem. The rasterio library handles them correctly and the image is generated as wanted! Thank you!

@JiaweiZhuang
Copy link
Owner

Great, glad to hear that

raphaeldussin added a commit to raphaeldussin/xESMF that referenced this issue Jan 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants