Exemplo n.º 1
0
def create_mv2_gridder_xyzt(nx=8,
                            ny=7,
                            nz=6,
                            nt=5,
                            xmin=-6.,
                            xmax=-3,
                            ymin=46,
                            ymax=48,
                            zmin=-200,
                            zmax=0,
                            tmin='2016',
                            tmax='2016-02',
                            tunits='days since 2016-01-01',
                            rotate=0):
    """Create a MV2 array on a grid

    Return
    ------
    MV2.array
    """

    # Axes
    shape = ()
    axes = []
    if nt != 0:
        time = create_time(lindates(tmin, tmax, nt), tunits)
        axes.append(time)
        shape += nt,
    if nz != 0:
        dep = create_dep(N.linspace(zmin, zmax, nz))
        axes.append(dep)
        shape += nz,
    if ny != 0:
        lat = create_lat(N.linspace(ymin, ymax, ny))
        axes.append(lat)
        shape += ny,
    if nx != 0:
        lon = create_lon(N.linspace(xmin, xmax, nx))
        axes.append(lon)
        shape += nx,

    # Array
    data = MV2.array(N.arange(N.multiply.reduce(shape)).reshape(shape),
                     copy=False,
                     axes=axes,
                     id='temp',
                     dtype='d')

    # Rotate grid
    if rotate:
        grid = data.getGrid()
        if grid is not None:
            grid = rotate_grid(grid, rotate)
            set_grid(data, grid)

    return data
Exemplo n.º 2
0

# Input grid and data
nxi = 20
nyi = 15
# - rect
xi = N.arange(nxi*1.)
yi = N.arange(nyi*1.)
gridri = create_grid(xi, yi)
xxri, yyri = N.meshgrid(xi, yi)
zzri = N.ma.array(yyri)
zzri[int(nyi*0.3):int(nyi*0.6), int(nxi*0.3):int(nxi*0.6)] = N.ma.masked
varri = MV2.asarray(zzri)
set_grid(varri, gridri)
# - curv
gridci = rotate_grid(gridri, 30)
xxci = gridci.getLongitude().getValue()
yyci = gridci.getLatitude().getValue()
zzci = N.ma.array(yyci)
zzci[int(nyi*0.3):int(nyi*0.6), int(nxi*0.3):int(nxi*0.6)] = N.ma.masked
varci = MV2.asarray(zzci)
set_grid(varci, gridci)

# Output positions
nxo = 25
nyo = 18
# - rect
dxi = xi[-1]-xi[0]
dyi = yi[-1]-yi[0]
xro = N.linspace(xi[0]+dxi*0.2, xi[-1]+dxi*0.2, nxo)
yro = N.linspace(yi[0]-dyi*0.2, yi[-1]-dyi*0.2, nyo)
Exemplo n.º 3
0
"""Test function :func:`~vacumm.misc.grid.resol`"""

from vcmq import create_lon, create_lat, create_grid, rotate_grid, assert_allclose
from vacumm.misc.grid.misc import resol2 as resol

lon1d = create_lon((0, 10., 1.))
lat1d = create_lat((43, 50, 1.))
rgrid = create_grid(lon1d, lat1d)
cgrid = rotate_grid(rgrid, 45)
lon2d = cgrid.getLongitude()
lat2d = cgrid.getLatitude()

assert_allclose(resol(lon1d, cache=False), 1.)
assert_allclose(resol(lon1d, meters=True, cache=False), 78626.2617245)

assert_allclose(resol(lon1d, lat=0, cache=False), 1.0)
assert_allclose(resol(lon1d, lat=0, meters=True, cache=False), 111195.031364)

assert_allclose(resol(rgrid, cache=False), (1.0, 1.0))
assert_allclose(resol(rgrid, meters=True, cache=False), (77242.051980165023, 111195.03136431401))

assert_allclose(resol((lon1d, lat1d), cache=False), (1.0, 1.0))
assert_allclose(resol((lon1d, lat1d), meters=True, cache=False), (77242.051980165023, 111195.03136431401))

assert_allclose(resol(cgrid, cache=False), (1.0, 1.0))
assert_allclose(resol(cgrid, meters=True, cache=False), (94804.663264739473, 94804.663264739473))


N.random.seed(0)
x = N.random.random(10)
y = N.random.random(10)
Exemplo n.º 4
0
mygrid = get_grid(gridc)        # -> TESTEZ AVEC (LON,LAT) ET PARAMS STRICT ET INTERCEPT
mylon2d = get_axis(gridc, 1)    # -> COMPAREZ AVEC .GETAXIS()


# Sélectionner
print coord2slice(lon, lon=(2, 4.5))# -> COMPAREZ AVEC .GETINTERVALEXT(...)
print coord2slice(grid, lon=(4, 8), lat=(44, 46)) # -> TESTEZ SUR GRIDC
# -> TESTEZ VARSEL


# Transformer
gridcr = curv2rect(gridc)       # -> TESTEZ AVEC VOTRE GRILLE NON RECT 
ax = create_lon([350, 0, 10.])
print monotonic(ax)
print xshift(var, 2)[0]         # -> COMPAREZ A VAR ET VERIFIEZ AXE
# -> TESTEZ XEXTEND
grido = rotate_grid(grid, 30)   # -> TRACEZ LES LONGITUDE (PCOLOR)
grids = shiftgrid(grid, 1, -1)  # -> VERIFIEZ PUIS TESTEZ SUR GRIDC + PASSER DE T À U
gride = extendgrid(grid, iext=(2, 3))   # -> VERIFIER PUIS TESTEZ LE MODE 


# Exploiter
print resol(grid)               # -> TESTEZ EN METRES ET SUR AXE
print depth2dz(dep)
print get_closest(lon2d, lat2d, 2.3, 1.2)


# Utilitaires sut les coordonnées
xx, yy = meshgrid(xx, yy[:, 0])
xxb, yyb = meshcells(xx, yy)      # -> EN 1D?
Exemplo n.º 5
0
nyi = 15
nt = 5
# - rect
xi = N.arange(nxi*1.)
yi = N.arange(nyi*1.)
ti = create_time((nt, ), 'years since 2000')
gridri = create_grid(xi, yi)
xxri, yyri = N.meshgrid(xi, yi)
zzri = N.ma.resize(yyri, (nt, nyi, nxi))
zzri[:, int(nyi*0.3):int(nyi*0.6), int(nxi*0.3):int(nxi*0.6)] = N.ma.masked
zzri[1] = N.ma.masked
varri = MV2.asarray(zzri)
varri.setAxis(0, ti)
set_grid(varri, gridri)
# - curv
gridci = rotate_grid(gridri, 30)
xxci = gridci.getLongitude().getValue()
yyci = gridci.getLatitude().getValue()
zzci = N.ma.resize(yyci, (nt, nyi, nxi))
zzci[:, int(nyi*0.3):int(nyi*0.6), int(nxi*0.3):int(nxi*0.6)] = N.ma.masked
zzci[1] = N.ma.masked
varci = MV2.asarray(zzci)
varci.setAxis(0, ti)
set_grid(varci, gridci)

# Output positions
nxo = 25
nyo = 18
# - rect
dxi = xi[-1]-xi[0]
dyi = yi[-1]-yi[0]
Exemplo n.º 6
0
"""Test the fortran function :f:func:`nearest2d`"""
from vcmq import N, P, code_file_name, P, os, rotate_grid, add_grid, meshbounds
from vacumm.misc.grid._interp_ import nearest2d

# Input grid
gridi = rotate_grid((N.arange(5), N.arange(4)), 30)
xxi = gridi.getLongitude()[:].filled()
yyi = gridi.getLatitude()[:].filled()
vari = N.resize(yyi, (20, ) + yyi.shape)
nb = 10
xxbi, yybi = meshbounds(xxi, yyi)

# Output grid
grido = rotate_grid((N.linspace(0, 6, 50) - 1, N.linspace(0, 4, 35) + 1.), -20)
xxo = grido.getLongitude()[:].filled()
yyo = grido.getLatitude()[:].filled()
xxbo, yybo = meshbounds(xxo, yyo)

# Nearest
varo = nearest2d(vari, xxi, yyi, xxo, yyo, nb)

# Plot
vmin = varo.min()
vmax = varo.max()
P.figure(figsize=(8, 4))
P.subplot(121, aspect=1)
P.pcolor(xxbi, yybi, vari[0], vmin=vmin, vmax=vmax)
add_grid(grido)
P.title('original')
P.subplot(122, aspect=1)
P.pcolor(xxbo, yybo, varo[0], vmin=vmin, vmax=vmax)
Exemplo n.º 7
0
# Récupérer
mygrid = get_grid(
    gridc)  # -> TESTEZ AVEC (LON,LAT) ET PARAMS STRICT ET INTERCEPT
mylon2d = get_axis(gridc, 1)  # -> COMPAREZ AVEC .GETAXIS()

# Sélectionner
print coord2slice(lon, lon=(2, 4.5))  # -> COMPAREZ AVEC .GETINTERVALEXT(...)
print coord2slice(grid, lon=(4, 8), lat=(44, 46))  # -> TESTEZ SUR GRIDC
# -> TESTEZ VARSEL

# Transformer
gridcr = curv2rect(gridc)  # -> TESTEZ AVEC VOTRE GRILLE NON RECT
ax = create_lon([350, 0, 10.])
print monotonic(ax)
print xshift(var, 2)[0]  # -> COMPAREZ A VAR ET VERIFIEZ AXE
# -> TESTEZ XEXTEND
grido = rotate_grid(grid, 30)  # -> TRACEZ LES LONGITUDE (PCOLOR)
grids = shiftgrid(grid, 1,
                  -1)  # -> VERIFIEZ PUIS TESTEZ SUR GRIDC + PASSER DE T À U
gride = extendgrid(grid, iext=(2, 3))  # -> VERIFIER PUIS TESTEZ LE MODE

# Exploiter
print resol(grid)  # -> TESTEZ EN METRES ET SUR AXE
print depth2dz(dep)
print get_closest(lon2d, lat2d, 2.3, 1.2)

# Utilitaires sut les coordonnées
xx, yy = meshgrid(xx, yy[:, 0])
xxb, yyb = meshcells(xx, yy)  # -> EN 1D?
"""Test fortran function :f:func:`dstwgt2dto1dc`"""

from vcmq import P, N, code_file_name, os, add_grid, rotate_grid
from vacumm.misc.grid._interp_ import dstwgt2dto1dc


# Input grid and data
nxy = 15
xi = N.arange(nxy*1.)
yi = N.arange(nxy*1.)
gridi = rotate_grid((xi, yi), 30)
xxi = gridi.getLongitude().getValue()
yyi = gridi.getLatitude().getValue()
zzi = N.ma.array(yyi)
zzi[int(nxy*0.3):int(nxy*0.8), int(nxy*0.3):int(nxy*0.8)] = N.ma.masked
zzi.shape = 1, nxy, nxy

# Output positions
no = 1000
xo = N.random.uniform(-nxy/4., nxy+nxy/4., no)
yo = N.random.uniform(-nxy/4., nxy+nxy/4., no)

# Interpolate
mv = zzi.get_fill_value()
zo = dstwgt2dto1dc(xxi,yyi,zzi.filled(mv),xo,yo,mv)
zo = N.ma.masked_values(zo, mv) 

# Plot
kw = dict(vmin=zzi.min(), vmax=zzi.max())
P.figure(figsize=(6, 6))
P.subplot(111, aspect=1)
Exemplo n.º 9
0
"""Test function :func:`~vacumm.misc.grid.resol`"""

from vcmq import create_lon, create_lat, create_grid, rotate_grid, assert_allclose
from vacumm.misc.grid.misc import resol

lon1d = create_lon((0, 10., 1.))
lat1d = create_lat((43, 50, 1.))
rgrid = create_grid(lon1d, lat1d)
cgrid = rotate_grid(rgrid, 45)
lon2d = cgrid.getLongitude()
lat2d = cgrid.getLatitude()

assert_allclose(resol(lon1d, cache=False), 1.)
assert_allclose(resol(lon1d, meters=True, cache=False), 78626.2617245)

assert_allclose(resol(lon1d, lat=0, cache=False), 1.0)
assert_allclose(resol(lon1d, lat=0, meters=True, cache=False), 111195.031364)

assert_allclose(resol(rgrid, cache=False), (1.0, 1.0))
assert_allclose(resol(rgrid, meters=True, cache=False), (77242.051980165023, 111195.03136431401))

assert_allclose(resol((lon1d, lat1d), cache=False), (1.0, 1.0))
assert_allclose(resol((lon1d, lat1d), meters=True, cache=False), (77242.051980165023, 111195.03136431401))

assert_allclose(resol(cgrid, cache=False), (1.0, 1.0))
assert_allclose(resol(cgrid, meters=True, cache=False), (94804.663264739473, 94804.663264739473))


N.random.seed(0)
x = N.random.random(10)
y = N.random.random(10)
Exemplo n.º 10
0
zi_5d = N.resize(dep[:], (nez, nt, ny, nx, nz))
zi_5d = N.moveaxis(zi_5d, -1, 2)
vo = grid2xy(vi, zi=zi_5d, xo=xo, yo=yo, zo=zo, to=to, method='linear')
assert vo.shape==(ne, np)
N.testing.assert_allclose(vo[0], yo)

# Reversed 5d z
zi_5d_rev = zi_5d[:, :, ::-1, :, :]
vo = grid2xy(vi_revz, zi=zi_5d_rev, xo=xo, yo=yo, zo=zo, to=to, method='linear')
N.testing.assert_allclose(vo[0], yo)

# Zi present but not requested
vo = grid2xy(vi, xo=xo, yo=yo, to=to, method='linear')
assert vo.shape==(ne, nz, np)
N.testing.assert_allclose(vo[0, 0], yo)

# Zi and Ti present but not requested
vo = grid2xy(vi, xo=xo, yo=yo, method='linear')
assert vo.shape==(ne, nt, nz, np)
N.testing.assert_allclose(vo[0, 0, 0], yo)

# Curvilinear xy only
vi_xyc = vi[:, 0, 0]
gridc = rotate_grid(vi_xyc.getGrid(), 30)
set_grid(vi_xyc, gridc)
vi_xyc[:] = N.ma.resize(gridc.getLatitude()[:], vi_xyc.shape)
vo = grid2xy(vi_xyc, xo=xo, yo=yo, method='linear')
assert vo.shape==(ne, np)
N.testing.assert_allclose(vo[0], yo)

Exemplo n.º 11
0
# %% Imports
from vcmq import N, MV2, create_grid, rotate_grid, GriddedSelector

# %% Rectangular grid
rgrid = create_grid((-14., -4, .25), (43., 52, .25), 'rect')
var = MV2.reshape(MV2.arange(2 * rgrid.shape[0] * rgrid.shape[1]),
                  (2, ) + rgrid.shape)
gs = GriddedSelector(rgrid, lon=(-9, -6, 'co'), lat=(44.1, 48.2))
rvar = gs(var)
assert rvar.shape == (2, 16, 12)
assert rvar.getLongitude()[-1] == -6.25
assert rvar.getLatitude()[-1] == 48

# %% Curvilinear
cgrid = rotate_grid(rgrid, 30.)
gs = GriddedSelector(cgrid, lon=(-9, -6, 'co'), lat=(44.1, 48.2))
cvar = gs(var)
assert cvar.shape == (2, 20, 18)
assert cvar.getLongitude().asma()[~gs.mask].min() == -9
assert cvar.getLatitude().asma()[~gs.mask].max() <= 48.2

# %% Unstructured
N.random.seed(0)
ugrid = create_grid(N.random.uniform(-14, -4, 100),
                    N.random.uniform(43, 52, 100), 'unstruct')
var = MV2.resize(ugrid.getLatitude().asma(), (2, ) + ugrid.shape)
gs = GriddedSelector(ugrid, lon=(-9, -6, 'co'), lat=(44.1, 48.2))
uvar = gs(var)
assert uvar.getGrid().shape == (34, )
assert uvar.getLongitude().min() >= -9
Exemplo n.º 12
0
"""Test fortran function :f:func:`nearest2dto1dc`"""

from vcmq import P, N, code_file_name, os, add_grid, rotate_grid
from vacumm.misc.grid._interp_ import nearest2dto1dc

# Input grid and data
nxy = 15
xi = N.arange(nxy * 1.)
yi = N.arange(nxy * 1.)
gridi = rotate_grid((xi, yi), 30)
xxi = gridi.getLongitude().getValue()
yyi = gridi.getLatitude().getValue()
zzi = N.ma.array(yyi)
zzi[int(nxy * 0.3):int(nxy * 0.8), int(nxy * 0.3):int(nxy * 0.8)] = N.ma.masked
zzi.shape = 1, nxy, nxy

# Output positions
no = 1000
xo = N.random.uniform(-nxy / 4., nxy + nxy / 4., no)
yo = N.random.uniform(-nxy / 4., nxy + nxy / 4., no)

# Interpolate
mv = zzi.get_fill_value()
zo = nearest2dto1dc(xxi, yyi, zzi.filled(mv), xo, yo, mv)
zo = N.ma.masked_values(zo, mv)

# Plot
kw = dict(vmin=zzi.min(), vmax=zzi.max())
P.figure(figsize=(6, 6))
P.subplot(111, aspect=1)
P.contourf(xxi, yyi, zzi[0], **kw)
Exemplo n.º 13
0
"""Test the class :class:`~vacumm.misc.grid.regridding.CurvedInterpolator`"""

from vcmq import (P, N, set_grid, plot2d, MV2, add_grid, code_file_name, os,
                  create_time, CurvedInterpolator, rotate_grid)

# Curved grid
nxy = 10
nt = 5
lon = N.arange(nxy * 1.)
lat = N.arange(nxy * 1.)
time = create_time((nt, ), 'years since 2000')
gridi = rotate_grid((lon, lat), 30)
xxi = gridi.getLongitude()[:].filled()
yyi = gridi.getLatitude()[:].filled()
vari = MV2.resize(yyi, (nt, nxy, nxy))
vari.setAxis(0, time)
set_grid(vari, gridi)
kw = dict(vmin=vari.min(), vmax=vari.max())
P.figure(figsize=(10, 3.5))
P.subplot(131, aspect=1)
P.contourf(xxi, yyi, vari[0].asma(), **kw)
add_grid(gridi, edges=False, centers=-1)
xylims = (xxi.min(), xxi.max(), yyi.min(), yyi.max())
P.axis(xylims)
P.title('Curved grid')

# Interpolate to grid
xg, yg = N.meshgrid(N.arange(-3.5, 14.5), N.arange(-3.5, 14.5))
nxyg = xg.shape
cig = CurvedInterpolator(gridi, (xg, yg), g2g=True)
varog = cig(vari)
Exemplo n.º 14
0
"""Test the fortran function :f:func:`nearest2d`"""
from vcmq import N, P, code_file_name, P, os, rotate_grid, add_grid, meshbounds
from vacumm.misc.grid._interp_ import nearest2d


# Input grid
gridi = rotate_grid((N.arange(5), N.arange(4)), 30)
xxi = gridi.getLongitude()[:].filled()
yyi = gridi.getLatitude()[:].filled()
vari = N.resize(yyi, (20, )+ yyi.shape)
nb = 10
xxbi, yybi = meshbounds(xxi, yyi)

# Output grid
grido = rotate_grid((N.linspace(0, 6, 50)-1, N.linspace(0, 4, 35)+1.), -20)
xxo = grido.getLongitude()[:].filled()
yyo = grido.getLatitude()[:].filled()
xxbo, yybo = meshbounds(xxo, yyo)

# Nearest
varo = nearest2d(vari, xxi, yyi, xxo, yyo, nb)

# Plot
vmin = varo.min()
vmax = varo.max()
P.figure(figsize=(8, 4))
P.subplot(121, aspect=1)
P.pcolor(xxbi, yybi, vari[0], vmin=vmin, vmax=vmax)
add_grid(grido)
P.title('original')
P.subplot(122, aspect=1)
Exemplo n.º 15
0
"""Test the fortran function :f:func:`closest2d`"""
from vcmq import N, P, code_file_name, P, os, rotate_grid, add_grid
from vacumm.misc.grid._interp_ import closest2d


# Input grid
grid = rotate_grid((N.arange(5), N.arange(4)), 30)
xxi = grid.getLongitude()[:].filled()
yyi = grid.getLatitude()[:].filled()

# Input random points
N.random.seed(0)
np = 100
xxo = N.random.random(np)*(xxi.max()-xxi.min()) + xxi.min()
yyo = N.random.random(np)*(yyi.max()-yyi.min()) + yyi.min()

# Closest
for xo, yo in zip(xxo, yyo):
    i,j = closest2d(xxi, yyi, xo, yo, nogeo=0)
    P.plot([xo, xxi[j-1, i-1]], [yo, yyi[j-1, i-1]], 'k')
add_grid(grid, color='k', markersize=10, centers=True, marker='o', alpha=1)
P.scatter(xxo, yyo, c='r', s=40, zorder=11)
P.title('closest2d')
P.axis('image')
figfile = code_file_name(ext='png')
if os.path.exists(figfile): os.remove(figfile)
P.savefig(figfile)
P.close()
Exemplo n.º 16
0
"""Test the fortran function :f:func:`closest2d`"""
from vcmq import N, P, code_file_name, P, os, rotate_grid, add_grid
from vacumm.misc.grid._interp_ import closest2d

# Input grid
grid = rotate_grid((N.arange(5), N.arange(4)), 30)
xxi = grid.getLongitude()[:].filled()
yyi = grid.getLatitude()[:].filled()

# Input random points
N.random.seed(0)
np = 100
xxo = N.random.random(np) * (xxi.max() - xxi.min()) + xxi.min()
yyo = N.random.random(np) * (yyi.max() - yyi.min()) + yyi.min()

# Closest
for xo, yo in zip(xxo, yyo):
    i, j = closest2d(xxi, yyi, xo, yo, nogeo=0)
    P.plot([xo, xxi[j - 1, i - 1]], [yo, yyi[j - 1, i - 1]], 'k')
add_grid(grid, color='k', markersize=10, centers=True, marker='o', alpha=1)
P.scatter(xxo, yyo, c='r', s=40, zorder=11)
P.title('closest2d')
P.axis('image')
figfile = code_file_name(ext='png')
if os.path.exists(figfile): os.remove(figfile)
P.savefig(figfile)
P.close()