2 Reading mesoNH netCDF files and providing an interface to read chunks
3 of data and interpolate points.
5 TODO : test with large netCDF files, add mapping of variables as an argument.
8 from __future__
import division, print_function, absolute_import
9 from netCDF4
import MFDataset, Dataset
11 from scipy.interpolate
import RegularGridInterpolator
15 'Find rightmost value less than x'
16 return np.searchsorted(a, x, side=
'left')-1
20 'Find rightmost value less than or equal to x'
21 return np.searchsorted(a, x, side=
'right')-1
25 'Find leftmost value greater than x'
26 return np.searchsorted(a, x, side=
'right')
30 'Find leftmost item greater than or equal to x'
31 return np.searchsorted(a, x, side=
'left')
40 XSCALE =
'S_N_direction'
41 YSCALE =
'W_E_direction'
51 Interpolation in space-time from mesoNH data over a grid.
53 The functions valued on the grid are assumed to be periodic in the
54 x and y axis (south->north and west->east) :
55 f(x)=f(x+xmax-xmin) where [xmin,xmax] are the bounds on x
58 Clipping is done on the z (height) and time axis :
59 f(z)=f(min(max(z,zmin),zmax)) where [zmin,zmax] are the bound on z
62 Two interpolation methods are currently supported : nearest neighbour
65 Uses RegularGridInterpolator from scipy package.
75 if np.isscalar(files):
79 pz = self.
datadatadata.variables[ZSCALE][:, 0, 0]
80 px = self.
datadatadata.variables[XSCALE]
81 py = self.
datadatadata.variables[YSCALE]
82 pt = np.array([tinit + i * tstep
for i
in
83 range(self.
datadatadata.variables[TIME].shape[0])])
84 self.
boundsmaxboundsmax = [pt[-1], pz[-1], px[-1], py[-1]]
85 self.
boundsminboundsmin = [pt[0], pz[0], px[0], py[0]]
88 self.
grid_shapegrid_shape = (len(pt), len(pz), len(px), len(py))
91 """ Get value of variable on points
94 points: a ndarray containing the point coordinates on the last
96 var: the name of the variable in the mesoNH file(s)
97 method: 'nearest' and 'linear' interpolation are currently supported
99 points = np.array(points)
101 caxes = tuple(range(p.ndim-1))
102 bounds = zip(p.min(axis=caxes), p.max(axis=caxes))
107 slice_indexes, coordinates = self.
_slicyfy_slicyfy(bounds)
109 ip = RegularGridInterpolator(coordinates, values, method)
115 for d, b
in enumerate(bounds):
118 slice_indexes += dslice,
121 return slice_indexes, coordinates
132 p = np.ndarray(point.shape)
143 """Convenience method for getting 3D wind. See get_points."""
144 return np.array((self.
get_pointsget_points(points, WX, method),
145 self.
get_pointsget_points(points, WY, method),
def _slicyfy(self, bounds)
def get_points(self, points, var, method='nearest')
def get_wind(self, points, method='nearest')
def _get_interpolator(self, bounds, var, method="nearest")
def _get_var_values(self, var, idx=Ellipsis)
def _apply_bounds(self, point)
def __init__(self, files, tstep, tinit=0)