0

I need to interpolate bilinearly some air data of a hdf4/netcdf4/hdf5 file from a 240x240 structured grid on an arbitrary collection of coordinates. I have no idea on how to do this. I have tried using pyresample but that needs an AreaDefinition of target grid which is not possible in my case of unstructured target data (arbitrary points). Here is my code:

import numpy as np
import pyresample
from   netCDF4 import Dataset

air_file  = Dataset('air.hdf', mode='r')
air_data  = air_file.variables['air_2m' ][:].flatten()
air_lon   = air_file.variables['air_lon'][:].flatten()
air_lat   = air_file.variables['air_lat'][:].flatten()
air_data  = air_data.reshape(240,240)
air_lon   = air_lon. reshape(240,240) # grid size is 240x240
air_lat   = air_lat. reshape(240,240)

tar_lon   = 100 * np.random.random((100,1)) # random points 
tar_lat   = 100 * np.random.random((100,1)) # random points

source_def = pyresample.geometry.SwathDefinition(lons=air_lon, lats=air_lat)
target_def = pyresample.geometry.SwathDefinition(lons=tar_lon, lats=tar_lat)
result     = pyresample.bilinear.resample_bilinear(gmt_1500, source_def, target_def, radius=50e3, neighbours=32, nprocs=1, fill_value=None, reduce_data=True, segments=None, epsilon=0)

I am getting the following error (which is understood as it needs an AreaDefinition for target):

AttributeError: 'SwathDefinition' object has no attribute 'proj_str'

Is there any other way of doing this?

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
MNK
  • 634
  • 4
  • 18
  • It's a little more involved, but fitting a Gaussian process to your data with an RBF kernel will let you extrapolate to any other points, regularly-sampled or otherwise. – T3am5hark Jul 31 '19 at 02:25

1 Answers1

0

I'm not familiar with the pyresample package, but for bilinear interpolation in python I suggest referring to this earlier stackexchange thread which gives a number of useful examples:

How to perform bilinear interpolation in Python

p.s: By the way, if anyone wants to perform this task from the command line, you can also extract a set of points using bilinear interpolation with cdo too

# some bash loop over a pairs of x and y
cdo remapbil,lon=${x}/lat=${x} in.nc mypoint_${x}_${y}.nc
ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86