Last active
June 6, 2017 00:58
-
-
Save ScottWales/79995e297ea9d5d215955efdcabed21a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xarray | |
import scipy | |
import numpy as np | |
def interpolate_points( | |
grid_lons, grid_lats, | |
lons, lats, values, | |
method='cubic'): | |
""" | |
Interpolate point data onto the DataArray | |
grid_lons: Longitude axis of the grid | |
grid_lats: Latitude axis of the grid | |
lons: Longitude of each point | |
lats: Latitude of each point | |
values: Value of each point | |
method: scipy.interpolate.griddata method to use | |
returns: xarray.DataArray holding the gridded data | |
""" | |
array = scipy.interpolate.griddata( | |
(lons, lats), values, | |
(grid_lons, grid_lats), | |
method=method) | |
darray = xarray.DataArray(array, | |
coords = [lons, lats], | |
dims = ['lon', 'lat']) | |
return darray | |
# Read lons, lats, values somehow | |
lons, lats, values = read_data(input_file) | |
# Interpolate the values to a grid | |
da = interpolate_points( | |
grid_lons = np.linspace(0, 360), | |
grid_lats = np.linspace(-90,90), | |
lons=lons, lats=lats, values=values) | |
# Add metadata | |
da.attrs['units'] = 'degreesC' | |
# Put into a dataset and write to a file | |
ds = xarray.DataSet({'temperature': da}) | |
ds.to_netcdf('temperature.nc') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment