def iter_calculation_targets(self, variable_names=None, yield_calculation_name=True, validate_units=True): if variable_names is None: seq = list(self.field.get_by_tag(self.tag)) else: seq = get_variables(variable_names, self.field) if len(seq) == 0: raise ValueError('No data variables available on the field.') for variable in seq: if validate_units: self.validate_units(variable) self._curr_variable = variable if yield_calculation_name: # Append the variable to the calculation if there are more than one to calculate across. if len(seq) > 1: calculation_name = '{}_{}'.format(self.alias, variable.name) else: calculation_name = self.alias else: calculation_name = None if yield_calculation_name: yield variable, calculation_name else: yield variable
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