sdf-xarray 0.3.0__tar.gz → 0.3.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of sdf-xarray might be problematic. Click here for more details.

Files changed (130) hide show
  1. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/PKG-INFO +1 -1
  2. sdf_xarray-0.3.1/docs/tutorial_dataset_2d/0000.sdf +0 -0
  3. sdf_xarray-0.3.1/docs/tutorial_dataset_2d/0001.sdf +0 -0
  4. sdf_xarray-0.3.1/docs/tutorial_dataset_2d/0002.sdf +0 -0
  5. sdf_xarray-0.3.1/docs/tutorial_dataset_2d/0003.sdf +0 -0
  6. sdf_xarray-0.3.1/docs/tutorial_dataset_2d/0004.sdf +0 -0
  7. sdf_xarray-0.3.1/docs/tutorial_dataset_2d/0005.sdf +0 -0
  8. sdf_xarray-0.3.1/docs/tutorial_dataset_2d/input.deck +65 -0
  9. sdf_xarray-0.3.1/docs/unit_conversion.rst +228 -0
  10. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/src/sdf_xarray/__init__.py +9 -2
  11. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/src/sdf_xarray/_version.py +3 -3
  12. sdf_xarray-0.3.1/src/sdf_xarray/dataset_accessor.py +73 -0
  13. sdf_xarray-0.3.1/tests/example_files_3D/0000.sdf +0 -0
  14. sdf_xarray-0.3.1/tests/example_files_3D/0001.sdf +0 -0
  15. sdf_xarray-0.3.1/tests/example_files_3D/input.deck +54 -0
  16. sdf_xarray-0.3.1/tests/test_epoch_dataset_accessor.py +146 -0
  17. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/uv.lock +481 -476
  18. sdf_xarray-0.3.0/docs/unit_conversion.rst +0 -175
  19. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/.github/workflows/black.yml +0 -0
  20. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/.github/workflows/build_publish.yml +0 -0
  21. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/.github/workflows/lint.yml +0 -0
  22. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/.github/workflows/tests.yml +0 -0
  23. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/.gitignore +0 -0
  24. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/.gitmodules +0 -0
  25. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/.readthedocs.yaml +0 -0
  26. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/BEAM.png +0 -0
  27. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/CITATION.cff +0 -0
  28. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/CMakeLists.txt +0 -0
  29. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/CONTRIBUTING.md +0 -0
  30. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/LICENCE +0 -0
  31. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/PlasmaFAIR.svg +0 -0
  32. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/README.md +0 -0
  33. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/.gitignore +0 -0
  34. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/_templates/custom-class-template.rst +0 -0
  35. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/_templates/custom-module-template.rst +0 -0
  36. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/api.rst +0 -0
  37. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/conf.py +0 -0
  38. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/contributing.rst +0 -0
  39. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/getting_started.rst +0 -0
  40. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/index.rst +0 -0
  41. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/key_functionality.rst +0 -0
  42. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/known_issues.rst +0 -0
  43. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/make.bat +0 -0
  44. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0000.sdf +0 -0
  45. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0001.sdf +0 -0
  46. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0002.sdf +0 -0
  47. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0003.sdf +0 -0
  48. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0004.sdf +0 -0
  49. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0005.sdf +0 -0
  50. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0006.sdf +0 -0
  51. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0007.sdf +0 -0
  52. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0008.sdf +0 -0
  53. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0009.sdf +0 -0
  54. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0010.sdf +0 -0
  55. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0011.sdf +0 -0
  56. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0012.sdf +0 -0
  57. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0013.sdf +0 -0
  58. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0014.sdf +0 -0
  59. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0015.sdf +0 -0
  60. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0016.sdf +0 -0
  61. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0017.sdf +0 -0
  62. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0018.sdf +0 -0
  63. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0019.sdf +0 -0
  64. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0020.sdf +0 -0
  65. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0021.sdf +0 -0
  66. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0022.sdf +0 -0
  67. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0023.sdf +0 -0
  68. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0024.sdf +0 -0
  69. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0025.sdf +0 -0
  70. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0026.sdf +0 -0
  71. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0027.sdf +0 -0
  72. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0028.sdf +0 -0
  73. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0029.sdf +0 -0
  74. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0030.sdf +0 -0
  75. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0031.sdf +0 -0
  76. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0032.sdf +0 -0
  77. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0033.sdf +0 -0
  78. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0034.sdf +0 -0
  79. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0035.sdf +0 -0
  80. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0036.sdf +0 -0
  81. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0037.sdf +0 -0
  82. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0038.sdf +0 -0
  83. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0039.sdf +0 -0
  84. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/0040.sdf +0 -0
  85. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/deck.status +0 -0
  86. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/epoch1d.dat +0 -0
  87. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/input.deck +0 -0
  88. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/normal.visit +0 -0
  89. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/docs/tutorial_dataset_1d/restart.visit +0 -0
  90. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/pyproject.toml +0 -0
  91. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/src/sdf_xarray/csdf.pxd +0 -0
  92. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/src/sdf_xarray/plotting.py +0 -0
  93. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/src/sdf_xarray/sdf_interface.pyx +0 -0
  94. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_array_no_grids/0000.sdf +0 -0
  95. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_array_no_grids/0001.sdf +0 -0
  96. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_array_no_grids/README.md +0 -0
  97. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_array_no_grids/input.deck +0 -0
  98. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_dist_fn/0000.sdf +0 -0
  99. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_dist_fn/0001.sdf +0 -0
  100. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_dist_fn/0002.sdf +0 -0
  101. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_dist_fn/input.deck +0 -0
  102. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_files_1D/0000.sdf +0 -0
  103. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_files_1D/0001.sdf +0 -0
  104. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_files_1D/0002.sdf +0 -0
  105. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_files_1D/0003.sdf +0 -0
  106. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_files_1D/0004.sdf +0 -0
  107. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_files_1D/0005.sdf +0 -0
  108. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_files_1D/0006.sdf +0 -0
  109. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_files_1D/0007.sdf +0 -0
  110. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_files_1D/0008.sdf +0 -0
  111. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_files_1D/0009.sdf +0 -0
  112. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_files_1D/0010.sdf +0 -0
  113. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_files_1D/README.md +0 -0
  114. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_files_1D/input.deck +0 -0
  115. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_files_2D_moving_window/0000.sdf +0 -0
  116. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_files_2D_moving_window/0001.sdf +0 -0
  117. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_files_2D_moving_window/0002.sdf +0 -0
  118. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_files_2D_moving_window/0003.sdf +0 -0
  119. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_files_2D_moving_window/0004.sdf +0 -0
  120. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_files_2D_moving_window/input.deck +0 -0
  121. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_mismatched_files/0000.sdf +0 -0
  122. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_mismatched_files/0001.sdf +0 -0
  123. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_mismatched_files/0002.sdf +0 -0
  124. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_two_probes_2D/0000.sdf +0 -0
  125. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_two_probes_2D/0001.sdf +0 -0
  126. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_two_probes_2D/0002.sdf +0 -0
  127. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/example_two_probes_2D/input.deck +0 -0
  128. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/test_basic.py +0 -0
  129. {sdf_xarray-0.3.0 → sdf_xarray-0.3.1}/tests/test_cython.py +0 -0
  130. /sdf_xarray-0.3.0/tests/test_epoch_accessor.py → /sdf_xarray-0.3.1/tests/test_epoch_dataarray_accessor.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sdf-xarray
3
- Version: 0.3.0
3
+ Version: 0.3.1
4
4
  Summary: Provides a backend for xarray to read SDF files as created by the EPOCH plasma PIC code.
5
5
  Author-Email: Peter Hill <peter.hill@york.ac.uk>, Joel Adams <joel.adams@york.ac.uk>, Shaun Doherty <shaun.doherty@york.ac.uk>
6
6
  License-Expression: BSD-3-Clause
@@ -0,0 +1,65 @@
1
+ begin:control
2
+ nx = 64
3
+ ny = 64
4
+
5
+ # Final time of simulation
6
+ t_end = 5 * femto
7
+
8
+ # Size of domain
9
+ x_min = 0
10
+ x_max = 6 * micron
11
+
12
+ y_min = 0
13
+ y_max = 6 * micron
14
+
15
+ stdout_frequency = 1
16
+ nparticles = nx * ny * 50
17
+ end:control
18
+
19
+ begin:constant
20
+ n_elec = 1000
21
+ L_target_x = 2 * micron
22
+ L_target_y = 4 * micron
23
+
24
+ x_center = (x_min + x_max) / 2
25
+ y_center = (y_min + y_max) / 2
26
+
27
+ density_profile = if( (abs(x - x_center) lt L_target_x/2), if( (abs(y - y_center) lt L_target_y/2), 1, 0), 0)
28
+ end:constant
29
+
30
+ begin:boundaries
31
+ bc_x_min = periodic
32
+ bc_x_max = periodic
33
+ bc_y_min = periodic
34
+ bc_y_max = periodic
35
+ end:boundaries
36
+
37
+
38
+ begin:species
39
+ name = Electron
40
+ frac = 0.5
41
+ number_density = n_elec * density_profile
42
+ identify:electron
43
+ end:species
44
+
45
+ begin:species
46
+ name = Ion
47
+ frac = 0.5
48
+ number_density = n_elec * density_profile
49
+ identify:proton
50
+ end:species
51
+
52
+ begin:output_global
53
+ force_last_to_be_restartable = F
54
+ end:output_global
55
+
56
+ begin:output
57
+ name = normal
58
+
59
+ dt_snapshot = 1 * femto
60
+
61
+ grid = always
62
+ number_density = always + species
63
+
64
+ end:output
65
+
@@ -0,0 +1,228 @@
1
+ .. _sec-unit-conversion:
2
+
3
+ ===============
4
+ Unit Conversion
5
+ ===============
6
+
7
+ The ``sdf-xarray`` package automatically extracts the units for each
8
+ coordinate/variable/constant from an SDF file and stores them as an :class:`xarray.Dataset`
9
+ attribute called ``"units"``. Sometimes we want to convert our data from one format to
10
+ another, e.g. converting the grid coordinates from meters to microns, time from seconds
11
+ to femto-seconds or particle energy from Joules to electron-volts.
12
+
13
+ .. ipython:: python
14
+
15
+ from sdf_xarray import open_mfdataset
16
+ import matplotlib.pyplot as plt
17
+ plt.rcParams.update({
18
+ "axes.labelsize": 16,
19
+ "xtick.labelsize": 14,
20
+ "ytick.labelsize": 14,
21
+ "axes.titlesize": 16
22
+ })
23
+
24
+
25
+ =====================
26
+ Rescaling Coordinates
27
+ =====================
28
+
29
+ For simple scaling and unit relabeling of coordinates (e.g., converting meters to microns),
30
+ the most straightforward approach is to use the ``rescale_coords()`` method
31
+ via the custom ``xarray.Dataset.epoch`` dataset accessor.
32
+
33
+ This method scales the coordinate values by a given multiplier and updates the ``"units"``
34
+ attribute in one step.
35
+
36
+ Rescaling Grid Coordinates
37
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
38
+
39
+ We can use the ``xarray.Dataset.epoch.rescale_coords()`` method to convert X, Y, and Z
40
+ coordinates from meters (m) to microns (µm) by applying a multiplier of ``1e6``.
41
+
42
+ .. ipython:: python
43
+
44
+ fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 6))
45
+
46
+ with open_mfdataset("tutorial_dataset_2d/*.sdf") as ds:
47
+ ds_in_microns = ds.epoch.rescale_coords(
48
+ multiplier=1e6,
49
+ unit_label="µm",
50
+ coord_names=["X_Grid_mid", "Y_Grid_mid"]
51
+ )
52
+ derived_number_density = ds["Derived_Number_Density_Electron"].isel(time=0).compute()
53
+ derived_number_density_microns = ds_in_microns["Derived_Number_Density_Electron"].isel(time=0).compute()
54
+
55
+ derived_number_density.plot(ax=ax1, x="X_Grid_mid", y="Y_Grid_mid")
56
+ ax1.set_title("Original X Coordinate (m)")
57
+
58
+ derived_number_density_microns.plot(ax=ax2, x="X_Grid_mid", y="Y_Grid_mid")
59
+ ax2.set_title("Rescaled X Coordinate (µm)")
60
+
61
+ @savefig coordinate_conversion.png width=9in
62
+ fig.tight_layout()
63
+
64
+
65
+ Rescaling Time Coordinate
66
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
67
+
68
+ We can also use the ``xarray.Dataset.epoch.rescale_coords()`` method to convert the time
69
+ coordinate from seconds (s) to femto-seconds (fs) by applying a multiplier of ``1e15``.
70
+
71
+ .. ipython:: python
72
+
73
+ with open_mfdataset("tutorial_dataset_2d/*.sdf") as ds:
74
+ ds_time_in_femto = ds.epoch.rescale_coords(
75
+ multiplier=1e15,
76
+ unit_label="fs",
77
+ coord_names="time"
78
+ )
79
+
80
+ print(f"[Original] units: {ds['time'].attrs['units']}, values: {ds['time'].values}")
81
+ print(f"[Rescaled] units: {ds_time_in_femto['time'].attrs['units']}, values: {ds_time_in_femto['time'].values}")
82
+
83
+
84
+ ================================
85
+ Unit Conversion with pint-xarray
86
+ ================================
87
+
88
+ While this is sufficient for most use cases, we can enhance this functionality
89
+ using the `pint <https://pint.readthedocs.io/en/stable/getting/index.html>`_ library.
90
+ Pint allows us to specify the units of a given array and convert them
91
+ to another, which is incredibly handy. We can take this a step further,
92
+ however, and utilize the `pint-xarray
93
+ <https://pint-xarray.readthedocs.io/en/latest/>`_ library. This library
94
+ allows us to infer units directly from an `xarray.Dataset.attrs` while
95
+ retaining all the information about the `xarray.Dataset`. This works
96
+ very similarly to taking a NumPy array and multiplying it by a constant or
97
+ another array, which returns a new array; however, this library will also
98
+ retain the unit logic (specifically the ``"units"`` information).
99
+
100
+ .. note::
101
+ Unit conversion is not supported on coordinates in ``pint-xarray`` which is due to an
102
+ underlying issue with how ``xarray`` implements indexes.
103
+
104
+ Installation
105
+ ~~~~~~~~~~~~
106
+
107
+ To install the pint libraries you can simply run the following optional
108
+ dependency pip command which will install both the ``pint`` and ``pint-xarray``
109
+ libraries. You can install these optional dependencies via pip:
110
+
111
+ .. code:: console
112
+
113
+ $ pip install "sdf_xarray[pint]"
114
+
115
+ .. note::
116
+ Once you install ``pint-xarray`` it is automatically picked up and loaded
117
+ by the code so you should have access to the ``xarray.Dataset.pint`` accessor.
118
+
119
+ Quantifying Arrays
120
+ ~~~~~~~~~~~~~~~~~~
121
+
122
+ When using ``pint-xarray``, the library attempts to infer units from the
123
+ ``"units"`` attribute on each `xarray.DataArray`. Alternatively, you can
124
+ also specify the units yourself by passing a string into the
125
+ ``xarray.Dataset.DataArray.pint.quantify()`` function call. Once the type is inferred
126
+ the original `xarray.DataArray` will be converted to a `pint.Quantity`
127
+ and the ``"units"`` attribute will
128
+ be removed.
129
+
130
+ In the following example we will extract the time-resolved total particle
131
+ energy of electrons which is measured in Joules and convert it to electron
132
+ volts.
133
+
134
+ .. ipython:: python
135
+
136
+ with open_mfdataset("tutorial_dataset_1d/*.sdf") as ds:
137
+ total_particle_energy = ds["Total_Particle_Energy_Electron"]
138
+
139
+ total_particle_energy
140
+
141
+ total_particle_energy = ds["Total_Particle_Energy_Electron"].pint.quantify()
142
+
143
+ total_particle_energy
144
+
145
+
146
+ Now that this dataset has been converted a `pint.Quantity`, we can check
147
+ it's units and dimensionality
148
+
149
+ .. ipython:: python
150
+
151
+ total_particle_energy.pint.units
152
+ total_particle_energy.pint.dimensionality
153
+
154
+
155
+ Converting Units
156
+ ~~~~~~~~~~~~~~~~
157
+
158
+ We can now convert it to electron volts utilising the `pint.Quantity.to`
159
+ function
160
+
161
+ .. ipython:: python
162
+
163
+ total_particle_energy_ev = total_particle_energy.pint.to("eV")
164
+
165
+ Unit Propagation
166
+ ~~~~~~~~~~~~~~~~
167
+
168
+ Suppose instead of converting to ``"eV"``, we want to convert to ``"W"``
169
+ (watts). To do this, we divide the total particle energy by time. However,
170
+ since coordinates in `xarray.Dataset` cannot be directly converted to
171
+ `pint.Quantity`, we must first extract the coordinate values manually
172
+ and create a new Pint quantity for time.
173
+
174
+ Once both arrays are quantified, Pint will automatically handle the unit
175
+ propagation when we perform arithmetic operations like division.
176
+
177
+ .. note::
178
+ Pint does not automatically simplify ``"J/s"`` to ``"W"``, so we use
179
+ `pint.Quantity.to` to convert the unit string. Since these units are
180
+ the same it will not change the underlying data, only the units. This is
181
+ only a small formatting choice and is not required.
182
+
183
+ .. ipython:: python
184
+
185
+ import pint
186
+ time_values = total_particle_energy.coords["time"].data
187
+ time = pint.Quantity(time_values, "s")
188
+ total_particle_energy_w = total_particle_energy / time
189
+ total_particle_energy_w.pint.units
190
+ total_particle_energy_w = total_particle_energy_w.pint.to("W")
191
+ total_particle_energy_w.pint.units
192
+
193
+ Dequantifying and Restoring Units
194
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
195
+
196
+ .. note::
197
+ If this function is not called prior to plotting then the ``units`` will be
198
+ inferred from the `pint.Quantity` array which will return the long
199
+ name of the units. i.e. instead of returning ``"eV"`` it will return
200
+ ``"electron_volt"``.
201
+
202
+ The ``xarray.Dataset.DataArray.pint.dequantify`` function converts the data from
203
+ `pint.Quantity` back to the original `xarray.DataArray` and adds
204
+ the ``"units"`` attribute back in. It also has an optional ``format`` parameter
205
+ that allows you to specify the formatting type of ``"units"`` attribute. We
206
+ have used the ``format="~P"`` option as it shortens the unit to its
207
+ "short pretty" format (``"eV"``). For more options, see the `Pint formatting
208
+ documentation <https://pint.readthedocs.io/en/stable/user/formatting.html>`_.
209
+
210
+ .. ipython:: python
211
+
212
+ total_particle_energy_ev = total_particle_energy_ev.pint.dequantify(format="~P")
213
+ total_particle_energy_w = total_particle_energy_w.pint.dequantify(format="~P")
214
+ total_particle_energy_ev
215
+
216
+ To confirm the conversion has worked correctly, we can plot the original and
217
+ converted `xarray.Dataset` side by side:
218
+
219
+ .. ipython:: python
220
+
221
+ fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(16,8))
222
+ ds["Total_Particle_Energy_Electron"].plot(ax=ax1)
223
+ total_particle_energy_ev.plot(ax=ax2)
224
+ total_particle_energy_w.plot(ax=ax3)
225
+ ax4.set_visible(False)
226
+ fig.suptitle("Comparison of conversion from Joules to electron volts and watts", fontsize="18")
227
+ @savefig unit_conversion.png width=9in
228
+ fig.tight_layout()
@@ -1,3 +1,4 @@
1
+ import contextlib
1
2
  import os
2
3
  import re
3
4
  from collections import Counter, defaultdict
@@ -18,10 +19,16 @@ from xarray.core import indexing
18
19
  from xarray.core.utils import close_on_error, try_read_magic_number_from_path
19
20
  from xarray.core.variable import Variable
20
21
 
21
- # NOTE: Do not delete this line, otherwise the "epoch" accessor will not be
22
- # imported when the user imports sdf_xarray
22
+ # NOTE: Do not delete these lines, otherwise the "epoch" dataset and dataarray
23
+ # accessors will not be imported when the user imports sdf_xarray
24
+ import sdf_xarray.dataset_accessor
23
25
  import sdf_xarray.plotting # noqa: F401
24
26
 
27
+ # NOTE: This attempts to initialise with the "pint" accessor if the user
28
+ # has installed the package
29
+ with contextlib.suppress(ImportError):
30
+ import pint_xarray # noqa: F401
31
+
25
32
  from .sdf_interface import Constant, SDFFile # type: ignore # noqa: PGH003
26
33
 
27
34
  # TODO Remove this once the new kwarg options are fully implemented
@@ -28,7 +28,7 @@ version_tuple: VERSION_TUPLE
28
28
  commit_id: COMMIT_ID
29
29
  __commit_id__: COMMIT_ID
30
30
 
31
- __version__ = version = '0.3.0'
32
- __version_tuple__ = version_tuple = (0, 3, 0)
31
+ __version__ = version = '0.3.1'
32
+ __version_tuple__ = version_tuple = (0, 3, 1)
33
33
 
34
- __commit_id__ = commit_id = 'gcca942b3f'
34
+ __commit_id__ = commit_id = 'gce5426d4a'
@@ -0,0 +1,73 @@
1
+ from typing import Union
2
+
3
+ import xarray as xr
4
+
5
+
6
+ @xr.register_dataset_accessor("epoch")
7
+ class EpochAccessor:
8
+ def __init__(self, xarray_obj: xr.Dataset):
9
+ # The xarray object is the Dataset, which we store as self._ds
10
+ self._ds = xarray_obj
11
+
12
+ def rescale_coords(
13
+ self,
14
+ multiplier: float,
15
+ unit_label: str,
16
+ coord_names: Union[str, list[str]],
17
+ ) -> xr.Dataset:
18
+ """
19
+ Rescales specified X and Y coordinates in the Dataset by a given multiplier
20
+ and updates the unit label attribute.
21
+
22
+ Parameters
23
+ ----------
24
+ multiplier : float
25
+ The factor by which to multiply the coordinate values (e.g., 1e6 for meters to microns).
26
+ unit_label : str
27
+ The new unit label for the coordinates (e.g., "µm").
28
+ coord_names : str or list of str
29
+ The name(s) of the coordinate variable(s) to rescale.
30
+ If a string, only that coordinate is rescaled.
31
+ If a list, all listed coordinates are rescaled.
32
+
33
+ Returns
34
+ -------
35
+ xr.Dataset
36
+ A new Dataset with the updated and rescaled coordinates.
37
+
38
+ Examples
39
+ --------
40
+ # Convert X, Y, and Z from meters to microns
41
+ >>> ds_in_microns = ds.epoch.rescale_coords(1e6, "µm", coord_names=["X_Grid", "Y_Grid", "Z_Grid"])
42
+
43
+ # Convert only X to millimeters
44
+ >>> ds_in_mm = ds.epoch.rescale_coords(1000, "mm", coord_names="X_Grid")
45
+ """
46
+
47
+ ds = self._ds
48
+ new_coords = {}
49
+
50
+ if isinstance(coord_names, str):
51
+ # Convert single string to a list
52
+ coords_to_process = [coord_names]
53
+ elif isinstance(coord_names, list):
54
+ # Use the provided list
55
+ coords_to_process = coord_names
56
+ else:
57
+ coords_to_process = list(coord_names)
58
+
59
+ for coord_name in coords_to_process:
60
+ if coord_name not in ds.coords:
61
+ raise ValueError(
62
+ f"Coordinate '{coord_name}' not found in the Dataset. Cannot rescale."
63
+ )
64
+
65
+ coord_original = ds[coord_name]
66
+
67
+ coord_rescaled = coord_original * multiplier
68
+ coord_rescaled.attrs = coord_original.attrs.copy()
69
+ coord_rescaled.attrs["units"] = unit_label
70
+
71
+ new_coords[coord_name] = coord_rescaled
72
+
73
+ return ds.assign_coords(new_coords)
@@ -0,0 +1,54 @@
1
+ begin:control
2
+ nx = 64
3
+ ny = 64
4
+ nz = 64
5
+
6
+ # Final time of simulation
7
+ t_end = 5 * femto
8
+
9
+ # Size of domain
10
+ x_min = 0
11
+ x_max = 5.0e5
12
+
13
+ y_min = 0
14
+ y_max = 5.0e5
15
+
16
+ z_min = 0
17
+ z_max = 5.0e5
18
+
19
+ stdout_frequency = 1
20
+ end:control
21
+
22
+
23
+ begin:boundaries
24
+ bc_x_min = periodic
25
+ bc_x_max = periodic
26
+ bc_y_min = periodic
27
+ bc_y_max = periodic
28
+ bc_z_min = periodic
29
+ bc_z_max = periodic
30
+ end:boundaries
31
+
32
+
33
+ begin:species
34
+ name = electron
35
+ charge = -1
36
+ mass = 1.0
37
+ temperature_x = 273
38
+ number_density = 10
39
+ nparticles = nx * ny * nz * 2
40
+ end:species
41
+
42
+ begin:output_global
43
+ force_last_to_be_restartable = F
44
+ end:output_global
45
+
46
+ begin:output
47
+ name = normal
48
+
49
+ dt_snapshot = 1 * femto
50
+
51
+ grid = always
52
+
53
+ end:output
54
+
@@ -0,0 +1,146 @@
1
+ import pathlib
2
+
3
+ import numpy as np
4
+ import pytest
5
+ import xarray as xr
6
+
7
+ from sdf_xarray import open_mfdataset
8
+
9
+ EXAMPLE_FILES_DIR = pathlib.Path(__file__).parent / "example_files_3D"
10
+
11
+
12
+ def test_rescale_coords_X():
13
+ multiplier = 1e3
14
+ unit_label = "mm"
15
+
16
+ with xr.open_dataset(EXAMPLE_FILES_DIR / "0000.sdf") as ds:
17
+ ds_rescaled = ds.epoch.rescale_coords(
18
+ multiplier=multiplier,
19
+ unit_label=unit_label,
20
+ coord_names="X_Grid_mid",
21
+ )
22
+
23
+ expected_x = ds["X_Grid_mid"].values * multiplier
24
+ assert np.allclose(ds_rescaled["X_Grid_mid"].values, expected_x)
25
+ assert ds_rescaled["X_Grid_mid"].attrs["units"] == unit_label
26
+ assert ds_rescaled["X_Grid_mid"].attrs["long_name"] == "X"
27
+ assert ds_rescaled["X_Grid_mid"].attrs["full_name"] == "Grid/Grid_mid"
28
+
29
+ assert np.allclose(ds_rescaled["Y_Grid_mid"].values, ds["Y_Grid_mid"].values)
30
+ assert ds_rescaled["Y_Grid_mid"].attrs["units"] == "m"
31
+ assert ds_rescaled["Y_Grid_mid"].attrs["long_name"] == "Y"
32
+ assert ds_rescaled["Y_Grid_mid"].attrs["full_name"] == "Grid/Grid_mid"
33
+
34
+ assert np.allclose(ds_rescaled["Z_Grid_mid"].values, ds["Z_Grid_mid"].values)
35
+ assert ds_rescaled["Z_Grid_mid"].attrs["units"] == "m"
36
+ assert ds_rescaled["Z_Grid_mid"].attrs["long_name"] == "Z"
37
+ assert ds_rescaled["Z_Grid_mid"].attrs["full_name"] == "Grid/Grid_mid"
38
+
39
+
40
+ def test_rescale_coords_X_Y():
41
+ multiplier = 1e2
42
+ unit_label = "cm"
43
+
44
+ with xr.open_dataset(EXAMPLE_FILES_DIR / "0000.sdf") as ds:
45
+ ds_rescaled = ds.epoch.rescale_coords(
46
+ multiplier=multiplier,
47
+ unit_label=unit_label,
48
+ coord_names=["X_Grid_mid", "Y_Grid_mid"],
49
+ )
50
+
51
+ expected_x = ds["X_Grid_mid"].values * multiplier
52
+ assert np.allclose(ds_rescaled["X_Grid_mid"].values, expected_x)
53
+ assert ds_rescaled["X_Grid_mid"].attrs["units"] == unit_label
54
+ assert ds_rescaled["X_Grid_mid"].attrs["long_name"] == "X"
55
+ assert ds_rescaled["X_Grid_mid"].attrs["full_name"] == "Grid/Grid_mid"
56
+
57
+ expected_y = ds["Y_Grid_mid"].values * multiplier
58
+ assert np.allclose(ds_rescaled["Y_Grid_mid"].values, expected_y)
59
+ assert ds_rescaled["Y_Grid_mid"].attrs["units"] == unit_label
60
+ assert ds_rescaled["Y_Grid_mid"].attrs["long_name"] == "Y"
61
+ assert ds_rescaled["Y_Grid_mid"].attrs["full_name"] == "Grid/Grid_mid"
62
+
63
+ assert np.allclose(ds_rescaled["Z_Grid_mid"].values, ds["Z_Grid_mid"].values)
64
+ assert ds_rescaled["Z_Grid_mid"].attrs["units"] == "m"
65
+ assert ds_rescaled["Z_Grid_mid"].attrs["long_name"] == "Z"
66
+ assert ds_rescaled["Z_Grid_mid"].attrs["full_name"] == "Grid/Grid_mid"
67
+
68
+
69
+ def test_rescale_coords_X_Y_tuple():
70
+ multiplier = 1e2
71
+ unit_label = "cm"
72
+
73
+ with xr.open_dataset(EXAMPLE_FILES_DIR / "0000.sdf") as ds:
74
+ ds_rescaled = ds.epoch.rescale_coords(
75
+ multiplier=multiplier,
76
+ unit_label=unit_label,
77
+ coord_names=("X_Grid_mid", "Y_Grid_mid"),
78
+ )
79
+
80
+ expected_x = ds["X_Grid_mid"].values * multiplier
81
+ assert np.allclose(ds_rescaled["X_Grid_mid"].values, expected_x)
82
+ assert ds_rescaled["X_Grid_mid"].attrs["units"] == unit_label
83
+ assert ds_rescaled["X_Grid_mid"].attrs["long_name"] == "X"
84
+ assert ds_rescaled["X_Grid_mid"].attrs["full_name"] == "Grid/Grid_mid"
85
+
86
+ expected_y = ds["Y_Grid_mid"].values * multiplier
87
+ assert np.allclose(ds_rescaled["Y_Grid_mid"].values, expected_y)
88
+ assert ds_rescaled["Y_Grid_mid"].attrs["units"] == unit_label
89
+ assert ds_rescaled["Y_Grid_mid"].attrs["long_name"] == "Y"
90
+ assert ds_rescaled["Y_Grid_mid"].attrs["full_name"] == "Grid/Grid_mid"
91
+
92
+ assert np.allclose(ds_rescaled["Z_Grid_mid"].values, ds["Z_Grid_mid"].values)
93
+ assert ds_rescaled["Z_Grid_mid"].attrs["units"] == "m"
94
+ assert ds_rescaled["Z_Grid_mid"].attrs["long_name"] == "Z"
95
+ assert ds_rescaled["Z_Grid_mid"].attrs["full_name"] == "Grid/Grid_mid"
96
+
97
+
98
+ def test_rescale_coords_attributes_copied():
99
+ multiplier = 1e6
100
+ unit_label = "µm"
101
+
102
+ with xr.open_dataset(EXAMPLE_FILES_DIR / "0000.sdf") as ds:
103
+ ds_rescaled = ds.epoch.rescale_coords(
104
+ multiplier=multiplier,
105
+ unit_label=unit_label,
106
+ coord_names=["X_Grid_mid"],
107
+ )
108
+
109
+ assert ds_rescaled["X_Grid_mid"].attrs["units"] == unit_label
110
+ assert ds_rescaled["X_Grid_mid"].attrs["long_name"] == "X"
111
+ assert ds_rescaled["X_Grid_mid"].attrs["full_name"] == "Grid/Grid_mid"
112
+
113
+
114
+ def test_rescale_coords_non_existent_coord():
115
+ with xr.open_dataset(EXAMPLE_FILES_DIR / "0000.sdf") as ds:
116
+ with pytest.raises(ValueError, match="Coordinate 'Time' not found"):
117
+ ds.epoch.rescale_coords(
118
+ multiplier=1.0,
119
+ unit_label="s",
120
+ coord_names="Time",
121
+ )
122
+
123
+ with pytest.raises(ValueError, match="Coordinate 'Bad_Coord' not found"):
124
+ ds.epoch.rescale_coords(
125
+ multiplier=1e6,
126
+ unit_label="µm",
127
+ coord_names=["X_Grid_mid", "Bad_Coord"],
128
+ )
129
+
130
+
131
+ def test_rescale_coords_time():
132
+ multiplier = 1e-15
133
+ unit_label = "fs"
134
+
135
+ with open_mfdataset(EXAMPLE_FILES_DIR.glob("*.sdf")) as ds:
136
+ ds_rescaled = ds.epoch.rescale_coords(
137
+ multiplier=multiplier,
138
+ unit_label=unit_label,
139
+ coord_names="time",
140
+ )
141
+
142
+ expected_time = ds["time"].values * multiplier
143
+ assert np.allclose(ds_rescaled["time"].values, expected_time)
144
+ assert ds_rescaled["time"].attrs["units"] == unit_label
145
+ assert ds_rescaled["time"].attrs["long_name"] == "Time"
146
+ assert ds_rescaled["time"].attrs["full_name"] == "time"