def write_textual_reel_header(fh, lines, encoding): """Write the SEG Y card image header, also known as the textual header Args: fh: A file-like object open in binary mode positioned such that the beginning of the textual header will be the next byte to read. lines: An iterable series of forty lines, each of which must be a Unicode string of CARD_LENGTH characters. The first three characters of each line are often "C 1" to "C40" (as required by the SEG Y standard) although this is not enforced by this function, since many widespread SEG Y readers and writers do not adhere to this constraint. To produce a SEG Y compliant series of header lines consider using the format_standard_textual_header() function. Any lines longer than CARD_LENGTH characters will be truncated without warning. Any excess lines over CARDS_PER_HEADER will be discarded. Short or omitted lines will be padded with spaces. encoding: Typically 'cp037' for EBCDIC or 'ascii' for ASCII. Post-condition: The file pointer in fh will be positioned at the first byte following the textual header. Raises: UnsupportedEncodingError: If encoding is neither EBCDIC nor ASCII. UnicodeError: If the data provided in lines cannot be encoded with the encoding. """ if not is_supported_encoding(encoding): raise UnsupportedEncodingError("Writing textual reel header", encoding) fh.seek(0) padded_lines = [line.encode(encoding).ljust(CARD_LENGTH, ' '.encode(encoding))[:CARD_LENGTH] for line in pad(lines, padding='', size=CARDS_PER_HEADER)] joined_header = EMPTY_BYTE_STRING.join(padded_lines) assert len(joined_header) == 3200 fh.write(joined_header) fh.seek(TEXTUAL_HEADER_NUM_BYTES)