Repository: yt-project/yt

Issue:
Error saving a Clump Dataset
<!--To help us understand and resolve your issue, please fill out the form to
the best of your ability.-->
<!--You can feel free to delete the sections that do not apply.-->

### Bug report

**Bug summary**

There is a problem saving 

**Code for reproduction**

<!--A minimum code snippet required to reproduce the bug, also minimizing the
number of dependencies required.-->

<!-- If you need to use a data file to trigger the issue you're having, consider
using one of the datasets from the yt data hub (http://yt-project.org/data). If
your issue cannot be triggered using a public dataset, you can use the yt
curldrop (https://docs.hub.yt/services.html#curldrop) to share data
files. Please include a link to the dataset in the issue if you use the
curldrop.-->

```python
# Paste your code here
#
#
import yt
from yt.analysis_modules.level_sets.api import *

ds=yt.load('IsolatedGalaxy/galaxy0030/galaxy0030')

disco=ds.disk('c', [0., 0., 1.],(25, 'kpc'), (2, 'kpc'))

master_clump = Clump(disco, ("gas", "number_density"))

cmin=disco['number_density'].max()/4
cmax=disco['number_density'].max()
step=2.0

find_clumps(master_clump, cmin, cmax, step)

master_clump.save_as_dataset(fields=["density","x","y","z"])

```

**Actual outcome**

<!--The output produced by the above code, which may be a screenshot, console
output, etc.-->

```
# If applicable, paste the console output here
#
#
    master_clump.save_as_dataset(fields=["density","x","y","z"])
  File "/home/jose/miniconda3/lib/python3.6/site-packages/yt/analysis_modules/level_sets/clump_handling.py", line 365, in save_as_dataset
    extra_attrs=extra_attrs)
  File "/home/jose/miniconda3/lib/python3.6/site-packages/yt/frontends/ytdata/utilities.py", line 149, in save_as_dataset
    _yt_array_hdf5(fh[field_type], field_name, data[field])
  File "/home/jose/miniconda3/lib/python3.6/site-packages/yt/frontends/ytdata/utilities.py", line 208, in _yt_array_hdf5
    dataset = fh.create_dataset(str(field), data=data)
  File "/home/jose/miniconda3/lib/python3.6/site-packages/h5py/_hl/group.py", line 111, in create_dataset
    self[name] = dset
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (/home/ilan/minonda/conda-bld/h5py_1496889914775/work/h5py/_objects.c:2846)
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (/home/ilan/minonda/conda-bld/h5py_1496889914775/work/h5py/_objects.c:2804)
  File "/home/jose/miniconda3/lib/python3.6/site-packages/h5py/_hl/group.py", line 276, in __setitem__
    h5o.link(obj.id, self.id, name, lcpl=lcpl, lapl=self._lapl)
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (/home/ilan/minonda/conda-bld/h5py_1496889914775/work/h5py/_objects.c:2846)
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (/home/ilan/minonda/conda-bld/h5py_1496889914775/work/h5py/_objects.c:2804)
  File "h5py/h5o.pyx", line 202, in h5py.h5o.link (/home/ilan/minonda/conda-bld/h5py_1496889914775/work/h5py/h5o.c:3901)
RuntimeError: Unable to create link (Name already exists)

```


**Version Information**
<!--Please specify your platform and versions of the relevant libraries you are
using:-->
  * Operating System: Ubuntu/Linux
  * Python Version: 3.5
  * yt version: 3.4

<!--Please tell us how you installed yt and python e.g., from source,
pip, conda. If you installed from conda, please specify which channel you used
if not the default-->
Updated yt using "conda update -c conda-forge yt"

Error saving a Clump Dataset
<!--To help us understand and resolve your issue, please fill out the form to
the best of your ability.-->
<!--You can feel free to delete the sections that do not apply.-->

### Bug report

**Bug summary**

There is a problem saving 

**Code for reproduction**

<!--A minimum code snippet required to reproduce the bug, also minimizing the
number of dependencies required.-->

<!-- If you need to use a data file to trigger the issue you're having, consider
using one of the datasets from the yt data hub (http://yt-project.org/data). If
your issue cannot be triggered using a public dataset, you can use the yt
curldrop (https://docs.hub.yt/services.html#curldrop) to share data
files. Please include a link to the dataset in the issue if you use the
curldrop.-->

```python
# Paste your code here
#
#
import yt
from yt.analysis_modules.level_sets.api import *

ds=yt.load('IsolatedGalaxy/galaxy0030/galaxy0030')

disco=ds.disk('c', [0., 0., 1.],(25, 'kpc'), (2, 'kpc'))

master_clump = Clump(disco, ("gas", "number_density"))

cmin=disco['number_density'].max()/4
cmax=disco['number_density'].max()
step=2.0

find_clumps(master_clump, cmin, cmax, step)

master_clump.save_as_dataset(fields=["density","x","y","z"])

```

**Actual outcome**

<!--The output produced by the above code, which may be a screenshot, console
output, etc.-->

```
# If applicable, paste the console output here
#
#
    master_clump.save_as_dataset(fields=["density","x","y","z"])
  File "/home/jose/miniconda3/lib/python3.6/site-packages/yt/analysis_modules/level_sets/clump_handling.py", line 365, in save_as_dataset
    extra_attrs=extra_attrs)
  File "/home/jose/miniconda3/lib/python3.6/site-packages/yt/frontends/ytdata/utilities.py", line 149, in save_as_dataset
    _yt_array_hdf5(fh[field_type], field_name, data[field])
  File "/home/jose/miniconda3/lib/python3.6/site-packages/yt/frontends/ytdata/utilities.py", line 208, in _yt_array_hdf5
    dataset = fh.create_dataset(str(field), data=data)
  File "/home/jose/miniconda3/lib/python3.6/site-packages/h5py/_hl/group.py", line 111, in create_dataset
    self[name] = dset
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (/home/ilan/minonda/conda-bld/h5py_1496889914775/work/h5py/_objects.c:2846)
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (/home/ilan/minonda/conda-bld/h5py_1496889914775/work/h5py/_objects.c:2804)
  File "/home/jose/miniconda3/lib/python3.6/site-packages/h5py/_hl/group.py", line 276, in __setitem__
    h5o.link(obj.id, self.id, name, lcpl=lcpl, lapl=self._lapl)
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (/home/ilan/minonda/conda-bld/h5py_1496889914775/work/h5py/_objects.c:2846)
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (/home/ilan/minonda/conda-bld/h5py_1496889914775/work/h5py/_objects.c:2804)
  File "h5py/h5o.pyx", line 202, in h5py.h5o.link (/home/ilan/minonda/conda-bld/h5py_1496889914775/work/h5py/h5o.c:3901)
RuntimeError: Unable to create link (Name already exists)

```


**Version Information**
<!--Please specify your platform and versions of the relevant libraries you are
using:-->
  * Operating System: Ubuntu/Linux
  * Python Version: 3.5
  * yt version: 3.4

<!--Please tell us how you installed yt and python e.g., from source,
pip, conda. If you installed from conda, please specify which channel you used
if not the default-->
Updated yt using "conda update -c conda-forge yt"



Discussion:
Able to reproduce, thank you for the example script.
Able to reproduce, thank you for the example script.

Resolving patch:
diff --git a/yt/analysis_modules/level_sets/clump_handling.py b/yt/analysis_modules/level_sets/clump_handling.py
--- a/yt/analysis_modules/level_sets/clump_handling.py
+++ b/yt/analysis_modules/level_sets/clump_handling.py
@@ -302,14 +302,18 @@ def save_as_dataset(self, filename=None, fields=None):
             field_data = {}
             need_grid_positions = False
             for f in self.base.data._determine_fields(fields) + contour_fields:
-                field_data[f] = self.base[f]
                 if ds.field_info[f].particle_type:
                     if f[0] not in ptypes:
                         ptypes.append(f[0])
                     ftypes[f] = f[0]
                 else:
                     need_grid_positions = True
+                    if f[1] in ('x', 'y', 'z', 'dx', 'dy', 'dz'):
+                        # skip 'xyz' if a user passes that in because they
+                        # will be added to ftypes below
+                        continue
                     ftypes[f] = "grid"
+                field_data[f] = self.base[f]
 
             if len(ptypes) > 0:
                 for ax in "xyz":


Test patch:
diff --git a/yt/analysis_modules/level_sets/tests/test_clump_finding.py b/yt/analysis_modules/level_sets/tests/test_clump_finding.py
--- a/yt/analysis_modules/level_sets/tests/test_clump_finding.py
+++ b/yt/analysis_modules/level_sets/tests/test_clump_finding.py
@@ -104,7 +104,8 @@ def test_clump_tree_save():
     find_clumps(master_clump, c_min, c_max, step)
     leaf_clumps = get_lowest_clumps(master_clump)
 
-    fn = master_clump.save_as_dataset(fields=["density", "particle_mass"])
+    fn = master_clump.save_as_dataset(fields=["density", "x", "y", "z",
+                                              "particle_mass"])
     ds2 = load(fn)
 
     # compare clumps in the tree