forked from jchelly/SOAP
-
Notifications
You must be signed in to change notification settings - Fork 3
/
recalculate_xrays.py
268 lines (243 loc) · 7.53 KB
/
recalculate_xrays.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import numpy as np
import h5py
import virgo.mpi.parallel_hdf5 as phdf5
import virgo.mpi.parallel_sort as psort
from virgo.util.partial_formatter import PartialFormatter
from mpi4py import MPI
import lustre
import swift_units
import xray_calculator
comm = MPI.COMM_WORLD
comm_rank = comm.Get_rank()
comm_size = comm.Get_size()
def recalculate_xrays(snap_file, output_filename, units, xray_calculator):
"""
Calculate Xray properties for gas particles, write them to the output file
"""
if comm_rank == 0:
print("Loading data")
datasets = ["Densities", "Temperatures", "SmoothedElementMassFractions", "Masses"]
data = snap_file.read(datasets, group="PartType0")
for dset in datasets:
data[dset] *= units[dset]
# Dictionary for holding recalculated values
output = {}
if comm_rank == 0:
print("Recalculating xrays")
idx_he, idx_T, idx_n, t_z, d_z, t_T, d_T, t_nH, d_nH, t_He, d_He, abundance_to_solar, joint_mask, volumes, data_n = xray_calculator.find_indices(
data["Densities"],
data["Temperatures"],
data["SmoothedElementMassFractions"],
data["Masses"],
fill_value=0,
)
xray_bands = ["erosita-low", "erosita-high", "ROSAT"]
observing_types = ["energies_intrinsic", "energies_intrinsic", "energies_intrinsic"]
output["XrayLuminosities"] = xray_calculator.interpolate_X_Ray(
idx_he,
idx_T,
idx_n,
t_z,
d_z,
t_T,
d_T,
t_nH,
d_nH,
t_He,
d_He,
abundance_to_solar,
joint_mask,
volumes,
data_n,
bands=xray_bands,
observing_types=observing_types,
fill_value=0,
).to(units["XrayLuminosities"])
observing_types = ["photons_intrinsic", "photons_intrinsic", "photons_intrinsic"]
output["XrayPhotonLuminosities"] = xray_calculator.interpolate_X_Ray(
idx_he,
idx_T,
idx_n,
t_z,
d_z,
t_T,
d_T,
t_nH,
d_nH,
t_He,
d_He,
abundance_to_solar,
joint_mask,
volumes,
data_n,
bands=xray_bands,
observing_types=observing_types,
fill_value=0,
).to(units["XrayPhotonLuminosities"])
observing_types = [
"energies_intrinsic_restframe",
"energies_intrinsic_restframe",
"energies_intrinsic_restframe",
]
output["XrayLuminositiesRestframe"] = xray_calculator.interpolate_X_Ray(
idx_he,
idx_T,
idx_n,
t_z,
d_z,
t_T,
d_T,
t_nH,
d_nH,
t_He,
d_He,
abundance_to_solar,
joint_mask,
volumes,
data_n,
bands=xray_bands,
observing_types=observing_types,
fill_value=0,
).to(units["XrayLuminosities"])
observing_types = [
"photons_intrinsic_restframe",
"photons_intrinsic_restframe",
"photons_intrinsic_restframe",
]
output["XrayPhotonLuminositiesRestframe"] = xray_calculator.interpolate_X_Ray(
idx_he,
idx_T,
idx_n,
t_z,
d_z,
t_T,
d_T,
t_nH,
d_nH,
t_He,
d_He,
abundance_to_solar,
joint_mask,
volumes,
data_n,
bands=xray_bands,
observing_types=observing_types,
fill_value=0,
).to(units["XrayPhotonLuminosities"])
attrs = {}
for k in output.keys():
attrs[k] = {
"Description": f"{k} recalculated by SOAP using table {xray_calculator.table_path}"
}
attrs[k].update(swift_units.attributes_from_units(output[k].units, 1, 0))
comm.barrier()
# Write these particles out with the same layout as the input snapshot
if comm_rank == 0:
print("Writing out xray properties")
elements_per_file = snap_file.get_elements_per_file(
"ParticleIDs", group="PartType0"
)
snap_file.write(
output,
elements_per_file,
filenames=output_filename,
mode="w",
group="PartType0",
attrs=attrs,
)
#
if __name__ == "__main__":
import datetime
start = datetime.datetime.now()
# Read parameters from command line and config file
from virgo.mpi.util import MPIArgumentParser
parser = MPIArgumentParser(
comm=comm, description="Recalculate xray properties from SWIFT snapshots."
)
parser.add_argument(
"swift_filename",
type=str,
help="Basename of the input snapshots, use {snap_nr:04d} for the snapshot number and {file_nr} for the file number.",
)
parser.add_argument(
"output_filename",
type=str,
help="Basename of the output snapshots, use {snap_nr:04d} for the snapshot number and {file_nr} for the file number.",
)
parser.add_argument(
"xray_table_path", type=str, help="Path to the xray tables to use"
)
parser.add_argument("--snap-nr", type=int, help="Snapshot number to process")
args = parser.parse_args()
# Substitute in the snapshot number where necessary
pf = PartialFormatter()
swift_filename = pf.format(args.swift_filename, snap_nr=args.snap_nr, file_nr=None)
output_filename = pf.format(
args.output_filename, snap_nr=args.snap_nr, file_nr=None
)
# Ensure output dir exists
if comm_rank == 0:
lustre.ensure_output_dir(output_filename)
comm.barrier()
# Load tables on rank 0
if comm_rank == 0:
units = {}
with h5py.File(swift_filename.format(file_nr=0), "r") as snap_file:
z = snap_file["Header"].attrs["Redshift"][0]
unit_registry = swift_units.unit_registry_from_snapshot(snap_file)
for dset in [
"Densities",
"Temperatures",
"SmoothedElementMassFractions",
"Masses",
"XrayLuminosities",
"XrayPhotonLuminosities",
]:
attrs = snap_file[f"PartType0/{dset}"].attrs
units[dset] = swift_units.units_from_attributes(attrs, unit_registry)
xray_bands = [
"erosita-low",
"erosita-high",
"ROSAT",
"erosita-low",
"erosita-high",
"ROSAT",
"erosita-low",
"erosita-high",
"ROSAT",
"erosita-low",
"erosita-high",
"ROSAT",
]
observing_types = [
"energies_intrinsic",
"energies_intrinsic",
"energies_intrinsic",
"photons_intrinsic",
"photons_intrinsic",
"photons_intrinsic",
"energies_intrinsic_restframe",
"energies_intrinsic_restframe",
"energies_intrinsic_restframe",
"photons_intrinsic_restframe",
"photons_intrinsic_restframe",
"photons_intrinsic_restframe",
]
xray_calculator = xray_calculator.XrayCalculator(
z, args.xray_table_path, xray_bands, observing_types
)
else:
units = None
xray_calculator = None
units = comm.bcast(units)
xray_calculator = comm.bcast(xray_calculator)
# Open the snapshot
snap_file = phdf5.MultiFile(
swift_filename, file_nr_attr=("Header", "NumFilesPerSnapshot")
)
# Recalculate xrays and write the output
recalculate_xrays(snap_file, output_filename, units, xray_calculator)
comm.barrier()
if comm_rank == 0:
runtime = datetime.datetime.now() - start
print(f"Done in {int(runtime.total_seconds())}s")