-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathread_save.py
More file actions
49 lines (44 loc) · 2.1 KB
/
read_save.py
File metadata and controls
49 lines (44 loc) · 2.1 KB
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
import h5py
import numpy as np
import pandas as pd
import astropy.units as u
from astropy.table import QTable
from astropy.time import Time
from astropy.coordinates import SkyCoord
def save_sim(iloc, path_to_save, my_own_model, pyLIMA_parameters, event_params):
print('Saving Simulation...')
# Save to an HDF5 file with specified names
with h5py.File(path_to_save + 'Event_' + str(iloc) + '.h5', 'w') as file:
# Save array with a specified name
file.create_dataset('Data', data=np.array([iloc, my_own_model.origin[0]], dtype='S'))
# Save dictionary with a specified name
dict_group = file.create_group('pyLIMA_parameters')
for key, value in pyLIMA_parameters.items():
dict_group.attrs[key] = value
dict_group_tril = file.create_group('TRILEGAL_params')
for key, value in event_params.items():
dict_group_tril.attrs[key] = value
# Save table with a specified name
for telo in my_own_model.event.telescopes:
table = telo.lightcurve
table_group = file.create_group(telo.name)
for col in table.colnames:
table_group.create_dataset(col, data=table[col])
print('File saved:',path_to_save + 'Event_' + str(iloc) + '.h5' )
def read_data(path_model):
# Open the HDF5 file and load data using specified names
with h5py.File(path_model, 'r') as file:
# Load array with string with info of dataset using its name
info_dataset = file['Data'][:]
info_dataset = [ file['Data'][:][0].decode('UTF-8'),
[file['Data'][:][1].decode('UTF-8'), [0, 0]]]
# Dictionary using its name
pyLIMA_parameters = {key: file['pyLIMA_parameters'].attrs[key] for key in file['pyLIMA_parameters'].attrs}
# Load table using its name
bands = {}
for band in ("u", "g", "r", "i", "z", "y"):
loaded_table = QTable()
for col in file[band]:
loaded_table[col] = file[band][col][:]
bands[band] = loaded_table
return info_dataset, pyLIMA_parameters, bands