123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- # from . import DataHandler #此时是导入文件
- from .DataStream import DataReadStream, day_stamp, open_hdf5
- from .DataStream import EChPosmap as pos_map
- import numpy as np
- import re
- from collections import defaultdict
- import time as time
- __all__ = ['ChannelReader']
- import logging
- logger = logging.getLogger('reader')
- class ChPlotFilter(object):
- def __init__(self, chnames: set = None, card_types: set = None, spec: int = None, all_type: int = 3):
- self._chnames = chnames
- self._card_types = card_types
- self._spec = spec
- self._all_type = all_type
- def channels(self):
- return self._chnames
- def card_types(self):
- return self._card_types
- def spec(self):
- return self._spec
- def _only_all(self):
- return True if self._all_type == 1 else False
- def _in_all(self):
- return True if self._all_type == 2 else False
- def _ex_all(self):
- return True if self._all_type == 3 else False
- def _show_detail_plot(self, card_type, spec):
- channel_count = len(self._chnames) if self._chnames is not None else 0
- type_count = len(self._card_types) if self._card_types is not None else 0
- if channel_count == 1:
- return True
- else:
- return False if card_type is not None or spec is not None else True
- def show_plot(self,chname,card_type,spec):
- if self._only_all():
- return True if chname == 'all' else False
- elif self._in_all():
- if chname == 'all':
- return True
- else:
- return self._show_detail_plot(card_type=card_type,spec=spec)
- else:
- if chname == 'all':
- return False
- else:
- return self._show_detail_plot(card_type=card_type,spec=spec)
- pass
- class ChPathFilter(object):
- def __init__(self,chnames: set = None, card_types: set = None,spec: int = None,only_all: bool=False):
- self._chnames = chnames
- self._card_types = card_types
- self._spec = spec
- self._only_all = only_all
- def channels(self):
- return self._chnames
- def card_types(self):
- return self._card_types
- def spec(self):
- return self._spec
- def only_all(self):
- return self._only_all
- def show_detail(self):
- if self._only_all:
- return False
- elif len(self._chnames) == 1:
- return True
- else:
- return False
- def show_channel(self):
- if self._only_all:
- return False
- else:
- 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
- return not_show == False
- pass
- class ChannelReader(DataReadStream):
- def __init__(self):
- file = '/var/www/html/data/stdata/channel.hdf5'
- hfive = open_hdf5(file, False)
- super(ChannelReader, self).__init__(hfive)
- def __del__(self):
- self.close()
- super(ChannelReader, self).__del__()
- pass
- def tuple_path(self, day: int, chnames: set = None, card_types: set = None, spec: int = None):
- def parse(path):
- items = re.split(r'/', path)
- (_prifix, _version, today, chnam, card_type, spec) = items
- return chnam, int(card_type), int(spec)
- tuples = defaultdict(list)
- pathes = self.datasets(day)
- for path in pathes:
- _chnam, _card_type, _spec = parse(path)
- tuples[_chnam].append((_card_type, _spec))
- def name_filter(tuples, chnames):
- all = True if chnames is None or len(chnames) == 0 else False
- if all:
- return tuples
- else:
- result = defaultdict(list)
- for chame, tup in tuples.items():
- if chame in chnames:
- result[chame] = tup
- return result
- tuples = name_filter(tuples, chnames)
- def typespec_filter(tuples, card_types, spec):
- result = defaultdict(list)
- for name, ls in tuples.items():
- for tup in ls:
- _card_type, _spec = tup
- if card_types is None or _card_type in card_types:
- if spec is None or _spec == spec:
- result[name].append((_card_type, _spec))
- return result
- tuples = typespec_filter(tuples,card_types,spec)
- return tuples
- def many_tuple_path(self, days: list, chnames: set = None, card_types: set = None, spec: int = None):
- def merge(l,r):
- for name,ls in l.items():
- if name in r:
- ls.extend(r[name])
- r[name] = list(set(ls))
- else:
- r[name] = ls
- return r
- all = defaultdict(set)
- for day in days:
- tuples = self.tuple_path(day, chnames, card_types, spec)
- all = merge(tuples,all)
- return all
- def init_data(self, days):
- dim = pos_map.dim()
- return np.zeros((dim, 86400 * days))
- def read(self, day_stamp, chname, card_type, spec):
- path = f'/{self._version}/{day_stamp}/{chname}/{card_type}/{spec}'
- hfive = self.file
- if path in hfive:
- dset = hfive[path]
- if dset.shape[0] == pos_map.dim():
- return dset
- else:
- dim = pos_map.dim()
- result = np.zeros((dim, 86400))
- result[0:dset.shape[0], :] = dset[:, :]
- return result
- else:
- return None
- def read_path(self, path):
- hfive = self.file
- if path in hfive:
- dset = hfive[path]
- if dset.shape[0] == pos_map.dim():
- return dset
- else:
- dim = pos_map.dim()
- result = np.zeros((dim, 86400))
- result[0:dset.shape[0], :] = dset[:, :]
- return result
- else:
- return None
- pass
|