
import pandas as pd
import scipy.io as sio

import sys

if len(sys.argv) != 2:
    print ("Usage: python ", sys.argv[0], " Calculixbasename")
    quit()
    
sti_filename = sys.argv[1] + ".sti"
mas_filename = sys.argv[1] + ".mas"

c_filename = sys.argv[1] + "_C.mtx"
m_filename = sys.argv[1] + "_M.mtx"



df_sti = pd.read_csv(sti_filename,sep = ' +', engine='python', header=None)

num_col = df_sti.iloc[:,0].max()
num_row = df_sti.iloc[:,1].max()
print ("Rows: ",num_row,"   Columns: ",num_col)
if num_row != num_col:
    print(" rows and col must match!")
    quit()
    

mat_dim = num_col

from scipy.sparse import lil_matrix



C_mat = lil_matrix((mat_dim,mat_dim))
for index, row_calculix in df_sti.iterrows():
    C_mat[row_calculix[0]-1,row_calculix[1]-1] = row_calculix[2]
    C_mat[row_calculix[1]-1,row_calculix[0]-1] = row_calculix[2]

sio.mmwrite(c_filename, C_mat)


df_mas = pd.read_csv(mas_filename,sep = ' +', engine='python')

M_mat = lil_matrix((mat_dim,mat_dim))
for index, row_calculix in df_mas.iterrows():
    M_mat[row_calculix[0]-1,row_calculix[1]-1] = row_calculix[2]
    M_mat[row_calculix[1]-1,row_calculix[0]-1] = row_calculix[2]

sio.mmwrite(m_filename, M_mat)


