-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile_tmin.py
65 lines (48 loc) · 1.94 KB
/
compile_tmin.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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import argparse
import numpy as np
import logging
import multiprocessing as mp
import yaml
from os.path import join
import h5py
# Compile tmin from HDF5 files into yaml files
def get_tmin_tmax(shotnr, datadir):
"""Return tmax attribute from HDF5 file.
shotnr (int) : Shot number file
datadir (str) : Directory of the HDF5 files.
"""
with h5py.File(join(datadir, f"{shotnr}.h5"), 'r') as df:
tmin = df.attrs["tmin"]
tmax = df.attrs["tmax"]
# Cast to float so that yaml can pickle this item later on.
return float(tmin), float(tmax)
if __name__ == "__main__":
logging.basicConfig(filename="instantiate.log",
format="%(asctime)s %(message)s",
encoding="utf-8",
level=logging.INFO)
parser = argparse.ArgumentParser(
prog="downloader.py",
description="Downloads D3D datasets according to yaml description")
parser.add_argument("--dataset_def", type=str,
help="YAML file that contains definition of the dataset")
parser.add_argument("--destination", type=str,
help="Destination for Dataset HDF5 files")
args = parser.parse_args()
with open(args.dataset_def, "r") as stream:
dataset_def = yaml.safe_load(stream)
# Generate list from shots in the dataset
shot_list = dataset_def["shots"][-10:]
t_min_max_dict = {}
for shotnr in shot_list:
tmin, tmax = get_tmin_tmax(shotnr, args.destination)
print(f"========== {shotnr} ========")
#if dataset_def["shots"][shotnr]["ttd"] > 0.0:
# tmax = float(dataset_def["shots"][shotnr]["ttd"])
print(f" tmin={tmin}, tmax={tmax}")
t_min_max_dict.update({shotnr: {"tmin": float(tmin), "tmax": float(tmax)}})
with open("shots_t_min_max.yaml", "w") as fp:
fp.write(yaml.safe_dump(t_min_max_dict))
# end of file compile_tmin.py