1+ ---
2+ file_format : mystnb
3+ kernelspec :
4+ name : python3
5+ ---
6+
17# Key Functionality
28
3- ``` {jupyter-execute}
9+ ``` {code-cell} ipython3
410import xarray as xr
511import sdf_xarray as sdfxr
612import matplotlib.pyplot as plt
@@ -10,15 +16,15 @@ import matplotlib.pyplot as plt
1016
1117### Loading single files
1218
13- ``` {jupyter-execute}
19+ ``` {code-cell} ipython3
1420sdfxr.open_dataset("tutorial_dataset_1d/0010.sdf")
1521```
1622
1723You can also load the data in as a < inv:#xarray.DataTree > , which organises the data
1824hierarchically into ` groups ` (for example grouping related quantities such as the individual
1925components of the electric and magnetic fields) while keeping each item as a < inv:#xarray.Dataset > .
2026
21- ``` {jupyter-execute}
27+ ``` {code-cell} ipython3
2228sdfxr.open_datatree("tutorial_dataset_1d/0010.sdf")
2329```
2430
@@ -29,7 +35,7 @@ sdfxr.open_datatree("tutorial_dataset_1d/0010.sdf")
2935If you wish to load data directly from the ` SDF.C ` library and ignore
3036the < inv:#xarray > interface layer.
3137
32- ``` {jupyter-execute}
38+ ``` {code-cell} ipython3
3339raw_ds = sdfxr.SDFFile("tutorial_dataset_1d/0010.sdf")
3440raw_ds.variables.keys()
3541```
@@ -46,7 +52,7 @@ to a nan value. To remove these nan values we suggest using the <inv:#xarray.Dat
4652function or following our implmentation in [](#loading-sparse-data).
4753```
4854
49- ``` {jupyter-execute}
55+ ``` {code-cell} ipython3
5056sdfxr.open_mfdataset("tutorial_dataset_1d/*.sdf")
5157```
5258
@@ -55,7 +61,7 @@ all the files we have do some processing of the data so that we can correctly al
5561the time dimension; This is done via the ` preprocess ` parameter utilising the
5662< project:#sdf_xarray.SDFPreprocess > function.
5763
58- ``` {jupyter-execute}
64+ ``` {code-cell} ipython3
5965xr.open_mfdataset(
6066 "tutorial_dataset_1d/*.sdf",
6167 join="outer",
@@ -68,7 +74,7 @@ You can also load the data in as a <inv:#xarray.DataTree>, which organises the d
6874hierarchically into ` groups ` (for example grouping related quantities such as the individual
6975components of the electric and magnetic fields) while keeping each item as a < inv:#xarray.Dataset > .
7076
71- ``` {jupyter-execute}
77+ ``` {code-cell} ipython3
7278sdfxr.open_mfdatatree("tutorial_dataset_1d/*.sdf")
7379```
7480
@@ -82,7 +88,7 @@ need for a single, large time dimension that would be filled with nan values. Th
8288significantly reduces memory consumption, though it requires more deliberate handling
8389if you need to compare variables that exist on these different time coordinates.
8490
85- ``` {jupyter-execute}
91+ ``` {code-cell} ipython3
8692sdfxr.open_mfdataset("tutorial_dataset_1d/*.sdf", separate_times=True)
8793```
8894
@@ -109,7 +115,7 @@ Pass `keep_particles=True` as a keyword argument to
109115< inv:#xarray.open_dataset > (for single files) or < inv:#xarray.open_mfdataset > (for
110116multiple files).
111117
112- ``` {jupyter-execute}
118+ ``` {code-cell} ipython3
113119sdfxr.open_dataset("tutorial_dataset_1d/0010.sdf", keep_particles=True)
114120```
115121
@@ -125,7 +131,7 @@ variables. This creates a dataset consisting only of the given variable(s)
125131and the relevant coordinates/dimensions, significantly reducing memory
126132consumption.
127133
128- ``` {jupyter-execute}
134+ ``` {code-cell} ipython3
129135sdfxr.open_mfdataset("tutorial_dataset_1d/*.sdf", data_vars=["Electric_Field_Ex"])
130136```
131137
@@ -154,8 +160,9 @@ There are a few ways you can load an input deck:
154160
155161An example of loading a deck can be seen below
156162
157- ```` {toggle}
158- ```{jupyter-execute}
163+ ``` {code-cell} ipython3
164+ :tags: [hide-output]
165+
159166import json
160167from IPython.display import Code
161168
@@ -167,7 +174,6 @@ deck = ds.attrs["deck"]
167174json_str = json.dumps(deck, indent=4)
168175Code(json_str, language='json')
169176```
170- ````
171177
172178## Data interaction examples
173179
@@ -185,7 +191,7 @@ It is important to note here that <inv:#xarray> lazily loads the data
185191meaning that it only explicitly loads the results your currently
186192looking at when you call ` .values `
187193
188- ``` {jupyter-execute}
194+ ``` {code-cell} ipython3
189195ds = sdfxr.open_mfdataset("tutorial_dataset_1d/*.sdf")
190196
191197ds["Electric_Field_Ex"]
@@ -197,7 +203,7 @@ using the built-in <inv:#xarray.DataArray.plot> function (see
197203a simple call to ` matplotlib ` . This also means that you can access
198204all the methods from ` matplotlib ` to manipulate your plot.
199205
200- ``` {jupyter-execute}
206+ ``` {code-cell} ipython3
201207# This is discretized in both space and time
202208ds["Electric_Field_Ex"].plot()
203209plt.title("Electric field along the x-axis")
@@ -210,7 +216,7 @@ This dimension represents all the recorded simulation steps and allows
210216for easy indexing. To quickly determine the number of time steps available,
211217you can check the size of the time dimension.
212218
213- ``` {jupyter-execute}
219+ ``` {code-cell} ipython3
214220# This corresponds to the number of individual SDF files loaded
215221print(f"There are a total of {ds['time'].size} time steps")
216222
@@ -224,7 +230,7 @@ index of the time step with the <inv:#xarray.Dataset.isel> function. This can be
224230done by passsing the index to the ` time ` parameter (e.g., ` time=0 ` for
225231the first snapshot).
226232
227- ``` {jupyter-execute}
233+ ``` {code-cell} ipython3
228234# We can plot the variable at a given time index
229235ds["Electric_Field_Ex"].isel(time=20)
230236```
@@ -237,7 +243,7 @@ If you know roughly what time you wish to select but not the exact value
237243you can use the parameter `method="nearest"`.
238244```
239245
240- ``` {jupyter-execute}
246+ ``` {code-cell} ipython3
241247ds["Electric_Field_Ex"].sel(time=sim_time)
242248```
243249
@@ -280,7 +286,7 @@ Below is an example gif of how this interfacing looks as seen on
280286These datasets can also be easily manipulated the same way as you
281287would with ` numpy ` arrays.
282288
283- ``` {jupyter-execute}
289+ ``` {code-cell} ipython3
284290ds["Laser_Absorption_Fraction_in_Simulation"] = (
285291 (ds["Total_Particle_Energy_in_Simulation"] - ds["Total_Particle_Energy_in_Simulation"][0])
286292 / ds["Absorption_Total_Laser_Energy_Injected"]
@@ -298,15 +304,15 @@ plt.show()
298304You can also call the ` plot() ` function on several variables with
299305labels by delaying the call to ` plt.show() ` .
300306
301- ``` {jupyter-execute}
307+ ``` {code-cell} ipython3
302308ds["Total_Particle_Energy_Electron"].plot(label="Electron")
303309ds["Total_Particle_Energy_Ion"].plot(label="Ion")
304310plt.title("Particle Energy in Simulation per Species")
305311plt.legend()
306312plt.show()
307313```
308314
309- ``` {jupyter-execute}
315+ ``` {code-cell} ipython3
310316print(f"Total laser energy injected: {ds["Absorption_Total_Laser_Energy_Injected"][-1].values:.1e} J")
311317print(f"Total particle energy absorbed: {ds["Total_Particle_Energy_in_Simulation"][-1].values:.1e} J")
312318print(f"The laser absorption fraction: {ds["Laser_Absorption_Fraction_in_Simulation"][-1].values:.1f} %")
0 commit comments