Emissions units with Pint

In this notebook we give some examples of how units are handled in SCMData and are built on top of the Pint package.

import traceback
import warnings

from pint.errors import DimensionalityError

from scmdata.units import UnitConverter
/home/docs/checkouts/readthedocs.org/user_builds/scmdata/checkouts/latest/src/scmdata/database/_database.py:9: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  import tqdm.autonotebook as tqdman

UnitConverter

The UnitConverter class handles all unit conversions for us. It is used as shown.

uc = UnitConverter("GtC/yr", "Mt CO2 / yr")
uc.convert_from(1)
3666.666666666667
uc.convert_to(1)
0.0002727272727272727

Pint Unit Registry

The unit_registry which sits underneath all conversions can be accessed via scmdata.units.get_unit_registryor via UnitConverter’s unit_registry property.

unit_registry = uc.unit_registry

Having accessed the unit_registry, all the units available in SCMData can be shown like so.

dir(unit_registry)
['%',
 'A',
 'AMMONIA',
 'A_90',
 'A_US',
 'A_it',
 'Ah',
 'At',
 'B',
 'BC',
 'BDFT',
 'BF',
 'BTU',
 'BUTANE',
 'Ba',
 'Bd',
 'Bi',
 'Bq',
 'Btu',
 'Btu_iso',
 'Btu_it',
 'Btu_th',
 'C',
 'C10F18',
 'C2F6',
 'C2H6',
 'C3F8',
 'C3H8',
 'C4F10',
 'C5F12',
 'C6F14',
 'C7F16',
 'C8F18',
 'CC3F6',
 'CC4F8',
 'CCL4',
 'CCl4',
 'CF4',
 'CFC11',
 'CFC113',
 'CFC114',
 'CFC115',
 'CFC12',
 'CFC13',
 'CFC400',
 'CH2CL2',
 'CH2Cl2',
 'CH3BR',
 'CH3Br',
 'CH3CCL3',
 'CH3CCl3',
 'CH3CL',
 'CH3Cl',
 'CH4',
 'CHCL3',
 'CHCl3',
 'CO',
 'CO2',
 'C_90',
 'Ci',
 'Cl',
 'Context',
 'D',
 'DPI',
 'Da',
 'ECC',
 'EC_therm',
 'E_h',
 'Eh',
 'F',
 'FBM',
 'F_90',
 'Fr',
 'G',
 'G_0',
 'Gal',
 'Gb',
 'Group',
 'Gy',
 'H',
 'H2O',
 'HALON1201',
 'HALON1202',
 'HALON1211',
 'HALON1301',
 'HALON2402',
 'HC170',
 'HC290',
 'HC436A',
 'HC436B',
 'HC436C',
 'HC436a',
 'HC436b',
 'HC436c',
 'HC441A',
 'HC441a',
 'HC50',
 'HC510A',
 'HC510a',
 'HC511A',
 'HC511a',
 'HC600',
 'HC600A',
 'HC600a',
 'HC601',
 'HC601A',
 'HC601a',
 'HCE170',
 'HCFC123',
 'HCFC124',
 'HCFC141B',
 'HCFC141b',
 'HCFC142B',
 'HCFC142b',
 'HCFC21',
 'HCFC22',
 'HCFC225CA',
 'HCFC225CB',
 'HCFC225ca',
 'HCFC225cb',
 'HCFC31',
 'HCFC401A',
 'HCFC401B',
 'HCFC401C',
 'HCFC401a',
 'HCFC401b',
 'HCFC401c',
 'HCFC402A',
 'HCFC402B',
 'HCFC402a',
 'HCFC402b',
 'HCFC403A',
 'HCFC403B',
 'HCFC403a',
 'HCFC403b',
 'HCFC405A',
 'HCFC405a',
 'HCFC406A',
 'HCFC406a',
 'HCFC408A',
 'HCFC408a',
 'HCFC409A',
 'HCFC409B',
 'HCFC409a',
 'HCFC409b',
 'HCFC412A',
 'HCFC412a',
 'HCFC414A',
 'HCFC414B',
 'HCFC414a',
 'HCFC414b',
 'HCFC415A',
 'HCFC415B',
 'HCFC415a',
 'HCFC415b',
 'HCFC416A',
 'HCFC416a',
 'HCFC418A',
 'HCFC418a',
 'HCFC420A',
 'HCFC420a',
 'HCFC500',
 'HCFC501',
 'HCFC502',
 'HCFC503',
 'HCFC504',
 'HCFC505',
 'HCFC506',
 'HCFC509A',
 'HCFC509a',
 'HCFE235DA2',
 'HCFE235da2',
 'HCFO411A',
 'HCFO411B',
 'HCFO411a',
 'HCFO411b',
 'HCO1130',
 'HCO514A',
 'HCO514a',
 'HFC125',
 'HFC1336MZZ',
 'HFC1336mzz',
 'HFC134',
 'HFC134A',
 'HFC134a',
 'HFC143',
 'HFC143A',
 'HFC143a',
 'HFC152',
 'HFC152A',
 'HFC152a',
 'HFC161',
 'HFC227EA',
 'HFC227ea',
 'HFC23',
 'HFC236CB',
 'HFC236EA',
 'HFC236FA',
 'HFC236cb',
 'HFC236ea',
 'HFC236fa',
 'HFC245CA',
 'HFC245FA',
 'HFC245ca',
 'HFC245fa',
 'HFC32',
 'HFC365MFC',
 'HFC365mfc',
 'HFC404A',
 'HFC404a',
 'HFC407A',
 'HFC407B',
 'HFC407C',
 'HFC407D',
 'HFC407E',
 'HFC407F',
 'HFC407G',
 'HFC407H',
 'HFC407I',
 'HFC407a',
 'HFC407b',
 'HFC407c',
 'HFC407d',
 'HFC407e',
 'HFC407f',
 'HFC407g',
 'HFC407h',
 'HFC407i',
 'HFC41',
 'HFC410A',
 'HFC410B',
 'HFC410a',
 'HFC410b',
 'HFC413A',
 'HFC413a',
 'HFC417A',
 'HFC417B',
 'HFC417C',
 'HFC417a',
 'HFC417b',
 'HFC417c',
 'HFC419A',
 'HFC419B',
 'HFC419a',
 'HFC419b',
 'HFC421A',
 'HFC421B',
 'HFC421a',
 'HFC421b',
 'HFC422A',
 'HFC422B',
 'HFC422C',
 'HFC422D',
 'HFC422E',
 'HFC422a',
 'HFC422b',
 'HFC422c',
 'HFC422d',
 'HFC422e',
 'HFC423A',
 'HFC423a',
 'HFC424A',
 'HFC424a',
 'HFC425A',
 'HFC425a',
 'HFC426A',
 'HFC426a',
 'HFC427A',
 'HFC427a',
 'HFC428A',
 'HFC428a',
 'HFC429A',
 'HFC429a',
 'HFC430A',
 'HFC430a',
 'HFC4310',
 'HFC4310MEE',
 'HFC4310mee',
 'HFC431A',
 'HFC431a',
 'HFC434A',
 'HFC434a',
 'HFC435A',
 'HFC435a',
 'HFC437A',
 'HFC437a',
 'HFC438A',
 'HFC438a',
 'HFC439A',
 'HFC439a',
 'HFC440A',
 'HFC440a',
 'HFC442A',
 'HFC442a',
 'HFC453A',
 'HFC453a',
 'HFC458A',
 'HFC458a',
 'HFC461A',
 'HFC461a',
 'HFC462A',
 'HFC462a',
 'HFC507A',
 'HFC507a',
 'HFC508A',
 'HFC508B',
 'HFC508a',
 'HFC508b',
 'HFC512A',
 'HFC512a',
 'HFE125',
 'HFE134',
 'HFE143A',
 'HFE143a',
 'HFE227EA',
 'HFE227ea',
 'HFE236CA12',
 'HFE236EA2',
 'HFE236FA',
 'HFE236ca12',
 'HFE236ea2',
 'HFE236fa',
 'HFE245CB2',
 'HFE245FA1',
 'HFE245FA2',
 'HFE245cb2',
 'HFE245fa1',
 'HFE245fa2',
 'HFE263FB2',
 'HFE263fb2',
 'HFE329MCC2',
 'HFE329mcc2',
 'HFE338MCF2',
 'HFE338PCC13',
 'HFE338mcf2',
 'HFE338pcc13',
 'HFE347MCC3',
 'HFE347MCF2',
 'HFE347PCF2',
 'HFE347mcc3',
 'HFE347mcf2',
 'HFE347pcf2',
 'HFE356MEC3',
 'HFE356PCC3',
 'HFE356PCF2',
 'HFE356PCF3',
 'HFE356mec3',
 'HFE356pcc3',
 'HFE356pcf2',
 'HFE356pcf3',
 'HFE365MCF3',
 'HFE365mcf3',
 'HFE374PC2',
 'HFE374pc2',
 'HFE4310PCCC124',
 'HFE4310pccc124',
 'HFE449SL',
 'HFE449sl',
 'HFE569SF2',
 'HFE569sf2',
 'HFO1234YF',
 'HFO1234ZE',
 'HFO1234yf',
 'HFO1234ze',
 'HFO444A',
 'HFO444B',
 'HFO444a',
 'HFO444b',
 'HFO445A',
 'HFO445a',
 'HFO446A',
 'HFO446a',
 'HFO447A',
 'HFO447B',
 'HFO447a',
 'HFO447b',
 'HFO448A',
 'HFO448a',
 'HFO449A',
 'HFO449B',
 'HFO449C',
 'HFO449a',
 'HFO449b',
 'HFO449c',
 'HFO450A',
 'HFO450a',
 'HFO451A',
 'HFO451B',
 'HFO451a',
 'HFO451b',
 'HFO452A',
 'HFO452B',
 'HFO452C',
 'HFO452a',
 'HFO452b',
 'HFO452c',
 'HFO454A',
 'HFO454B',
 'HFO454C',
 'HFO454a',
 'HFO454b',
 'HFO454c',
 'HFO455A',
 'HFO455a',
 'HFO456A',
 'HFO456a',
 'HFO457A',
 'HFO457a',
 'HFO459A',
 'HFO459B',
 'HFO459a',
 'HFO459b',
 'HFO460A',
 'HFO460B',
 'HFO460C',
 'HFO460a',
 'HFO460b',
 'HFO460c',
 'HFO463A',
 'HFO463a',
 'HFO464A',
 'HFO464a',
 'HFO465A',
 'HFO465a',
 'HFO513A',
 'HFO513B',
 'HFO513a',
 'HFO513b',
 'HFO515A',
 'HFO515a',
 'HFO516A',
 'HFO516a',
 'HO1270',
 'HO432A',
 'HO432a',
 'HO433A',
 'HO433B',
 'HO433C',
 'HO433a',
 'HO433b',
 'HO433c',
 'HO443A',
 'HO443a',
 'H_90',
 'Halon1201',
 'Halon1202',
 'Halon1211',
 'Halon1301',
 'Halon2402',
 'Hg',
 'Hg_0C',
 'Hg_32F',
 'Hg_60F',
 'Hz',
 'ISOBUTANE',
 'ISOPENTANE',
 'J',
 'K',
 'KPH',
 'K_J',
 'K_J90',
 'K_alpha_Cu_d_220',
 'K_alpha_Mo_d_220',
 'K_alpha_W_d_220',
 'L',
 'Ly',
 'M',
 'MPH',
 'Measurement',
 'Mx',
 'N',
 'N2O',
 'N2ON',
 'NF3',
 'NH3',
 'NMVOC',
 'NO2',
 'NOX',
 'NOx',
 'N_A',
 'Ne',
 'NeC',
 'Nm',
 'Np',
 'OC',
 'Oe',
 'P',
 'PENTANE',
 'PFC116',
 'PFC218',
 'PFCC318',
 'PFPMIE',
 'PPCM',
 'PPI',
 'PROPENE',
 'PSH',
 'Pa',
 'Phi_0',
 'Quantity',
 'R',
 'RKM',
 'R_K',
 'R_K90',
 'R_inf',
 'R_∞',
 'Rd',
 'Ry',
 'S',
 'SF5CF3',
 'SF6',
 'SO2',
 'SO2F2',
 'SOX',
 'SOx',
 'SPL',
 'St',
 'Sv',
 'System',
 'T',
 'Ta',
 'Td',
 'Tj',
 'Tt',
 'U',
 'UK_bbl',
 'UK_bushel',
 'UK_cup',
 'UK_cwt',
 'UK_fluid_ounce',
 'UK_force_ton',
 'UK_gallon',
 'UK_gill',
 'UK_horsepower',
 'UK_hundredweight',
 'UK_pint',
 'UK_pk',
 'UK_quart',
 'UK_ton',
 'UK_ton_force',
 'US_cwt',
 'US_dry_barrel',
 'US_dry_gallon',
 'US_dry_pint',
 'US_dry_quart',
 'US_fluid_dram',
 'US_fluid_ounce',
 'US_force_ton',
 'US_hundredweight',
 'US_international_ampere',
 'US_international_ohm',
 'US_international_volt',
 'US_liquid_cup',
 'US_liquid_dram',
 'US_liquid_fifth',
 'US_liquid_gallon',
 'US_liquid_gill',
 'US_liquid_ounce',
 'US_liquid_quart',
 'US_pint',
 'US_shot',
 'US_therm',
 'US_ton',
 'US_ton_force',
 'Unit',
 'UnitsContainer',
 'V',
 'VA',
 'VOC',
 'V_90',
 'V_US',
 'V_it',
 'W',
 'W_90',
 'Wb',
 'Wh',
 'Xu_Cu',
 'Xu_Mo',
 'Z_0',
 '__annotations__',
 '__call__',
 '__class__',
 '__class_getitem__',
 '__contains__',
 '__deepcopy__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattr__',
 '__getattribute__',
 '__getitem__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__orig_bases__',
 '__parameters__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__slots__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 '_active_ctx',
 '_add_alias',
 '_add_contexts',
 '_add_defaults',
 '_add_derived_dimension',
 '_add_dimension',
 '_add_gases',
 '_add_group',
 '_add_gwp_to_context',
 '_add_mass_emissions_joint_version',
 '_add_metric_conversions',
 '_add_metric_conversions_from_df',
 '_add_prefix',
 '_add_ref_of_log_or_offset_unit',
 '_add_system',
 '_add_transformations_to_context',
 '_add_unit',
 '_adders',
 '_after_init',
 '_base_units',
 '_base_units_cache',
 '_build_cache',
 '_cache',
 '_caches',
 '_context_units',
 '_contexts',
 '_contexts_added',
 '_convert',
 '_dedup_candidates',
 '_def_parser',
 '_default_system_name',
 '_defaults',
 '_dimensions',
 '_diskcache',
 '_eval_token',
 '_filename',
 '_get_base_units',
 '_get_compatible_units',
 '_get_conversion_factor',
 '_get_dimensionality',
 '_get_dimensionality_ratio',
 '_get_dimensionality_recurse',
 '_get_root_units',
 '_get_root_units_recurse',
 '_get_symbol',
 '_groups',
 '_helper_adder',
 '_helper_dispatch_adder',
 '_helper_single_adder',
 '_init_dynamic_classes',
 '_initialized',
 '_is_multiplicative',
 '_is_protocol',
 '_metric_conversions',
 '_non_int_type',
 '_on_redefinition',
 '_parse_unit_name',
 '_parse_units_as_container',
 '_prefixes',
 '_redefine',
 '_register_adder',
 '_register_definition_adders',
 '_suffixes',
 '_switch_context_cache_and_units',
 '_systems',
 '_units',
 '_units_casei',
 '_validate_and_extract',
 '_yield_unit_triplets',
 'a',
 'a0',
 'a_0',
 'a_u_action',
 'a_u_current',
 'a_u_electric_field',
 'a_u_energy',
 'a_u_force',
 'a_u_intensity',
 'a_u_length',
 'a_u_mass',
 'a_u_temp',
 'a_u_time',
 'abA',
 'abC',
 'abF',
 'abH',
 'abS',
 'abV',
 'abampere',
 'abcoulomb',
 'aberdeen',
 'abfarad',
 'abhenry',
 'abmho',
 'abohm',
 'absiemens',
 'abvolt',
 'abΩ',
 'acre',
 'acre_feet',
 'acre_foot',
 'add_context',
 'add_standards',
 'alpha',
 'ammonia',
 'amp',
 'ampere',
 'ampere_hour',
 'ampere_turn',
 'amu',
 'angstrom',
 'angstrom_star',
 'angular_degree',
 'angular_minute',
 'angular_second',
 'annum',
 'ap_dr',
 'ap_lb',
 'ap_oz',
 'apothecary_dram',
 'apothecary_ounce',
 'apothecary_pound',
 'arc_minute',
 'arc_second',
 'arcdeg',
 'arcdegree',
 'arcmin',
 'arcminute',
 'arcsec',
 'arcsecond',
 'are',
 'astronomical_unit',
 'at',
 'atm',
 'atm_l',
 'atmosphere',
 'atmosphere_liter',
 'atomic_mass_constant',
 'atomic_unit_of_action',
 'atomic_unit_of_current',
 'atomic_unit_of_electric_field',
 'atomic_unit_of_energy',
 'atomic_unit_of_force',
 'atomic_unit_of_intensity',
 'atomic_unit_of_length',
 'atomic_unit_of_mass',
 'atomic_unit_of_temperature',
 'atomic_unit_of_time',
 'au',
 'auto_reduce_dimensions',
 'autoconvert_offset_to_baseunit',
 'autoconvert_to_preferred',
 'avdp_dram',
 'avdp_ounce',
 'avdp_pound',
 'avogadro_constant',
 'avogadro_number',
 'avoirdupois_dram',
 'avoirdupois_ounce',
 'avoirdupois_pound',
 'b',
 'bag',
 'bar',
 'barad',
 'barie',
 'barn',
 'barrel',
 'barrie',
 'baryd',
 'barye',
 'baud',
 'bbl',
 'becquerel',
 'beer_barrel',
 'beer_bbl',
 'big_point',
 'biot',
 'biot_turn',
 'bit',
 'bits_per_pixel',
 'black_carbon',
 'blob',
 'board_feet',
 'board_foot',
 'bohr',
 'bohr_magneton',
 'bohr_radius',
 'boiler_horsepower',
 'boltzmann_constant',
 'bp',
 'bpp',
 'bps',
 'british_thermal_unit',
 'bu',
 'buckingham',
 'bushel',
 'butane',
 'byte',
 'c',
 'cC3F6',
 'cC4F8',
 'c_0',
 'c_1',
 'c_2',
 'cables_length',
 'cache_folder',
 'cal',
 'cal_15',
 'cal_it',
 'cal_th',
 'calorie',
 'candela',
 'candle',
 'carat',
 'carbon',
 'carbon_dioxide',
 'carbon_monoxide',
 'case_sensitive',
 'cc',
 'cd',
 'celsius',
 'centimeter',
 'centimeter_H2O',
 'centimeter_Hg',
 'centimeter_Hg_0C',
 'centipoise',
 'centuries',
 'century',
 'chain',
 'characteristic_impedance_of_vacuum',
 'check',
 'cicero',
 'circle',
 'circular_mil',
 'classical_electron_radius',
 'clausius',
 'cmH2O',
 'cmHg',
 'cm_1',
 'cm_H2O',
 'cm_Hg',
 'cmil',
 'common_year',
 'conductance_quantum',
 'context',
 'conventional_ampere_90',
 'conventional_coulomb_90',
 'conventional_farad_90',
 'conventional_henry_90',
 'conventional_josephson_constant',
 'conventional_mercury',
 'conventional_ohm_90',
 'conventional_volt_90',
 'conventional_von_klitzing_constant',
 'conventional_water',
 'conventional_watt_90',
 'convert',
 'cooling_tower_ton',
 'coulomb',
 'coulomb_constant',
 'count',
 'counts_per_second',
 'cp',
 'cps',
 'css_pixel',
 'ct',
 'cu_ft',
 'cu_in',
 'cu_yd',
 'cubic_centimeter',
 'cubic_feet',
 'cubic_foot',
 'cubic_inch',
 'cubic_yard',
 'cup',
 'curie',
 'cwt',
 'cycle',
 'd',
 'dB',
 'dBm',
 'dBu',
 'd_220',
 'dalton',
 'darcy',
 'day',
 'debye',
 'decade',
 'decibel',
 'decibelmicrowatt',
 'decibelmilliwatt',
 'decimeter',
 'decitex',
 'default_as_delta',
 'default_format',
 'default_system',
 'define',
 'deg',
 'degC',
 'degF',
 'degK',
 'degR',
 'degRe',
 'degree',
 'degreeC',
 'degreeF',
 'degreeK',
 'degreeR',
 'degreeRe',
 'degree_Celsius',
 'degree_Fahrenheit',
 'degree_Kelvin',
 'degree_Rankine',
 'degree_Reaumur',
 'degree_Réaumur',
 'delta_celsius',
 'delta_degC',
 'delta_degF',
 'delta_degRe',
 'delta_degreeC',
 'delta_degreeF',
 'delta_degreeRe',
 'delta_degree_Celsius',
 'delta_degree_Fahrenheit',
 'delta_degree_Reaumur',
 'delta_degree_Réaumur',
 'delta_fahrenheit',
 'delta_reaumur',
 'delta_réaumur',
 'den',
 'denier',
 'dgal',
 'didot',
 'dirac_constant',
 'disable_contexts',
 'dot',
 'dots_per_inch',
 'dpi',
 'dqt',
 'dr',
 'drachm',
 'dram',
 'dry_barrel',
 'dry_gallon',
 'dry_pint',
 'dry_quart',
 'dtex',
 'dwt',
 'dyn',
 'dyne',
 'e',
 'eV',
 'electric_constant',
 'electrical_horsepower',
 'electron_g_factor',
 'electron_mass',
 'electron_volt',
 'elementary_charge',
 'enable_contexts',
 'entropy_unit',
 'enzyme_unit',
 'enzymeunit',
 'eon',
 'eps0',
 'eps_0',
 'epsilon_0',
 'erg',
 'esu',
 'ethane',
 'eu',
 'eulers_number',
 'fahrenheit',
 'farad',
 'faraday',
 'faraday_constant',
 'fathom',
 'feet',
 'feet_H2O',
 'femtometer',
 'fermi',
 'fifteen_degree_calorie',
 'fifth',
 'fine_structure_constant',
 'first_radiation_constant',
 'fldr',
 ...]

Additional units can be added to the unit registry using define as shown below. By default, scmdata.units.UNIT_REGISTRY uses the same registry as openscm_units.unit_registry so any additional units will be available for any other packages which use openscm_units.unit_registry.

unit_registry.define("population = [population]")

assert "population" in dir(unit_registry)

Using Pint Directly

For completeness, below we show how to use pint directly. Note that all of these operations are used by UnitConverter so the user shouldn’t ever have to access pint in this way.

With the unit_registry, we can also create Pint variables/arrays which are unit aware.

one_carbon = 1 * unit_registry("C")
print(one_carbon)
1 C
type(one_carbon)
pint.Quantity
one_co2 = 1 * unit_registry.CO2
three_sulfur = 3 * unit_registry.S

Pint quantities also print in an intuitive way.

print(one_co2)
print(three_sulfur)
1 CO2
3 S

We can convert them to base units or to each other.

print(one_carbon.to_base_units())
print(one_co2.to("C"))
print(three_sulfur.to("SO2"))
1 C
0.2727272727272727 C
6.0 SO2

Operations are units aware.

print(one_carbon + one_co2)
print(one_carbon * one_co2)
print((one_carbon * one_co2).to_base_units())
print(one_carbon / one_co2)
print((one_carbon / one_co2).to_base_units())
1.2727272727272727 C
1 C * CO2
0.2727272727272727 C ** 2
1.0 C / CO2
3.666666666666667 dimensionless

If we have compound units (e.g. emissions units which are [mass] * [substance] / [time]), we can convert any bit of the unit we want.

eg1 = 1 * unit_registry("Mt") * unit_registry("C") / unit_registry("yr")
print(eg1)
eg2 = 5 * unit_registry("t") * unit_registry("CO2") / unit_registry("s")
print(eg2)
1.0 C * megametric_ton / a
5.0 CO2 * metric_ton / second
print(eg1.to("Gt CO2 / day"))
print(eg2.to("Gt C / yr"))
1.0038786219484371e-05 CO2 * gigametric_ton / day
0.04303309090909091 C * gigametric_ton / a

Contexts

With a context, we can use metric conversion definitions to do emissions conversions that would otherwise raise a DimensionalityError. For example, converting CO2 to N2O using AR4GWP100 (where 298 tCO2 = 1 tN2O).

ar4gwp100uc = UnitConverter("N2O", "CO2", context="AR4GWP100")
ar4gwp100uc.convert_from(1)
298.0
ar4gwp100uc = UnitConverter("N2O", "CH4", context="AR4GWP100")
ar4gwp100uc.convert_from(1)
11.92

We can see which contexts we have (which we can use for e.g. metric conversions).

ar4gwp100uc.contexts
['Gaussian',
 'Gau',
 'ESU',
 'esu',
 'spectroscopy',
 'sp',
 'boltzmann',
 'energy',
 'chemistry',
 'chem',
 'textile',
 'CH4_conversions',
 'N2O_conversions',
 'NOx_conversions',
 'NH3_conversions',
 'SARGWP100',
 'AR4GWP100',
 'AR5GWP100',
 'AR5CCFGWP100',
 'AR6GWP100',
 'AR6GWP20',
 'AR6GWP500',
 'AR6GTP100']

Such context dependent conversions can also be done directly with Pint.

base = 1 * unit_registry("N2O")
with unit_registry.context("AR4GWP100"):
    print(one_carbon)
    print(one_carbon.to("CO2"))
    print(
        one_carbon.to("N2O") + 3 * unit_registry.N2O
    )  # I am not sure why you need to force the conversion of `a` first...
1 C
3.666666666666667 CO2
3.0123042505592843 N2O

Without a context to tell us about metrics, if we try to do an invalid conversion, a DimensionalityError will be raised.

try:
    ar4gwp100uc = UnitConverter("N2O", "CO2")
    ar4gwp100uc.convert_from(1)
except DimensionalityError:
    traceback.print_exc(limit=0, chain=False)
pint.errors.DimensionalityError: Cannot convert from 'N2O' ([nitrous_oxide]) to 'CO2' ([carbon])
try:
    base.to("CO2")
except DimensionalityError:
    traceback.print_exc(limit=0, chain=False)
pint.errors.DimensionalityError: Cannot convert from 'N2O' ([nitrous_oxide]) to 'CO2' ([carbon])

If the context you use does not have the conversion you request, a warning will be raised. Any subsequent conversions will result in NaN’s.

# modify the way the warning appears to remove the path,
# thank you https://stackoverflow.com/a/26433913
def custom_formatting(message, category, filename, lineno, file=None, line=None):
    """
    Create a format string for a warning message
    """
    return f"{category.__name__}: {message}\n"


warnings.formatwarning = custom_formatting

ucnan = UnitConverter("N2O", "Halon2402", context="SARGWP100")
ucnan.convert_from(1)
UserWarning: No conversion from N2O to Halon2402 available, nan will be returned upon conversion
nan