| Class | NumRu::GPhys |
| In: |
interpolate.rb
|
| Parent: | Object |
This method supports interpolation regarding combinations of 1D and 2D coordinate variables. For instance, suppose self is 4D with coordinates named ["x", "y", "z", "t"] and associated coordinates "sigma"["z"] ("sigma" is 1D and its axis is "z"), "p"["x","y"], "q"["x","y"] ("p" and "q" are 2D having the coordinates "x" and "y"). You can make interpolation by specifying 1D VArrays whose names are among "x", "y", "z", "t", "sigma", "p", "q". You can also use a Hash like {"z" => 1.0} to specify a single point along the "x" coordinate.
If the units of the target coordinate and the current coordinate are different, a converstion was made so that slicing is made correctly, as long as the two units are comvertible; if the units are not convertible, it is just warned.
If you specify only "x", "y", and "t" coordinates for interpolation, the remaining coordinates "z" is simply retained. So the result will be 4 dimensional with coordinates named ["x", "y", "z", "t"], but the lengths of "x", "y", and "t" dimensions are changed according to the specification. Note that the result could be 3-or-smaller dimensional — see below.
Suppose you have two 1D VArrays, xnew and ynew, having names "x" and "y", respectively, and the lengths of xnew and the ynew are the same. Then, you can give an array of the two, [xnew, ynew], for coord0 as
gp_int = gp_org.interpolate( [xnew, ynew] )
(Here, gp_org represents a GPhys object, and the return value pointed by gp_int is also a GPhys.) In this case, the 1st dimension of the result (gp_int) will be sampled at the points [xnew[0],ynew[0]], [xnew[1],ynew[1]], [xnew[2],ynew[2]], …, while the 2nd and the third dimensions are "z" and "t" (no interpolation). This way, the rank of the result will be reduced from that of self.
If you instead give xnew to coord0 and ynew to coord1 as
gp_int = gp_org.interpolate( xnew, ynew )
The result will be 4-dimensional with the first coordinate sampled at xnew[0], xnew[1], xnew[2],… and the second coordinate sampled at ynew[0], ynew[1], ynew[2],…
You can also cut regarding 2D coordinate variable as
gp_int = gp_org.interpolate( pnew, qnew ) gp_int = gp_org.interpolate( xnew, qnew ) gp_int = gp_org.interpolate( [pnew, qnew] ) gp_int = gp_org.interpolate( [xnew, qnew] )
In any case, the desitination VArrays such as xnew ynew pnew qnew must be one-dimensional.
Note that
gp_int = gp_org.interpolate( qnew )
fails (exception raised), since it is ambiguous. If you tempted to do so, perhaps what you want is covered by the following special form:
As a special form, you can specify a particular dimension like this:
gp_int = gp_org.interpolate( "x"=>pnew )
Here, interpolation along "x" is made, while other axes are retained. This is useful if pnew corresponds to a multi-D coordinate variable where there are two or more corresponding axes (otherwise, this special form is not needed.)
See the test part at the end of this file for more examples.
LIMITATION
Currently associated coordinates expressed by 3D or greater dimensional arrays are not supported.
Computational efficiency of pure two-dimensional coordinate support should be improved by letting C extensions cover deeper and improving the search algorithm for grid (which is usually ordered quasi-regularly).
COVERAGE
Extrapolation is covered for 1D coordinates, but only interpolation is covered for 2D coordinates (which is limited by gt2dlib in DCL — exception will be raised if you specify a grid point outside the original 2D grid points.).
MATHEMATICAL SPECIFICATION
The multi-dimensional linear interpolation is done by supposing a (hyper-) "rectangular" grid, where each dimension is independently sampled one-dimensionally. In case of interpolation along two dimensional coordinates such as "p" and "q" in the example above, a mapping from a rectangular grid is assumed, and the corresponding points in the rectangular grid is solved inversely (currently by using gt2dlib in DCL).
For 1D and 2D cases, linear interpolations may be expressed as
1D: zi = (1-a)*z0 + a*z1 2D: zi = (1-a)*(1-b)*z00 + a*(1-b)*z10 + (1-a)*b*z01 + a*b*z11
This method is extended to arbitrary number of dimensions. Thus, if the number of dimensions to interpolate is S, then 2**S grid points are used for each interpolation (8 points for 3D, 16 points for 4D,…). Thus, the linearity of this interpolation is only along each dimension, not over the whole dimensionality.
USAGE
interpolate(coord0, coord1, ...)
ARGUMENTS
gphys.interpolate("x"=>varray)
where varray is a coordinate onto which interpolation is made. This is espcially useful if varray is multi-D. If varray‘s name "p" (name of a 2D coordnate var), for example, you can interpolate only regarding "x" by retaining other axes. If varray is 1-diemnsional, the same thing can be done simply by
gphys.interpolate(varray)
since the corresponding 1D coordinate is found aotomatically.
RETURN VALUE
ARGUMENTS
RETURN VALUE