1

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?

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
Barry Baker
  • 19
  • 1
  • 3
  • 3
    This should be possible with scipy.interpolate.griddata: http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.griddata.html. Please post sample data and the code you already have. – Jakob S. Aug 09 '12 at 15:01
  • 2
    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 Aug 09 '12 at 15:21
  • If the data is already on a grid (I did not suppose so), @JoeKington is perfectly right! – Jakob S. Aug 09 '12 at 15:51
  • 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 Aug 09 '12 at 18:37
  • 1
    Next version (0.11.0) of Scipy will have RectSphereBivariateSpline, which should be good for this purpose: http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.RectSphereBivariateSpline.html – pv. Aug 13 '12 at 16:54
  • Thanks everyone for the response. I'm not even exactly sure where to begin. because what I have is a 2d array for latitude and longitude so lat = [x,y] and lon = [x,y] on the grid. now i have another value lets say wind speed that also has a vertical component so ws = [z, x, y]. I want to interpolate the wind speed to a value of latitude and longitude not on the regular grid provided by lat and lon. dont really see how any of those functions are really doing what I need. – Barry Baker Aug 14 '12 at 19:16
  • Sorry I've been a little busy with my thesis proposal lately and haven't had much time to work on this. This is the solution I've come up with. It is essentially just a weighted average from the 9 closest grid points within a great circle from the wanted location. any help on speeding this up or errors you see let me know. CODE --------------->[link](http://userpages.umbc.edu/~bake1/stackoverflow/wrf.py) – Barry Baker Aug 23 '12 at 21:21
  • I was able to figure a simple way to do this using the [pyresample](https://github.com/pytroll/pyresample) library. This allowed me to define my WRF grid and then a grid of observations. It interpolates using pyKDTree and is extremely fast – Barry Baker Jan 10 '17 at 18:27

2 Answers2

2

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
eclark
  • 481
  • 5
  • 14
1

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.

zxdawn
  • 825
  • 1
  • 9
  • 19