Skip to content

Commit

Permalink
lib.fieldio: improve compatibility with qio by adding a null byte to …
Browse files Browse the repository at this point in the history
…xml data
  • Loading branch information
jxy committed Oct 11, 2023
1 parent e943c1f commit ba4ba46
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions lib/fieldio.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ def xmlFind(s,r):
raise ValueError(f'finding record {r} in unsupported xml: {s}')
return m[0]

def trimNull(d):
n = d.find(b'\0')
if n>=0:
return d[:n]
else:
return d

def printLimeHeaders(file):
f = open(file,'rb')
while True:
Expand All @@ -51,7 +58,7 @@ def printLimeHeaders(file):
mend = 1 & mbeg_end_res>>14
mres = ~(3<<14) & mbeg_end_res
print(f'mbeg mend res: {mbeg} , {mend} , {mres}')
type = type[:type.find(b'\0')]
type = trimNull(type)
print(f'size: {size}')
print(f'type: {type}')
loca = f.tell()
Expand All @@ -60,9 +67,7 @@ def printLimeHeaders(file):
next = (size+7)//8*8
else:
data = f.read(size)
s = data.find(b'\0')
if s>0:
data = data[:s]
data = trimNull(data)
print(f'* DATA: {data}')
next = 7-(size+7)%8
# print(f'next: {next}')
Expand Down Expand Up @@ -98,7 +103,7 @@ def logput(*args,**kwargs):
# mend = 1 & mbeg_end_res>>14
# mres = ~(3<<14) & mbeg_end_res
# logput(f'mbeg mend res: {mbeg} , {mend} , {mres}')
type = type[:type.find(b'\0')]
type = trimNull(type)
# logput(f'size: {size}')
# logput(f'type: {type}')
loca = f.tell()
Expand All @@ -110,7 +115,7 @@ def logput(*args,**kwargs):
next = 7-(size+7)%8
else:
data = f.read(size)
data = data[:data.find(b'\0')]
data = trimNull(data)
if type==b'scidac-private-file-xml':
# logput('version: ',xmlFind(data,'version'))
spacetime = int(xmlFind(data,'spacetime'))
Expand Down Expand Up @@ -240,16 +245,16 @@ def writeLattice(gauge, file):
binary = numpy.ascontiguousarray(gauge, dtype=f'>c{latprec}').tobytes()
suma,sumb = scidacChecksum(binary, vol, latdatacount*lattypesize)

scidac_private_file = f'<?xml version="1.0" encoding="UTF-8"?><scidacFile><version>1.1</version><spacetime>{latdatacount}</spacetime><dims>{dims}</dims><volfmt>0</volfmt></scidacFile>'.encode()
scidac_private_record = f'<?xml version="1.0" encoding="UTF-8"?><scidacRecord><version>1.1</version><date>{datetime.datetime.now(datetime.timezone.utc).ctime()} UTC</date><recordtype>0</recordtype><datatype>numpy.ndarray</datatype><precision>{precision}</precision><colors>{latnc}</colors><spins>1</spins><typesize>{lattypesize}</typesize><datacount>{latdatacount}</datacount></scidacRecord>'.encode()
scidac_checksum = f'<?xml version="1.0" encoding="UTF-8"?><scidacChecksum><version>1.0</version><suma>{suma:x}</suma><sumb>{sumb:x}</sumb></scidacChecksum>'.encode()
scidac_private_file = f'<?xml version="1.0" encoding="UTF-8"?><scidacFile><version>1.1</version><spacetime>{latdatacount}</spacetime><dims>{dims}</dims><volfmt>0</volfmt></scidacFile>\0'.encode()
scidac_private_record = f'<?xml version="1.0" encoding="UTF-8"?><scidacRecord><version>1.1</version><date>{datetime.datetime.now(datetime.timezone.utc).ctime()} UTC</date><recordtype>0</recordtype><datatype>numpy.ndarray</datatype><precision>{precision}</precision><colors>{latnc}</colors><spins>1</spins><typesize>{lattypesize}</typesize><datacount>{latdatacount}</datacount></scidacRecord>\0'.encode()
scidac_checksum = f'<?xml version="1.0" encoding="UTF-8"?><scidacChecksum><version>1.0</version><suma>{suma:x}</suma><sumb>{sumb:x}</sumb></scidacChecksum>\0'.encode()

f = open(file,'wb')

limeItemWrite(f, True, False, scidac_private_file, b'scidac-private-file-xml')
limeItemWrite(f, False, True, b'<?xml version="1.0"?><note>generated by NTHMC</note>', b'scidac-file-xml')
limeItemWrite(f, False, True, b'<?xml version="1.0"?><note>generated by NTHMC</note>\0', b'scidac-file-xml')
limeItemWrite(f, True, False, scidac_private_record, b'scidac-private-record-xml')
limeItemWrite(f, False, False, b'<?xml version="1.0"?><note>gauge configuration</note>', b'scidac-record-xml')
limeItemWrite(f, False, False, b'<?xml version="1.0"?><note>gauge configuration</note>\0', b'scidac-record-xml')
limeItemWrite(f, False, False, binary, b'scidac-binary-data')
limeItemWrite(f, False, True, scidac_checksum, b'scidac-checksum')

Expand Down

0 comments on commit ba4ba46

Please sign in to comment.