def pack_main_header_keywords(hdr):
"""
Replace the 'keywords' field of the given BLUE header dictionary
with the X-Midas packed (str) form of the main header keywords.
The order of the key value pairs is indeterminate.
In packed form, keys are separated from values by a single '=',
key-value pairs are separated from one another by a single '\0'
and all values are stringized using str(). Hence, each key value
pair takes up keylength + stringized value length + 2 characters.
If the resulting packed string is longer than the max allowed for
the BLUE header main keywords (96 characters) the string is
truncated. If no 'keywords' field is present or if it is an empty
dict, then the keyword fields are updated to represent an empty
main header keywords section.
"""
keydict = hdr.get('keywords', {})
if keydict:
hdr['keywords'] = '\0'.join([k + '=' + str(v)
for k,v in keydict.items()]) + '\0'
hdr['keylength'] = min(len(hdr['keywords']),
struct.calcsize(_bluestructs['HEADER']
['lookups']['keywords'][1]))
if hdr['keylength'] < len(hdr['keywords']):
print "WARNING: Main header keywords truncated"
else:
hdr['keywords'] = '\0'
hdr['keylength'] = 0
评论列表
文章目录