Here is some code that places your NbyN matrix into a GWT sparse format....
import locale as LOCALE
fo = open("weights.txt", "r")
fw = open("weights.gwt", "w")
header = fo.readline()
fw.write("%s" % header)
data = fo.readlines()
for row in data:
rowVals = row.split()
rowID = rowVals[0]
weights = rowVals[1:]
for colID, weight in enumerate(weights):
w = LOCALE.atof(weight)
if w != 0.0:
fw.write("%s %i %s\n" % (rowID, colID+1, weight))
fo.close()
fw.close()
I put your format into "weights.txt" and get out "weights.gwt". This will allow you to use it directly in most of our tools. However, if you want to construct a SWM format... it is perhaps best to change the header line to:fw.write("%s %s %s\n" % (header.strip(), "NID", "WEIGHT"))
Then open the output file in excel and save in DBF format. Lastly, call the "Generate Spatial Weights Matrix" tool in ArcGIS and use the "Convert from Table" conceptualization in order to give you the binary SWM format. There are many ways to skin this cat and I really like that you are attempting to use PySAL for it. Unfortunately, I have been to busy with release to get to working with conversions in the latest and greatest PySAL. Perhaps Ill wrap some code into my WeightsUtilities to convert full NumPy arrays to SWM formats?....