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 Answers
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
griddata
will be quite inefficient. You'll probably wantscipy.ndimage.map_coordinates
instead. – Joe Kington