ChannelReader.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 ChPlotFilter(object):
  12. def __init__(self, chnames: set = None, card_types: set = None, spec: int = None, all_type: int = 3):
  13. self._chnames = chnames
  14. self._card_types = card_types
  15. self._spec = spec
  16. self._all_type = all_type
  17. def channels(self):
  18. return self._chnames
  19. def card_types(self):
  20. return self._card_types
  21. def spec(self):
  22. return self._spec
  23. def _only_all(self):
  24. return True if self._all_type == 1 else False
  25. def _in_all(self):
  26. return True if self._all_type == 2 else False
  27. def _ex_all(self):
  28. return True if self._all_type == 3 else False
  29. def _show_detail_plot(self, card_type, spec):
  30. channel_count = len(self._chnames) if self._chnames is not None else 0
  31. type_count = len(self._card_types) if self._card_types is not None else 0
  32. if channel_count == 1:
  33. return True
  34. else:
  35. return False if card_type is not None or spec is not None else True
  36. def show_plot(self,chname,card_type,spec):
  37. if self._only_all():
  38. return True if chname == 'all' else False
  39. elif self._in_all():
  40. if chname == 'all':
  41. return True
  42. else:
  43. return self._show_detail_plot(card_type=card_type,spec=spec)
  44. else:
  45. if chname == 'all':
  46. return False
  47. else:
  48. return self._show_detail_plot(card_type=card_type,spec=spec)
  49. pass
  50. class ChPathFilter(object):
  51. def __init__(self,chnames: set = None, card_types: set = None,spec: int = None,only_all: bool=False):
  52. self._chnames = chnames
  53. self._card_types = card_types
  54. self._spec = spec
  55. self._only_all = only_all
  56. def channels(self):
  57. return self._chnames
  58. def card_types(self):
  59. return self._card_types
  60. def spec(self):
  61. return self._spec
  62. def only_all(self):
  63. return self._only_all
  64. def show_detail(self):
  65. if self._only_all:
  66. return False
  67. elif len(self._chnames) == 1:
  68. return True
  69. else:
  70. return False
  71. def show_channel(self):
  72. if self._only_all:
  73. return False
  74. else:
  75. not_show = self._spec is not None and self._chnames is not None and len(self._chnames) == 1 and self._card_types is not None and len(self._card_types) == 1
  76. return not_show == False
  77. pass
  78. class ChannelReader(DataReadStream):
  79. def __init__(self):
  80. file = '/var/www/html/data/stdata/channel.hdf5'
  81. hfive = open_hdf5(file, False)
  82. super(ChannelReader, self).__init__(hfive)
  83. def __del__(self):
  84. self.close()
  85. super(ChannelReader, self).__del__()
  86. pass
  87. def tuple_path(self, day: int, chnames: set = None, card_types: set = None, spec: int = None):
  88. def parse(path):
  89. items = re.split(r'/', path)
  90. (_prifix, _version, today, chnam, card_type, spec) = items
  91. return chnam, int(card_type), int(spec)
  92. tuples = defaultdict(list)
  93. pathes = self.datasets(day)
  94. for path in pathes:
  95. _chnam, _card_type, _spec = parse(path)
  96. tuples[_chnam].append((_card_type, _spec))
  97. def name_filter(tuples, chnames):
  98. all = True if chnames is None or len(chnames) == 0 else False
  99. if all:
  100. return tuples
  101. else:
  102. result = defaultdict(list)
  103. for chame, tup in tuples.items():
  104. if chame in chnames:
  105. result[chame] = tup
  106. return result
  107. tuples = name_filter(tuples, chnames)
  108. def typespec_filter(tuples, card_types, spec):
  109. result = defaultdict(list)
  110. for name, ls in tuples.items():
  111. for tup in ls:
  112. _card_type, _spec = tup
  113. if card_types is None or _card_type in card_types:
  114. if spec is None or _spec == spec:
  115. result[name].append((_card_type, _spec))
  116. return result
  117. tuples = typespec_filter(tuples,card_types,spec)
  118. return tuples
  119. def many_tuple_path(self, days: list, chnames: set = None, card_types: set = None, spec: int = None):
  120. def merge(l,r):
  121. for name,ls in l.items():
  122. if name in r:
  123. ls.extend(r[name])
  124. r[name] = list(set(ls))
  125. else:
  126. r[name] = ls
  127. return r
  128. all = defaultdict(set)
  129. for day in days:
  130. tuples = self.tuple_path(day, chnames, card_types, spec)
  131. all = merge(tuples,all)
  132. return all
  133. def init_data(self, days):
  134. dim = pos_map.dim()
  135. return np.zeros((dim, 86400 * days))
  136. def read(self, day_stamp, chname, card_type, spec):
  137. path = f'/{self._version}/{day_stamp}/{chname}/{card_type}/{spec}'
  138. hfive = self.file
  139. if path in hfive:
  140. dset = hfive[path]
  141. if dset.shape[0] == pos_map.dim():
  142. return dset
  143. else:
  144. dim = pos_map.dim()
  145. result = np.zeros((dim, 86400))
  146. result[0:dset.shape[0], :] = dset[:, :]
  147. return result
  148. else:
  149. return None
  150. def read_path(self, path):
  151. hfive = self.file
  152. if path in hfive:
  153. dset = hfive[path]
  154. if dset.shape[0] == pos_map.dim():
  155. return dset
  156. else:
  157. dim = pos_map.dim()
  158. result = np.zeros((dim, 86400))
  159. result[0:dset.shape[0], :] = dset[:, :]
  160. return result
  161. else:
  162. return None
  163. pass