def get_driver(self, as_class=False): """ Return the driver key or class associated with the dimension map. :param bool as_class: If ``True``, return the driver class instead of the driver string key. :rtype: str | :class:`ocgis.driver.base.AbstractDriver` """ from ocgis.driver.registry import get_driver_class ret = self._storage.get(DMK.DRIVER, DEFAULT_DRIVER) if as_class: ret = get_driver_class(ret) return ret
def write(self, *args, **kwargs): """ See :meth:`ocgis.VariableCollection.write`. .. note:: If no ``driver`` is provided, then the field's dimension map driver will be used. """ from ocgis.driver.registry import get_driver_class to_load = (DimensionMapKey.REALIZATION, DimensionMapKey.TIME, DimensionMapKey.LEVEL, DimensionMapKey.Y, DimensionMapKey.X) # Attempt to load all instrumented dimensions once. Do not do this for the geometry variable. This is done to # ensure proper attributes are applied to dimension variables before writing. for k in to_load: getattr(self, k) driver = kwargs.pop('driver', None) if driver is None: driver = self.dimension_map.get_driver(as_class=True) else: driver = get_driver_class(driver) args = list(args) args.insert(0, self) return driver.write_field(*args, **kwargs)
def set_driver(self, driver): from ocgis.driver.registry import get_driver_class klass = get_driver_class(driver) self._storage[DMK.DRIVER] = klass.key
def get_driver(driver): return get_driver_class(key_class_or_instance=driver, default=constants.DEFAULT_DRIVER)
def iter(self, **kwargs): """ :return: Yield record dictionaries for variables in the field applying standard names to dimensions by default. :rtype: dict """ if self.is_empty: return from ocgis.driver.registry import get_driver_class standardize = kwargs.pop(KeywordArgument.STANDARDIZE, KeywordArgument.Defaults.STANDARDIZE) tag = kwargs.pop(KeywordArgument.TAG, TagName.DATA_VARIABLES) driver = kwargs.get(KeywordArgument.DRIVER) primary_mask = kwargs.pop(KeywordArgument.PRIMARY_MASK, None) header_map = kwargs.pop(KeywordArgument.HEADER_MAP, None) melted = kwargs.pop(KeywordArgument.MELTED, False) variable = kwargs.pop(KeywordArgument.VARIABLE, None) followers = kwargs.pop(KeywordArgument.FOLLOWERS, None) allow_masked = kwargs.get(KeywordArgument.ALLOW_MASKED, False) if melted and not standardize: raise ValueError('"standardize" must be True when "melted" is True.') if KeywordArgument.ALLOW_MASKED not in kwargs: kwargs[KeywordArgument.ALLOW_MASKED] = False if driver is not None: driver = get_driver_class(driver) # Holds follower variables to pass to the generic iterator. if followers is None: followers = [] else: for ii, f in enumerate(followers): if not isinstance(f, Iterator): followers[ii] = get_variables(f, self)[0] if variable is None: # The primary variable(s) to iterate. tagged_variables = self.get_by_tag(tag, create=True) if len(tagged_variables) == 0: msg = 'Tag "{}" has no associated variables. Nothing to iterate.'.format(tag) raise ValueError(msg) variable = tagged_variables[0] if len(tagged_variables) > 1: followers += tagged_variables[1:] else: variable = get_variables(variable, self)[0] if self.geom is not None: if primary_mask is None: primary_mask = self.geom if standardize: add_geom_uid = True else: add_geom_uid = False followers.append(self.geom.get_iter(**{KeywordArgument.ADD_GEOM_UID: add_geom_uid, KeywordArgument.ALLOW_MASKED: allow_masked, KeywordArgument.PRIMARY_MASK: primary_mask})) geom = self.geom else: geom = None if self.realization is not None: followers.append(self.realization.get_iter(driver=driver, allow_masked=allow_masked, primary_mask=primary_mask)) if self.time is not None: followers.append(self.time.get_iter(add_bounds=True, driver=driver, allow_masked=allow_masked, primary_mask=primary_mask)) if self.level is not None: followers.append(self.level.get_iter(add_bounds=True, driver=driver, allow_masked=allow_masked, primary_mask=primary_mask)) # Collect repeaters from the target variable and followers. This initializes the iterator twice, but the # operation is not expensive. itr_for_repeaters = Iterator(variable, followers=followers) found = kwargs.get(KeywordArgument.REPEATERS) if found is not None: found = [ii[0] for ii in found] repeater_headers = itr_for_repeaters.get_repeaters(headers_only=True, found=found) if standardize: if header_map is None: header_map = OrderedDict() if len(repeater_headers) > 0: for k in repeater_headers: header_map[k] = k if self.geom is not None and self.geom.ugid is not None: header_map[self.geom.ugid.name] = self.geom.ugid.name if self.realization is not None: header_map[self.realization.name] = HeaderName.REALIZATION if self.time is not None: header_map[self.time.name] = HeaderName.TEMPORAL update_header_rename_bounds_names(HeaderName.TEMPORAL_BOUNDS, header_map, self.time) header_map['YEAR'] = 'YEAR' header_map['MONTH'] = 'MONTH' header_map['DAY'] = 'DAY' if self.level is not None: header_map[self.level.name] = HeaderName.LEVEL update_header_rename_bounds_names(HeaderName.LEVEL_BOUNDS, header_map, self.level) if melted: melted = tagged_variables else: melted = None kwargs[KeywordArgument.HEADER_MAP] = header_map kwargs[KeywordArgument.MELTED] = melted kwargs[KeywordArgument.VARIABLE] = variable kwargs[KeywordArgument.FOLLOWERS] = followers kwargs[KeywordArgument.GEOM] = geom for yld in super(Field, self).iter(**kwargs): yield yld