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):
78 self.
data = MFDataset(files)
79 pz = self.data.variables[ZSCALE][:, 0, 0]
80 px = self.data.variables[XSCALE]
81 py = self.data.variables[YSCALE]
82 pt = np.array([tinit + i * tstep
for i
in
83 range(self.data.variables[TIME].shape[0])])
84 self.
boundsmax = [pt[-1], pz[-1], px[-1], py[-1]]
85 self.
boundsmin = [pt[0], pz[0], px[0], py[0]]
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(bounds)
109 ip = RegularGridInterpolator(coordinates, values, method)
115 for d, b
in enumerate(bounds):
118 slice_indexes += dslice,
121 return slice_indexes, coordinates
124 return self.
data[var][idx]
132 p = np.ndarray(point.shape)
143 """Convenience method for getting 3D wind. See get_points."""
144 return np.array((self.
get_points(points, WX, method),