Ejemplo n.º 1
0
"""Test :class:`~vacumm.misc.stats.StatAccum` with dumping and loading """

# Imports
from vcmq import MV2, N, code_file_name, StatAccum
from numpy.testing import assert_array_almost_equal
from numpy.random import seed

cdms2.setAutoBounds('off')

# Setup masked data
seed(0)
nt, ny, nx = 20, 3, 3
var1 = MV2.array(N.random.random((nt, ny, nx)))
var1.getAxis(0).designateTime()
var1.getAxis(0).units = 'months since 2000'
var1.getAxis(0).id = 'time'
var1.getAxis(1).id = 'lat'
var1.getAxis(1).designateLatitude()
var1.getAxis(2).id = 'lon'
var1.getAxis(2).designateLongitude()
var1.units = 'm'
var1.id = 'ssh'
var2 = var1.clone()
var2[:] = N.random.random((nt, ny, nx))
var1[3:13, :1, :1] = MV2.masked
var2[5:15, -1:, -1:] = MV2.masked
var2.long_name = 'Sea level'
var2.id = 'sla'
mask = var1.mask|var2.mask # common mask
vmax = var2.max()
bins = N.linspace(-0.1*vmax, 0.9*vmax, 14)
Ejemplo n.º 2
0
#!/usr/bin/env python
"""Create fake observation netcdf files
"""

from vcmq import (cdms2, MV2, os, create_dep, create_lon, create_lat, N,
                  masked_polygon, map2)

cdms2.setAutoBounds(False)

# Profiles
ncfile = '../data/obs.profiles.nc'
lons = MV2.array([-5.8, -5.7, -4.6, -2.8], id='lon')
lats = MV2.array([48.1, 47.5, 47.4, 47.3], id='lat')
daxis = create_dep((-100., 1, 5.))
nz = len(daxis)
np = len(lons)
temp_error = N.resize([.2, .5, .3, .8], (nz, np))
sal_error = N.resize([.3, .1, .15, .4], (nz, np))
temp_error = MV2.array(temp_error, id='temp_error')
sal_error = MV2.array(sal_error, id='sal_error')
temp_error[:nz / 2, 2] = MV2.masked
sal_error[:nz / 2, 2] = MV2.masked
temp_error[:3 * nz / 4, 3] = MV2.masked
sal_error[:3 * nz / 4, 3] = MV2.masked
mob = MV2.array([0, 1, 1, 0], id='mobility', fill_value=-1)
paxis = lons.getAxis(0)
paxis.id = 'station'
axes = [daxis, paxis]
f = cdms2.open(ncfile, 'w')
for var in lons, lats, mob, temp_error, sal_error:
    var.setAxis(-1, paxis)
Ejemplo n.º 3
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)

Ejemplo n.º 4
0
# 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(),
          lon=(xr[0], xr[-1]),
          cmap_lum=0.7,
          linewidth=.5,
          lat=(yr[0], yr[-1]),
          show=False,
Ejemplo n.º 5
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)
"""Test :class:`~vacumm.misc.stats.StatAccum` with dumping and loading """

# Imports
from vcmq import MV2, N, code_file_name, StatAccum
from numpy.testing import assert_array_almost_equal
from numpy.random import seed

cdms2.setAutoBounds('off')

# Setup masked data
seed(0)
nt, ny, nx = 20, 3, 3
var1 = MV2.array(N.random.random((nt, ny, nx)))
var1.getAxis(0).designateTime()
var1.getAxis(0).units = 'months since 2000'
var1.getAxis(0).id = 'time'
var1.getAxis(1).id = 'lat'
var1.getAxis(1).designateLatitude()
var1.getAxis(2).id = 'lon'
var1.getAxis(2).designateLongitude()
var1.units = 'm'
var1.id = 'ssh'
var2 = var1.clone()
var2[:] = N.random.random((nt, ny, nx))
var1[3:13, :1, :1] = MV2.masked
var2[5:15, -1:, -1:] = MV2.masked
var2.long_name = 'Sea level'
var2.id = 'sla'
mask = var1.mask | var2.mask  # common mask
vmax = var2.max()
bins = N.linspace(-0.1 * vmax, 0.9 * vmax, 14)
Ejemplo n.º 7
0
    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

# Input field
vari = MV2.array(N.arange(nyi*nxi).reshape(nyi,nxi))+10.
vari[1,1:3] = 100
vari[:2,1:3] = MV2.masked
set_grid(vari, gridi) # set grid and axes
#gridi.setMask(vari.mask)

# Define plot function
figfile = code_file_name(ext=False)+'_%(ifig)i.png'
#'%(tool)s_%(method)s.png'
figfiles = []
rc('font',size=9)
kw = dict(vmin=vari.min(),vmax=vari.max())
    
# Define logger
logfile = code_file_name(ext='log')
f = open(logfile, 'w')
Ejemplo n.º 8
0
"""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():
Ejemplo n.º 9
0
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
vo = grid2xy(vi, xo=xo, yo=yo, zo=zo, to=to, method='linear')
von = grid2xy(vi, xo=xo, yo=yo, zo=zo, to=to, method='nearest')
assert vo.shape==(ne, np)
N.testing.assert_allclose(vo[0], yo)
kwp = dict(vmin=vi.min(), vmax=vi.max())
Ejemplo n.º 10
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)
Ejemplo n.º 11
0
"""Test :func:`~vacumm.misc.atime.interp_clim`"""
from vcmq import (code_file_name, interp_clim, MV2, N, create_time, lindates,
                  curve, strftime)

# Original clim
N.random.seed(0)
s = N.resize(N.sin(N.linspace(0, 1, 13)[:12] * 2 * N.pi), (2, 12)).T
clim = MV2.array(s, fill_value=1e20)
p = curve(clim[:, 0],
          'o-',
          show=False,
          subplot=211,
          title='Original climatology',
          xmin=-.5,
          xmax=11.5,
          xticks=range(12),
          xticklabels=[strftime('%b', '2000-%i' % i) for i in range(1, 13)])

# Target times
times = lindates('2000-01-01', '2001-12-31', 5, 'day')

#  Interpolations
for i, method in enumerate((
        'linear',
        'cubic',
)):
    climo = interp_clim(clim, times, method=method)
    c = curve(climo[:, 0],
              'o-',
              color='gr'[i],
              show=False,
Ejemplo n.º 12
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))
depi4d += 500 * (N.random.random(depi4d.shape) - 0.5)
depo1d = create_dep(N.arange(-4000., 1, 333.33))
vari = MV2.array(depi4d, fill_value=1e20)
vari.getAxis(1).designateLevel()
depi4d = MV2.asarray(depi4d)
depi4d.getAxis(1).designateLevel()
varol1 = regrid1d(vari, depo1d, method='linear', axi=depi4d)
varol2 = regrid1d(vari, depo1d, method='linear', iaxi=1, axi=depi4d)
result.append(('assertEqual', [(varol1 - varol2).std(), 0]))
varoc = regrid1d(vari, depo1d, method='cellave', iaxi=1, axi=depi4d)
myplot(vari, depi4d, varol1, varoc, depo1d, code_file_name(ext='_1.png'))

# 4d->4d
depi1d = N.arange(-4500., 1, 500)
nzi = depi1d.shape[0]
depi4d = N.resize(N.resize(depi1d, (nx, ny, nzi)).T, (nt, nzi, ny, nx))
depi4d += 500 * (N.random.random(depi4d.shape) - 0.5)
depo1d = create_dep(N.arange(-4000., 1, 333.33))
"""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)
Ejemplo n.º 14
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))
depi4d += 500*(N.random.random(depi4d.shape)-0.5)
depo1d = create_dep(N.arange(-4000., 1, 333.33))
vari = MV2.array(depi4d, fill_value=1e20)
vari.getAxis(1).designateLevel()
depi4d = MV2.asarray(depi4d)
depi4d.getAxis(1).designateLevel()
varol1 = regrid1d(vari, depo1d, method='linear', axi=depi4d)
varol2 = regrid1d(vari, depo1d, method='linear', iaxi=1, axi=depi4d)
result.append(('assertEqual', [(varol1-varol2).std(), 0]))
varoc = regrid1d(vari, depo1d, method='cellave', iaxi=1, axi=depi4d)
myplot(vari, depi4d, varol1, varoc, depo1d, code_file_name(ext='_1.png'))


# 4d->4d
depi1d = N.arange(-4500., 1, 500)
nzi = depi1d.shape[0]
depi4d = N.resize(N.resize(depi1d, (nx, ny, nzi)).T, (nt, nzi, ny, nx))
depi4d += 500*(N.random.random(depi4d.shape)-0.5)
Ejemplo n.º 15
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()
Ejemplo n.º 16
0
# Add text
m.add_text(0.1, 0.1, 'My text', color='r', shadow=True, weight='bold')
m.add_text(-2, 50.5, 'My text with transform', transform='data', ha='center', 
    bbox=dict(facecolor='y', alpha=.3))
m.add_lon_label(-8, 47.5, -8, va='center', ha='center', transform='data', fmt='%g')
m.add_lat_label(-8, 47, 47, va='center', ha='center', transform='data')
m.add_time_label(.1, .9, '2000')

# Add place
m.add_place(-2, 49, 'My place', text_family='monospace', text_size=15)

# Add lines
m.add_line([-8, 46, -1, 50], color='b')
m.add_lines([-8, -1, -4, -8], [45, 45, 47, 45], color='r', linewidth=2, shadow=True)
m.add_box([-8, 46, -1, 50], color='c')

m.savefig(figfile%0)
m.show()
m.close()


# Test with dates
var = MV2.array([3, 6])
time = create_time(['2000', '2005'], units='years since 2000')
var.setAxis(0, time)
c = curve2(var, show=False)
c.add_point('2001', 4, marker='+', size=100, linewidth=2)
c.savefig(figfile%1)
m.close()
Ejemplo n.º 17
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'),
Ejemplo n.º 18
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)
Ejemplo n.º 19
0
                -8,
                va='center',
                ha='center',
                transform='data',
                fmt='%g')
m.add_lat_label(-8, 47, 47, va='center', ha='center', transform='data')
m.add_time_label(.1, .9, '2000')

# Add place
m.add_place(-2, 49, 'My place', text_family='monospace', text_size=15)

# Add lines
m.add_line([-8, 46, -1, 50], color='b')
m.add_lines([-8, -1, -4, -8], [45, 45, 47, 45],
            color='r',
            linewidth=2,
            shadow=True)
m.add_box([-8, 46, -1, 50], color='c')

m.savefig(figfile % 0)
m.close()

# Test with dates
var = MV2.array([3, 6])
time = create_time(['2000', '2005'], units='years since 2000')
var.setAxis(0, time)
c = curve2(var, show=False)
c.add_point('2001', 4, marker='+', size=100, linewidth=2)
c.savefig(figfile % 1)
m.close()
Ejemplo n.º 20
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