def dump_GDX_file(H, filename): assert gdxcc.gdxOpenRead(H, filename)[0], "Couldn't open %s" % filename info = gdxx.file_info(H) print "* File Version : %s" % info["version"] print "* Producer : %s" % info["producer"] print "* Symbols : %d" % info["symbol_count"] print "* Unique Elements: %d" % info["element_count"] print "$ontext" for i in range(1, info["symbol_count"] + 1): sinfo = gdxx.symbol_info(H, i) print "%-15s %3d %-12s %s" % (sinfo["name"], sinfo["dims"], sinfo["typename"], sinfo["description"]) print "$offtext\n$onempty onembedded" for i in range(1, info["symbol_count"] + 1): sinfo = gdxx.symbol_info(H, i) name = "%s %s" % (sinfo["full_typename"], sinfo["name"]) dim_string = "" if sinfo["dims"] > 0: dim_string = "(" for j in sinfo["domain"]: if j["index"] > 0: dim_string += "," d = sinfo["domain"][j["index"]] dim_string += d["key"] dim_string += ")" desc = sinfo["description"] sm = '"' if "'" in desc else "'" print "%s%s %s%s%s /" % (name, dim_string, sm, desc, sm) ok, records = gdxcc.gdxDataReadStrStart(H, i) for i in range(records): ok, elements, values, afdim = gdxcc.gdxDataReadStr(H) if not ok: raise GDX_error(H, "Error in gdxDataReadStr") if values[gdxcc.GMS_VAL_LEVEL] == 0: continue dim_string = "" for d in range(sinfo["dims"]): if d > 0: dim_string += "." dim_string += "'%s'" % elements[d] value_string = "" if sinfo["type"] == gdxcc.GMS_DT_PAR or sinfo["type"] == gdxcc.GMS_DT_VAR: value_string = "%g" % values[gdxcc.GMS_VAL_LEVEL] if sinfo["type"] == gdxcc.GMS_DT_SET: ret, description, node = gdxcc.gdxGetElemText(H, int(values[gdxcc.GMS_VAL_LEVEL])) if ret != 0: sm = '"' if "'" in description else "'" value_string = "%s%s%s" % (sm, description, sm) print "%s %s" % (dim_string, value_string) print "/;\n"
def dump_GDX_file(H, filename): assert gdxcc.gdxOpenRead(H, filename)[0], "Couldn't open %s" % filename info = gdxx.file_info(H) print "* File Version : %s" % info["version"] print "* Producer : %s" % info["producer"] print "* Symbols : %d" % info["symbol_count"] print "* Unique Elements: %d" % info["element_count"] print "$ontext" for i in range(1, info["symbol_count"]+1): sinfo = gdxx.symbol_info(H, i) print "%-15s %3d %-12s %s" % (sinfo["name"], sinfo["dims"], sinfo["typename"], sinfo["description"]) print "$offtext\n$onempty onembedded" for i in range(1, info["symbol_count"]+1): sinfo = gdxx.symbol_info(H, i) name = "%s %s" % (sinfo["full_typename"], sinfo["name"]) dim_string = "" if sinfo["dims"] > 0: dim_string = "(" for j in sinfo["domain"]: if j["index"] > 0: dim_string += "," d = sinfo["domain"][j["index"]] dim_string += d["key"] dim_string += ")" desc = sinfo["description"] sm = '"' if "'" in desc else "'" print "%s%s %s%s%s /" % (name, dim_string, sm, desc, sm) ok, records = gdxcc.gdxDataReadStrStart(H, i) for i in range(records): ok, elements, values, afdim = gdxcc.gdxDataReadStr(H) if not ok: raise GDX_error(H, "Error in gdxDataReadStr") if values[gdxcc.GMS_VAL_LEVEL] == 0: continue dim_string = "" for d in range(sinfo["dims"]): if d > 0: dim_string += "." dim_string += "'%s'" % elements[d] value_string = "" if sinfo["type"] == gdxcc.GMS_DT_PAR or sinfo["type"] == gdxcc.GMS_DT_VAR: value_string = "%g" % values[gdxcc.GMS_VAL_LEVEL] if sinfo["type"] == gdxcc.GMS_DT_SET: ret, description, node = gdxcc.gdxGetElemText(H, int(values[gdxcc.GMS_VAL_LEVEL])) if ret != 0: sm = '"' if "'" in description else "'" value_string = "%s%s%s" % (sm, description, sm) print "%s %s" % (dim_string, value_string) print "/;\n"
def __read_in_now(self): self.parent.read_symbol(self.symbol_name) assert(self.__read_in) #- Reading tools --------------------------------------------------------------- def read_symbol(H, d, name, typename, values): d[name] = True if typename == "Set" else values[gdxcc.GMS_VAL_LEVEL] if typename[0] == "V" or typename[0] == "E": # typename is 'Variable' or 'Equation' limits = OrderedDict() for i, level_name in enumerate(level_names):
def read_symbol(H, d, name, typename, values): d[name] = True if typename == "Set" else values[gdxcc.GMS_VAL_LEVEL] if typename[0] == "V" or typename[0] == "E": # typename is 'Variable' or 'Equation' limits = OrderedDict() for i, level_name in enumerate(level_names): limits[level_name] = values[i] d.setinfo(name)["limits"] = limits elif typename[0:2] == "Se": # typename is 'Set' ret, description, node = gdxcc.gdxGetElemText( H, int(values[gdxcc.GMS_VAL_LEVEL])) if ret != 0: d.setinfo(name)["description"] = description
def read_symbol(H, d, name, typename, values): if typename == "Set": d[name] = True else: d[name] = values[gdxcc.GMS_VAL_LEVEL] if typename == "Variable" or typename == "Equation": limits = {} for i in range(5): limits[level_names[i]] = values[i] d.setinfo(name)["limits"] = limits if typename == "Set": ret, description, node = gdxcc.gdxGetElemText(H, int(values[gdxcc.GMS_VAL_LEVEL])) if ret != 0: d.setinfo(name)["description"] = description
def read_symbol(H, d, name, typename, values): if typename == "Set": d[name] = True else: d[name] = values[gdxcc.GMS_VAL_LEVEL] if typename == "Variable" or typename == "Equation": limits = {} for i in range(5): limits[level_names[i]] = values[i] d.setinfo(name)["limits"] = limits if typename == "Set": ret, description, node = gdxcc.gdxGetElemText( H, int(values[gdxcc.GMS_VAL_LEVEL])) if ret != 0: d.setinfo(name)["description"] = description
def read(self, filename, gams_dir=None): if self.lazy_load: self.filename = filename self.gams_dir = gams_dir H = gdxx.open(gams_dir) assert gdxcc.gdxOpenRead(H, filename)[0], "Couldn't open %s" % filename logger.debug("Opened the file. " + gdxpds.memory_use_str()) info = gdxx.file_info(H) for k in info: if not k in self.file_info: self.file_info[k] = info[k] logger.debug("Retrieved the file info. " + gdxpds.memory_use_str()) # read the universal set uinfo = gdxx.symbol_info(H, 0) for k in uinfo: if not k in self.universal_info: self.universal_info[k] = uinfo[k] logger.debug("Read the universal set. " + gdxpds.memory_use_str()) ok, records = gdxcc.gdxDataReadStrStart(H, 0) for i in range(records): ok, elements, values, afdim = gdxcc.gdxDataReadStr(H) if not ok: raise gdxx.GDX_error(H, "Error in gdxDataReadStr") key = elements[0] ret, description, node = gdxcc.gdxGetElemText( H, int(values[gdxcc.GMS_VAL_LEVEL])) if ret == 0: description = None self.add_key(key, description) all_keys = {} # Read all the 1-D sets # Map backwards so we have a map from every set key back to all the sets it's in set_map = {} for i in range(1, info["symbol_count"] + 1): sinfo = gdxx.symbol_info(H, i) if sinfo["typename"] == "Set" and sinfo["dims"] == 1: self.add_symbol(sinfo) symbol_name = sinfo["name"] all_keys[symbol_name] = [{}] keys = all_keys[symbol_name] symbol = self[symbol_name] symbol._gdxdim__read_in = True ok, records = gdxcc.gdxDataReadStrStart(H, i) for i in range(records): ok, elements, values, afdim = gdxcc.gdxDataReadStr(H) if not ok: raise gdxx.GDX_error(H, "Error in gdxDataReadStr") e = elements[0] read_symbol(H, symbol, e, sinfo["typename"], values) if not e in set_map: set_map[e] = {} set_map[e][symbol_name] = True keys[0][e] = True # Read all the other symbols for i in range(1, info["symbol_count"] + 1): sinfo = gdxx.symbol_info(H, i) if sinfo["typename"] == "Set" and sinfo["dims"] == 1: continue self.add_symbol(sinfo) if self.lazy_load and sinfo["dims"] > 0: continue logger.debug("Reading {}. ".format(sinfo['name']) + gdxpds.memory_use_str()) self.__read_one_symbol(H, sinfo, all_keys) logger.debug("Read {}. ".format(sinfo['name']) + gdxpds.memory_use_str()) gdxcc.gdxClose(H) gdxcc.gdxFree(H) logger.debug("Closed the gdx file. " + gdxpds.memory_use_str()) guess_domains(self, set_map, all_keys) guess_ancestor_domains(self) logger.debug("Finished guessing domains. " + gdxpds.memory_use_str()) if self.lazy_load: self.set_map = set_map self.all_keys = all_keys
info["type"] = typecode info["typename"] = typename # -- Read a gdx file ----------------------------------------------------------- def read(self, filename, gams_dir=None): if self.lazy_load: self.filename = filename self.gams_dir = gams_dir H = gdxx.open(gams_dir) assert gdxcc.gdxOpenRead(H, filename)[0], "Couldn't open %s" % filename logger.debug("Opened the file. " + gdxpds.memory_use_str()) info = gdxx.file_info(H) for k in info: if not k in self.file_info: self.file_info[k] = info[k] logger.debug("Retrieved the file info. " + gdxpds.memory_use_str()) # read the universal set uinfo = gdxx.symbol_info(H, 0) for k in uinfo: if not k in self.universal_info: self.universal_info[k] = uinfo[k] logger.debug("Read the universal set. " + gdxpds.memory_use_str()) ok, records = gdxcc.gdxDataReadStrStart(H, 0) for i in range(records): ok, elements, values, afdim = gdxcc.gdxDataReadStr(H) if not ok: raise gdxx.GDX_error(H, "Error in gdxDataReadStr") key = elements[0] ret, description, node = gdxcc.gdxGetElemText(H, int(values[gdxcc.GMS_VAL_LEVEL])) if ret == 0: description = None self.add_key(key, description) all_keys = {} # Read all the 1-D sets # Map backwards so we have a map from every set key back to all the sets it's in set_map = {} for i in range(1, info["symbol_count"]+1): sinfo = gdxx.symbol_info(H, i) if sinfo["typename"] == "Set" and sinfo["dims"] == 1: self.add_symbol(sinfo) symbol_name = sinfo["name"] all_keys[symbol_name] = [{}] keys = all_keys[symbol_name] symbol = self[symbol_name] symbol._gdxdim__read_in = True ok, records = gdxcc.gdxDataReadStrStart(H, i) for i in range(records): ok, elements, values, afdim = gdxcc.gdxDataReadStr(H) if not ok: raise gdxx.GDX_error(H, "Error in gdxDataReadStr") e = elements[0] read_symbol(H, symbol, e, sinfo["typename"], values) if not e in set_map: set_map[e] = {} set_map[e][symbol_name] = True keys[0][e] = True # Read all the other symbols for i in range(1, info["symbol_count"]+1): sinfo = gdxx.symbol_info(H, i) if sinfo["typename"] == "Set" and sinfo["dims"] == 1: continue self.add_symbol(sinfo)
def read(self, filename, gams_dir=None): H = gdxx.open(gams_dir) assert gdxcc.gdxOpenRead(H, filename)[0], "Couldn't open %s" % filename info = gdxx.file_info(H) for k in info: if not k in self.file_info: self.file_info[k] = info[k] # read the universal set uinfo = gdxx.symbol_info(H, 0) for k in uinfo: if not k in self.universal_info: self.universal_info[k] = uinfo[k] ok, records = gdxcc.gdxDataReadStrStart(H, 0) for i in range(records): ok, elements, values, afdim = gdxcc.gdxDataReadStr(H) if not ok: raise gdxx.GDX_error(H, "Error in gdxDataReadStr") key = elements[0] ret, description, node = gdxcc.gdxGetElemText(H, int(values[gdxcc.GMS_VAL_LEVEL])) if ret == 0: description = None self.add_key(key, description) all_keys = {} # Read all the 1-D sets # Map backwards so we have a map from every set key back to all the sets it's in set_map = {} for i in range(1, info["symbol_count"]+1): sinfo = gdxx.symbol_info(H, i) if sinfo["typename"] == "Set" and sinfo["dims"] == 1: self.add_symbol(sinfo) symbol_name = sinfo["name"] all_keys[symbol_name] = [{}] keys = all_keys[symbol_name] symbol = self[symbol_name] ok, records = gdxcc.gdxDataReadStrStart(H, i) for i in range(records): ok, elements, values, afdim = gdxcc.gdxDataReadStr(H) if not ok: raise gdxx.GDX_error(H, "Error in gdxDataReadStr") e = elements[0] read_symbol(H, symbol, e, sinfo["typename"], values) if not e in set_map: set_map[e] = {} set_map[e][symbol_name] = True keys[0][e] = True # Read all the other symbols for i in range(1, info["symbol_count"]+1): sinfo = gdxx.symbol_info(H, i) if sinfo["typename"] == "Set" and sinfo["dims"] == 1: continue self.add_symbol(sinfo) symbol_name = sinfo["name"] all_keys[symbol_name] = [] keys = all_keys[symbol_name] for d in range(sinfo["dims"]): keys.append({}) ok, records = gdxcc.gdxDataReadStrStart(H, i) for i in range(records): ok, elements, values, afdim = gdxcc.gdxDataReadStr(H) if not ok: raise gdxx.GDX_error(H, "Error in gdxDataReadStr") if sinfo["dims"] == 0: read_symbol(H, self, symbol_name, sinfo["typename"], values) else: symbol = self[sinfo["name"]] current = symbol for d in range(sinfo["dims"]-1): key = elements[d] keys[d][key] = True if not key in current: current[key] = gdxdim(self) current = current[key] d = sinfo["dims"]-1 key = elements[d] keys[d][key] = True read_symbol(H, current, key, sinfo["typename"], values) gdxcc.gdxClose(H) gdxcc.gdxFree(H) guess_domains(self, set_map, all_keys) guess_ancestor_domains(self)
def _get_set_assoc_text(self, value): ret, assoc_text, _node = gdxcc.gdxGetElemText(self._h, int(value)) if ret: return assoc_text else: return None
def read(self, filename, gams_dir=None): H = gdxx.open(gams_dir) assert gdxcc.gdxOpenRead(H, filename)[0], "Couldn't open %s" % filename info = gdxx.file_info(H) for k in info: if not k in self.file_info: self.file_info[k] = info[k] # read the universal set uinfo = gdxx.symbol_info(H, 0) for k in uinfo: if not k in self.universal_info: self.universal_info[k] = uinfo[k] ok, records = gdxcc.gdxDataReadStrStart(H, 0) for i in range(records): ok, elements, values, afdim = gdxcc.gdxDataReadStr(H) if not ok: raise gdxx.GDX_error(H, "Error in gdxDataReadStr") key = elements[0] ret, description, node = gdxcc.gdxGetElemText( H, int(values[gdxcc.GMS_VAL_LEVEL])) if ret == 0: description = None self.add_key(key, description) all_keys = {} # Read all the 1-D sets # Map backwards so we have a map from every set key back to all the sets it's in set_map = {} for i in range(1, info["symbol_count"] + 1): sinfo = gdxx.symbol_info(H, i) if sinfo["typename"] == "Set" and sinfo["dims"] == 1: self.add_symbol(sinfo) symbol_name = sinfo["name"] all_keys[symbol_name] = [{}] keys = all_keys[symbol_name] symbol = self[symbol_name] ok, records = gdxcc.gdxDataReadStrStart(H, i) for i in range(records): ok, elements, values, afdim = gdxcc.gdxDataReadStr(H) if not ok: raise gdxx.GDX_error(H, "Error in gdxDataReadStr") e = elements[0] read_symbol(H, symbol, e, sinfo["typename"], values) if not e in set_map: set_map[e] = {} set_map[e][symbol_name] = True keys[0][e] = True # Read all the other symbols for i in range(1, info["symbol_count"] + 1): sinfo = gdxx.symbol_info(H, i) if sinfo["typename"] == "Set" and sinfo["dims"] == 1: continue self.add_symbol(sinfo) symbol_name = sinfo["name"] all_keys[symbol_name] = [] keys = all_keys[symbol_name] for d in range(sinfo["dims"]): keys.append({}) ok, records = gdxcc.gdxDataReadStrStart(H, i) for i in range(records): ok, elements, values, afdim = gdxcc.gdxDataReadStr(H) if not ok: raise gdxx.GDX_error(H, "Error in gdxDataReadStr") if sinfo["dims"] == 0: read_symbol(H, self, symbol_name, sinfo["typename"], values) else: symbol = self[sinfo["name"]] current = symbol for d in range(sinfo["dims"] - 1): key = elements[d] keys[d][key] = True if not key in current: current[key] = gdxdim(self) current = current[key] d = sinfo["dims"] - 1 key = elements[d] keys[d][key] = True read_symbol(H, current, key, sinfo["typename"], values) gdxcc.gdxClose(H) gdxcc.gdxFree(H) guess_domains(self, set_map, all_keys) guess_ancestor_domains(self)