Adjust vector slicing

Note

The Macro to Python converter is available from Metview version 5.22.0

Vectors in Macro are automatically converted to numpy arrays in Python and slicing is correctly resolved, like this:

Vector literals and slicing converted to Python

Macro

Generated Python code

v = |1, 2, 3, 4, 5|
# first 2 elements
v1 = v[1, 2]
# every second element
v1 = v[1, 5, 2]
v = np.array([1, 2, 3, 4, 5])
# first 2 elements
v1 = v[0:2]
# every second element
v1 = v[0:5:2]

However, Macro supports vector slicing using 4 arguments with start,end,step,num, which is not supported in numpy. To overcome this problem whenever this happens Metview’s built-in mv.compat.index4() method is used to generate the right indices for the slicing.

Note

mv.compat.index4() is available from Metview Python version 1.16.0

The following examples shows how mv.compat.index4() is actually used.

Resolving slicing based on 4 arguments

Macro

Generated Python code

v = |1, 2, 3, 4, 5, 6, 7|
# two values from every 3rd element
# resulting in: 1, 2, 4, 5
v1 = v[1, 6, 3, 2]
v = np.array([1, 2, 3, 4, 5, 6, 7])
# two values from every 3rd element
# resulting in: 1, 2, 4, 5
v1 = v[mv.compat.index4(v, 0, 6, 3, 2)]