# Buggy code
def da2cf(dataarray, epoch=EPOCH):
    """Convert the dataarray to something cf-compatible."""
    new_data = dataarray.copy()
    # TODO: make these boundaries of the time dimension
    new_data.attrs.pop("start_time", None)
    new_data.attrs.pop("end_time", None)

    # Remove the area
    new_data.attrs.pop("area", None)

    anc = [ds.attrs["name"] for ds in new_data.attrs.get("ancillary_variables", [])]
    if anc:
        new_data.attrs["ancillary_variables"] = " ".join(anc)
    # TODO: make this a grid mapping or lon/lats
    # new_data.attrs['area'] = str(new_data.attrs.get('area'))
    for key, val in new_data.attrs.copy().items():
        if val is None:
            new_data.attrs.pop(key)
    new_data.attrs.pop("_last_resampler", None)

    if "time" in new_data.coords:
        new_data["time"].encoding["units"] = epoch
        new_data["time"].attrs["standard_name"] = "time"
        new_data["time"].attrs.pop("bounds", None)

    if "x" in new_data.coords:
        new_data["x"].attrs["standard_name"] = "projection_x_coordinate"
        new_data["x"].attrs["units"] = "m"

    if "y" in new_data.coords:
        new_data["y"].attrs["standard_name"] = "projection_y_coordinate"
        new_data["y"].attrs["units"] = "m"

    new_data.attrs.setdefault("long_name", new_data.attrs.pop("name"))
    return new_data


# Traceback (AttributeError)
[INFO: 2017-12-12 08:14:04 : satpy.writers.cf_writer] Saving datasets to NetCDF4/CF.
[INFO: 2017-12-12 08:14:04 : satpy.writers.cf_writer] No longitude and latitude data to save.
[INFO: 2017-12-12 08:14:04 : satpy.writers.cf_writer] No grid mapping to save.
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-24-58ece08c15f3> in <module>()
----> 1 lcl.save_datasets(writer='cf', filename='/tmp/foo.nc')

/home/lahtinep/Software/pytroll/packages/satpy/satpy/scene.pyc in save_datasets(self, writer, datasets, **kwargs)
632             datasets = self.datasets.values()
633         writer = self.get_writer(writer, **kwargs)
--> 634         writer.save_datasets(datasets, **kwargs)
635
636     def get_writer(self, writer="geotiff", **kwargs):

/home/lahtinep/Software/pytroll/packages/satpy/satpy/writers/cf_writer.pyc in save_datasets(self, datasets, filename, **kwargs)
176                     coords = [line_coord, pixel_coord]
177
--> 178                 domain = cf.Domain(dim=coords,
179                                    aux=aux,
180                                    ref=grid_mapping)

AttributeError: 'module' object has no attribute 'Domain'

# Fixed code
def da2cf(dataarray, epoch=EPOCH):
    """Convert the dataarray to something cf-compatible."""
    new_data = dataarray.copy()

    # Remove the area
    new_data.attrs.pop("area", None)

    anc = [ds.attrs["name"] for ds in new_data.attrs.get("ancillary_variables", [])]
    if anc:
        new_data.attrs["ancillary_variables"] = " ".join(anc)
    # TODO: make this a grid mapping or lon/lats
    # new_data.attrs['area'] = str(new_data.attrs.get('area'))
    for key, val in new_data.attrs.copy().items():
        if val is None:
            new_data.attrs.pop(key)
    new_data.attrs.pop("_last_resampler", None)

    if "time" in new_data.coords:
        new_data["time"].encoding["units"] = epoch
        new_data["time"].attrs["standard_name"] = "time"
        new_data["time"].attrs.pop("bounds", None)

    if "x" in new_data.coords:
        new_data["x"].attrs["standard_name"] = "projection_x_coordinate"
        new_data["x"].attrs["units"] = "m"

    if "y" in new_data.coords:
        new_data["y"].attrs["standard_name"] = "projection_y_coordinate"
        new_data["y"].attrs["units"] = "m"

    new_data.attrs.setdefault("long_name", new_data.attrs.pop("name"))
    return new_data