Exemple #1
0
    def read(self, filename):
        """
        Opens gdx file at filename and reads meta-data. If not self.lazy_load, 
        also loads all symbols.

        Throws an Error if not self.empty.

        Throws a GdxError if any calls to gdxcc fail.
        """
        if not self.empty:
            raise Error(
                "GdxFile.read can only be used if the GdxFile is .empty")

        # open the file
        rc = gdxcc.gdxOpenRead(self.H, filename)
        if not rc[0]:
            raise GdxError(self.H, "Could not open '{}'".format(filename))
        self._filename = filename

        # read in meta-data ...
        # ... for the file
        ret, self._version, self._producer = gdxcc.gdxFileVersion(self.H)
        if ret != 1:
            raise GdxError(self.H, "Could not get file version")
        ret, symbol_count, element_count = gdxcc.gdxSystemInfo(self.H)
        logger.debug(
            "Opening '{}' with {} symbols and {} elements with lazy_load = {}."
            .format(filename, symbol_count, element_count, self.lazy_load))
        # ... for the symbols
        ret, name, dims, data_type = gdxcc.gdxSymbolInfo(self.H, 0)
        if ret != 1:
            raise GdxError(self.H,
                           "Could not get symbol info for the universal set")
        self.universal_set = GdxSymbol(name,
                                       data_type,
                                       dims=dims,
                                       file=self,
                                       index=0)
        for i in range(symbol_count):
            index = i + 1
            ret, name, dims, data_type = gdxcc.gdxSymbolInfo(self.H, index)
            if ret != 1:
                raise GdxError(
                    self.H,
                    "Could not get symbol info for symbol {}".format(index))
            self.append(
                GdxSymbol(name, data_type, dims=dims, file=self, index=index))

        # read all symbols if not lazy_load
        if not self.lazy_load:
            for symbol in self:
                symbol.load()
        return
Exemple #2
0
 def _get_symtype(self, symno):
     """Get symbol type"""
     ret, _, _, symtype = gdxcc.gdxSymbolInfo(self._h, symno)
     if ret:
         return symtype
     else:
         return None
Exemple #3
0
 def _get_symname(self, symno):
     """Get symbol name for a number"""
     ret, sym, _, _ = gdxcc.gdxSymbolInfo(self._h, symno)
     if ret:
         return sym
     else:
         return None
Exemple #4
0
 def get_sid_info(self,j):
     '''Return a dict of metadata for symbol with ID j.'''
     h = self.gdx_handle
     r, name, dims, stype = gdxcc.gdxSymbolInfo(h, j)
     assert r, '%d is not a valid symbol number' % j
     r, records, userinfo, description = gdxcc.gdxSymbolInfoX(h, j)
     assert r, '%d is not a valid symbol number' % j
     return {'name':name, 'stype':stype, 'desc':description, 'dim':dims}
Exemple #5
0
def symbol_info(H, num):
    ret, name, dims, type = gdxcc.gdxSymbolInfo(H, num)
    if ret != 1:
        raise GDX_error(H, "Couldn't get symbol info")
    ret, records, userinfo, description = gdxcc.gdxSymbolInfoX(H, num)
    if ret != 1:
        raise GDX_error(H, "Couldn't get extended symbol info")

    if type == gdxcc.GMS_DT_PAR and dims == 0:
        typename = "Scalar"
    else:
        typename = symbol_type_text[type]

    full_typename = ""
    if type == gdxcc.GMS_DT_VAR:
        full_typename = variable_type_text[userinfo] + " "
    full_typename += typename

    domain = [None] * dims
    if dims > 0 and num > 0:
        ret, gdx_domain = gdxcc.gdxSymbolGetDomain(H, num)
        if ret != 1:
            raise GDX_error(H, "Couldn't get symbol domain")
        if len(gdx_domain) < dims:
            gdx_domain = None
        for i in range(dims):
            d = gdx_domain[i] if gdx_domain else 0
            domain[i] = {"index": d}
            if d == 0:
                domain[i]["key"] = "*"
            else:
                ret, domain[i]["key"], dummy1, dummy2 = gdxcc.gdxSymbolInfo(H, d)

    return {
        "name": name,
        "number": num,
        "type": type,
        "typename": typename,
        "full_typename": full_typename,
        "dims": dims,
        "records": records,
        "userinfo": userinfo,
        "description": description,
        "domain": domain,
    }
Exemple #6
0
 def get_sid_info(self,j):
     if __gdxpy_mode__ != GDX_MODE_API:
         raise Exception('Function "get_sid_info" not available outside GDX API mode')
     h = self.gdxHandle
     r, name, dims, stype = gdxcc.gdxSymbolInfo(h, j)
     assert r, '%d is not a valid symbol number' % j
     r, records, userinfo, description = gdxcc.gdxSymbolInfoX(h, j)
     assert r, '%d is not a valid symbol number' % j
     return {'name':name, 'stype':stype, 'desc':description, 'dim':dims}
Exemple #7
0
def symbol_info(H, num):
    ret, name, dims, type = gdxcc.gdxSymbolInfo(H, num)
    if ret != 1: raise GDX_error(H, "Couldn't get symbol info")
    ret, records, userinfo, description = gdxcc.gdxSymbolInfoX(H, num)
    if ret != 1: raise GDX_error(H, "Couldn't get extended symbol info")

    if type == gdxcc.GMS_DT_PAR and dims == 0:
        typename = "Scalar"
    else:
        typename = symbol_type_text[type]

    full_typename = ""
    if type == gdxcc.GMS_DT_VAR:
        full_typename = variable_type_text[userinfo] + " "
    full_typename += typename

    domain = [None] * dims
    if dims > 0 and num > 0:
        ret, gdx_domain = gdxcc.gdxSymbolGetDomain(H, num)
        if ret != 1: raise GDX_error(H, "Couldn't get symbol domain")
        if len(gdx_domain) < dims: gdx_domain = None
        for i in range(dims):
            d = gdx_domain[i] if gdx_domain else 0
            domain[i] = { "index": d }
            if d == 0:
                domain[i]["key"] = "*"
            else:
                ret, domain[i]["key"], dummy1, dummy2 = gdxcc.gdxSymbolInfo(H, d)

    return {
      "name": name,
      "number": num,
      "type": type,
      "typename": typename,
      "full_typename": full_typename,
      "dims": dims,
      "records": records,
      "userinfo": userinfo,
      "description": description,
      "domain": domain
     }
Exemple #8
0
def gdx_to_list(gams_dir, filename, varname='all', verbose=False):
    """
    This function loads the gdx with the results of the simulation
    All results are stored in an unordered list

    :param gams_dir:    Gams working directory
    :param filename:    Path to the gdx file to be read
    :param varname:     In case online one variable is needed, specify it name (otherwise specify 'all')
    :returns:        Dictionary with all the collected values (within lists)
    """



    from gdxcc import gdxSymbolInfo, gdxCreateD, gdxOpenRead, GMS_SSSIZE, gdxDataReadDone, new_gdxHandle_tp, \
        gdxDataReadStr, gdxFindSymbol, gdxErrorStr, gdxDataReadStrStart, gdxGetLastError
    out = {}
    tgdx = tm.time()
    gdxHandle = new_gdxHandle_tp()
    gdxCreateD(gdxHandle, gams_dir, GMS_SSSIZE)

    # make sure the file path is properly formatted:
    filename = filename.replace('/', os.path.sep).replace(
        '\\\\', os.path.sep).replace('\\', os.path.sep)
    filename = str(filename)  # removing possible unicode formatting

    if not os.path.isfile(filename):
        logging.critical('Gdx file "' + filename + '" does not exist')
        sys.exit(1)

    gdxOpenRead(gdxHandle, filename)

    if varname == 'all':
        # go through all the symbols one by one and add their data to the dict
        symNr = 0
        SymbolInfo = gdxSymbolInfo(gdxHandle, 0)
        while SymbolInfo[0] > 0:
            ret, nrRecs = gdxDataReadStrStart(gdxHandle, symNr)
            assert ret, "Error in gdx data string" + gdxErrorStr(
                gdxHandle, gdxGetLastError(gdxHandle))[1]

            res = []
            for i in range(nrRecs):
                ret, elements, values, afdim = gdxDataReadStr(gdxHandle)
                res.append(elements + [values[0]])
            out[SymbolInfo[1]] = res
            symNr += 1
            SymbolInfo = gdxSymbolInfo(gdxHandle, symNr)
    else:
        # find the number of the required symbol:
        ret, symNr = gdxFindSymbol(gdxHandle, varname)
        assert ret, "Symbol not found"

        ret, nrRecs = gdxDataReadStrStart(gdxHandle, symNr)
        assert ret, "Error in gdx data string" + gdxErrorStr(
            gdxHandle, gdxGetLastError(gdxHandle))[1]

        res = []
        for i in range(nrRecs):
            ret, elements, values, afdim = gdxDataReadStr(gdxHandle)
            res.append(elements + [values[0]])
        out[varname] = res

    gdxDataReadDone(gdxHandle)
    if verbose:
        logging.info("Loading gdx file " + filename +
                     " took {}s".format(tm.time() - tgdx))
    return out
Exemple #9
0
    def _read_symbol(self, symno: int):
        """Read a GAMS symbol as a Pandas Series object

        Args:
            symno: Symbol number

        Raises:
            RuntimeError
        """

        # Get symbol info
        ret, _sym, dim, symtype = gdxcc.gdxSymbolInfo(self._h, symno)
        if not ret:
            warn("Symbol not found!")
            return None

        # Get domain of symbol
        domain = [(d if d != '*' else None) for d in self._get_domain(symno)]
        if not any(domain):
            domain = None

        # Get explanatory text
        expl_text = self._get_expl_text(symno)

        # Get length of longest label
        _label_maxlen = gdxcc.gdxSymbIndxMaxLength(self._h, symno)[0]

        # Start reading symbol
        ret, recs = gdxcc.gdxDataReadStrStart(self._h, symno)
        if not ret:
            raise Exception(
                gdxcc.gdxErrorStr(self._h, gdxcc.gdxGetLastError(self._h))[1])

        # Initialize keys and values arrays
        keys = recs * [tuple()]
        values = recs * [float()]
        if gdxcc.gdxSetHasText(self._h, symno):
            assoc_texts = recs * [str()]
        else:
            assoc_texts = None

        # Read GDX data
        # Code inside the loop is as small as possible for performance,
        # so the if statements are outside.
        if symtype == GMS_DT_SET and assoc_texts:
            for i in range(recs):
                _ret, key_arr, value_arr, _afdim = gdxcc.gdxDataReadStr(
                    self._h)
                keys[i] = (key_arr[0] if dim == 1 else tuple(key_arr)
                           )  # Squeze out dimension of 1-dim keys
                assoc_texts[i] = self._get_set_assoc_text(
                    value_arr[GMS_VAL_LEVEL])
        elif symtype == GMS_DT_SET and not assoc_texts:
            for i in range(recs):
                _ret, key_arr, _value_arr, _afdim = gdxcc.gdxDataReadStr(
                    self._h)
                keys[i] = (key_arr[0] if dim == 1 else tuple(key_arr)
                           )  # Squeze out dimension of 1-dim keys
        elif symtype == GMS_DT_PAR:
            for i in range(recs):
                _ret, key_arr, value_arr, _afdim = gdxcc.gdxDataReadStr(
                    self._h)
                keys[i] = (key_arr[0] if dim == 1 else tuple(key_arr)
                           )  # Squeze out dimension of 1-dim keys
                val = value_arr[GMS_VAL_LEVEL]
                values[i] = SPECIAL_VALUES.get(val, val)

        # Done reading
        gdxcc.gdxDataReadDone(self._h)

        # Construct GAMS symbols and return
        if symtype == GMS_DT_SET:
            return GAMSSet(keys,
                           domain,
                           expl_text=expl_text,
                           assoc_texts=assoc_texts)
        elif symtype == GMS_DT_PAR:
            if dim == 0:
                return GAMSScalar(values[0], expl_text=expl_text)
            else:
                return GAMSParameter(dict(zip(keys, values)),
                                     domain=domain,
                                     expl_text=expl_text)