-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid_to_binary.py
executable file
·55 lines (42 loc) · 1.95 KB
/
grid_to_binary.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
#!/usr/bin/env python
import os
import argparse
import numpy as np
def main():
ps = argparse.ArgumentParser()
ps.add_argument('--remove', action='store_true', default=False, help='Removes ascii files.')
ps.add_argument('--overwrite', action='store_true', default=False, help='Overwrites binary files -- mandatory for every machine swap. ')
args = ps.parse_args()
print(args)
# get grids directory
for gdname in ['grids', 'grids_ABS']:
cwd = os.getcwd()
gdir = os.path.join(cwd, gdname)
dl = os.listdir(gdir)
# go through each grid directory
for direc in dl:
# path to directory
path = os.path.join(gdir, direc)
# directories only
if not os.path.isdir(path):
continue
# list of spectra
gl = os.path.join(path, 'gridlist')
# load the list
synlist = np.loadtxt(gl, dtype=str, unpack=True, usecols=[0])
# transform each spectrum to binary
for synspec in synlist:
# define name of the binary file
bin_synspec = synspec + '.npz'
if os.path.isfile(os.path.join(path, bin_synspec)) and not args.overwrite:
print("File: %s exists." % bin_synspec)
if os.path.isfile(os.path.join(path, synspec)) and args.remove:
os.remove(os.path.join(path, synspec))
continue
# load the ascii spectrum and save it as binary file
w, i = np.loadtxt(os.path.join(path, synspec), unpack=True, usecols=[0, 1])
np.savez(os.path.join(path, bin_synspec), w, i)
if os.path.isfile(os.path.join(path, synspec)) and args.remove:
os.remove(os.path.join(path, synspec))
if __name__ == '__main__':
main()