'Save dipole in a .dip file.
Parameters
fname : str
The name of the .dip file.'

def save(self, fname):

    fmt = '        %7.1f    %7.1f    %8.2f    %8.2f    %8.2f    %8.3f    %8.3f    %8.3f    %8.3f    %6.2f'
    header = '#            begin                    end            X    (mm)            Y    (mm)            Z    (mm)            Q(nAm)        Qx(nAm)        Qy(nAm)        Qz(nAm)                g/%'
    t = (self.times[:, np.newaxis] * 1000.0)
    gof = self.gof[:, np.newaxis]
    amp = (1000000000.0 * self.amplitude[:, np.newaxis])
    out = (t, t, (self.pos / 0.001), amp, (self.ori * amp), gof)
    fmts = dict(khi2=('                khi^2', '    %8.1f', 1.0), nfree=('        free', '    %5d', 1), vol=('        vol/mm^3', '    %9.3f', 1000000000.0), depth=('        depth/mm', '    %9.3f', 1000.0), long=('        long/mm', '    %8.3f', 1000.0), trans=('        trans/mm', '    %9.3f', 1000.0), qlong=('        Qlong/nAm', '    %10.3f', 1000000000.0), qtrans=('        Qtrans/nAm', '    %11.3f', 1000000000.0))
    for key in ('khi2', 'nfree'):
        data = getattr(self, key)
        if (data is not None):
            header += fmts[key][0]
            fmt += fmts[key][1]
            out += ((data[:, np.newaxis] * fmts[key][2]),)
    for key in ('vol', 'depth', 'long', 'trans', 'qlong', 'qtrans'):
        data = self.conf.get(key)
        if (data is not None):
            header += fmts[key][0]
            fmt += fmts[key][1]
            out += ((data[:, np.newaxis] * fmts[key][2]),)
    out = np.concatenate(out, axis=(-1))
    with open(fname, 'wb') as fid:
        fid.write('#    CoordinateSystem    "Head"\n'.encode('utf-8'))
        fid.write((header + '\n').encode('utf-8'))
        np.savetxt(fid, out, fmt=fmt)
        if (self.name is not None):
            fid.write(('##    Name    "%s    dipoles"    Style    "Dipoles"' % self.name).encode('utf-8'))
