<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, errors=False, **kwargs):
 """ Save spherical harmonic coefficients to a file. Usage ----- x.to_file(filename, [format='shtools', header, errors]) 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. errors : bool, optional, default = False If True, save the errors in the file (for 'shtools' formatted files only). **kwargs : keyword argument list, optional for format = 'npy' Keyword arguments of numpy.save(). Description If format='shtools', the coefficients and meta-data will be written to an ascii formatted file. The first line is an optional user provided header line, and the following line provides the attributes r0, gm, omega, and lmax. 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 the errors are to be saved, the format of each line will be l, m, coeffs[0, l, m], coeffs[1, l, m], error[0, l, m], error[1, l, m] If format='npy', the spherical harmonic coefficients (but not the meta-data nor errors) will be saved to a binary numpy 'npy' file using numpy.save(). """

        if format is 'shtools':
            if errors is True and self.errors is None:
                raise ValueError('Can not save errors when then have not been '
                                 'initialized.')

            if self.omega is None:
                omega = 0.
            else:
                omega = self.omega

            with open(filename, mode='w') as file:
                if header is not None:
                    file.write(header + '\n')
                file.write('{:.16e}, {:.16e}, {:.16e}, {:d}\n'.format(
                    self.r0, self.gm, omega, self.lmax))
                for l in range(self.lmax+1):
                    for m in range(l+1):
                        if errors is True:
                            file.write('{:d}, {:d}, {:.16e}, {:.16e}, '
                                       '{:.16e}, {:.16e}\n'
                                       .format(l, m, self.coeffs[0, l, m],
                                               self.coeffs[1, l, m],
                                               self.errors[0, l, m],
                                               self.errors[1, l, m]))
                        else:
                            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)))