# Path: gridded/pyugrid/util.py
# def point_in_tri(face_points, point, return_weights=False):
#     """
#     Calculates whether point is internal/external
#     to element by comparing summed area of sub triangles with area of triangle
#     element.
# 
#     NOTE: this seems like a pretty expensive way to do it
#            -- compared to three side of line tests..
#     """
#     sub_tri_areas = np.zeros(3)
#     sub_tri_areas[0] = _signed_area_tri(np.vstack((face_points[(0, 1), :],
#                                                    point)))
#     sub_tri_areas[1] = _signed_area_tri(np.vstack((face_points[(1, 2), :],
#                                                    point)))
#     sub_tri_areas[2] = _signed_area_tri(np.vstack((face_points[(0, 2), :],
#                                                    point)))
#     tri_area = _signed_area_tri(face_points)
# 
#     if abs(np.abs(sub_tri_areas).sum() - tri_area) / tri_area <= epsilon:
#         if return_weights:
#             raise NotImplementedError
#             # weights = sub_tri_areas/tri_area
#             # weights[1] = max(0., min(1., weights[1]))
#             # weights[2] = max(0., min(1., weights[2]))
#             # if (weights[0]+weights[1]>1):
#             #     weights[2] = 0
#             #     weights[1] = 1-weights[0]
#             # else:
#             #     weights[2] = 1-weights[0]-weights[1]
#             #
#             # return weights
#         else:
#             return True
#     return False
# 
# Path: gridded/utilities.py
# def get_writable_dataset(ncfile, format="netcdf4"):
#     """
#     Utility to create a writable netCDF4 Dataset from a filename, list of filenames,
#     or just pass it through if it's already a netCDF4.Dataset
# 
#     if dataset is not None, it should be a valid netCDF4 Dataset object,
#     and it will simply be returned
#     """
#     if isinstance(ncfile, nc4.Dataset):
#         # fixme: check for writable
#         return ncfile
#     elif isstring(ncfile):  # Fixme: should be pathlike...
#         print("filename is:", ncfile)
#         return nc4.Dataset(ncfile,
#                            mode="w",
#                            clobber=True,
#                            format="NETCDF4")
#     else:
#         raise ValueError("Must be a string path or a netcdf4 Dataset")


import hashlib
import numpy as np
import gridded.pyugrid.read_netcdf as read_netcdf
                import cell_tree2d
from collections import OrderedDict
from gridded.pyugrid.util import point_in_tri
from gridded.utilities import get_writable_dataset
            from scipy.spatial import cKDTree
        from cell_tree2d import CellTree
            from scipy.spatial import cKDTree
                                  'simple' is very, very slow for large grids.
        :type simple: str

        This version utilizes the CellTree data structure.

        """
        points = np.asarray(points, dtype=np.float64)
        just_one = (points.ndim == 1)
        points.shape = (-1, 2)

        if _memo:
            if _hash is None:
                _hash = self._hash_of_pts(points)
            result = self._get_memoed(points, self._ind_memo_dict, _copy, _hash)
            if result is not None:
                return result

        if method == 'celltree':
            try:
            except ImportError:
                raise ImportError("the cell_tree2d package must be installed to use the celltree search:\n"
                                  "https://github.com/NOAA-ORR-ERD/cell_tree2d/")
            if self._cell_tree is None:
                self.build_celltree()
            indices = self._cell_tree.locate(points)
        elif method == 'simple':
            indices = np.zeros((points.shape[0]), dtype=IND_DT)
            for n, point in enumerate(points):
                for i, face in enumerate(self._faces):
                    f = self._nodes[face]

# Path: gridded/pyugrid/util.py
# def point_in_tri(face_points, point, return_weights=False):
#     """
#     Calculates whether point is internal/external
#     to element by comparing summed area of sub triangles with area of triangle
#     element.
# 
#     NOTE: this seems like a pretty expensive way to do it
#            -- compared to three side of line tests..
#     """
#     sub_tri_areas = np.zeros(3)
#     sub_tri_areas[0] = _signed_area_tri(np.vstack((face_points[(0, 1), :],
#                                                    point)))
#     sub_tri_areas[1] = _signed_area_tri(np.vstack((face_points[(1, 2), :],
#                                                    point)))
#     sub_tri_areas[2] = _signed_area_tri(np.vstack((face_points[(0, 2), :],
#                                                    point)))
#     tri_area = _signed_area_tri(face_points)
# 
#     if abs(np.abs(sub_tri_areas).sum() - tri_area) / tri_area <= epsilon:
#         if return_weights:
#             raise NotImplementedError
#             # weights = sub_tri_areas/tri_area
#             # weights[1] = max(0., min(1., weights[1]))
#             # weights[2] = max(0., min(1., weights[2]))
#             # if (weights[0]+weights[1]>1):
#             #     weights[2] = 0
#             #     weights[1] = 1-weights[0]
#             # else:
#             #     weights[2] = 1-weights[0]-weights[1]
#             #
#             # return weights
#         else:
#             return True
#     return False
# 
# Path: gridded/utilities.py
# def get_writable_dataset(ncfile, format="netcdf4"):
#     """
#     Utility to create a writable netCDF4 Dataset from a filename, list of filenames,
#     or just pass it through if it's already a netCDF4.Dataset
# 
#     if dataset is not None, it should be a valid netCDF4 Dataset object,
#     and it will simply be returned
#     """
#     if isinstance(ncfile, nc4.Dataset):
#         # fixme: check for writable
#         return ncfile
#     elif isstring(ncfile):  # Fixme: should be pathlike...
#         print("filename is:", ncfile)
#         return nc4.Dataset(ncfile,
#                            mode="w",
#                            clobber=True,
#                            format="NETCDF4")
#     else:
#         raise ValueError("Must be a string path or a netcdf4 Dataset")


# Path: gridded/pyugrid/ugrid.py
import hashlib
import numpy as np
import gridded.pyugrid.read_netcdf as read_netcdf
                import cell_tree2d
from collections import OrderedDict
from gridded.pyugrid.util import point_in_tri
from gridded.utilities import get_writable_dataset
            from scipy.spatial import cKDTree
        from cell_tree2d import CellTree
            from scipy.spatial import cKDTree

                                  'simple' is very, very slow for large grids.
        :type simple: str

        This version utilizes the CellTree data structure.

        """
        points = np.asarray(points, dtype=np.float64)
        just_one = (points.ndim == 1)
        points.shape = (-1, 2)

        if _memo:
            if _hash is None:
                _hash = self._hash_of_pts(points)
            result = self._get_memoed(points, self._ind_memo_dict, _copy, _hash)
            if result is not None:
                return result

        if method == 'celltree':
            try:
            except ImportError:
                raise ImportError("the cell_tree2d package must be installed to use the celltree search:\n"
                                  "https://github.com/NOAA-ORR-ERD/cell_tree2d/")
            if self._cell_tree is None:
                self.build_celltree()
            indices = self._cell_tree.locate(points)
        elif method == 'simple':
            indices = np.zeros((points.shape[0]), dtype=IND_DT)
            for n, point in enumerate(points):
                for i, face in enumerate(self._faces):
                    f = self._nodes[face]

                    if point_in_tri(f, point):