Tethys package

tethys.model

@Date: 09/20/2022 @author: Isaac Thompson (isaac.thompson@pnnl.gov) @Project: Tethys V2.0

License: BSD 2-Clause, see LICENSE and DISCLAIMER files Copyright (c) 2022, Battelle Memorial Institute

class tethys.model.Tethys(config_file=None, years=None, resolution=0.125, demand_type='withdrawals', perform_temporal=False, gcam_db=None, csv=None, output_file=None, downscaling_rules=None, proxy_files=None, map_files=None, temporal_files=None, temporal_methods=None)[source]

Bases: object

Model wrapper for Tethys

downscale(distribution, inputs, region_masks)[source]

Actual spatial downscaling happens here

Parameters
  • distribution – DataArray (sector, year, lat, lon) spatial distribution of proxies

  • inputs – DataArray (region, sector, year) demand values by region, sector, year

  • region_masks – DataArray (lat, lon, region) of bools, True if (lat, lon) belongs to region

Returns

distribution scaled to match inputs in all regions

reaggregate(region_masks=None)[source]

Reaggregate from grid cells to regions

Parameters

region_masks – boolean mask of regions, if other than the input regions

Returns

dataframe with columns region, sector, year, value

run_model()[source]
tethys.model.run_model(config_file)[source]

Run a Tethys configuration

tethys.datareader.gridded

tethys.datareader.gridded.clean_spatial_dims(ds)[source]
tethys.datareader.gridded.interp_helper(da, target_years=None)[source]

Linearly interpolate da to target_years more control over chunks, works with sparse

Parameters
  • da – xarray DataArray with source years

  • target_years – list of target years to interpolate to. If None, then interpolate to annual

Returns

da linearly interpolated to target_years

tethys.datareader.gridded.load_file(filename, target_resolution, years, variables=None, flags=None, regrid_method='extensive')[source]

Prepare a dataset from single file to be merged into a dataset of all proxies

handles many oddities found in proxies

Parameters
  • filename – name of file

  • target_resolution – resolution in degrees to regrid to

  • years – years to extract from the file

  • variables – variables to extract from the file

  • flags – list potentially containing ‘cell_area_share’ or ‘short_name_as_name’

  • regrid_method – passed along to regrid

Returns

preprocessed data set

tethys.datareader.gridded.pad_global(ds)[source]

pad inputs to global resolution

Parameters

ds – xarray Dataset

Returns

xarray Dataset with global extent

tethys.datareader.gridded.percent_to_area(ds)[source]

Convert landcover dataset from cell proportion to area

Parameters

ds – xarray Dataset where values represent proportion of grid cell

Returns

xarray Dataset where values represent area in square kilometers

tethys.datareader.gridded.regrid(ds, target_resolution, method='extensive')[source]

Simple regridding algorithm

Parameters
  • ds – xarray Dataset or DataArray, needs lat and lon and global extent

  • target_resolution – target resolution in degrees

  • method – choice of ‘extensive’ (preserves sums, default), ‘intensive’ (take average), or ‘label’ (for maps)

Returns

ds regridded to target_resolution

tethys.datareader.gridded.set_global_coords(ds, resolution)[source]

tethys.datareader.maps

tethys.datareader.maps.load_region_map(mapfile, masks=False, namefile=None, target_resolution=None, nodata=None, flip_lat=False)[source]

Load region map.

Parameters
  • mapfile – path to map file

  • masks – bool whether to convert categorical map to layer of region masks

  • namefile – optional path to csv with region names

  • target_resolution – resolution to coerce map to. If None (default), use base resolution

  • nodata – nodata value (like 9999), will be replaced with 0

  • flip_lat – bool, whether the map is “upside down”

tethys.datareader.regional

tethys.datareader.regional.elec_sector_rename(x)[source]

Helper for electricity demand sectors

tethys.datareader.regional.elec_sector_weights(gcam_db)[source]

Get the electricity sector weights from GCAM database

tethys.datareader.regional.extract_basin_name(x)[source]

Maps ‘water_td_irr_basin_C’ to ‘_basin’, and water_td_elec_C to ‘’

tethys.datareader.regional.friendly_sector_name(x)[source]

convert from GCAM water input name to friendly name

tethys.datareader.regional.greedy_match(x, names)[source]
tethys.datareader.regional.load_region_data(gcam_db, sectors, demand_type='withdrawals')[source]

Load region-scale water demand from GCAM needed to carry out a configuration

Parameters
  • gcam_db – path to GCAM database (the folder containing the .basex files)

  • sectors – GCAM sectors to filter to (friendly names will be converted to unfriendly names)

  • demand_type – ‘withdrawals’ or ‘consumption’

Returns

pandas dataframe with columns ‘region’, ‘sector’, ‘year’, ‘value’

tethys.datareader.regional.unfriendly_sector_name(x)[source]

convert from friendly name to start of GCAM name

tethys.datareader.easy_query

tethys.datareader.easy_query.easy_query(variable, year_axis=None, **kwargs)[source]

Build a query for a GCAM database

kwargs act as filters on nodes, eg. “sector=’Beef’” is converted to the xpath “[@type=’sector’ and @name=’Beef’]” Lists are also accepted, so “sector=[‘Beef’, ‘Dairy’]” matches ‘Beef’ or ‘Dairy’

Order of kwargs must follow the sector -> subsector -> technology -> input/output/etc hierarchy

Some shortcuts are supported. ‘*’ can be used as a wildcard, so input=’water_td_irr_*_C’ matches any input starting with ‘water_td_irr’ and ending with ‘_C’

Starting a filter string with ‘!’ negates it, so sector=’!elec*’ will exclude any sector names that begin with ‘elec’. Combinations are allowed, so input=[‘water_td_irr_*_C’, ‘!water_td_irr_A*’] would select all input basins, except those beginning with the letter ‘A’.

Parameters
  • variable – name of variable in database, such as ‘demand-physical’ or ‘emissions’

  • year_axis – optional name of year axis (‘year’ or ‘vintage’). If None, will be inferred from variable

  • kwargs – refer to description for details

Returns

gcamreader Query object

tethys.datareader.easy_query.get_year_axis(variable)[source]
tethys.datareader.easy_query.handle_filter(key, value)[source]

Handle a kwarg for build_query

Parameters
  • key – xml node @type attribute to match

  • value – string, or list of strings, indicating what the node @name attribute must match or not match

Returns

xpath to filter nodes

tethys.datareader.easy_query.parse_name(name)[source]

convert a string to an xpath condition the basic case converts “foo” to “@name=’foo’”

‘*’ acts as a wildcard, so “foo*” is converted to “(starts-with(@name, ‘foo’))”

Parameters

name – the string to parse

Returns

xpath condition indicated by string

tethys.tdmethods.domestic

tethys.tdmethods.domestic.temporal_distribution(model)[source]

Temporal downscaling for domestic water demand using algorithm from Wada et al. (2011)

tethys.tdmethods.electricity

tethys.tdmethods.electricity.temporal_distribution(model)[source]

Temporal downscaling of water demand for electricity generation using algorithm from Voisin et al. (2013)

tethys.tdmethods.irrigation

tethys.tdmethods.irrigation.temporal_distribution(model)[source]

Temporal downscaling of irrigation water demand