|
| 1 | +.. _loading-data: |
| 2 | + |
1 | 3 | Loading Data |
2 | 4 | ============ |
3 | 5 |
|
@@ -269,3 +271,95 @@ in SWIFT will be automatically read. |
269 | 271 | "extra_test.hdf5", |
270 | 272 | ) |
271 | 273 |
|
| 274 | +
|
| 275 | +Reading from an open file |
| 276 | +------------------------- |
| 277 | + |
| 278 | +:mod:`swiftsimio` normally opens and closes the HDF5 snapshot file for |
| 279 | +each operation. This is convenient for interactive use and avoids |
| 280 | +leaving files open for long periods of time, but sometimes it might be |
| 281 | +desirable to minimize the number of file open and close operations. |
| 282 | + |
| 283 | +It is possible to pass an open :obj:`h5py.File` object to |
| 284 | +:mod:`swiftsimio.load` and :mod:`swiftsimio.mask` in place of the |
| 285 | +filename. In this case swiftsimio will do all file access through the |
| 286 | +provided file object. This allows us to read multiple datasets while |
| 287 | +only opening and closing the file once. For example: |
| 288 | + |
| 289 | +.. code-block:: python |
| 290 | +
|
| 291 | + import h5py |
| 292 | + import swiftsimio as sw |
| 293 | +
|
| 294 | + with h5py.File("cosmo_volume_example.hdf5","r") as snap_file: |
| 295 | + data = sw.load(snap_file) |
| 296 | + pos = data.dark_matter.coordinates |
| 297 | + vel = data.dark_matter.velocities |
| 298 | + ids = data.dark_matter.particle_ids |
| 299 | +
|
| 300 | +This would open the snapshot file, read the dark matter particle |
| 301 | +positions, velocities and IDs, then close the file. |
| 302 | + |
| 303 | + |
| 304 | +Reading from a remote file |
| 305 | +-------------------------- |
| 306 | + |
| 307 | +:mod:`swiftsimio` is able to read from snapshots hosted on a remote |
| 308 | +server using the `hdfstream |
| 309 | +<https://hdfstream-python.readthedocs.io/en/latest>`_ python |
| 310 | +module. This is useful if you're interested in accessing a small part |
| 311 | +of a larger snapshot: you can read a small region or a subset of |
| 312 | +particle properties without downloading the whole snapshot. |
| 313 | + |
| 314 | +To open a remote snapshot, you can pass a :obj:`hdfstream.RemoteFile` |
| 315 | +object to :mod:`swiftsimio.load` and :mod:`swiftsimio.mask` in place |
| 316 | +of the filename. For example, you can open one of the SWIFT example |
| 317 | +snapshots with: |
| 318 | + |
| 319 | +.. code-block:: python |
| 320 | +
|
| 321 | + import hdfstream |
| 322 | + from swiftsimio import load |
| 323 | +
|
| 324 | + snap_file = hdfstream.open("cosma", "Tests/SWIFT/IOExamples/ssio_ci_04_2025/EagleSingle.hdf5") |
| 325 | + data = load(snap_file) |
| 326 | +
|
| 327 | +Here, ``data`` will be a :obj:`swiftsimio.reader.SWIFTDataset`. It |
| 328 | +functions in the same way as described in the :ref:`loading-data` |
| 329 | +section above, except that instead of reading data from a local HDF5 |
| 330 | +file, it requests data from the server. |
| 331 | + |
| 332 | +Opening a snapshot like this only downloads a small amount of |
| 333 | +metadata. Accessing particle properties, such as coordinates, will |
| 334 | +trigger another download: |
| 335 | + |
| 336 | +.. code-block:: python |
| 337 | +
|
| 338 | + pos = data.dark_matter.coordinates |
| 339 | +
|
| 340 | +This will download the dark matter particle coordinates and return an |
| 341 | +array with units and cosmological factors attached. |
| 342 | + |
| 343 | +To read part of a remote snapshot, we can use swiftsimio's |
| 344 | +:ref:`masking` feature as we would with a local snapshot, but passing |
| 345 | +the remote file to :mod:`swiftsimio.mask` :mod:`swiftsimio.load` in |
| 346 | +place of the filename. |
| 347 | + |
| 348 | +.. code-block:: python |
| 349 | +
|
| 350 | + import hdfstream |
| 351 | + import swiftsimio as sw |
| 352 | +
|
| 353 | + snap_file = hdfstream.open("cosma", "Tests/SWIFT/IOExamples/ssio_ci_04_2025/EagleSingle.hdf5") |
| 354 | +
|
| 355 | + mask = sw.mask(snap_file) |
| 356 | + # The full metadata object is available from within the mask |
| 357 | + boxsize = mask.metadata.boxsize |
| 358 | + # load_region is a 3x2 list [[left, right], [bottom, top], [front, back]] |
| 359 | + load_region = [[0.0 * b, 0.5 * b] for b in boxsize] |
| 360 | +
|
| 361 | + # Constrain the mask |
| 362 | + mask.constrain_spatial(load_region) |
| 363 | +
|
| 364 | + # Now load the snapshot with this mask |
| 365 | + data = sw.load(snap_file, mask=mask) |
0 commit comments