def write_mhd_file(filename, data, shape=None, meta_dict=None):
<EOL>ext = get_extension(filename)<EOL>fname = op.basename(filename)<EOL>if ext != '<STR_LIT>' or ext != '<STR_LIT>':<EOL><INDENT>mhd_filename = fname + '<STR_LIT>'<EOL>raw_filename = fname + '<STR_LIT>'<EOL><DEDENT>elif ext == '<STR_LIT>':<EOL><INDENT>mhd_filename = fname<EOL>raw_filename = remove_ext(fname) + '<STR_LIT>'<EOL><DEDENT>elif ext == '<STR_LIT>':<EOL><INDENT>mhd_filename = remove_ext(fname) + '<STR_LIT>'<EOL>raw_filename = fname<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>'.format(ext, filename))<EOL><DEDENT>if meta_dict is None:<EOL><INDENT>meta_dict = {}<EOL><DEDENT>if shape is None:<EOL><INDENT>shape = data.shape<EOL><DEDENT>meta_dict['<STR_LIT>']             = meta_dict.get('<STR_LIT>',             '<STR_LIT>')<EOL>meta_dict['<STR_LIT>']             = meta_dict.get('<STR_LIT>',             '<STR_LIT:True>' )<EOL>meta_dict['<STR_LIT>'] = meta_dict.get('<STR_LIT>', '<STR_LIT:False>')<EOL>meta_dict['<STR_LIT>']            = meta_dict.get('<STR_LIT>',            NUMPY_TO_MHD_TYPE[data.dtype.type])<EOL>meta_dict['<STR_LIT>']                  = meta_dict.get('<STR_LIT>',                  str(len(shape)))<EOL>meta_dict['<STR_LIT>']                = meta_dict.get('<STR_LIT>',                '<STR_LIT:U+0020>'.join([str(i) for i in shape]))<EOL>meta_dict['<STR_LIT>']        = meta_dict.get('<STR_LIT>',        raw_filename)<EOL>mhd_filename = op.join(op.dirname(filename), mhd_filename)<EOL>raw_filename = op.join(op.dirname(filename), raw_filename)<EOL>write_meta_header(mhd_filename, meta_dict)<EOL>dump_raw_data(raw_filename, data)<EOL>return mhd_filename, raw_filename<EOL>
Write the `data` and `meta_dict` in two files with names
    that use `filename` as a prefix.

    Parameters
    ----------
    filename: str
        Path to the output file.
        This is going to be used as a preffix.
        Two files will be created, one with a '.mhd' extension
        and another with '.raw'. If `filename` has any of these already
        they will be taken into account to build the filenames.

    data: numpy.ndarray
        n-dimensional image data array.

    shape: tuple
        Tuple describing the shape of `data`
        Default: data.shape

    meta_dict: dict
        Dictionary with the fields of the metadata .mhd file
        Default: {}

    Returns
    -------
    mhd_filename: str
        Path to the .mhd file

    raw_filename: str
        Path to the .raw file