Skip to content

Instantly share code, notes, and snippets.

@smutch
Last active July 27, 2020 00:19
Show Gist options
  • Save smutch/eba5d65a5c519afb0d7388593af691b8 to your computer and use it in GitHub Desktop.
Save smutch/eba5d65a5c519afb0d7388593af691b8 to your computer and use it in GitHub Desktop.
py: Read in a Genesis grid directly with xarray
import numpy as np
import xarray as xa
import h5py as h5
from pathlib import Path
from tqdm import tqdm
GRIDS_DIR = Path("PATH_GOES_HERE")
def grid_path(snapshot: int, sub: int):
return GRIDS_DIR / f"snapshot_{snapshot:03d}.den.{sub}"
def read_grid(snapshot: int):
with h5.File(grid_path(snapshot, 0), 'r') as fp:
boxsize = fp.attrs['BoxSize']
nfiles = fp.attrs['Num_files']
dim = fp.attrs['Ngrid_X']
grid = xa.DataArray(coords=[np.linspace(0, boxsize, dim, endpoint=False)]*3, dims=('x', 'y', 'z'))
for ii in tqdm(range(nfiles)):
with h5.File(grid_path(snapshot, ii), 'r') as fp:
xstart = fp.attrs['Local_x_start']
nx = fp.attrs['Local_nx']
grid[xstart:xstart+nx, ...] = fp['Density'][:].reshape([nx, dim, dim])
return grid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment