Skip to content

Commit 77ff344

Browse files
committed
coarser resolution in FSLE example
1 parent 0930741 commit 77ff344

File tree

2 files changed

+54
-17
lines changed

2 files changed

+54
-17
lines changed

examples/07_cube_manipulation/pet_fsle_med.py

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
FSLE experiment in med
33
======================
44
5-
Example to build FSLE, parameter values must be adapted for your case.
5+
Example to build Finite Size Lyapunov Exponents, parameter values must be adapted for your case.
66
77
Example use a method similar to `AVISO flse`_
88
@@ -25,7 +25,7 @@
2525

2626
# %%
2727
# ADT in med
28-
# ---------------------
28+
# ----------
2929
c = GridCollection.from_netcdf_cube(
3030
get_path("dt_med_allsat_phy_l4_2005T2.nc"),
3131
"longitude",
@@ -36,7 +36,7 @@
3636

3737

3838
# %%
39-
# Methods to compute fsle
39+
# Methods to compute FSLE
4040
# -----------------------
4141
@njit(cache=True, fastmath=True)
4242
def check_p(x, y, g, m, dt, dist_init=0.02, dist_max=0.6):
@@ -86,13 +86,33 @@ def build_triplet(x, y, step=0.02):
8686
return x_, y_
8787

8888

89+
# %%
90+
# Settings
91+
# --------
92+
93+
# Step in degrees for ouput
94+
step_grid_out = 1 / 25.0
95+
# Initial separation in degrees
96+
dist_init = 1 / 50.0
97+
# Final separation in degrees
98+
dist_max = 1 / 5.0
99+
# Time of start
100+
t0 = 20268
101+
# Number of time step by days
102+
time_step_by_days = 5
103+
# Maximal time of advection
104+
# Here we limit because our data cube cover only 3 month
105+
nb_days = 85
106+
# Backward or forward
107+
backward = True
108+
89109
# %%
90110
# Particles
91111
# ---------
92-
step = 0.02
93-
t0 = 20268
94112
x0_, y0_ = -5, 30
95-
lon_p, lat_p = arange(x0_, x0_ + 43, step), arange(y0_, y0_ + 16, step)
113+
lon_p, lat_p = arange(x0_, x0_ + 43, step_grid_out), arange(
114+
y0_, y0_ + 16, step_grid_out
115+
)
96116
x0, y0 = meshgrid(lon_p, lat_p)
97117
grid_shape = x0.shape
98118
x0, y0 = x0.reshape(-1), y0.reshape(-1)
@@ -103,26 +123,25 @@ def build_triplet(x, y, step=0.02):
103123
# %%
104124
# FSLE
105125
# ----
106-
time_step_by_days = 5
126+
107127
# Array to compute fsle
108128
fsle = zeros(x0.shape[0], dtype="f4")
109-
x, y = build_triplet(x0, y0)
129+
x, y = build_triplet(x0, y0, dist_init)
110130
used = zeros(x.shape[0], dtype="bool")
111131

112132
# advection generator
113-
kw = dict(t_init=t0, nb_step=1, backward=True, mask_particule=used)
133+
kw = dict(t_init=t0, nb_step=1, backward=backward, mask_particule=used)
114134
p = c.advect(x, y, "u", "v", time_step=86400 / time_step_by_days, **kw)
115135

116-
nb_days = 85
117136
# We check at each step of advection if particle distance is over `dist_max`
118137
for i in range(time_step_by_days * nb_days):
119138
t, xt, yt = p.__next__()
120139
dt = t / 86400.0 - t0
121-
check_p(xt, yt, fsle, used, dt, dist_max=0.2, dist_init=step)
140+
check_p(xt, yt, fsle, used, dt, dist_max=dist_max, dist_init=dist_init)
122141

123142
# Get index with original_position
124-
i = ((x0 - x0_) / step).astype("i4")
125-
j = ((y0 - y0_) / step).astype("i4")
143+
i = ((x0 - x0_) / step_grid_out).astype("i4")
144+
j = ((y0 - y0_) / step_grid_out).astype("i4")
126145
fsle_ = empty(grid_shape, dtype="f4")
127146
used_ = zeros(grid_shape, dtype="bool")
128147
fsle_[j, i] = fsle

notebooks/python_module/07_cube_manipulation/pet_fsle_med.ipynb

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"cell_type": "markdown",
1616
"metadata": {},
1717
"source": [
18-
"\n# FSLE experiment in med\n\nExample to build FSLE, parameter values must be adapted for your case.\n\nExample use a method similar to `AVISO flse`_\n\n https://www.aviso.altimetry.fr/en/data/products/value-added-products/\n fsle-finite-size-lyapunov-exponents/fsle-description.html\n"
18+
"\n# FSLE experiment in med\n\nExample to build Finite Size Lyapunov Exponents, parameter values must be adapted for your case.\n\nExample use a method similar to `AVISO flse`_\n\n https://www.aviso.altimetry.fr/en/data/products/value-added-products/\n fsle-finite-size-lyapunov-exponents/fsle-description.html\n"
1919
]
2020
},
2121
{
@@ -51,7 +51,7 @@
5151
"cell_type": "markdown",
5252
"metadata": {},
5353
"source": [
54-
"## Methods to compute fsle\n\n"
54+
"## Methods to compute FSLE\n\n"
5555
]
5656
},
5757
{
@@ -65,6 +65,24 @@
6565
"@njit(cache=True, fastmath=True)\ndef check_p(x, y, g, m, dt, dist_init=0.02, dist_max=0.6):\n \"\"\"\n Check if distance between eastern or northern particle to center particle is bigger than `dist_max`\n \"\"\"\n nb_p = x.shape[0] // 3\n delta = dist_max ** 2\n for i in range(nb_p):\n i0 = i * 3\n i_n = i0 + 1\n i_e = i0 + 2\n # If particle already set, we skip\n if m[i0] or m[i_n] or m[i_e]:\n continue\n # Distance with north\n dxn, dyn = x[i0] - x[i_n], y[i0] - y[i_n]\n dn = dxn ** 2 + dyn ** 2\n # Distance with east\n dxe, dye = x[i0] - x[i_e], y[i0] - y[i_e]\n de = dxe ** 2 + dye ** 2\n\n if dn >= delta or de >= delta:\n s1 = dxe ** 2 + dxn ** 2 + dye ** 2 + dyn ** 2\n s2 = ((dxn + dye) ** 2 + (dxe - dyn) ** 2) * (\n (dxn - dye) ** 2 + (dxe + dyn) ** 2\n )\n g[i] = 1 / (2 * dt) * log2(1 / (2 * dist_init ** 2) * (s1 + s2 ** 0.5))\n m[i0], m[i_n], m[i_e] = True, True, True\n\n\n@njit(cache=True)\ndef build_triplet(x, y, step=0.02):\n \"\"\"\n Triplet building for each position we add east and north point with defined step\n \"\"\"\n nb_x = x.shape[0]\n x_ = empty(nb_x * 3, dtype=x.dtype)\n y_ = empty(nb_x * 3, dtype=y.dtype)\n for i in range(nb_x):\n i0 = i * 3\n i_n, i_e = i0 + 1, i0 + 2\n x__, y__ = x[i], y[i]\n x_[i0], y_[i0] = x__, y__\n x_[i_n], y_[i_n] = x__, y__ + step\n x_[i_e], y_[i_e] = x__ + step, y__\n return x_, y_"
6666
]
6767
},
68+
{
69+
"cell_type": "markdown",
70+
"metadata": {},
71+
"source": [
72+
"## Settings\n\n"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": null,
78+
"metadata": {
79+
"collapsed": false
80+
},
81+
"outputs": [],
82+
"source": [
83+
"# Step in degrees for ouput\nstep_grid_out = 1/25.\n# Initial separation in degrees\ndist_init = 1/50.\n# Final separation in degrees\ndist_max = 1/5.\n# Time of start\nt0 = 20268\n# Number of time step by days\ntime_step_by_days = 5\n# Maximal time of advection\n# Here we limit because our data cube cover only 3 month\nnb_days = 85\n# Backward or forward\nbackward=True"
84+
]
85+
},
6886
{
6987
"cell_type": "markdown",
7088
"metadata": {},
@@ -80,7 +98,7 @@
8098
},
8199
"outputs": [],
82100
"source": [
83-
"step = 0.02\nt0 = 20268\nx0_, y0_ = -5, 30\nlon_p, lat_p = arange(x0_, x0_ + 43, step), arange(y0_, y0_ + 16, step)\nx0, y0 = meshgrid(lon_p, lat_p)\ngrid_shape = x0.shape\nx0, y0 = x0.reshape(-1), y0.reshape(-1)\n# Identify all particle not on land\nm = ~isnan(c[t0].interp(\"adt\", x0, y0))\nx0, y0 = x0[m], y0[m]"
101+
"x0_, y0_ = -5, 30\nlon_p, lat_p = arange(x0_, x0_ + 43, step_grid_out), arange(y0_, y0_ + 16, step_grid_out)\nx0, y0 = meshgrid(lon_p, lat_p)\ngrid_shape = x0.shape\nx0, y0 = x0.reshape(-1), y0.reshape(-1)\n# Identify all particle not on land\nm = ~isnan(c[t0].interp(\"adt\", x0, y0))\nx0, y0 = x0[m], y0[m]"
84102
]
85103
},
86104
{
@@ -98,7 +116,7 @@
98116
},
99117
"outputs": [],
100118
"source": [
101-
"time_step_by_days = 5\n# Array to compute fsle\nfsle = zeros(x0.shape[0], dtype=\"f4\")\nx, y = build_triplet(x0, y0)\nused = zeros(x.shape[0], dtype=\"bool\")\n\n# advection generator\nkw = dict(t_init=t0, nb_step=1, backward=True, mask_particule=used)\np = c.advect(x, y, \"u\", \"v\", time_step=86400 / time_step_by_days, **kw)\n\nnb_days = 85\n# We check at each step of advection if particle distance is over `dist_max`\nfor i in range(time_step_by_days * nb_days):\n t, xt, yt = p.__next__()\n dt = t / 86400.0 - t0\n check_p(xt, yt, fsle, used, dt, dist_max=0.2, dist_init=step)\n\n# Get index with original_position\ni = ((x0 - x0_) / step).astype(\"i4\")\nj = ((y0 - y0_) / step).astype(\"i4\")\nfsle_ = empty(grid_shape, dtype=\"f4\")\nused_ = zeros(grid_shape, dtype=\"bool\")\nfsle_[j, i] = fsle\nused_[j, i] = used[::3]\n# Create a grid object\nfsle_custom = RegularGridDataset.with_array(\n coordinates=(\"lon\", \"lat\"),\n datas=dict(\n fsle=ma.array(fsle_.T, mask=~used_.T),\n lon=lon_p,\n lat=lat_p,\n ),\n centered=True,\n)"
119+
"# Array to compute fsle\nfsle = zeros(x0.shape[0], dtype=\"f4\")\nx, y = build_triplet(x0, y0, dist_init)\nused = zeros(x.shape[0], dtype=\"bool\")\n\n# advection generator\nkw = dict(t_init=t0, nb_step=1, backward=backward, mask_particule=used)\np = c.advect(x, y, \"u\", \"v\", time_step=86400 / time_step_by_days, **kw)\n\n# We check at each step of advection if particle distance is over `dist_max`\nfor i in range(time_step_by_days * nb_days):\n t, xt, yt = p.__next__()\n dt = t / 86400.0 - t0\n check_p(xt, yt, fsle, used, dt, dist_max=dist_max, dist_init=dist_init)\n\n# Get index with original_position\ni = ((x0 - x0_) / step_grid_out).astype(\"i4\")\nj = ((y0 - y0_) / step_grid_out).astype(\"i4\")\nfsle_ = empty(grid_shape, dtype=\"f4\")\nused_ = zeros(grid_shape, dtype=\"bool\")\nfsle_[j, i] = fsle\nused_[j, i] = used[::3]\n# Create a grid object\nfsle_custom = RegularGridDataset.with_array(\n coordinates=(\"lon\", \"lat\"),\n datas=dict(\n fsle=ma.array(fsle_.T, mask=~used_.T),\n lon=lon_p,\n lat=lat_p,\n ),\n centered=True,\n)"
102120
]
103121
},
104122
{

0 commit comments

Comments
 (0)