sadd_pandana.py
Go to the documentation of this file.
1 import glob
2 import h5py
3 import sys
4 import os
5 sys.path.append('../..')
6 from PandAna.core import spectrum, save_tree, load_tree
7 
8 def main(dest, source):
9  if not type(source) is list: source = [source]
10  print('Saving spectra from {} files to {}'.format(len(source), dest))
11  # assume all of the files have the same groups in them
12  first = h5py.File(source[0], 'r')
13  groups = list(first.keys())
14  first.close()
15 
16  print('Found {} groups'.format(len(groups)))
17  # this will work if all files have the exact same spectra
18  source_spectra = {}
19  dest_spectra = {}
20 
21  # create dictionaries of empty lists. We'll put spectra into these later
22  for g in groups:
23  source_spectra[g] = []
24  dest_spectra[g] = None
25 
26  # for each source file, load all groups
27  for s in source:
28  print('Loading spectra from {}'.format(s))
29  spectra = load_tree(s, groups)
30 
31  # add each spectra to the dictionary of source_spectra according to group
32  for group, spec in spectra:
33  source_spectra[group].append(spec)
34 
35  # get list of source_spectra corresponding to each group
36  for g in groups:
37  for spec in source_spectra[g]:
38  # if first spectra make assignment
39  if dest_spectra[g] is None:
40  dest_spectra[g] = spec
41  # else add it to the spectrum that already exists for that group
42  else:
43  # make sure all spectra being summed have the same name
44  assert str(dest_spectra[g]._df.name) == str(spec._df.name), \
45  'Spectra must have the same name to add them together ({} =/= {})'.format(dest_spectra[g]._df.name, spec._df.name)
46  dest_spectra[g] = dest_spectra[g] + spec
47 
48  save_tree(dest, list(dest_spectra.values()), list(dest_spectra.keys()))
49 
50 
51 
52 
53 if __name__ == '__main__':
54  dest = sys.argv[1]
55  if os.path.isfile(dest):
56  print('{} already exists. Will not overwrite'.format(dest))
57  sys.exit()
58  source = []
59  for arg in sys.argv[1:]:
60  these = glob.glob(arg)
61  if type(these) is list: source = source + these
62  else: source.append(these)
63  print(dest, source)
64  main(dest, source)
def main(dest, source)
Definition: sadd_pandana.py:8
void append()
Definition: append.C:24
bool print
std::string format(const int32_t &value, const int &ndigits=8)
Definition: HexUtils.cpp:14
def save_tree(fname, spectra, groups, attrs=True)
Definition: core.py:133
def load_tree(fname, groups, attrs=True)
Definition: core.py:183