1
0
Fork 0
mirror of https://github.com/Findus23/halo_comparison.git synced 2024-09-19 16:03:50 +02:00
halo_comparison/slices.py

79 lines
2.4 KiB
Python
Raw Normal View History

2022-08-05 16:13:05 +02:00
from pathlib import Path
2022-08-23 17:29:44 +02:00
from typing import List, Tuple
2022-08-05 16:13:05 +02:00
import h5py
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LogNorm
from scipy.interpolate import griddata
2022-08-23 16:38:33 +02:00
from temperatures import calculate_T
2022-08-05 16:13:05 +02:00
from utils import create_figure
2022-08-23 17:29:44 +02:00
def filter_3d(
coords: np.ndarray, data: np.ndarray,
extent: List[float]
) -> Tuple[np.ndarray, np.ndarray]:
filter = (
(extent[0] < coords[::, 0]) &
(coords[::, 0] < extent[1]) &
(extent[2] < coords[::, 1]) &
(coords[::, 1] < extent[3])
)
print("before", coords.shape)
data = data[filter]
coords = coords[filter]
print("after", coords.shape)
return coords, data
def create_2d_slice(
2022-08-23 17:29:44 +02:00
input_file: Path, center: List[float], extent,
property="InternalEnergies", method="nearest"
) -> np.ndarray:
cut_axis = 2 # Z
2022-08-05 16:13:05 +02:00
with h5py.File(input_file) as f:
pt0 = f["PartType0"]
2022-08-23 16:36:48 +02:00
coords = pt0["Coordinates"][:]
2022-08-23 17:29:44 +02:00
data = pt0[property if property != "Temperatures" else "InternalEnergies"][:]
2022-08-23 16:36:48 +02:00
2022-08-23 17:29:44 +02:00
coords, data = filter_3d(coords, data, extent)
if property == "Temperatures":
print("calculating temperatures")
data = np.array([calculate_T(u) for u in data])
2022-08-23 16:36:48 +02:00
2022-08-23 17:29:44 +02:00
xrange = np.linspace(extent[0],extent[1], 1000)
yrange = np.linspace(extent[2],extent[3], 1000)
2022-08-05 16:13:05 +02:00
gx, gy, gz = np.meshgrid(xrange, yrange, center[cut_axis])
print("interpolating")
2022-08-23 17:29:44 +02:00
grid = griddata(coords, data, (gx, gy, gz), method=method)[::, ::, 0]
return grid
2022-08-05 16:13:05 +02:00
print(grid.shape)
# stats, x_edge, y_edge, _ = binned_statistic_2d(
# coords_in_slice[::, x_axis],
# coords_in_slice[::, y_axis],
# data_in_slice,
# bins=500,
# statistic="mean"
# )
fig, ax = create_figure()
# stats = np.nan_to_num(stats)
print("plotting")
img = ax.imshow(
2022-08-23 16:36:48 +02:00
grid,
2022-08-05 16:13:05 +02:00
norm=LogNorm(),
interpolation="nearest",
2022-08-23 16:36:48 +02:00
origin="lower",
extent=[xrange[0], xrange[-1], yrange[0], yrange[-1]],
2022-08-05 16:13:05 +02:00
)
ax.set_title(input_file.parent.stem)
ax.set_xlabel(x_axis_label)
ax.set_ylabel(y_axis_label)
2022-08-23 16:36:48 +02:00
ax.set_aspect("equal")
fig.colorbar(img, label="Temperatures")
2022-08-05 16:13:05 +02:00
fig.tight_layout()
plt.show()