<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def to_file(self, filename, format='shtools', header=None, **kwargs):
 """ Save raw spherical harmonic coefficients to a file. Usage ----- x.to_file(filename, [format='shtools', header]) x.to_file(filename, [format='npy', **kwargs]) Parameters filename : str Name of the output file. format : str, optional, default = 'shtools' 'shtools' or 'npy'. See method from_file() for more information. header : str, optional, default = None A header string written to an 'shtools'-formatted file directly before the spherical harmonic coefficients. **kwargs : keyword argument list, optional for format = 'npy' Keyword arguments of numpy.save(). Description If format='shtools', the coefficients will be written to an ascii formatted file. The first line of the file is an optional user provided header line, and the spherical harmonic coefficients are then listed, with increasing degree and order, with the format l, m, coeffs[0, l, m], coeffs[1, l, m] where l and m are the spherical harmonic degree and order, respectively. If format='npy', the spherical harmonic coefficients will be saved to a binary numpy 'npy' file using numpy.save(). """

        if format is 'shtools':
            with open(filename, mode='w') as file:
                if header is not None:
                    file.write(header + '\n')
                for l in range(self.lmax+1):
                    for m in range(l+1):
                        file.write('{:d}, {:d}, {:.16e}, {:.16e}\n'
                                   .format(l, m, self.coeffs[0, l, m],
                                           self.coeffs[1, l, m]))
        elif format is 'npy':
            _np.save(filename, self.coeffs, **kwargs)
        else:
            raise NotImplementedError(
                'format={:s} not implemented'.format(repr(format)))