understanding pyresample to regrid irregular grid data to a regular grid
<p>I need to regrid data on a irregular grid (lambert conical) to a regular grid. I think pyresample is my best bet. Infact my original lat,lon are not 1D (which seems to be needed to use basemap.interp or scipy.interpolate.griddata).</p>

<p>I found <a href="http://stackoverflow.com/questions/35734070/interpolating-data-from-one-latitude-longitude-grid-onto-a-different-one/35734381#35734381">this SO's answer</a> helpful. However I get empty interpolated data. I think it has to do with the choice of my radius of influence and with the fact that my data are wrapped (??).</p>

<p>This is my code:</p>

<pre><code>import numpy as np
from matplotlib import pyplot as plt
import netCDF4
%matplotlib inline
url = "http://www.esrl.noaa.gov/psd/thredds/dodsC/Datasets/NARR/Dailies/monolevel/hlcy.2009.nc"
SRHtemp = netCDF4.Dataset(url).variables['hlcy'][0,::]
Y_n     = netCDF4.Dataset(url).variables['y'][:]
X_n     = netCDF4.Dataset(url).variables['x'][:]
T_n     = netCDF4.Dataset(url).variables['time'][:]

lat_n     = netCDF4.Dataset(url).variables['lat'][:]
lon_n     = netCDF4.Dataset(url).variables['lon'][:]
</code></pre>

<p>lat_n and lon_n are irregular and the latitude and longitude corresponding to the projected coordinates x,y.</p>

<p>Because of the way lon_n is, I added:</p>

<pre><code>lon_n[lon_n&lt;0] = lon_n[lon_n&lt;0]+360
</code></pre>

<p>so that now if I plot them they look nice and ok:</p>

<p><a href="http://i.stack.imgur.com/S9L51.png" rel="nofollow"><img src="http://i.stack.imgur.com/S9L51.png" alt="enter image description here"></a></p>

<p>Then I create my new set of regular coordinates:</p>

<pre><code>XI = np.arange(148,360)
YI = np.arange(0,87)
XI, YI = np.meshgrid(XI,YI)
</code></pre>

<p>Following the answer above I wrote the following code:</p>

<p>from pyresample.geometry import SwathDefinition
from pyresample.kd_tree import resample_nearest</p>

<pre><code>def_a = SwathDefinition(lons=XI, lats=YI)
def_b = SwathDefinition(lons=lon_n, lats=lat_n)
interp_dat = resample_nearest(def_b,SRHtemp,def_a,radius_of_influence = 70000,fill_value = -9.96921e+36)
</code></pre>

<p>the resolution of the data is about 30km, so I put 70km, the fill_value I put is the one from the data, but of course I can just put zero or nan.</p>

<p>however I get an empty array. </p>

<p>What do I do wrong? also - if there is another way of doing it, I am interested in knowing it. Pyresample documentation is a bit thin, and I need a bit more help.</p>

<p>I did find <a href="http://stackoverflow.com/questions/3864899/resampling-irregularly-spaced-data-to-a-regular-grid-in-python">this answer</a> suggesting to use another griddata function:</p>

<pre><code>import matplotlib.mlab as ml
resampled_data = ml.griddata(lon_n.ravel(), lat_n.ravel(),SRHtemp.ravel(),XI,YI,interp = "linear")
</code></pre>

<p>and it seems to be ok:</p>

<p><a href="http://i.stack.imgur.com/1Zro7.png" rel="nofollow"><img src="http://i.stack.imgur.com/1Zro7.png" alt="enter image description here"></a></p>

<p>But I would like to understand more about pyresample, since it seems so powerful.</p>

<p>The problem is that XI and XI are integers, not floats. You can fix this by simply doing</p>

<pre><code>XI = np.arange(148,360.)
YI = np.arange(0,87.)
XI, YI = np.meshgrid(XI,YI)
</code></pre>

<p>The inability to handle integer datatypes is an undocumented, unintuitive, and possibly buggy behavior from pyresample.</p>

<p>A few more notes on your coding style:</p>

<ul>
<li>It's not necessary to overwrite the XI and YI variables, you don't gain much by this</li>
<li>You should just load the netCDF dataset once and the access the variables via that object</li>
</ul>

python netcdf netcdf4