'Basically copy/pasted from `_ImageBaseHDU._writedata_internal()`, but
we have to get the data\'s byte order a different way...
TODO: Might be nice to store some indication of the data\'s byte order
as an attribute or function so that we don\'t have to do this.'

def _writedata_internal(self, fileobj):

    size = 0
    if (self.data is not None):
        self.data._scale_back()
        if (sys.byteorder == 'little'):
            swap_types = ('<', '=')
        else:
            swap_types = ('<',)
        if _is_pseudo_unsigned(self.data.dtype):
            output = np.array((self.data - _unsigned_zero(self.data.dtype)), dtype='>i{}'.format(self.data.dtype.itemsize))
            should_swap = False
        else:
            output = self.data
            fname = self.data.dtype.names[0]
            byteorder = self.data.dtype.fields[fname][0].str[0]
            should_swap = (byteorder in swap_types)
        if (not fileobj.simulateonly):
            if should_swap:
                if output.flags.writeable:
                    output.byteswap(True)
                    try:
                        fileobj.writearray(output)
                    finally:
                        output.byteswap(True)
                else:
                    fileobj.writearray(output.byteswap(False))
            else:
                fileobj.writearray(output)
        size += (output.size * output.itemsize)
    return size
