<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def savefits(cube, fitsname, **kwargs):
 """Save a cube to a 3D-cube FITS file. Args: cube (xarray.DataArray):
 Cube to be saved. fitsname (str):
 Name of output FITS file. kwargs (optional):
 Other arguments common with astropy.io.fits.writeto(). """

    ### pick up kwargs
    dropdeg = kwargs.pop('dropdeg', False)
    ndim    = len(cube.dims)

    ### load yaml
    FITSINFO = get_data('decode', 'data/fitsinfo.yaml')
    hdrdata = yaml.load(FITSINFO, dc.utils.OrderedLoader)

    ### default header
    if ndim == 2:
        header = fits.Header(hdrdata['dcube_2d'])
        data   = cube.values.T
    elif ndim == 3:
        if dropdeg:
            header = fits.Header(hdrdata['dcube_2d'])
            data   = cube.values[:, :, 0].T
        else:
            header = fits.Header(hdrdata['dcube_3d'])

            kidfq     = cube.kidfq.values
            freqrange = ~np.isnan(kidfq)
            orderedfq = np.argsort(kidfq[freqrange])
            newcube   = cube[:, :, orderedfq]
            data      = newcube.values.T
    else:
        raise TypeError(ndim)

    ### update Header
    if cube.coordsys == 'AZEL':
        header.update({'CTYPE1': 'dAZ', 'CTYPE2': 'dEL'})
    elif cube.coordsys == 'RADEC':
        header.update({'OBSRA': float(cube.xref), 'OBSDEC': float(cube.yref)})
    else:
        pass
    header.update({'CRVAL1': float(cube.x[0]),
                   'CDELT1': float(cube.x[1] - cube.x[0]),
                   'CRVAL2': float(cube.y[0]),
                   'CDELT2': float(cube.y[1] - cube.y[0]),
                   'DATE': datetime.now(timezone('UTC')).isoformat()})
    if (ndim == 3) and (not dropdeg):
        header.update({'CRVAL3': float(newcube.kidfq[0]),
                       'CDELT3': float(newcube.kidfq[1] - newcube.kidfq[0])})

    fitsname = str(Path(fitsname).expanduser())
    fits.writeto(fitsname, data, header, **kwargs)
    logger.info('{} has been created.'.format(fitsname))