#!/usr/bin/env python
"""
makes a sequence of lines on stdin suitable for use as FITS header
cards (i.e., expands each line to 80 chars and removes line feeds)
and writes the result to the file named on the command line.

Oversized lines cause the program to error out.
"""

import sys

CARD_SIZE = 80

def reformat(inF, outF):
	content = []
	for index, line in enumerate(inF):
		line = line.strip()
		if len(line)>CARD_SIZE:
			raise Exception("Line %d too long"%index)
		content.append(line+" "*(80-len(line)))
	outF.write("".join(content))

if __name__=="__main__":
	with open(sys.argv[1], "w") as outF:
		reformat(sys.stdin, outF)
