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(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 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)