def mercator_transform(data, lat_bounds, origin='<STR_LIT>', height_out=None):
import numpy as np<EOL>def mercator(x):<EOL><INDENT>return np.arcsinh(np.tan(x*np.pi/<NUM_LIT>))*<NUM_LIT>/np.pi<EOL><DEDENT>array = np.atleast_3d(data).copy()<EOL>height, width, nblayers = array.shape<EOL>lat_min = max(lat_bounds[<NUM_LIT:0>], -<NUM_LIT>)<EOL>lat_max = min(lat_bounds[<NUM_LIT:1>], <NUM_LIT>)<EOL>if height_out is None:<EOL><INDENT>height_out = height<EOL><DEDENT>if origin == '<STR_LIT>':<EOL><INDENT>array = array[::-<NUM_LIT:1>, :, :]<EOL><DEDENT>lats = (lat_min + np.linspace(<NUM_LIT:0.5>/height, <NUM_LIT:1.>-<NUM_LIT:0.5>/height, height) *<EOL>(lat_max-lat_min))<EOL>latslats = (mercator(lat_min) +<EOL>np.linspace(<NUM_LIT:0.5>/height_out, <NUM_LIT:1.>-<NUM_LIT:0.5>/height_out, height_out) *<EOL>(mercator(lat_max)-mercator(lat_min)))<EOL>out = np.zeros((height_out, width, nblayers))<EOL>for i in range(width):<EOL><INDENT>for j in range(nblayers):<EOL><INDENT>out[:, i, j] = np.interp(latslats, mercator(lats),  array[:, i, j])<EOL><DEDENT><DEDENT>if origin == '<STR_LIT>':<EOL><INDENT>out = out[::-<NUM_LIT:1>, :, :]<EOL><DEDENT>return out<EOL>
Transforms an image computed in (longitude,latitude) coordinates into
the a Mercator projection image.

Parameters
----------

data: numpy array or equivalent list-like object.
    Must be NxM (mono), NxMx3 (RGB) or NxMx4 (RGBA)

lat_bounds : length 2 tuple
    Minimal and maximal value of the latitude of the image.
    Bounds must be between -85.051128779806589 and 85.051128779806589
    otherwise they will be clipped to that values.

origin : ['upper' | 'lower'], optional, default 'upper'
    Place the [0,0] index of the array in the upper left or lower left
    corner of the axes.

height_out : int, default None
    The expected height of the output.
    If None, the height of the input is used.

See https://en.wikipedia.org/wiki/Web_Mercator for more details.