def update(self, other, **kwargs): """ concatenate sequence of station level containers into one container """ datlist = [self, other] dd = com.defaultdict(list) handles = set() for idat in datlist: if not (hasattr(idat, 'degree') and idat.degree == self.degree): continue for cID, child in idat.children.items(): dd[cID].append(child) handles |= idat.handles for cID in dd.keys(): for idat in datlist: if hasattr(idat, 'degree') and idat.degree == self.degree: continue dd[cID].append(idat) handles |= idat.handles attrs = Attributes({**self.attrs, **other.attrs}) cdat = type(self)(name=self.name, attrs=attrs) cdat._sub = self._sub for cID in dd.keys(): if len(dd[cID]) == 1: cdat.children[cID] = dd[cID][0] else: cdat.children[cID] = dd[cID][0].update(dd[cID][1], **kwargs) return cdat
def attrs(self, values): # set default values for standard station attributes attrs = Attributes() attrs['system_type'] = com.get_fill(str) attrs['system_manufacturer'] = com.get_fill(str) attrs['system_model'] = com.get_fill(str) # only for point device # if self.pixeldims == 0: # self.attrs['site_id'] = common.get_fill(str) # self.attrs['site_latitude'] = common.get_fill(float) # self.attrs['site_longitude'] = common.get_fill(float) # self.attrs['site_altitude'] = common.get_fill(float) # # elif self.pixeldims == 1: # # only for link # self.attrs['site_a_id'] = common.get_fill(str) # self.attrs['site_a_latitude'] = common.get_fill(float) # self.attrs['site_a_longitude'] = common.get_fill(float) # self.attrs['site_a_altitude'] = common.get_fill(float) # self.attrs['site_b_id'] = common.get_fill(str) # self.attrs['site-b_latitude'] = common.get_fill(float) # self.attrs['site_b_longitude'] = common.get_fill(float) # self.attrs['site_b_altitude'] = common.get_fill(float) if values is not None: for key, value in values.items(): attrs[key] = value self._attrs = attrs
def attrs(self, values): # set default values for standard Network attributes attrs = Attributes() attrs['file_format'] = 'CMLh5' attrs['file_format version'] = '0.1' attrs['pro_id'] = com.get_fill(str) attrs['level'] = com.get_fill(str) attrs['source_type'] = com.get_fill(str) attrs['temporal_resolution'] = com.get_fill(str) if values is not None: for key, value in values.items(): attrs[key] = value self._attrs = attrs
def from_store(cls, inh, children=None, **kwargs): attrs = Attributes.from_store(inh) name = attrs.pop(cls.treename + '_id') if children is None: items = list(inh.items()) else: items = [(key, inh[key]) for key in children] data = cls(name=name, attrs=attrs) for cID, ichan in items: child = cls._sub.from_store(ichan, **kwargs) data.children[cID] = child return data
def apply(self, func, *args, recurse=False, **kwargs): """ apply function to all channels of the link """ outchildren = {} for cID, child in self.children.items(): logging.info(self.name + ' ' + cID) mattrs = Attributes.merged([self, child]) if recurse is True: outchild = child.apply(func, mattrs, *args, **kwargs) else: outchild = func(child, mattrs, *args, **kwargs) if outchild is not None: outchildren[cID] = outchild return self._new(outchildren, self.name, self.attrs, self.handles)
def from_yaml(cls, inh, children=None, degree=None, **kwargs): if children is None: items = list(inh['branches'].items()) else: items = [(key, inh['branches'][key]) for key in children] data = cls(attrs=Attributes(inh['attrs'])) if degree is None: degree = inh['degree'] data.degree = degree degree -= 1 if degree == 0: data._sub = cb.MetaChan data._sub.treename = inh['branchname'] for cID, ichan in items: child = data._sub.from_yaml(ichan, degree=degree, **kwargs) data.children[cID] = child return data
def __getitem__(self, key): if isinstance(key, (cb.Array, Node)) and key.dtype == bool: return self.mask(~key) if isinstance(key, tuple): if len(key) == 1: return self[key[0]] elif key[0] is Ellipsis: if self.level == len(key) - 1: return self[key[1:]] elif self.level >= len(key): return self[(slice(None),) + key] elif key[0] == slice(None): new = type(self)(name=self.name, attrs=self.attrs) for cID, child in self.children.items(): try: grandchild = child[key[1:]] except KeyError: continue else: if not hasattr(grandchild, 'keys'): grandchild.attrs = Attributes.merged([grandchild, child]) new.children[cID] = grandchild return new elif isinstance(key[0], list): newchildren = OrderedDict((k, v[key[1:]]) for k, v in self.children.items() if k in key[0]) return self._new(newchildren, self.name, self.attrs, self.handles) else: new = self.children[key[0]][key[1:]] return new elif isinstance(key, list): newchildren = OrderedDict((k, v) for k, v in self.children.items() if k in key) return self._new(newchildren, self.name, self.attrs, self.handles) else: new = self.children[key] #new.attrs = Attributes.merged([new, self]) return new
def merged(cls, datlist, xdim=None, **kwargs): """ concatenate sequence of station level containers into one container """ if not datlist: return cls() datlist = com.prep_merger(datlist) dat0 = next(iter(datlist.values())) if len(datlist) <= 1: return dat0 dd = com.defaultdict(OrderedDict) handles = set() for i, idat in datlist.items(): if not (hasattr(idat, 'degree') and idat.degree == cls.degree): continue for cID, child in idat.children.items(): dd[cID][i] = child handles |= idat.handles for i, idat in datlist.items(): if hasattr(idat, 'degree') and idat.degree == cls.degree: continue cID = idat.name dd[cID][i] = idat handles |= idat.handles if xdim is None: attrs = Attributes.merged(datlist) else: attrs, xdim = cb.merge_attrs(datlist, xdim) cdat = cls(name=dat0.name, attrs=attrs) cdat._sub = dat0._sub for cID in dd.keys(): cdat.children[cID] = cdat._sub.merged(dd[cID], xdim=xdim, **kwargs) return cdat
def attrs(self): if not hasattr(self, '_attrs'): self._attrs = Attributes() return self._attrs