Accessing data from GDX files

class gdx.File(filename='', lazy=True, implicit=True, skip={})

Load the file at filename into memory.

If lazy is True (default), then the data for GDX Parameters is not loaded until each individual parameter is first accessed; otherwise all parameters except those listed in skip (default: empty) are loaded immediately.

If implicit is True (default) then, for each dimension of any GDX Parameter declared over ‘*’ (the universal set), an implicit set is constructed, containing only the labels appearing in the respective dimension of that parameter.

Note

For instance, the GAMS Parameter foo(*,*,*) is loaded as foo(_foo_0,_foo_1,_foo_2), where _foo_0 is an implicit set that contains only labels appearing along the first dimension of foo, etc. This workaround is essential for GDX files where * is large; otherwise, loading foo as declared raises MemoryError.

File is a subclass of xarray.Dataset. The GDX data is represented as follows:

  • One-dimensional GDX Sets are stored as xray coordinates.
  • GDX Parameters and multi-dimensional GDX Sets are stored as xarray.DataArray variables within the xarray.Dataset.
  • Other information and metadata on GDX Symbols is stored as attributes of the File, or attributes of individual data variables or coordinates.

Individual Symbols are thus available in one of three ways:

  1. As dict-like members of the xarray.Dataset; see the xarray documentation for further examples.

    >>> from gdx import File
    >>> f = File('example.gdx')
    >>> f['myparam']
    
  2. As attributes of the File:

    >>> f.myparam
    
  3. Using get_symbol_by_index(), using the numerical index of the Symbol within the GDX file.

dealias(name)

Identify the GDX Symbol that name refers to, and return the corresponding xarray.DataArray.

extract(name)

Extract the GAMS Symbol name from the dataset.

The Sets and Parameters in the File can be accessed directly, as e.g. f[‘name’]; but for more complex xarray operations, such as concatenation and merging, this carries along sub-Sets and other Coordinates which confound xarray.

extract() returns a self-contained xarray.DataArray with the declared dimensions of the Symbol (and only those dimensions), which does not make reference to the File.

get_symbol_by_index(index)

Retrieve the GAMS Symbol from the index-th position of the File.

info(name)

Informal string representation of the Symbol with name.

parameters()

Return a list of all GDX Parameters.

set(name, as_dict=False)

Return the elements of GAMS Set name.

Because xarray stores non-null labels for each element of a coord, a GAMS sub-Set will contain some '' elements, corresponding to elements of the parent Set which do not appear in name. set() returns the elements without these placeholders.

sets()

Return a list of all GDX Sets.