def test_pset_merge_inplace(fieldset, mode, npart=100):
    pset1 = ParticleSet(fieldset, pclass=ptype[mode],
                        lon=np.linspace(0, 1, npart),
                        lat=np.linspace(1, 0, npart))
    pset2 = ParticleSet(fieldset, pclass=ptype[mode],
                        lon=np.linspace(0, 1, npart),
                        lat=np.linspace(0, 1, npart))
    assert(pset1.size == 100)
    assert(pset2.size == 100)
    pset1.add(pset2)
    assert(pset1.size == 200)
def test_pset_add_explicit(fieldset, mode, npart=100):
    lon = np.linspace(0, 1, npart)
    lat = np.linspace(1, 0, npart)
    pset = ParticleSet(fieldset, lon=[], lat=[], pclass=ptype[mode], lonlatdepth_dtype=np.float64)
    for i in range(npart):
        particle = ParticleSet(pclass=ptype[mode], lon=lon[i], lat=lat[i],
                               fieldset=fieldset, lonlatdepth_dtype=np.float64)
        pset.add(particle)
    assert(pset.size == 100)
    assert np.allclose([p.lon for p in pset], lon, rtol=1e-12)
    assert np.allclose([p.lat for p in pset], lat, rtol=1e-12)
def test_delay_start_example(mode, npart=10, show_movie=False):
    """Example script that shows how to 'delay' the start of particle advection.
    This is useful for example when particles need to be started at different times

    In this example, we use pset.add statements to add one particle every hour
    in the peninsula fieldset. Note that the title in the movie may not show correct time"""

    fieldset = FieldSet.from_nemo(path.join(path.dirname(__file__),
                                            'Peninsula_data', 'peninsula'),
                                  extra_fields={'P': 'P'},
                                  allow_time_extrapolation=True)

    # Initialise particles as in the Peninsula example
    x = 3. * (1. / 1.852 / 60)  # 3 km offset from boundary
    y = (fieldset.U.lat[0] + x, fieldset.U.lat[-1] - x
         )  # latitude range, including offsets

    lat = np.linspace(y[0], y[1], npart, dtype=np.float32)
    pset = ParticleSet(fieldset, lon=[], lat=[], pclass=ptype[mode])

    delaytime = delta(hours=1)  # delay time between particle releases

    # Since we are going to add particles during runtime, we need "indexed" NetCDF file
    output_file = pset.ParticleFile(name="DelayParticle", type="indexed")

    for t in range(npart):
        pset.add(ptype[mode](lon=x, lat=lat[t], fieldset=fieldset))
        pset.execute(AdvectionRK4,
                     runtime=delaytime,
                     dt=delta(minutes=5),
                     interval=delta(hours=1),
                     show_movie=show_movie,
                     starttime=delaytime * t,
                     output_file=output_file)

    # Note that time on the movie is not parsed correctly
    pset.execute(AdvectionRK4,
                 runtime=delta(hours=24) - npart * delaytime,
                 starttime=delaytime * npart,
                 dt=delta(minutes=5),
                 interval=delta(hours=1),
                 show_movie=show_movie,
                 output_file=output_file)

    londist = np.array([(p.lon - x) for p in pset])
    assert (londist > 0.1).all()

    # Test whether time was written away correctly in file
    pfile = Dataset("DelayParticle.nc", 'r')
    id = pfile.variables['trajectory'][:]
    time = pfile.variables['time'][id == id[0]]
    assert all(time[1:] - time[0:-1] == time[1] - time[0])
    pfile.close()
Exemple #4
0
def test_pset_merge_inplace(grid, mode, npart=100):
    pset1 = ParticleSet(grid,
                        pclass=ptype[mode],
                        lon=np.linspace(0, 1, npart, dtype=np.float32),
                        lat=np.linspace(1, 0, npart, dtype=np.float32))
    pset2 = ParticleSet(grid,
                        pclass=ptype[mode],
                        lon=np.linspace(0, 1, npart, dtype=np.float32),
                        lat=np.linspace(0, 1, npart, dtype=np.float32))
    assert (pset1.size == 100)
    assert (pset2.size == 100)
    pset1.add(pset2)
    assert (pset1.size == 200)
Exemple #5
0
def test_inversedistance_nearland(mode, arrtype, k_sample_p, npart=81):
    dims = (4, 4, 6)
    P = np.random.rand(dims[0], dims[1],
                       dims[2]) + 2 if arrtype == 'rand' else np.ones(
                           dims, dtype=np.float32)
    P[1, 1:2, 1:6] = np.nan  # setting some values to land (NaN)
    dimensions = {
        'lon': np.linspace(0., 1., dims[2], dtype=np.float32),
        'lat': np.linspace(0., 1., dims[1], dtype=np.float32),
        'depth': np.linspace(0., 1., dims[0], dtype=np.float32)
    }
    data = {
        'U': np.zeros(dims, dtype=np.float32),
        'V': np.zeros(dims, dtype=np.float32),
        'P': P
    }
    fieldset = FieldSet.from_data(data, dimensions, mesh='flat')
    fieldset.P.interp_method = 'linear_invdist_land_tracer'

    xv, yv = np.meshgrid(np.linspace(0.1, 0.9, int(np.sqrt(npart))),
                         np.linspace(0.1, 0.9, int(np.sqrt(npart))))
    # combine a pset at 0m with pset at 1m, as meshgrid does not do 3D
    pset = ParticleSet(fieldset,
                       pclass=pclass(mode),
                       lon=xv.flatten(),
                       lat=yv.flatten(),
                       depth=np.zeros(npart))
    pset2 = ParticleSet(fieldset,
                        pclass=pclass(mode),
                        lon=xv.flatten(),
                        lat=yv.flatten(),
                        depth=np.ones(npart))
    pset.add(pset2)
    pset.execute(k_sample_p, endtime=1, dt=1)
    if arrtype == 'rand':
        assert np.all((pset.p > 2) & (pset.p < 3))
    else:
        assert np.allclose(pset.p, 1.0, rtol=1e-5)

    success = False
    try:
        fieldset.U.interp_method = 'linear_invdist_land_tracer'
        fieldset.check_complete()
    except NotImplementedError:
        success = True
    assert success
Exemple #6
0
def test_nearest_neighbour_interpolation3D(mode, k_sample_p, npart=81):
    dims = (2, 2, 2)
    dimensions = {
        'lon': np.linspace(0., 1., dims[0], dtype=np.float32),
        'lat': np.linspace(0., 1., dims[1], dtype=np.float32),
        'depth': np.linspace(0., 1., dims[2], dtype=np.float32)
    }
    data = {
        'U': np.zeros(dims, dtype=np.float32),
        'V': np.zeros(dims, dtype=np.float32),
        'P': np.zeros(dims, dtype=np.float32)
    }
    data['P'][0, 1, 1] = 1.
    fieldset = FieldSet.from_data(data,
                                  dimensions,
                                  mesh='flat',
                                  transpose=True)
    fieldset.P.interp_method = 'nearest'
    xv, yv = np.meshgrid(np.linspace(0, 1.0, int(np.sqrt(npart))),
                         np.linspace(0, 1.0, int(np.sqrt(npart))))
    # combine a pset at 0m with pset at 1m, as meshgrid does not do 3D
    pset = ParticleSet(fieldset,
                       pclass=pclass(mode),
                       lon=xv.flatten(),
                       lat=yv.flatten(),
                       depth=np.zeros(npart))
    pset2 = ParticleSet(fieldset,
                        pclass=pclass(mode),
                        lon=xv.flatten(),
                        lat=yv.flatten(),
                        depth=np.ones(npart))
    pset.add(pset2)

    pset.execute(k_sample_p, endtime=1, dt=1)
    assert np.allclose(pset.p[(pset.lon < 0.5) & (pset.lat > 0.5) &
                              (pset.depth > 0.5)],
                       1.0,
                       rtol=1e-5)
    assert np.allclose(pset.p[(pset.lon > 0.5) | (pset.lat < 0.5) &
                              (pset.depth < 0.5)],
                       0.0,
                       rtol=1e-5)