# Buggy code
def _interpret_fields(fields):
    """
    Turn the fields read with load and FF2PP._extract_field into useable
    fields. One of the primary purposes of this function is to either convert
    "deferred bytes" into "deferred arrays" or "loaded bytes" into actual
    numpy arrays (via the _create_field_data) function.

    """
    land_mask = None
    landmask_compressed_fields = []
    for field in fields:
        # Store the first reference to a land mask, and use this as the
        # definitive mask for future fields in this generator.
        if (
            land_mask is None
            and field.lbuser[6] == 1
            and (field.lbuser[3] // 1000) == 0
            and (field.lbuser[3] % 1000) == 30
        ):
            land_mask = field

        # Handle land compressed data payloads,
        # when lbpack.n2 is 2.
        if (field.raw_lbpack // 10 % 10) == 2:
            if land_mask is None:
                landmask_compressed_fields.append(field)
                continue

            # Land compressed fields don't have a lbrow and lbnpt.
            field.lbrow, field.lbnpt = land_mask.lbrow, land_mask.lbnpt

        data_shape = (field.lbrow, field.lbnpt)
        _create_field_data(field, data_shape, land_mask)
        yield field

    if landmask_compressed_fields:
        if land_mask is None:
            warnings.warn(
                "Landmask compressed fields existed without a "
                "landmask to decompress with. The data will have "
                "a shape of (0, 0) and will not read."
            )
            mask_shape = (0, 0)
        else:
            mask_shape = (land_mask.lbrow, land_mask.lbnpt)

        for field in landmask_compressed_fields:
            field.lbrow, field.lbnpt = mask_shape
            _create_field_data(field, (field.lbrow, field.lbnpt), land_mask)
            yield field


# Traceback (thread.error)
Traceback (most recent call last):
File "/.../create_jules_ic_v2.py", line 279, in <module>
main()
File "/.../create_jules_ic_v2.py", line 274, in main
nameoffile=create_jules_ic(Ddate,INDIR,UM_FILE_IN,OUTDIR)
File "/.../create_jules_ic_v2.py", line 152, in create_jules_ic
print(type(cube_vegfrac.data <=0.0001))
File "/.../python2.7/site-packages/iris/cube.py", line 1644, in data
return self._data_manager.data
File "/.../python2.7/site-packages/iris/_data_manager.py", line 216, in data
result = as_concrete_data(self._lazy_array)
File "/.../python2.7/site-packages/iris/_lazy_data.py", line 128, in as_concrete_data
data = np.asanyarray(data.compute())
File "/.../python2.7/site-packages/dask/base.py", line 143, in compute
(result,) = compute(self, traverse=False, **kwargs)
File "/.../python2.7/site-packages/dask/base.py", line 392, in compute
results = get(dsk, keys, **kwargs)
File "/.../python2.7/site-packages/dask/threaded.py", line 75, in get
pack_exception=pack_exception, **kwargs)
File "/.../python2.7/site-packages/dask/local.py", line 521, in get_async
raise_exception(exc, tb)
File "/.../python2.7/site-packages/dask/local.py", line 290, in execute_task
result = _execute_task(task, data)
File "/.../python2.7/site-packages/dask/local.py", line 270, in _execute_task
args2 = [_execute_task(a, cache) for a in args]
File "/.../python2.7/site-packages/dask/local.py", line 271, in _execute_task
return func(*args2)
File "/.../python2.7/site-packages/dask/array/core.py", line 74, in getter
c = a[b]
File "/.../python2.7/site-packages/iris/fileformats/pp.py", line 647, in __getitem__
self.mdi, self.mask)
File "/.../python2.7/site-packages/iris/fileformats/pp.py", line 770, in _data_bytes_to_shaped_array
land_mask = mask.data.astype(np.bool)
File "/.../python2.7/site-packages/iris/fileformats/pp.py", line 1033, in data
self._data = as_concrete_data(self._data)
File "/.../python2.7/site-packages/iris/_lazy_data.py", line 128, in as_concrete_data
data = np.asanyarray(data.compute())
File "/.../python2.7/site-packages/dask/base.py", line 143, in compute
(result,) = compute(self, traverse=False, **kwargs)
File "/.../python2.7/site-packages/dask/base.py", line 392, in compute
results = get(dsk, keys, **kwargs)
File "/.../python2.7/site-packages/dask/threaded.py", line 70, in get
pool = ThreadPool(num_workers)
File "/.../python2.7/multiprocessing/pool.py", line 727, in __init__
Pool.__init__(self, processes, initializer, initargs)
File "/.../python2.7/multiprocessing/pool.py", line 159, in __init__
self._repopulate_pool()
File "/.../python2.7/multiprocessing/pool.py", line 223, in _repopulate_pool
w.start()
File "/.../python2.7/multiprocessing/dummy/__init__.py", line 75, in start
threading.Thread.start(self)
File "/.../python2.7/threading.py", line 736, in start
_start_new_thread(self.__bootstrap, ())
thread.error: can't start new thread

# Fixed code
def _interpret_fields(fields):
    """
    Turn the fields read with load and FF2PP._extract_field into useable
    fields. One of the primary purposes of this function is to either convert
    "deferred bytes" into "deferred arrays" or "loaded bytes" into actual
    numpy arrays (via the _create_field_data) function.

    """
    land_mask_field = None
    landmask_compressed_fields = []
    for field in fields:
        # Store the first reference to a land mask, and use this as the
        # definitive mask for future fields in this generator.
        if (
            land_mask_field is None
            and field.lbuser[6] == 1
            and (field.lbuser[3] // 1000) == 0
            and (field.lbuser[3] % 1000) == 30
        ):
            land_mask_field = field

        # Handle land compressed data payloads, when lbpack.n2 is 2.
        if (field.raw_lbpack // 10 % 10) == 2:
            # Field uses land-mask packing, so needs a related land-mask field.
            if land_mask_field is None:
                landmask_compressed_fields.append(field)
                # Land-masked fields have their size+shape defined by the
                # reference landmask field, so we can't yield them if they
                # are encountered *before* the landmask.
                # In that case, defer them, and process them all afterwards at
                # the end of the file.
                continue

            # Land-mask compressed fields don't have an lbrow and lbnpt.
            field.lbrow, field.lbnpt = land_mask_field.lbrow, land_mask_field.lbnpt
            _create_field_data(
                field, (field.lbrow, field.lbnpt), land_mask_field=land_mask_field
            )
        else:
            # Field does not use land-mask packing.
            _create_field_data(field, (field.lbrow, field.lbnpt))

        yield field

    # At file end, return any land-masked fields that were deferred because
    # they were encountered before the landmask reference field.
    if landmask_compressed_fields:
        if land_mask_field is None:
            warnings.warn(
                "Landmask compressed fields existed without a "
                "landmask to decompress with. The data will have "
                "a shape of (0, 0) and will not read."
            )
            mask_shape = (0, 0)
        else:
            mask_shape = (land_mask_field.lbrow, land_mask_field.lbnpt)

        for field in landmask_compressed_fields:
            field.lbrow, field.lbnpt = mask_shape
            _create_field_data(field, mask_shape, land_mask_field=land_mask_field)
            yield field