Skip to content

Commit 692a4de

Browse files
Quarto output
1 parent ad9b0ca commit 692a4de

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

notebooks/courses/microwave-remote-sensing/unit_03/09_in_class_exercise.ipynb

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@
3030
"metadata": {},
3131
"outputs": [],
3232
"source": [
33+
"import contextlib\n",
34+
"import os\n",
35+
"\n",
3336
"import cmcrameri as cmc # noqa: F401\n",
3437
"import intake\n",
3538
"import matplotlib.patches as patches\n",
3639
"import matplotlib.pyplot as plt\n",
3740
"import numpy as np\n",
3841
"import seaborn as sns\n",
3942
"import snaphu\n",
40-
"import xarray as xr\n",
41-
"from IPython.display import clear_output"
43+
"import xarray as xr"
4244
]
4345
},
4446
{
@@ -120,6 +122,24 @@
120122
"metadata": {},
121123
"outputs": [],
122124
"source": [
125+
"@contextlib.contextmanager\n",
126+
"def suppress_output():\n",
127+
" with open(os.devnull, \"w\") as devnull:\n",
128+
" old_stdout = os.dup(1)\n",
129+
" old_stderr = os.dup(2)\n",
130+
"\n",
131+
" os.dup2(devnull.fileno(), 1)\n",
132+
" os.dup2(devnull.fileno(), 2)\n",
133+
"\n",
134+
" try:\n",
135+
" yield\n",
136+
" finally:\n",
137+
" os.dup2(old_stdout, 1)\n",
138+
" os.dup2(old_stderr, 2)\n",
139+
" os.close(old_stdout)\n",
140+
" os.close(old_stderr)\n",
141+
"\n",
142+
"\n",
123143
"def unwrap_array(\n",
124144
" data: xr.DataArray,\n",
125145
" complex_var: str = \"cmplx\",\n",
@@ -176,18 +196,16 @@
176196
" coherence = np.ones_like(data_arr.real)\n",
177197
"\n",
178198
" # Unwrap the phase (already in complex form)\n",
179-
" unw, _ = snaphu.unwrap(\n",
180-
" data_arr,\n",
181-
" coherence,\n",
182-
" nlooks=nlooks,\n",
183-
" cost=cost,\n",
184-
" init=init,\n",
185-
" mask=mask,\n",
186-
" **kwargs,\n",
187-
" )\n",
188-
"\n",
189-
" # Clear the output to avoid printing the snaphu output\n",
190-
" clear_output()\n",
199+
" with suppress_output():\n",
200+
" unw, _ = snaphu.unwrap(\n",
201+
" data_arr,\n",
202+
" coherence,\n",
203+
" nlooks=nlooks,\n",
204+
" cost=cost,\n",
205+
" init=init,\n",
206+
" mask=mask,\n",
207+
" **kwargs,\n",
208+
" )\n",
191209
"\n",
192210
" # Build xarray DataArray with the unwrapped phase\n",
193211
" # unw_da = xr.DataArray(unw, coords=data.coords, dims=data.dims)\n",

0 commit comments

Comments
 (0)