Add legend file for id mapping

See AT-563
I'd consider renaming this to something like `create_legend_writer` now that it has changed to do more than write a header.