def convert_pyam(self, quantities, year_time_dim, tag='iamc', drop={}, collapse=None): """Add conversion of one or more **quantities** to IAMC format. Parameters ---------- quantities : str or Key or list of (str, Key) Quantities to transform to :mod:`pyam`/IAMC format. year_time_dim : str Label of the dimension use for the ‘Year’ or ‘Time’ column of the resulting :class:`pyam.IamDataFrame`. The column is labelled ‘Time’ if ``year_time_dim=='h'``, otherwise ‘Year’. tag : str, optional Tag to append to new Keys. drop : iterable of str, optional Label of additional dimensions to drop from the resulting data frame. Dimensions ``h``, ``y``, ``ya``, ``yr``, and ``yv``— except for the one named by `year_time_dim`—are automatically dropped. collapse : callable, optional Callback to handle additional dimensions of the quantity. A :class:`pandas.DataFrame` is passed as the sole argument to `collapse`, which must return a modified dataframe. Returns ------- list of Key Each key converts a :class:`Quantity <ixmp.reporting.utils.Quantity>` into a :class:`pyam.IamDataFrame`. See also -------- message_ix.reporting.computations.as_pyam """ if isinstance(quantities, (str, Key)): quantities = [quantities] quantities = self.check_keys(*quantities) keys = [] for qty in quantities: # Dimensions to drop automatically qty = Key.from_str_or_key(qty) to_drop = set(drop) | set( qty.dims) & ({'h', 'y', 'ya', 'yr', 'yv'} - {year_time_dim}) new_key = ':'.join([qty.name, tag]) comp = partial(computations.as_pyam, year_time_dim=year_time_dim, drop=to_drop, collapse=collapse) self.add(new_key, (comp, 'scenario', qty)) keys.append(new_key) return keys
def convert_pyam( self, quantities, year_time_dim, tag="iamc", drop={}, collapse=None, unit=None, replace_vars=None, ): """Add conversion of one or more **quantities** to IAMC format. Parameters ---------- quantities : str or Key or list of (str, Key) Quantities to transform to :mod:`pyam`/IAMC format. year_time_dim : str Label of the dimension use for the ‘Year’ or ‘Time’ column of the resulting :class:`pyam.IamDataFrame`. The column is labelled ‘Time’ if ``year_time_dim=='h'``, otherwise ‘Year’. tag : str, optional Tag to append to new Keys. drop : iterable of str, optional Label of additional dimensions to drop from the resulting data frame. Dimensions ``h``, ``y``, ``ya``, ``yr``, and ``yv``— except for the one named by `year_time_dim`—are automatically dropped. collapse : callable, optional Callback to handle additional dimensions of the quantity. A :class:`pandas.DataFrame` is passed as the sole argument to `collapse`, which must return a modified dataframe. unit : str or pint.Unit, optional Convert values to these units. replace_vars : str or Key Other reporting key containing a :class:`dict` mapping variable names to replace. Returns ------- list of Key Each key converts a :class:`Quantity <ixmp.reporting.utils.Quantity>` into a :class:`pyam.IamDataFrame`. See also -------- message_ix.reporting.computations.as_pyam """ if isinstance(quantities, (str, Key)): quantities = [quantities] quantities = self.check_keys(*quantities) keys = [] for qty in quantities: # Key for the new quantity qty = Key.from_str_or_key(qty) new_key = ":".join([qty.name, tag]) # Dimensions to drop automatically to_drop = set(drop) | set( qty.dims) & ({"h", "y", "ya", "yr", "yv"} - {year_time_dim}) # Prepare the computation comp = [ partial( computations.as_pyam, year_time_dim=year_time_dim, drop=to_drop, collapse=collapse, unit=unit, ), "scenario", qty, ] if replace_vars: comp.append(replace_vars) # Add and store self.add(new_key, tuple(comp)) keys.append(new_key) return keys