def plot(xx,
         yy,
         target,
         label,
         figfiles,
         figfile,
         lon=None,
         lat=None,
         show=False):
    xs, ys, mask = coord2slice(target, lon=lon, lat=lat)
    P.figure(figsize=(6, 3.5))
    P.title('Target=%(label)s / select: lon=%(lon)s, lat=%(lat)s' % locals())
    add_grid((xx, yy))
    xx = xx.asma()
    yy = yy.asma()
    if isinstance(lon, tuple):
        P.axvline(lon[0], color='m', ls='--', lw=2)
        P.axvline(lon[1], color='m', ls='--', lw=2)
    elif isinstance(lon, slice):
        i, j, k = lon.indices(xx.shape[1])
        P.plot(xx[:, i], yy[:, i], 'c--', lw=2)
        P.plot(xx[:, j - 1], yy[:, j - 1], 'c--', lw=2)
    if isinstance(lat, tuple):
        P.axhline(lat[0], color='m', ls='--', lw=2)
        P.axhline(lat[1], color='m', ls='--', lw=2)
    elif isinstance(lat, slice):
        i, j, k = lat.indices(yy.shape[0])
        P.plot(xx[i], yy[i], 'c--', lw=2)
        P.plot(xx[j - 1], yy[j - 1], 'c--', lw=2)
    P.xticks(N.arange(xx.min() - 1, xx.max() + 1))
    P.yticks(N.arange(yy.min() - 1, yy.max() + 1))
    xxi, yyi = xx, yy
    xx = xx[ys, xs]
    yy = yy[ys, xs]
    #    mask = mask[ys, xs]
    xxb, yyb = meshbounds(xx, yy)
    P.pcolormesh(xxb, yyb, mask, shading='faceted')
    P.scatter(xx.ravel(), yy.ravel(), c=(0, 1, 0))
    P.grid(True)
    P.axis('image')
    P.tight_layout()
    i = len(figfiles)
    savefig = figfile % i
    if os.path.exists(savefig): os.remove(savefig)
    P.savefig(savefig)
    figfiles.append(savefig)
    if show: P.show()
    else: P.close()
Beispiel #2
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
def plot(xx, yy, target, label, figfiles, figfile, lon=None, lat=None, show=False):
    xs, ys, mask = coord2slice(target, lon=lon, lat=lat)
    P.figure(figsize=(6, 3.5))
    P.title('Target=%(label)s / select: lon=%(lon)s, lat=%(lat)s'%locals())
    add_grid((xx, yy))
    xx = xx.asma()
    yy = yy.asma()
    if isinstance(lon, tuple): 
        P.axvline(lon[0], color='m', ls='--', lw=2)
        P.axvline(lon[1], color='m', ls='--', lw=2)
    elif isinstance(lon, slice):
        i, j, k = lon.indices(xx.shape[1])
        P.plot(xx[:, i], yy[:, i], 'c--', lw=2)
        P.plot(xx[:, j-1], yy[:, j-1], 'c--', lw=2)
    if isinstance(lat, tuple): 
        P.axhline(lat[0], color='m', ls='--', lw=2)
        P.axhline(lat[1], color='m', ls='--', lw=2)
    elif isinstance(lat, slice):
        i, j, k = lat.indices(yy.shape[0])
        P.plot(xx[i], yy[i], 'c--', lw=2)
        P.plot(xx[j-1], yy[j-1], 'c--', lw=2)
    P.xticks(N.arange(xx.min()-1, xx.max()+1))
    P.yticks(N.arange(yy.min()-1, yy.max()+1))
    xxi, yyi = xx, yy
    xx = xx[ys, xs]
    yy = yy[ys, xs]
#    mask = mask[ys, xs]
    xxb, yyb = meshbounds(xx, yy)
    P.pcolor(xxb, yyb, mask, shading='faceted')
    P.scatter(xx.ravel(), yy.ravel(), c=(0, 1, 0))
    P.grid('on')
    P.axis('image')
    P.tight_layout()
    i = len(figfiles)
    savefig = figfile%i
    if os.path.exists(savefig): os.remove(savefig)
    P.savefig(savefig)
    figfiles.append(savefig)
    if show: P.show()
    else: P.close()
Beispiel #4
0
def create_mv2_scattered_xyzt(np=10,
                              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'):
    """Create a VM2 array of scattered data

    Return
    ------
    array: longitudes
    array: latitude
    MV2.array: data
    """

    # Axes
    shape = ()
    axes = []
    if nt != 0:
        time = create_time(lindates(tmin, tmax, nt), tunits)
        shape += nt,
        axes.append(time)
    if nz != 0:
        dep = create_dep(N.linspace(zmin, zmax, nz))
        axes.append(dep)
        shape += nz,
    shape += np,
    axes.append(create_axis((np, )))

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

    # Positiions
    lons = N.linspace(xmin, xmax, np)
    lats = N.linspace(ymin, ymax, np)

    return lons, lats, data
Beispiel #5
0
"""Test the :func:`~vacumm.misc.grid.regridding.regrid2d` function"""
from vcmq import (P, N, MV2, code_file_name, os, add_grid, rotate_grid, set_grid,
    create_grid, rc, rcdefaults, plot2d, regrid2d, create_time)


# Input grid and data
nxi = 20
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)
"""Test CDAT conservative regridding"""
from vcmq import N, meshbounds, bounds1d, cdms2, MV2, rc, P, add_grid, rcdefaults, \
    create_lon, create_lat, savefigs, code_file_name

# Input grid
x0, y0, nx, ny, dx, dy = 0., 0., 20, 15, 5., 5.
x = x0 + N.arange(0, nx * dx, dx)
y = y0 + N.arange(0, ny * dy, dy)
xxbi, yybi = meshbounds(x, y)
xb = bounds1d(x)
yb = bounds1d(y)
lon = create_lon(x)
lat = create_lat(y)
lon.setBounds(xb)
lat.setBounds(yb)
gridi = cdms2.createRectGrid(lat, lon)

# Input data
vari = MV2.ones(gridi.shape) * 50
vari[5:10, 7:13] = -10
#vari[:, -1] = MV2.masked # <<< THIS MAKES IT WORK!

vari.setAxisList([gridi.getLatitude(), gridi.getLongitude()])
vari.setGrid(gridi)

# Output grid
grido, xxbo, yybo = gridi, xxbi, yybi

# Regrid
diag = {'dstAreaFractions': None}
varo = vari.regrid(grido,
"""Test the fortran function :f:func:`interp1dxx`"""
from vcmq import N, P, meshcells, minmax, code_file_name, os
from vacumm.misc.grid._interp_ import interp1dxx

nx = nyi = 10
mv = 1.e20
u, v = N.mgrid[-3:3:nx * 1j, -3:3:10j] - 2
vari = N.ma.asarray(u**2 + v**2)
vari.set_fill_value(mv)
yi = N.linspace(-1000., 0., nyi)
yo = N.linspace(-1200, 100, 30.)
vari[nx / 3:2 * nx / 3, nyi / 3:2 * nyi / 3] = N.ma.masked
x = N.arange(nx)
dyi = (yi[1] - yi[0]) * 0.49
dyo = (yo[1] - yo[0]) * 0.49
yyi = N.resize(yi, vari.shape) + N.random.uniform(-dyi, dyi, vari.shape)
yyo = N.resize(yo, (nx, len(yo))) + N.random.uniform(-dyo, dyo, (nx, len(yo)))
yyib, xxib = meshcells(yyi, x)
yyob, xxob = meshcells(yyo, x)

varon = N.ma.masked_values(
    interp1dxx(vari.filled(), yyi, yyo, mv, 0, extrap=0), mv)
varol = N.ma.masked_values(
    interp1dxx(vari.filled(), yyi, yyo, mv, 1, extrap=0), mv)
varoh = N.ma.masked_values(
    interp1dxx(vari.filled(), yyi, yyo, mv, 3, extrap=0), mv)

kw = dict(vmin=vari.min(), vmax=vari.max())
axlims = [x[0], x[-1], yo[0], yo[-1]]
P.figure(figsize=(8, 8))
P.subplot(221)
Beispiel #8
0
#!/usr/bin/env python
# -*- coding: utf8 -*-
"""Axes et grilles avec VACUMM"""

from vcmq import N, MV2, create_lon, create_lat, create_grid, isgrid, isrect, islon, set_grid, get_grid, get_axis, varsel, resol, curv2rect, get_xy, meshgrid, meshcells, create_dep, isregular, P, rotate_grid, shiftgrid, extendgrid, create_axes2d, isdepthup, coord2slice, monotonic, xshift, depth2dz, get_closest

# Créer

# - axes
lon = create_lon((2., 11, 2.))  # -> SPECIFIEZ LE LONG_NAME
lat = create_lat(N.arange(43, 50.))
dep = create_dep((0., 10))
# -> AFFICHEZ LES INFOS
xx, yy = N.meshgrid(N.arange(5.), N.arange(4.))
lon2d, lat2d = create_axes2d(xx, yy)
ii = lon2d.getAxis(1)

# - grille
grid = create_grid(lon, lat)  # -> ESSAYEZ AVEC LON EXPLICITE
gridc = create_grid(lon2d, lat2d)

# Verifier
print islon(lon)
print isgrid(grid)  # -> TEST PARAM CURV=...
print isrect(gridc)  # -> CREEZ GRILLE NON RECT ET RETESTER
print isdepthup(dep)  # -> TESTEZ EN CHANGEANT ATTRIBUT POSITIVE ET VALEURS
print isregular(lon)

# Affecter
var = MV2.ones(grid.shape)
set_grid(var, grid)
Beispiel #9
0
"""Test :func:`~vacumm.misc.grid.regridding.griddata"""

from vcmq import (griddata, MV2, code_file_name, N, create_grid, set_grid,
                  map2)

# Generate data
# - reference
xr = N.arange(20.) - 25
yr = N.arange(10.) + 43.
xxr, yyr = N.meshgrid(xr, yr)
zzr = (N.sin(xxr*N.pi/6)*N.sin(yyr*N.pi/6) + \
    N.exp(-((xxr-7.)**2+(yyr-7.)**2)/4.**2))*100.
zzr -= zzr.mean()
zzr = N.ma.asarray(zzr)
zzr[5:, 10:] = N.ma.masked
# - input at random locations
ij = N.unique((N.random.rand(150) * zzr.size).astype('i'))
xi, yi, zi = xxr.flat[ij], yyr.flat[ij], zzr.flat[ij]
zi = N.ma.resize(zi, (3, zi.size))
# - format
zi = MV2.array(zi, copy=False, id='sst')
taxis = zi.getAxis(0)
taxis.units = 'hours since 2000'
taxis.axis = 'T'
ggo = create_grid(xr, yr)
zzr = MV2.array(zzr)
set_grid(zzr, ggo)

# Call and plot
kw = dict(vmin=zzr.min(),
          vmax=zzr.max(),
Beispiel #10
0
"""Test :func:`~vacumm.misc.grid.regridding.extend1d` and :func:`~vacumm.misc.grid.regridding.extend2d`"""

from vcmq import N, create_grid2d, P, rc, plot2d, MV2, set_grid, savefigs, code_file_name
from vacumm.misc.grid.regridding import extend1d, extend2d

# Input data
nxi = 4
nyi = 3
xxi, yyi = N.meshgrid(N.arange(nxi) + .25, N.arange(nyi) - .25)
for j in xrange(nyi):
    xxi[j, :] -= j * 0.5
for i in xrange(nxi):
    yyi[:, i] += i * 0.5
gridi = create_grid2d(xxi, yyi)  # input cdms grid
vari = MV2.array(N.arange(nyi * nxi).reshape(nyi, nxi)) + 10.
set_grid(vari, gridi)  # set grid and axes

# Extend and plot
rc('font', size=9)
P.figure(figsize=(6, 6))
kw = dict(xmin=xxi.min() - 3,
          xmax=xxi.max() + 3,
          ymin=yyi.min() - 3,
          ymax=yyi.max() + 3,
          show=False,
          xhide='auto',
          yhide='auto')
# - original
plot2d(vari, title='Original', subplot=(2, 2, 1), **kw)
# - extend1d
for i, (axis, ext, mode) in enumerate([(-1, (2, 2), 'same'),
"""Test the class :class:`~vacumm.misc.grid.regridding.CurvedInterpolator`"""

from vacumm.misc.grid.misc import rotate_grid
from vacumm.misc.grid.regridding import CurvedInterpolator
from vcmq import P, N, set_grid, plot2d, MV2, add_grid, code_file_name, os


# Curved grid
nxy = 10
lon = N.arange(nxy*1.)
lat = N.arange(nxy*1.)
gridi = rotate_grid((lon, lat), 30)
xxi = gridi.getLongitude()[:].filled()
yyi = gridi.getLatitude()[:].filled()
vari = MV2.array(yyi)
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.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)
P.subplot(132, aspect=1)
Beispiel #12
0
"""Test :func:`~vacumm.misc.plot.hov2` with a TZ variable"""

# Imports
from vcmq import N, MV2, create_dep, create_time, hov2, os, rc, code_file_name

# Init data with z 1D
nt = 10
nz = 8
var = N.dot(N.hanning(nt).reshape(nt, 1), N.hanning(nz).reshape(1, nz))
var = MV2.array(var)
time = create_time((0., nt), units="days since 2000")
z1d = create_dep((-nz + 1, 1.))
var.setAxis(0, time)
var.setAxis(1, z1d)
z2d = N.resize(z1d, var.shape)
z2d *= N.resize((N.arange(1., nt + 1) / nt).reshape(1, nt), (nz, nt)).T

# Plot with z 1D
rc('font', size=8)
kw = dict(show=False, bgcolor='0.5', date_fmt="%a")
hov2(var, subplot=211, **kw)

# Plot with z 2D
figfile = code_file_name(ext='png')
if os.path.exists(figfile): os.remove(figfile)
hov2(var, xaxis=z2d, subplot=212, twin='x', savefig=figfile, close=True, **kw)

# Unittest
result = dict(files=figfile)
Beispiel #13
0
    ax = P.subplot(131)
    P.pcolormesh(xbi, ybi, vi, **kw)
    P.title('Original')
    P.subplot(132, sharex=ax, sharey=ax)
    P.pcolormesh(xbo, ybo, vol, **kw)
    P.title('Linear')
    P.subplot(133, sharex=ax, sharey=ax)
    P.pcolormesh(xbo, ybo, voc, **kw)
    P.ylim(ymin=min(ybi.min(), ybo.min()), ymax=max(ybi.max(), ybo.max()))
    P.title('Cellave')
    P.tight_layout()
    P.savefig(figfile)


# 1d->1d
depi1d = create_dep(N.arange(-4500., 1, 500))
depo1d = create_dep(N.arange(-4000., 1, 333.33))
nzi = depi1d.shape[0]
vari = MV2.asarray(N.ma.resize(depi1d[:], (nt, ny, nx, nzi)).transpose([0, 3, 1, 2]))
vari.setAxis(1, depi1d)
varol1 = regrid1d(vari, depo1d, method='linear')
varol2 = regrid1d(vari, depo1d, method='linear', iaxi=0, iaxo=0, axi=depi1d)
result.append(('assertEqual', [(varol1-varol2).std(), 0]))
varoc = regrid1d(vari, depo1d, method='cellave')
myplot(vari, depi1d, varol1, varoc, depo1d, code_file_name(ext='_0.png'))


# 4d->1d
depi1d = N.arange(-4500., 1, 500)
nzi = depi1d.shape[0]
depi4d = N.resize(N.resize(depi1d, (nx, ny, nzi)).T, (nt, nzi, ny, nx))
"""Test CDAT conservative regridding"""
from vcmq import N, meshbounds, bounds1d, cdms2, MV2, rc, P, add_grid, rcdefaults, \
    create_lon, create_lat, savefigs, code_file_name
    
# Input grid
x0, y0, nx, ny, dx, dy = 0., 0., 20, 15, 5., 5.
x = x0+N.arange(0, nx*dx, dx)
y = y0+N.arange(0, ny*dy, dy)
xxbi, yybi = meshbounds(x, y)
xb = bounds1d(x)
yb = bounds1d(y)
lon = create_lon(x)
lat = create_lat(y)
lon.setBounds(xb)
lat.setBounds(yb)
gridi = cdms2.createRectGrid(lat, lon)

# Input data
vari = MV2.ones(gridi.shape)*50
vari[5:10, 7:13] = -10
#vari[:, -1] = MV2.masked # <<< THIS MAKES IT WORK!

vari.setAxisList([gridi.getLatitude(), gridi.getLongitude()])
vari.setGrid(gridi)

# Output grid
grido, xxbo, yybo = gridi, xxbi, yybi

# Regrid
diag = {'dstAreaFractions':None}
varo = vari.regrid(grido, tool='esmf', method='conservative', 
Beispiel #15
0
def generate_pseudo_ensemble(ncpat,
                             varnames=None,
                             nrens=50,
                             enrich=2.,
                             norms=None,
                             getmodes=False,
                             logger=None,
                             asdicts=False,
                             anomaly=True,
                             ncensfile=None,
                             **kwargs):
    """Generate a static pseudo-ensemble from a single simulation


    Parameters
    ----------
    ncpat: string
        netcdf file name or pattern
    nrens: int
        Ensemble size
    enrich: float
        Enrichment factor
    getmodes: bool
        Get also EOFs end eigen values
    **kwargs:
        Extra parameters are passed to :func:`load_model_at_dates`

    Return
    ------
    list (or dict) of arrays:
        variables with their name as keys
    dict: eofs, ev and variance, optional
        eofs: list (or dict) of arrays(nmodes, ...), optional
            EOFs
        ev: array(nmodes), optional
            Eigen values
        var: array
            Variance

    """
    # Logger
    kwlog = kwfilter(kwargs, 'logger_')
    if logger is None:
        logger = get_logger(**kwlog)
    logger.verbose('Generating pseudo-ensemble')

    # Ensembe size
    enrich = max(enrich, 1.)
    nt = int(nrens * enrich)
    logger.debug(
        ' enrich={enrich},  nt={nt}, ncpat={ncpat}, varnames={varnames}'.
        format(**locals()))

    # Read variables
    logger.debug('Reading the model at {} dates'.format(nt))
    data = load_model_at_regular_dates(ncpat,
                                       varnames=varnames,
                                       nt=nt,
                                       asdict=False,
                                       **kwargs)
    single = not isinstance(data, list)

    # Norms
    if isinstance(norms, dict):
        norms = var_prop_dict2list(data, norms)

    # Enrichment
    witheofs = nrens != nt
    if witheofs:
        logger.debug('Computing reduced rank ensemble with EOFs analysis')

        # Stack packed variables together
        stacker = Stacker(data, norms=norms, logger=logger)
        meanstate = N.zeros(stacker.ns)
        states = N.asfortranarray(stacker.stacked_data.copy())

        # Compute EOFs
        stddev, svals, svecs, status = f_eofcovar(dim_fields=stacker.ns,
                                                  offsets=1,
                                                  remove_mstate=0,
                                                  do_mv=0,
                                                  states=states,
                                                  meanstate=meanstate)
        if status != 0:
            raise SONATError('Error while calling fortran eofcovar routine')
        neof = svals.size  # computed
        neofr = nrens - 1  # retained
        svals = svals[:neofr] * N.sqrt(
            (neof - 1.) / neof)  # to be consistent with total variance
        svecs = svecs[:, :neofr]

        # Generate ensemble
        sens = f_sampleens(svecs, svals, meanstate, flag=0)

        # Unstack
        ens = stacker.unstack(sens,
                              format=2,
                              rescale='norm' if anomaly else True)
        if getmodes:

            # Modes
            mode_axis = create_axis(N.arange(1, neofr + 1, dtype='i'),
                                    id='mode')
            eofs = stacker.unstack(svecs,
                                   firstdims=mode_axis,
                                   id='{id}_eof',
                                   rescale=False,
                                   format=1)
            svals = MV2.array(svals,
                              axes=[mode_axis],
                              id='ev',
                              attributes={'long_name': 'Eigen values'})
            svals.total_variance = float(stacker.ns)

            # Variance
            vv = stacker.format_arrays([d.var(axis=0) for d in stacker.datas],
                                       id='{id}_variance',
                                       mode=1)
            variance = stacker.unmap(vv)

    else:  # No enrichment -> take the anomaly if requested

        logger.debug('Getting the anomaly to build the ensemble')
        ens = data

        if anomaly:
            if single:
                ens[:] = ens.asma() - ens.asma().mean(axis=0)
            else:
                for i, e in enumerate(ens):
                    ens[i][:] = e.asma() - e.asma().mean(axis=0)

    # Finalize
    getmodes = getmodes and witheofs
    member_axis = create_axis(N.arange(nrens, dtype='i'),
                              id='member',
                              long_name='Member')
    if single:
        ens.setAxis(0, member_axis)
    else:
        for var in ens:
            var.setAxis(0, member_axis)

    # Dump to file
    if ncensfile:
        logger.debug('Dump the ensemble to netcdf')
        checkdir(ncensfile)
        f = cdms2.open(ncensfile, 'w')
        ensvars = list(ens) if not single else [ens]
        if getmodes:
            if single:
                ensvars.append(eofs)
                ensvars.append(variance)
            else:
                ensvars.extend(eofs)
                ensvars.extend(variance)
            ensvars.append(svals)
        for var in ensvars:
            f.write(var)
        f.close()
        logger.created(ncensfile)

    # As dicts
    if asdicts:
        if single:
            ens = OrderedDict([(ens.id, ens)])
            if getmodes:
                eofs = OrderedDict([(eofs.id, eofs)])
                variance = OrderedDict([(variance.id, variance)])
        else:
            ens = OrderedDict([(var.id, var) for var in ens])
            if getmodes:
                eofs = OrderedDict([(var.id, var) for var in eofs])
                variance = OrderedDict([(var.id, var) for var in variance])

    # Return
    if not getmodes:
        return ens
    return ens, dict(eofs=eofs, eigenvalues=svals, variance=variance)
Beispiel #16
0
    ax = P.subplot(131)
    P.pcolormesh(xbi, ybi, vi, **kw)
    P.title('Original')
    P.subplot(132, sharex=ax, sharey=ax)
    P.pcolormesh(xbo, ybo, vol, **kw)
    P.title('Linear')
    P.subplot(133, sharex=ax, sharey=ax)
    P.pcolormesh(xbo, ybo, voc, **kw)
    P.ylim(ymin=min(ybi.min(), ybo.min()), ymax=max(ybi.max(), ybo.max()))
    P.title('Cellave')
    P.tight_layout()
    P.savefig(figfile)


# 1d->1d
depi1d = create_dep(N.arange(-4500., 1, 500))
depo1d = create_dep(N.arange(-4000., 1, 333.33))
nzi = depi1d.shape[0]
vari = MV2.asarray(
    N.ma.resize(depi1d[:], (nt, ny, nx, nzi)).transpose([0, 3, 1, 2]))
vari.setAxis(1, depi1d)
varol1 = regrid1d(vari, depo1d, method='linear')
varol2 = regrid1d(vari, depo1d, method='linear', iaxi=0, iaxo=0, axi=depi1d)
result.append(('assertEqual', [(varol1 - varol2).std(), 0]))
varoc = regrid1d(vari, depo1d, method='cellave')
myplot(vari, depi1d, varol1, varoc, depo1d, code_file_name(ext='_0.png'))

# 4d->1d
depi1d = N.arange(-4500., 1, 500)
nzi = depi1d.shape[0]
depi4d = N.resize(N.resize(depi1d, (nx, ny, nzi)).T, (nt, nzi, ny, nx))
"""Test :func:`~vacumm.misc.plot.section2` in quiver mode"""

# Imports
from vcmq import create_lon, N, MV2, create_dep, os, code_file_name, section2
from vacumm.misc.phys.units import deg2m

# Init data with z 1D
nz = 11
nx = 11
x = create_lon(N.arange(nx))
xm = deg2m(x[:],lat=45.) # meters
dx = xm[:].ptp()
z = create_dep((-nz+1, 1.), units='m', long_name='Depth')
dz = z[:].ptp()
scale = dz/dx
u = MV2.ones((nz,nx)) # 1 m/s
w = u*scale           # 1 m/s * scale
for var in u,w:
    var.setAxis(0, z)
    var.setAxis(1, x)
    var.units = 'm/s'

# Plot
figfile = code_file_name(ext='png')
if os.path.exists(figfile): os.remove(figfile)
s = section2((u,w), quiver_norm=1, fill=False, show=False,
    savefig=figfile, close=False, axes_aspect=1)

# Result
result = dict(files=figfile)
"""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)
result.append(('AssertEqual', (coord2slice(lon1d, lon=slice(3, 6)), slice(3, 6, None))))
result.append(('AssertEqual', (coord2slice(lon1d, lat=(6, 8)), slice(0, 10, 1))))
result.append(('AssertEqual', (coord2slice(lon1d, lon=(60, 70)), None)))

# Rect grid
grid = create_grid((0, 10.), (20, 30.))
result.append(('AssertEqual', (coord2slice(grid, lon=(0., 3.5), lat=slice(3, 5)), 
    (slice(0, 4, 1), slice(3, 5, None), None))))
result.append(('AssertEqual', (coord2slice(grid, lat=(21,21, 'ccb')),
    (slice(0, 10, 1), slice(1, 2, 1), None))))

# 2D axis
lon2d = N.empty((10, 10.))
for i in xrange(10): 
    lon2d[i] = lon1d[:]+i
lat2d = N.resize((N.arange(10)+20), (10, 10)).T
lon2d, lat2d = create_axes2d(lon2d, lat2d)
kw = dict(show=False)
plot(lon2d, lat2d, lon2d, 'lon2d', figfiles, figfile, lon=(2, 4), **kw)
plot(lon2d, lat2d, lon2d, 'lon2d', figfiles, figfile, lon=(2, 4), lat=slice(0, 2), **kw)
plot(lon2d, lat2d, lat2d,  'lat2d', figfiles, figfile, lat=(22, 26.6,'ccb'), **kw)

# Curv grid
grid = create_grid(lon2d, lat2d)
plot(lon2d, lat2d, grid, 'grid', figfiles, figfile, lon=(8, 11, 'cc'), lat=(21.9, 26., 'cc'), **kw)
plot(lon2d, lat2d, grid, 'grid', figfiles, figfile, lon=slice(2, 5), lat=(23.4, 23.6, 'ccb'), **kw)
res = coord2slice(grid,lon=(8,8,'ccb'),lat=(24,24,'ccb'))
result.append(('AssertEqual', (res[:2], (slice(3, 6, 1), slice(4, 5, 1)))))
result.append(('AssertEqual', (coord2slice(grid,lon=(8,8,'ccb'),lat=(24,24,'ccb'), mode='a').tolist(), 
    [[4, 4, 4], [3, 4, 5]])))
"""Test :func:`~vacumm.misc.plot.section2` in quiver mode"""

# Imports
from vcmq import create_lon, N, MV2, create_dep, os, code_file_name, section2
from vacumm.misc.phys.units import deg2m

# Init data with z 1D
nz = 11
nx = 11
x = create_lon(N.arange(nx))
xm = deg2m(x[:], lat=45.)  # meters
dx = xm[:].ptp()
z = create_dep((-nz + 1, 1.), units='m', long_name='Depth')
dz = z[:].ptp()
scale = dz / dx
u = MV2.ones((nz, nx))  # 1 m/s
w = u * scale  # 1 m/s * scale
for var in u, w:
    var.setAxis(0, z)
    var.setAxis(1, x)
    var.units = 'm/s'

# Plot
figfile = code_file_name(ext='png')
if os.path.exists(figfile): os.remove(figfile)
s = section2((u, w),
             quiver_norm=1,
             fill=False,
             show=False,
             savefig=figfile,
             close=True,
Beispiel #21
0
    def create_Dthnc(self, fileout, TimeSeries):
        if '2D' in fileout:
            self.i23d = 2
        else:
            self.i23d = 3

        # create file
        if self.i23d == 3:
            Nlev = self.zz.shape[1]
        else:
            Nlev = 1

        time_Series, nc = create_ncTH(
            fileout, len(self.llon), Nlev, self.ivs,
            np.round((TimeSeries - TimeSeries[0]) * 24 * 3600))

        for n in range(0, len(TimeSeries)):
            tin = create_time(np.ones(len(self.llon) * Nlev) *
                              (TimeSeries[n] + 1),
                              units='days since 1-1-1')

            total = np.zeros(shape=(self.ivs, len(self.llon), Nlev))

            # get tide
            if self.tidal:
                var = self.HC.keys()

                for i, v in enumerate(sorted(var)):
                    # horizontal interpolation
                    tmp = get_tide(self.constidx, self.tfreq, self.HC[v],
                                   np.array(TimeSeries[n]), self.lat0)

                    if self.i23d > 2:  # vertical interpolation
                        tmp = vertical_extrapolation(tmp, self.zz, z0=self.z0)

                    total[i, :, :] = total[i, :, :] + tmp

            if self.residual:
                var = self.res_vars

                for i, v in enumerate(sorted(var)):
                    arri = self.res_file[v][:]
                    if self.i23d > 2:
                        dep = create_depth(arri.getAxis(1)[:])
                        extra = create_axis(N.arange(1), id='member')
                        arri2 = np.tile(arri, [1, 1, 1, 1, 1])
                        arri3 = MV2.array(arri2,
                                          axes=[
                                              extra,
                                              arri.getAxis(0), dep,
                                              arri.getAxis(2),
                                              arri.getAxis(3)
                                          ],
                                          copy=False,
                                          fill_value=1e20)

                        zi = arri.getAxis(1)[:]
                        if np.mean(zi) > 0:
                            zi = zi * -1
                        tb = grid2xy(arri3,
                                     xo=np.tile(self.llon,
                                                [Nlev, 1]).T.flatten(),
                                     yo=np.tile(self.llat,
                                                [Nlev, 1]).T.flatten(),
                                     zo=self.zz.flatten(),
                                     method='linear',
                                     to=tin,
                                     zi=zi)

                    else:
                        tb = grid2xy(arri,
                                     xo=self.llon,
                                     yo=self.llat,
                                     method='linear',
                                     to=tin)

                    if np.any(tb.mask == True):
                        bad = tb.mask == True
                        if len(bad.shape) > 1:
                            bad = bad[0, :]
                        tin_bad = create_time(np.ones(len(bad)) *
                                              (TimeSeries[n] + 1),
                                              units='days since 1-1-1')

                        if self.i23d > 2:
                            llon = np.tile(self.llon, [Nlev, 1]).T.flatten()
                            llat = np.tile(self.llat, [Nlev, 1]).T.flatten()
                            zz = self.zz.flatten()
                            zi = arri.getAxis(1)[:]
                            if np.mean(zi) > 0:
                                zi = zi * -1

                            tb[0, bad] = grid2xy(arri3,
                                                 xo=llon[bad],
                                                 yo=llat[bad],
                                                 zo=zz[bad],
                                                 method='nearest',
                                                 to=tin_bad,
                                                 zi=zi)

                        else:
                            tb[bad] = grid2xy(
                                arri,
                                xo=np.array(self.llon)[bad].tolist(),
                                yo=np.array(self.llat)[bad].tolist(),
                                method='nearest',
                                to=tin_bad)

                    if np.any(tb.mask == True):
                        print('probleme')

                    total[i, :, :] = total[i, :, :] + np.reshape(
                        tb, (len(self.llon), Nlev))

            total = np.transpose(total, (1, 2, 0))

            if np.isnan(total).any():
                import pdb
                pdb.set_trace()

            if n % 100 == 0:
                self.logger.info(
                    'For timestep=%.f, max=%.4f, min=%.4f , max abs diff=%.4f'
                    % (TimeSeries[n], total.max(), total.min(),
                       abs(np.diff(total, n=1, axis=0)).max()))

            time_Series[n, :, :, :] = total

        nc.close()
"""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)
from vcmq import create_grid2d, meshbounds, set_grid, code_file_name, P, N, MV2, rc, add_grid
from collections import OrderedDict

configs = OrderedDict(
    libcf=['linear'], 
    esmf=[
        'linear',
        'patch',
        'conservative',
        ]
)

# Input curved grid
nxi = 5
nyi = 4
xxi, yyi = N.meshgrid(N.arange(nxi)+.25, N.arange(nyi)-.25)
for j in xrange(nyi):
    xxi[j,:] -= j*0.5
    #yyi[j,:] += j
for i in xrange(nxi):
    yyi[:,i] += i*0.5
gridi = create_grid2d(xxi,yyi) # input cdms grid
xxib,yyib = meshbounds(xxi,yyi) # coordinates of cell corners

# Output curved grid
nxo = 7
nyo = 7
xxo, yyo = N.meshgrid(N.arange(nxo)+.5, N.arange(nyo)-.5)
grido = create_grid2d(xxo, yyo) # output cdms grid
xxob,yyob = meshbounds(xxo,yyo) # coordinates of cell corners
"""Compare CDAT regridding speed with rectangular and rectangular grids"""

config = {
   'esmf':['linear', 'patch', 'conserv'], 
   'libcf':['linear'], 
}

# Imports
from vcmq import MV2, create_grid2d, code_file_name, os, CDATRegridder, N, set_grid, psinfo
from vacumm.misc.grid import rotate_grid
from time import time

# Input
nx = ny = 300
vari = MV2.array(N.arange(nx*ny*1.).reshape(ny, nx))
gridi = create_grid2d(vari.getAxis(1)[:]*50/nx,  vari.getAxis(0)[:]*50/nx)
set_grid(vari, gridi)

# Output grid
gridor = create_grid2d(vari.getAxis(1)[:]*0.09*50/nx,  
    vari.getAxis(0)[:]*0.09*50/nx)
gridoc = rotate_grid(gridi, 30)

# Log
logfile = code_file_name(ext='log')
if os.path.exists(logfile): os.remove(logfile)
f = open(logfile, 'w')
print >>f, 'NY=%(ny)i, NX=%(nx)i'%locals()

# Loop on methods
for tool, methods in config.items():
"""Test the traditionnal CDAT regrid2 regridder"""

from vcmq import MV2, create_grid, meshbounds, P, add_grid, N, bounds1d, plot2d, savefigs,code_file_name
from regrid2 import Horizontal

# Input
nx, ny = 6, 4
vari = MV2.array(N.arange(nx*ny*1.).reshape(ny, nx), fill_value=1e20)
xi = vari.getAxis(-1)
xi[:] *= 2
yi = vari.getAxis(-2)
yi[:] *= 3
xi.designateLongitude()
yi.designateLatitude()
xi.setBounds(bounds1d(xi))
yi.setBounds(bounds1d(yi))
vari[1:2, 2:4] = MV2.masked
gridi = vari.getGrid()


# Output
grido = create_grid(xi[:]+2*2.5, yi[:]+3*1.5)
xo = grido.getLongitude()
yo = grido.getLatitude()
xo.setBounds(bounds1d(xo))
yo.setBounds(bounds1d(yo))
xxob, yyob = meshbounds(xo, yo)

# Regridding
varo, wo = vari.regrid(grido, tool='regrid2', returnTuple=1)
"""Test the fortran function :f:func:`curv2rect`"""
from vcmq import N, P, code_file_name, P, os
from vacumm.misc.grid._interp_ import curv2rect

# Input
x1, y1 = 0., 0.
x2, y2 = 3., 1.
x3, y3 = 2., 4.
x4, y4 = -1., 2.

# Format and convert
xx, yy = N.meshgrid(N.arange(-2, 4, 0.25), N.arange(-1, 5, 0.25))
nxy = xx.shape
xx.shape = -1
yy.shape = -1
pp, qq = [], []
for x, y in zip(xx, yy):
    p, q = curv2rect(x1,x2,x3,x4,y1,y2,y3,y4,x,y)
    pp.append(p)
    qq.append(q)
pp = N.array(pp)
qq = N.array(qq)

# Plot
xp = [x1, x2, x3, x4, x1]
yp = [y1, y2, y3, y4, y1]
P.subplot(211)
levels = N.array([-10, 0, 1, 10.])
o = P.contourf(xx.reshape(nxy), yy.reshape(nxy), pp.reshape(nxy), levels=levels)
P.colorbar(o)
P.plot(xp, yp, 'k')
#if rank==0: print 'expand in time and depth'
vari = MV2.resize(vari2d, (nt, nz)+vari2d.shape)
cp_props(vari2d, vari)


#if rank==0: print 'grido'
loni = gridi.getLongitude()
lati = gridi.getLatitude()
xib, yib = bounds2d(loni, lati)
loni.setBounds(xib)
lati.setBounds(yib)
xi = loni.getValue()
yi = lati.getValue()
dx = N.diff(xi[0]).mean()
dy = N.diff(yi[:, 0]).mean()
xo = N.arange(xi.min()+10*dx, -30*dx+xi.max(), dx)
yo = N.arange(yi.min()-20*dy, yi.max()-20*dy, dy)
lono = cdms2.createAxis(xo)
lono.designateLongitude() ; lono.units= 'degrees_east'
lato = cdms2.createAxis(yo)
lato.designateLatitude() ; lato.units = 'degrees_north'
xob = bounds1d(lono) ; lono.setBounds(xob)
yob = bounds1d(lato) ; lato.setBounds(yob)
grido = cdms2.createRectGrid(lato, lono)
xmin, xmax = minmax(loni.asma(),lono)
ymin, ymax = minmax(lati.asma(), lato)
nyo,nxo = grido.shape
#print 'rank',rank
basefile = code_file_name(ext=False)
repfile = basefile+'.nt%(nt)s-nz%(nz)s-nyi%(nyi)s-nxi%(nxi)s.log'%locals()
if rank==0: 
Beispiel #28
0
"""Test :func:`~vacumm.misc.plot.section2` with a Z- variable"""

# Imports
from vcmq import N, MV2, cdms2, create_dep, rc, section2, code_file_name, os

# Init data with z 1D
nz = 8
nd = 10
var = N.dot(N.hanning(nz).reshape(nz, 1), N.hanning(nd).reshape(1, nd))
var = MV2.array(var)
d = cdms2.createAxis(N.arange(nd))
d.units='km'
d.long_name='Distance'
z1d = create_dep((-nz+1, 1.))
var.setAxis(0, z1d)
var.setAxis(1, d)
z2d = N.resize(z1d[:].reshape(1, nz), (nd, nz)).T
z2d *= N.arange(1., nd+1)/nd

# Plot with z 1D
rc('font', size=8)
kw = dict(show=False, bgcolor='0.5')
section2(var, subplot=211, **kw)

# Plot with z 2D
figfile = code_file_name(ext='png')
if os.path.exists(figfile): os.remove(figfile)
section2(var, yaxis=z2d, subplot=212, savefig=figfile, close=True, **kw)

# Result
result = dict(files=figfile)
"""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)
"""Test the fortran function :f:func:`curv2rel`"""
from vcmq import N, P, code_file_name, P, os, meshbounds
from vacumm.misc.grid._interp_ import curv2rel

# Input grid
x0, y0 = 0., 2.
nxi = 3
nyi = 3
dxi = (2., 2.)
dyi = (-2., 2.)
xxi = N.zeros((nyi, nxi))
xxi[0] = x0 + N.arange(nxi) * dxi[0]
for j in range(1, nyi):
    xxi[j] = xxi[j - 1] + dyi[0]
yyi = N.zeros((nyi, nxi))
yyi[:, 0] = y0 + N.arange(nyi) * dyi[1]
for j in range(1, nyi):
    yyi[:, j] = yyi[:, j - 1] + dxi[1]
xxbi, yybi = meshbounds(xxi, yyi)
relpos2index = lambda fi, fj: fj * nyi + fi
ii, jj = N.meshgrid(N.arange(nxi) + .5, N.arange(nyi) + .5)
iib, jjb = meshbounds(ii, jj)
zzi = jj * nyi + ii  #relpos2index(ii, jj)
zzbi = jjb * nyi + iib  # ny+1? relpos2index(iib, jjb)

# Input random points
N.random.seed(0)
np = 100
xxo = N.random.random(np) * (xxbi.max() - xxbi.min()) + xxbi.min()
yyo = N.random.random(np) * (yybi.max() - yybi.min()) + yybi.min()
xxo = N.concatenate((xxo, xxi.ravel()))
"""Test fortran function :f:func:`nearest2dto1d`"""

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


# Input grid and data
nxy = 15
xi = N.arange(nxy*1.)
yi = N.arange(nxy*1.)
xxi, yyi = N.meshgrid(xi, yi)
zi = N.ma.array(yyi)
zi[int(nxy*0.3):int(nxy*0.8), int(nxy*0.3):int(nxy*0.8)] = N.ma.masked
zi.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 = zi.get_fill_value()
zo = nearest2dto1d(xi,yi,zi.filled(mv),xo,yo,mv)
zo = N.ma.masked_values(zo, mv) 

# Plot
kw = dict(vmin=zi.min(), vmax=zi.max())
P.figure(figsize=(6, 6))
P.subplot(111, aspect=1)
P.contourf(xxi, yyi, zi[0], **kw)
add_grid((xi, yi), edges=False, centers=True, marker='o')
"""Test the fortran function :f:func:`curv2rel_single`"""
from vcmq import N, P, code_file_name, P, os, meshbounds
from vacumm.misc.grid._interp_ import curv2rel_single


# Input grid
x0, y0 = 0., 2.
nxi = 3
nyi = 3
dxi = (2., 2.)
dyi = (-2., 2.)
xxi = N.zeros((nyi, nxi))
xxi[0] = x0 + N.arange(nxi) * dxi[0]
for j in range(1, nyi):
    xxi[j] = xxi[j-1] + dyi[0]
yyi = N.zeros((nyi, nxi))
yyi[:, 0] = y0 + N.arange(nyi) * dyi[1]
for j in range(1, nyi):
    yyi[:, j] = yyi[:, j-1] + dxi[1]
xxbi, yybi = meshbounds(xxi, yyi)
relpos2index = lambda fi, fj, nyi: fj * nyi + fi
ii, jj = N.meshgrid(N.arange(nxi)+.5, N.arange(nyi)+.5)
iib, jjb = meshbounds(ii, jj)
zzi = relpos2index(ii, jj, nyi)
zzbi = relpos2index(iib, jjb, nyi)

# Input random points
N.random.seed(0)
np = 100
xxo = N.random.random(np)*(xxbi.max()-xxbi.min()) + xxbi.min()
yyo = N.random.random(np)*(yybi.max()-yybi.min()) + yybi.min()
Beispiel #33
0
"""Test :func:`~vacumm.misc.plot.hov2` with a TZ variable"""

# Imports
from vcmq import N, MV2, create_dep, create_time, hov2, os, rc, code_file_name

# Init data with z 1D
nt = 10
nz = 8
var = N.dot(N.hanning(nt).reshape(nt, 1), N.hanning(nz).reshape(1, nz))
var = MV2.array(var)
time = create_time((0., nt), units="days since 2000")
z1d = create_dep((-nz+1, 1.))
var.setAxis(0, time)
var.setAxis(1, z1d)
z2d = N.resize(z1d, var.shape)
z2d *= N.resize((N.arange(1., nt+1)/nt).reshape(1, nt), (nz, nt)).T

# Plot with z 1D
rc('font', size=8)
kw = dict(show=False, bgcolor='0.5', date_fmt="%a")
hov2(var, subplot=211, **kw)

# Plot with z 2D
figfile = code_file_name(ext='png')
if os.path.exists(figfile): os.remove(figfile)
hov2(var, xaxis=z2d, subplot=212, twin='x', savefig=figfile, close=True, **kw)

# Unittest
result = dict(files=figfile)

sp = f("speed")
spe = f("speed_error")
f.close()

# Create hourly time axis
taxi = sp.getTime()
taxi.toRelativeTime("hours since 2000")
ctimesi = taxi.asComponentTime()
ct0 = round_date(ctimesi[0], "hour")
ct1 = round_date(ctimesi[-1], "hour")
taxo = create_time(lindates(ct0, ct1, 1, "hour"), taxi.units)

# Lag error
# - estimation
els = []
lags = N.arange(1, 6)
for lag in lags:
    els.append(N.sqrt(((sp[lag:] - sp[:-lag]) ** 2).mean()))
els = N.array(els)
a, b, _, _, _ = linregress(lags, els)
# - plot
P.figure(figsize=(6, 6))
P.subplot(211)
P.plot(lags, els, "o")
P.plot([0, lags[-1]], [b, a * lags[-1] + b], "g")
P.axhline(b, color="0.8", ls="--")
P.ylim(ymin=0)
P.xlabel("Lag [hour]")
P.ylabel("Error [m s-1]")
add_key(1)
P.title("Linear lag error model")
Beispiel #35
0
#!/usr/bin/env python
# -*- coding: utf8 -*-
"""Axes et grilles avec VACUMM"""

from vcmq import N, MV2, create_lon, create_lat, create_grid, isgrid, isrect, islon, set_grid,  get_grid, get_axis, varsel, resol, curv2rect, get_xy, meshgrid, meshcells, create_dep, isregular, P, rotate_grid, shiftgrid, extendgrid, create_axes2d, isdepthup, coord2slice, monotonic, xshift, depth2dz, get_closest

# Créer

# - axes
lon = create_lon((2., 11, 2.))  # -> SPECIFIEZ LE LONG_NAME
lat = create_lat(N.arange(43, 50.))
dep = create_dep((0., 10))
# -> AFFICHEZ LES INFOS
xx, yy = N.meshgrid(N.arange(5.), N.arange(4.))
lon2d, lat2d = create_axes2d(xx, yy)
ii = lon2d.getAxis(1)

# - grille
grid = create_grid(lon, lat)    # -> ESSAYEZ AVEC LON EXPLICITE
gridc = create_grid(lon2d, lat2d)


# Verifier
print islon(lon)
print isgrid(grid)              # -> TEST PARAM CURV=...
print isrect(gridc)             # -> CREEZ GRILLE NON RECT ET RETESTER
print isdepthup(dep)            # -> TESTEZ EN CHANGEANT ATTRIBUT POSITIVE ET VALEURS
print isregular(lon)            


# Affecter
"""Test fortran function :f:func:`dstwgt2dto1d`"""

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

# Input grid and data
nxy = 15
xi = N.arange(nxy * 1.)
yi = N.arange(nxy * 1.)
xxi, yyi = N.meshgrid(xi, yi)
zi = N.ma.array(yyi)
zi[int(nxy * 0.3):int(nxy * 0.8), int(nxy * 0.3):int(nxy * 0.8)] = N.ma.masked
zi.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 = zi.get_fill_value()
zo = dstwgt2dto1d(xi, yi, zi.filled(mv), xo, yo, mv)
zo = N.ma.masked_values(zo, mv)

# Plot
kw = dict(vmin=zi.min(), vmax=zi.max())
P.figure(figsize=(6, 6))
P.subplot(111, aspect=1)
P.contourf(xxi, yyi, zi[0], **kw)
add_grid((xi, yi), edges=False, centers=True, marker='o')
P.scatter(xo, yo, c=zo[0], s=50, **kw)
Beispiel #37
0
"""Test :meth:`vacumm.data.misc.arakawa.CGrid.interp`"""

from vcmq import MV2, N, create_grid, create_dep, set_grid, map2, \
    code_file_name, CGrid, minmax, curve2, add_grid

# Initial variable
grid = create_grid(N.arange(-7, 0.), N.arange(43, 50.))
dep = create_dep([-5000, -3000, -2000, -1000, -500, -300, -200, -100.])
var = {}
var['t'] = MV2.reshape(N.arange(grid.size()*len(dep))*1., (len(dep), )+grid.shape)
set_grid(var['t'], grid)
var['t'].setAxis(0, dep)


# Arakawa manager
ag = CGrid()

# Interpolations
for p in 'u', 'v', 'f', 'w':
    var[p] = ag.interp(var['t'], 't', p, mode='extrap')

# Surface plots
vmin, vmax = minmax(*[var[p][-1] for p in ['u', 'v', 'f']])
kw = dict(show=False, res=None, vmin=vmin, vmax=vmax, colorbar=False, grid=False, cmap='jet')
m = map2(var['t'][-1], fill='pcolor', 
    title='Interpolations on an Arakawa C grid: T->U/V/F', **kw)
add_grid(var['t'], linestyle='-')
kw.update(fill='scatter', contour=False, fill_s=60)
markers = dict(u='>', v='^', f='D', t='o')
for p in 't', 'u', 'v', 'f':
    m = map2(var[p][-1], fill_marker=markers[p], shadow=True, zorder=100, **kw)
"""Test the fortran function :f:func:`curv2rect`"""
from vcmq import N, P, code_file_name, P, os
from vacumm.misc.grid._interp_ import curv2rect

# Input
x1, y1 = 0., 0.
x2, y2 = 3., 1.
x3, y3 = 2., 4.
x4, y4 = -1., 2.

# Format and convert
xx, yy = N.meshgrid(N.arange(-2, 4, 0.25), N.arange(-1, 5, 0.25))
nxy = xx.shape
xx.shape = -1
yy.shape = -1
pp, qq = [], []
for x, y in zip(xx, yy):
    p, q = curv2rect(x1, x2, x3, x4, y1, y2, y3, y4, x, y)
    pp.append(p)
    qq.append(q)
pp = N.array(pp)
qq = N.array(qq)

# Plot
xp = [x1, x2, x3, x4, x1]
yp = [y1, y2, y3, y4, y1]
P.subplot(211)
levels = N.array([-10, 0, 1, 10.])
o = P.contourf(xx.reshape(nxy),
               yy.reshape(nxy),
               pp.reshape(nxy),
Beispiel #39
0
"""Test the fortran function :f:func:`bilin`"""
from vcmq import N, P, meshcells, minmax, code_file_name, os
from vacumm.misc.grid._interp_ import dstwgt


nxi = 15
nyi = 10
mv = 1.e20
u, v = N.mgrid[-3:3:nyi*1j, -3:3:nxi*1j]-2
vari = N.ma.asarray(u**2+v**2)
vari.set_fill_value(mv)
xi = N.arange(nxi)
yi = N.arange(nyi)
vari[int(nyi*0.4):int(nyi*0.4)+3, int(nxi*0.4):int(nxi*0.4)+2] = N.ma.masked
xxib, yyib  = meshcells(xi, yi)

nxo = 40
nyo = 25
xo = N.linspace(int(nxi*0.2),int(nxi*1.2),nxo)
yo = N.linspace(int(-nyi*0.2),int(nyi*0.8),nyo)
xxob, yyob  = meshcells(xo, yo)

vari.shape = (1, )+vari.shape
varo = N.ma.masked_values(dstwgt(vari.filled(), xi, yi, xo, yo, mv, 0), mv)

kw = dict(vmin=vari.min(), vmax=vari.max())
axlims = [min(xi.min(), xo.min()), max(xi.max(), xo.max()), 
    min(yi.min(), yo.min()), max(yi.max(), yo.max())]
P.figure(figsize=(8, 4))
P.subplot(211)
P.pcolor(xxib, yyib, vari[0], **kw)
Beispiel #40
0
nez = 2



# Imports
from vcmq import (N, MV2, code_file_name, os, P, create_lon, create_lat, create_dep,
                  create_time, lindates, create_axis, reltime, grid2xy,
                  comptime, set_grid, rotate_grid, add_grid)

# Rectangular xyzt with 1d z data and coords
# - data
lon = create_lon(N.linspace(lon0, lon1, nx))
lat = create_lat(N.linspace(lat0, lat1, ny))
dep = create_dep(N.linspace(dep0, dep1, nz))
time = create_time(lindates(time0, time1, nt))
extra = create_axis(N.arange(ne), id='member')
data = N.resize(lat[:], (ne, nt, nz, nx, ny)) # function of y
data = N.moveaxis(data, -1, -2)
#data = N.arange(nx*ny*nz*nt*ne, dtype='d').reshape(ne, nt, nz, ny, nx)
vi = MV2.array(data,
                 axes=[extra, time, dep, lat, lon], copy=False,
                 fill_value=1e20)
N.random.seed(0)
xo = N.random.uniform(lon0, lon1, np)
yo = N.random.uniform(lat0, lat1, np)
zo = N.random.uniform(dep0, dep1, np)
to = comptime(N.random.uniform(reltime(time0, time.units).value,
                      reltime(time1, time.units).value, np),
                      time.units)

# Rectangular xyzt with 1d z
sp = f('speed')
spe = f('speed_error')
f.close()

# Create hourly time axis
taxi = sp.getTime()
taxi.toRelativeTime('hours since 2000')
ctimesi = taxi.asComponentTime()
ct0 = round_date(ctimesi[0], 'hour')
ct1 = round_date(ctimesi[-1], 'hour')
taxo = create_time(lindates(ct0, ct1, 1, 'hour'), taxi.units)

# Lag error
# - estimation
els = []
lags = N.arange(1, 6)
for lag in lags:
    els.append(N.sqrt(((sp[lag:] - sp[:-lag])**2).mean()))
els = N.array(els)
a, b, _, _, _ = linregress(lags, els)
# - plot
P.figure(figsize=(6, 6))
P.subplot(211)
P.plot(lags, els, 'o')
P.plot([0, lags[-1]], [b, a * lags[-1] + b], 'g')
P.axhline(b, color='0.8', ls='--')
P.ylim(ymin=0)
P.xlabel('Lag [hour]')
P.ylabel('Error [m s-1]')
add_key(1)
P.title('Linear lag error model')
Beispiel #42
0
"""Test :func:`~vacumm.misc.plot.section2` with a Z- variable"""

# Imports
from vcmq import N, MV2, cdms2, create_dep, rc, section2, code_file_name, os

# Init data with z 1D
nz = 8
nd = 10
var = N.dot(N.hanning(nz).reshape(nz, 1), N.hanning(nd).reshape(1, nd))
var = MV2.array(var)
d = cdms2.createAxis(N.arange(nd))
d.units = 'km'
d.long_name = 'Distance'
z1d = create_dep((-nz + 1, 1.))
var.setAxis(0, z1d)
var.setAxis(1, d)
z2d = N.resize(z1d[:].reshape(1, nz), (nd, nz)).T
z2d *= N.arange(1., nd + 1) / nd

# Plot with z 1D
rc('font', size=8)
kw = dict(show=False, bgcolor='0.5')
section2(var, subplot=211, **kw)

# Plot with z 2D
figfile = code_file_name(ext='png')
if os.path.exists(figfile): os.remove(figfile)
section2(var, yaxis=z2d, subplot=212, savefig=figfile, close=True, **kw)

# Result
result = dict(files=figfile)
Beispiel #43
0
"""Test the :func:`~vacumm.misc.grid.regridding.regrid2d` function"""
from vcmq import P, N, MV2, code_file_name, os, add_grid, rotate_grid, set_grid, \
    create_grid, rc, rcdefaults, plot2d
from vacumm.misc.grid.regridding import regrid2d


# 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
Beispiel #44
0
"""Test :meth:`vacumm.data.misc.arakawa.CGrid.interp`"""

from vcmq import MV2, N, create_grid, create_dep, set_grid, map2, \
    code_file_name, CGrid, minmax, curve2, add_grid

# Initial variable
grid = create_grid(N.arange(-7, 0.), N.arange(43, 50.))
dep = create_dep([-5000, -3000, -2000, -1000, -500, -300, -200, -100.])
var = {}
var['t'] = MV2.reshape(
    N.arange(grid.size() * len(dep)) * 1., (len(dep), ) + grid.shape)
set_grid(var['t'], grid)
var['t'].setAxis(0, dep)

# Arakawa manager
ag = CGrid()

# Interpolations
for p in 'u', 'v', 'f', 'w':
    var[p] = ag.interp(var['t'], 't', p, mode='extrap')

# Surface plots
vmin, vmax = minmax(*[var[p][-1] for p in ['u', 'v', 'f']])
kw = dict(show=False,
          res=None,
          vmin=vmin,
          vmax=vmax,
          colorbar=False,
          grid=False,
          cmap='jet')
m = map2(var['t'][-1],
"""Test the fortran function :f:func:`interp1dxx`"""
from vcmq import N, P,meshcells, minmax, code_file_name, os
from vacumm.misc.grid._interp_ import interp1dxx


nx = nyi = 10
mv = 1.e20
u, v = N.mgrid[-3:3:nx*1j, -3:3:10j]-2
vari = N.ma.asarray(u**2+v**2)
vari.set_fill_value(mv)
yi = N.linspace(-1000.,0., nyi)
yo = N.linspace(-1200, 100, 30.)
vari[nx/3:2*nx/3, nyi/3:2*nyi/3] = N.ma.masked
x = N.arange(nx)
dyi = (yi[1]-yi[0])*0.49
dyo = (yo[1]-yo[0])*0.49
yyi = N.resize(yi, vari.shape)+N.random.uniform(-dyi, dyi, vari.shape)
yyo = N.resize(yo, (nx, len(yo)))+N.random.uniform(-dyo, dyo, (nx, len(yo)))
yyib, xxib  = meshcells(yyi, x)
yyob, xxob  = meshcells(yyo, x)

varon = N.ma.masked_values(interp1dxx(vari.filled(), yyi, yyo, mv, 0, extrap=0), mv)
varol = N.ma.masked_values(interp1dxx(vari.filled(), yyi, yyo, mv, 1, extrap=0), mv)
varoh = N.ma.masked_values(interp1dxx(vari.filled(), yyi, yyo, mv, 3, extrap=0), mv)

kw = dict(vmin=vari.min(), vmax=vari.max())
axlims = [x[0], x[-1], yo[0], yo[-1]]
P.figure(figsize=(8, 8))
P.subplot(221)
P.pcolor(xxib, yyib, vari)
P.axis(axlims)
Beispiel #46
0
"""Test the traditionnal CDAT regrid2 regridder"""

from vcmq import MV2, create_grid, meshbounds, P, add_grid, N, bounds1d, plot2d, savefigs, code_file_name
from regrid2 import Horizontal

# Input
nx, ny = 6, 4
vari = MV2.array(N.arange(nx * ny * 1.).reshape(ny, nx), fill_value=1e20)
xi = vari.getAxis(-1)
xi[:] *= 2
yi = vari.getAxis(-2)
yi[:] *= 3
xi.designateLongitude()
yi.designateLatitude()
xi.setBounds(bounds1d(xi))
yi.setBounds(bounds1d(yi))
vari[1:2, 2:4] = MV2.masked
gridi = vari.getGrid()

# Output
grido = create_grid(xi[:] + 2 * 2.5, yi[:] + 3 * 1.5)
xo = grido.getLongitude()
yo = grido.getLatitude()
xo.setBounds(bounds1d(xo))
yo.setBounds(bounds1d(yo))
xxob, yyob = meshbounds(xo, yo)

# Regridding
varo, wo = vari.regrid(grido, tool='regrid2', returnTuple=1)

# Plot