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 asfoo(_foo_0,_foo_1,_foo_2)
, where_foo_0
is an implicit set that contains only labels appearing along the first dimension offoo
, etc. This workaround is essential for GDX files where*
is large; otherwise, loadingfoo
as declared raisesMemoryError
.File
is a subclass ofxarray.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 thexarray.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:
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']
As attributes of the
File
:>>> f.myparam
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-containedxarray.DataArray
with the declared dimensions of the Symbol (and only those dimensions), which does not make reference to theFile
.
-
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.