ChannelReader.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # from . import DataHandler #此时是导入文件
  2. from .DataStream import DataReadStream, day_stamp, open_hdf5
  3. from .DataStream import EChPosmap as pos_map
  4. import numpy as np
  5. import re
  6. from collections import defaultdict
  7. import time as time
  8. __all__ = ['ChannelReader']
  9. import logging
  10. logger = logging.getLogger('reader')
  11. class ChannelReader(DataReadStream):
  12. def __init__(self):
  13. file = '/var/www/html/data/stdata/channel.hdf5'
  14. hfive = open_hdf5(file, False)
  15. super(ChannelReader, self).__init__(hfive)
  16. def __del__(self):
  17. self.close()
  18. super(ChannelReader, self).__del__()
  19. pass
  20. def tuple_path(self, day: int, chnames: set = None, card_types: set = None, spec: int = None):
  21. def parse(path):
  22. items = re.split(r'/', path)
  23. (_prifix, _version, today, chnam, card_type, spec) = items
  24. return chnam, int(card_type), int(spec)
  25. tuples = defaultdict(list)
  26. pathes = self.datasets(day)
  27. for path in pathes:
  28. _chnam, _card_type, _spec = parse(path)
  29. tuples[_chnam].append((_card_type, _spec))
  30. def name_filter(tuples, chnames):
  31. all = True if chnames is None or len(chnames) == 0 else False
  32. if all:
  33. return tuples
  34. else:
  35. result = defaultdict(list)
  36. for chame, tup in tuples.items():
  37. if chame in chnames:
  38. result[chame] = tup
  39. return result
  40. tuples = name_filter(tuples, chnames)
  41. def typespec_filter(tuples, card_types, spec):
  42. result = defaultdict(list)
  43. for name, ls in tuples.items():
  44. for tup in ls:
  45. _card_type, _spec = tup
  46. if _card_type in card_types:
  47. if spec is None or _spec == spec:
  48. result[name].append((_card_type, _spec))
  49. return result
  50. tuples = typespec_filter(tuples,card_types,spec)
  51. return tuples
  52. def many_tuple_path(self, days: list, chnames: set = None, card_types: set = None, spec: int = None):
  53. def merge(l,r):
  54. for name,ls in l.items():
  55. if name in r:
  56. ls.extend(r[name])
  57. r[name] = list(set(ls))
  58. else:
  59. r[name] = ls
  60. return r
  61. all = defaultdict(set)
  62. for day in days:
  63. tuples = self.tuple_path(day, chnames, card_types, spec)
  64. all = merge(tuples,all)
  65. return all
  66. def init_data(self, days):
  67. dim = pos_map.dim()
  68. return np.zeros((dim, 86400 * days))
  69. def read(self, day_stamp, chname, card_type, spec):
  70. path = f'/{self._version}/{day_stamp}/{chname}/{card_type}/{spec}'
  71. hfive = self.file
  72. if path in hfive:
  73. return hfive[path]
  74. else:
  75. return None
  76. pass