本文整理汇总了Python中parcels.Grid类的典型用法代码示例。如果您正苦于以下问题:Python Grid类的具体用法?Python Grid怎么用?Python Grid使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Grid类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_grid_from_nemo
def test_grid_from_nemo(xdim, ydim, tmpdir, filename='test_nemo'):
""" Simple test for grid initialisation from NEMO file format. """
filepath = tmpdir.join(filename)
u, v, lon, lat, depth, time = generate_grid(xdim, ydim)
grid_out = Grid.from_data(u, lon, lat, v, lon, lat, depth, time)
grid_out.write(filepath)
grid = Grid.from_nemo(filepath)
u_t = np.transpose(u).reshape((lat.size, lon.size))
v_t = np.transpose(v).reshape((lat.size, lon.size))
assert len(grid.U.data.shape) == 3 # Will be 4 once we use depth
assert len(grid.V.data.shape) == 3
assert np.allclose(grid.U.data[0, :], u_t, rtol=1e-12)
assert np.allclose(grid.V.data[0, :], v_t, rtol=1e-12)
示例2: test_meridionalflow_sperical
def test_meridionalflow_sperical(mode, xdim=100, ydim=200):
""" Create uniform NORTHWARD flow on sperical earth and advect particles
As flow is so simple, it can be directly compared to analytical solution
"""
maxvel = 1.
lon = np.linspace(-180, 180, xdim, dtype=np.float32)
lat = np.linspace(-90, 90, ydim, dtype=np.float32)
U = np.zeros([xdim, ydim])
V = maxvel * np.ones([xdim, ydim])
grid = Grid.from_data(np.array(U, dtype=np.float32), lon, lat,
np.array(V, dtype=np.float32), lon, lat)
lonstart = [0, 45]
latstart = [0, 45]
endtime = delta(hours=24)
pset = grid.ParticleSet(2, pclass=pclass(mode), lon=lonstart, lat=latstart)
pset.execute(pset.Kernel(AdvectionRK4), endtime=endtime, dt=delta(hours=1))
assert(pset[0].lat - (latstart[0] + endtime.total_seconds() * maxvel / 1852 / 60) < 1e-4)
assert(pset[0].lon - lonstart[0] < 1e-4)
assert(pset[1].lat - (latstart[1] + endtime.total_seconds() * maxvel / 1852 / 60) < 1e-4)
assert(pset[1].lon - lonstart[1] < 1e-4)
示例3: radial_rotation_grid
def radial_rotation_grid(xdim=200, ydim=200): # Define 2D flat, square grid for testing purposes.
lon = np.linspace(0, 60, xdim, dtype=np.float32)
lat = np.linspace(0, 60, ydim, dtype=np.float32)
x0 = 30. # Define the origin to be the centre of the grid.
y0 = 30.
U = np.zeros((xdim, ydim), dtype=np.float32)
V = np.zeros((xdim, ydim), dtype=np.float32)
T = delta(days=1)
omega = 2*np.pi/T.total_seconds() # Define the rotational period as 1 day.
for i in range(lon.size):
for j in range(lat.size):
r = np.sqrt((lon[i]-x0)**2 + (lat[j]-y0)**2) # Define radial displacement.
assert(r >= 0.)
assert(r <= np.sqrt(x0**2 + y0**2))
theta = math.atan2((lat[j]-y0), (lon[i]-x0)) # Define the polar angle.
assert(abs(theta) <= np.pi)
U[i, j] = r * math.sin(theta) * omega
V[i, j] = -r * math.cos(theta) * omega
return Grid.from_data(U, lon, lat, V, lon, lat, mesh='flat')
示例4: test_zonalflow_sperical
def test_zonalflow_sperical(mode, k_sample_p, xdim=100, ydim=200):
""" Create uniform EASTWARD flow on sperical earth and advect particles
As flow is so simple, it can be directly compared to analytical solution
Note that in this case the cosine conversion is needed
"""
maxvel = 1.
p_fld = 10
lon = np.linspace(-180, 180, xdim, dtype=np.float32)
lat = np.linspace(-90, 90, ydim, dtype=np.float32)
V = np.zeros([xdim, ydim])
U = maxvel * np.ones([xdim, ydim])
P = p_fld * np.ones([xdim, ydim])
grid = Grid.from_data(np.array(U, dtype=np.float32), lon, lat,
np.array(V, dtype=np.float32), lon, lat,
field_data={'P': np.array(P, dtype=np.float32)})
lonstart = [0, 45]
latstart = [0, 45]
endtime = delta(hours=24)
pset = grid.ParticleSet(2, pclass=pclass(mode), lon=lonstart, lat=latstart)
pset.execute(pset.Kernel(AdvectionRK4) + k_sample_p,
endtime=endtime, dt=delta(hours=1))
assert(pset[0].lat - latstart[0] < 1e-4)
assert(pset[0].lon - (lonstart[0] + endtime.total_seconds() * maxvel / 1852 / 60
/ cos(latstart[0] * pi / 180)) < 1e-4)
assert(abs(pset[0].p - p_fld) < 1e-4)
assert(pset[1].lat - latstart[1] < 1e-4)
assert(pset[1].lon - (lonstart[1] + endtime.total_seconds() * maxvel / 1852 / 60
/ cos(latstart[1] * pi / 180)) < 1e-4)
assert(abs(pset[1].p - p_fld) < 1e-4)
示例5: test_delay_start_example
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 grid. Note that the title in the movie may not show correct time"""
grid = Grid.from_nemo('examples/Peninsula_data/peninsula', extra_vars={'P': 'P'})
# Initialise particles as in the Peninsula example
x = 3. * (1. / 1.852 / 60) # 3 km offset from boundary
y = (grid.U.lat[0] + x, grid.U.lat[-1] - x) # latitude range, including offsets
lat = np.linspace(y[0], y[1], npart, dtype=np.float32)
pset = grid.ParticleSet(0, lon=[], lat=[], pclass=ptype[mode])
delaytime = delta(hours=1) # delay time between particle releases
for t in range(npart):
pset.add(ptype[mode](lon=x, lat=lat[t], grid=grid))
pset.execute(AdvectionRK4, runtime=delaytime, dt=delta(minutes=5),
interval=delta(hours=1), show_movie=show_movie)
# Note that time on the movie is not parsed correctly
pset.execute(AdvectionRK4, runtime=delta(hours=24)-npart*delaytime,
dt=delta(minutes=5), interval=delta(hours=1), show_movie=show_movie)
londist = np.array([(p.lon - x) for p in pset])
assert(londist > 0.1).all()
示例6: set_ofam_grid
def set_ofam_grid():
filenames = {'U': "examples/OFAM_example_data/OFAM_simple_U.nc",
'V': "examples/OFAM_example_data/OFAM_simple_V.nc"}
variables = {'U': 'u', 'V': 'v'}
dimensions = {'lat': 'yu_ocean', 'lon': 'xu_ocean', 'depth': 'st_ocean',
'time': 'Time'}
return Grid.from_netcdf(filenames, variables, dimensions)
示例7: set_globcurrent_grid
def set_globcurrent_grid():
filenames = {'U': "examples/GlobCurrent_example_data/20*-GLOBCURRENT-L4-CUReul_hs-ALT_SUM-v02.0-fv01.0.nc",
'V': "examples/GlobCurrent_example_data/20*-GLOBCURRENT-L4-CUReul_hs-ALT_SUM-v02.0-fv01.0.nc"}
variables = {'U': 'eastward_eulerian_current_velocity', 'V': 'northward_eulerian_current_velocity'}
dimensions = {'lat': 'lat', 'lon': 'lon',
'time': 'time'}
return Grid.from_netcdf(filenames, variables, dimensions)
示例8: moving_eddies_grid
def moving_eddies_grid(xdim=200, ydim=350):
"""Generate a grid encapsulating the flow field consisting of two
moving eddies, one moving westward and the other moving northwestward.
Note that this is not a proper geophysical flow. Rather, a Gaussian eddy
is moved artificially with uniform velocities. Velocities are calculated
from geostrophy.
"""
# Set NEMO grid variables
depth = np.zeros(1, dtype=np.float32)
time = np.arange(0.0, 25.0 * 86400.0, 86400.0, dtype=np.float64)
# Coordinates of the test grid (on A-grid in deg)
lon = np.linspace(0, 4, xdim, dtype=np.float32)
lat = np.linspace(45, 52, ydim, dtype=np.float32)
# Grid spacing in m
def cosd(x):
return math.cos(math.radians(float(x)))
dx = (lon[1] - lon[0]) * 1852 * 60 * cosd(lat.mean())
dy = (lat[1] - lat[0]) * 1852 * 60
# Define arrays U (zonal), V (meridional), W (vertical) and P (sea
# surface height) all on A-grid
U = np.zeros((lon.size, lat.size, time.size), dtype=np.float32)
V = np.zeros((lon.size, lat.size, time.size), dtype=np.float32)
P = np.zeros((lon.size, lat.size, time.size), dtype=np.float32)
# Some constants
corio_0 = 1.0e-4 # Coriolis parameter
h0 = 1 # Max eddy height
sig = 0.5 # Eddy e-folding decay scale (in degrees)
g = 10 # Gravitational constant
eddyspeed = 0.1 # Translational speed in m/s
dX = eddyspeed * 86400 / dx # Grid cell movement of eddy max each day
dY = eddyspeed * 86400 / dy # Grid cell movement of eddy max each day
[x, y] = np.mgrid[: lon.size, : lat.size]
for t in range(time.size):
hymax_1 = lat.size / 7.0
hxmax_1 = 0.75 * lon.size - dX * t
hymax_2 = 3.0 * lat.size / 7.0 + dY * t
hxmax_2 = 0.75 * lon.size - dX * t
P[:, :, t] = h0 * np.exp(
-(x - hxmax_1) ** 2 / (sig * lon.size / 4.0) ** 2 - (y - hymax_1) ** 2 / (sig * lat.size / 7.0) ** 2
)
P[:, :, t] += h0 * np.exp(
-(x - hxmax_2) ** 2 / (sig * lon.size / 4.0) ** 2 - (y - hymax_2) ** 2 / (sig * lat.size / 7.0) ** 2
)
V[:-1, :, t] = -np.diff(P[:, :, t], axis=0) / dx / corio_0 * g
V[-1, :, t] = V[-2, :, t] # Fill in the last column
U[:, :-1, t] = np.diff(P[:, :, t], axis=1) / dy / corio_0 * g
U[:, -1, t] = U[:, -2, t] # Fill in the last row
return Grid.from_data(U, lon, lat, V, lon, lat, depth, time, field_data={"P": P})
示例9: grid
def grid(xdim=20, ydim=20):
""" Standard unit mesh grid """
lon = np.linspace(0., 1., xdim, dtype=np.float32)
lat = np.linspace(0., 1., ydim, dtype=np.float32)
U, V = np.meshgrid(lat, lon)
return Grid.from_data(np.array(U, dtype=np.float32), lon, lat,
np.array(V, dtype=np.float32), lon, lat,
mesh='flat')
示例10: grid
def grid(xdim=100, ydim=100):
U = np.zeros((xdim, ydim), dtype=np.float32)
V = np.zeros((xdim, ydim), dtype=np.float32)
lon = np.linspace(0, 1, xdim, dtype=np.float32)
lat = np.linspace(0, 1, ydim, dtype=np.float32)
depth = np.zeros(1, dtype=np.float32)
time = np.zeros(1, dtype=np.float64)
return Grid.from_data(U, lon, lat, V, lon, lat, depth, time)
示例11: test_add_field
def test_add_field(xdim, ydim, tmpdir, filename="test_add"):
filepath = tmpdir.join(filename)
u, v, lon, lat, depth, time = generate_grid(xdim, ydim)
grid = Grid.from_data(u, lon, lat, v, lon, lat, depth, time)
field = Field("newfld", grid.U.data, grid.U.lon, grid.U.lat)
grid.add_field(field)
assert grid.newfld.data.shape == grid.U.data.shape
grid.write(filepath)
示例12: peninsula_grid
def peninsula_grid(xdim, ydim):
"""Construct a grid encapsulating the flow field around an
idealised peninsula.
:param xdim: Horizontal dimension of the generated grid
:param xdim: Vertical dimension of the generated grid
The original test description can be found in Fig. 2.2.3 in:
North, E. W., Gallego, A., Petitgas, P. (Eds). 2009. Manual of
recommended practices for modelling physical - biological
interactions during fish early life.
ICES Cooperative Research Report No. 295. 111 pp.
http://archimer.ifremer.fr/doc/00157/26792/24888.pdf
Note that the problem is defined on an A-grid while NEMO
normally returns C-grids. However, to avoid accuracy
problems with interpolation from A-grid to C-grid, we
return NetCDF files that are on an A-grid.
"""
# Set NEMO grid variables
depth = np.zeros(1, dtype=np.float32)
time = np.zeros(1, dtype=np.float64)
# Generate the original test setup on A-grid in km
dx = 100. / xdim / 2.
dy = 50. / ydim / 2.
La = np.linspace(dx, 100.-dx, xdim, dtype=np.float32)
Wa = np.linspace(dy, 50.-dy, ydim, dtype=np.float32)
# Define arrays U (zonal), V (meridional), W (vertical) and P (sea
# surface height) all on A-grid
U = np.zeros((xdim, ydim), dtype=np.float32)
V = np.zeros((xdim, ydim), dtype=np.float32)
W = np.zeros((xdim, ydim), dtype=np.float32)
P = np.zeros((xdim, ydim), dtype=np.float32)
u0 = 1
x0 = 50.
R = 0.32 * 50.
# Create the fields
x, y = np.meshgrid(La, Wa, sparse=True, indexing='ij')
P = u0*R**2*y/((x-x0)**2+y**2)-u0*y
U = u0-u0*R**2*((x-x0)**2-y**2)/(((x-x0)**2+y**2)**2)
V = -2*u0*R**2*((x-x0)*y)/(((x-x0)**2+y**2)**2)
# Set land points to NaN
I = P >= 0.
U[I] = np.nan
V[I] = np.nan
W[I] = np.nan
# Convert from km to lat/lon
lon = La / 1.852 / 60.
lat = Wa / 1.852 / 60.
return Grid.from_data(U, lon, lat, V, lon, lat, depth, time, field_data={'P': P})
示例13: test_peninsula_file
def test_peninsula_file(gridfile, mode):
"""Open grid files and execute"""
grid = Grid.from_nemo(gridfile, extra_vars={'P': 'P'})
pset = pensinsula_example(grid, 100, mode=mode, degree=1)
# Test advection accuracy by comparing streamline values
err_adv = np.array([abs(p.p_start - p.p) for p in pset])
assert(err_adv <= 1.e-3).all()
# Test grid sampling accuracy by comparing kernel against grid sampling
err_smpl = np.array([abs(p.p - pset.grid.P[0., p.lon, p.lat]) for p in pset])
assert(err_smpl <= 1.e-3).all()
示例14: test_grid_from_data
def test_grid_from_data(xdim, ydim):
""" Simple test for grid initialisation from data. """
u, v, lon, lat, depth, time = generate_grid(xdim, ydim)
grid = Grid.from_data(u, lon, lat, v, lon, lat, depth, time)
u_t = np.transpose(u).reshape((lat.size, lon.size))
v_t = np.transpose(v).reshape((lat.size, lon.size))
assert len(grid.U.data.shape) == 3 # Will be 4 once we use depth
assert len(grid.V.data.shape) == 3
assert np.allclose(grid.U.data[0, :], u_t, rtol=1e-12)
assert np.allclose(grid.V.data[0, :], v_t, rtol=1e-12)
示例15: grid
def grid(xdim=200, ydim=100):
""" Standard grid spanning the earth's coordinates with U and V
equivalent to longitude and latitude in deg.
"""
lon = np.linspace(-180, 180, xdim, dtype=np.float32)
lat = np.linspace(-90, 90, ydim, dtype=np.float32)
U, V = np.meshgrid(lat, lon)
return Grid.from_data(np.array(U, dtype=np.float32), lon, lat,
np.array(V, dtype=np.float32), lon, lat,
mesh='flat')