<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def write_data(filename, data, data_format=None, compress=False, add=False):
 """ Write image data to file Function to write image data to specified file. If file format is not provided explicitly, it is guessed from the filename extension. If format is TIFF, geo information and compression can be optionally added. :param filename: name of file to write data to :type filename: str :param data: image data to write to file :type data: numpy array :param data_format: format of output file. Default is ``None`` :type data_format: MimeType :param compress: whether to compress data or not. Default is ``False`` :type compress: bool :param add: whether to append to existing text file or not. Default is ``False`` :type add: bool :raises: exception if numpy format is not supported or file cannot be written """

    create_parent_folder(filename)

    if not isinstance(data_format, MimeType):
        data_format = get_data_format(filename)

    if data_format.is_tiff_format():
        return write_tiff_image(filename, data, compress)
    if data_format.is_image_format():
        return write_image(filename, data)
    if data_format is MimeType.TXT:
        return write_text(filename, data, add=add)

    try:
        return {
            MimeType.CSV: write_csv,
            MimeType.JSON: write_json,
            MimeType.XML: write_xml,
            MimeType.GML: write_xml
        }[data_format](filename, data)
    except KeyError:
        raise ValueError('Writing data format .{} is not supported'.format(data_format.value))