1
votes

Im new to python and I'm trying to write a function that will take a numpy array from a netcdf file with dimensions [time,height,longitude,latitude] and interpolate the function to a specified lat and lon. I have looked into scipy.interpolate but am still not sure where to go from there. any help?

2
This should be possible with scipy.interpolate.griddata: docs.scipy.org/doc/scipy/reference/generated/…. Please post sample data and the code you already have.Jakob S.
Is the data already on a regular grid? (I ask because netcdf's often are.) If so, using griddata will be quite inefficient. You'll probably want scipy.ndimage.map_coordinates instead.Joe Kington
If the data is already on a grid (I did not suppose so), @JoeKington is perfectly right!Jakob S.
If you are using the interpolation for something that is really important to you, you might need to be careful away from the equator since the distance between lines changes.Ken
Next version (0.11.0) of Scipy will have RectSphereBivariateSpline, which should be good for this purpose: docs.scipy.org/doc/scipy/reference/generated/…pv.

2 Answers

2
votes

I can't tell from the question if you are looking to (a) interpolate the entire data set to a new set of latitude/longitude coordinates or (b) get the value at a single latitude/longitude location.

Either way, if you're not married to doing this in python, it would be a lot easier to use remapgrid in Climate Data Operators (make sure you install with netCDF support). This does require the netCDF files to follow CF-Conventions (see this post on making your WRF file CF-compliant).

Documentation for remapgrid is found here. See in particular section 1.3 (pp. 9-12) for info on grid options and section 2.10 (starts p.106) for info on interpolation techniques. There are several options for how to remap, and the command you use depends on your application.

(a)Here's an example remapping using bilinear interpolation to a gaussian (128x64) grid

cdo remapbil,n32 wrfout_d01_1999-01-01-01_00:00:00.nc out.nc

(b) Here's an example remapping using nearest neighbor mapping to one latitude,longitude point:

cdo remapnn,lon=-107.0_lat=34.0 wrfout_d01_1999-01-01-01_00:00:00.nc out.nc
1
votes

You can use XESMF to regrid it.

It has tutorials for GEOSChem, but it's almost the same for WRF.

I've written a notebook about how to regrid WRF data.