Skip to content

Instantly share code, notes, and snippets.

@paulromano
Created February 28, 2013 16:05
Show Gist options
  • Save paulromano/5057822 to your computer and use it in GitHub Desktop.
Save paulromano/5057822 to your computer and use it in GitHub Desktop.
Convert endianness for ACE format cross sections
#!/usr/bin/python
import sys
from struct import pack, unpack
# Get name of original ACE file
filename = sys.argv[1]
# Open unconverted and converted files
aceOld = open(filename, 'rb')
aceNew = open(filename + '_conv', 'wb')
endianOld = '<' # little endian
endianNew = '>' # big endian
recl_length = 4096 # Record length in bytes
entries = 512 # Number of entries per record
while True:
# Check for EOF
start = aceOld.tell()
if aceOld.read(1) == '':
break
aceOld.seek(start)
# Header block
headerSpec = '10sdd10s70s10s'
header = unpack(endianOld + headerSpec, aceOld.read(116))
aceNew.write(pack(endianNew + headerSpec, *header))
print(header[0])
# ZAID combinations
zaid = unpack(endianOld + 16*'id', aceOld.read(192))
aceNew.write(pack(endianNew + 16*'id', *zaid))
# NXS array
nxs = unpack(endianOld + '16i', aceOld.read(64))
aceNew.write(pack(endianNew + '16i', *nxs))
# JXS array
jxs = unpack(endianOld + '32i', aceOld.read(128))
aceNew.write(pack(endianNew + '32i', *jxs))
# Copy bytes between end of JXS and start of XSS on next record
n_bytes = start + recl_length - aceOld.tell()
data = aceOld.read(n_bytes)
aceNew.write(data)
# XSS array
length = nxs[0]
xss = unpack(endianOld + '{0}d'.format(length), aceOld.read(length*8))
aceNew.write(pack(endianNew + '{0}d'.format(length), *xss))
# Copy remaining bytes between end of XSS and beginning of next nuclide
n_records = (length + entries - 1)/entries
n_bytes = start + recl_length*(n_records + 1) - aceOld.tell()
data = aceOld.read(n_bytes)
aceNew.write(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment