Skip to content

Instantly share code, notes, and snippets.

@gdlmx
Created June 13, 2019 13:05
Show Gist options
  • Save gdlmx/13cf4b4cb7ccb25c3f0aa18fecb6e4d6 to your computer and use it in GitHub Desktop.
Save gdlmx/13cf4b4cb7ccb25c3f0aa18fecb6e4d6 to your computer and use it in GitHub Desktop.
Preprocessing scripts for MICRESS
import numpy as np
import re, sys
def reindex_micress_vtk(vtkfilename):
pLUT = re.compile('\s*LOOKUP_TABLE\s+', flags=re.I)
headers=[]
with open(vtkfilename) as f:
for l in f:
headers.append(l.strip())
if pLUT.match(l):
break
A= np.loadtxt(vtkfilename, skiprows=len(headers))
a=set()
for i in A.reshape([A.size,]):
a.add(i)
a=sorted(list(a))
B = np.zeros(A.shape)
for i in range(len(a)):
B[A==a[i]]=i+1
B = B.astype('int')
assert np.all(B>0)
return B, headers
if __name__ == "__main__":
vtkfname = sys.argv[1]
Tab, Hdrs = reindex_micress_vtk(vtkfname)
np.savetxt('out_'+vtkfname, Tab, header='\n'.join(Hdrs) , fmt='%d', comments='')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment