# 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
        :param filename: full path to file to save to.

        :param format: format to save -- 'netcdf3' or 'netcdf4'
                       are the only options at this point.
        """
        self.save(filename, format='netcdf4')


    def save(self, filepath, format='netcdf4', variables={}):
        """
        Save the ugrid object as a netcdf file.

        :param filepath: path to file you want o save to.  An existing one
                         will be clobbered if it already exists.

        :param variables: dict of gridded.Variable objects to save to file

        Follows the convention established by the netcdf UGRID working group:

        http://ugrid-conventions.github.io/ugrid-conventions

        NOTE: Variables are saved here, because different conventions do it
              differently.
        """
        format_options = ('netcdf3', 'netcdf4')
        if format not in format_options:
            raise ValueError("format: {} not supported. Options are: {}".format(format, format_options))

        mesh_name = self.mesh_name


# 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

        :param filename: full path to file to save to.

        :param format: format to save -- 'netcdf3' or 'netcdf4'
                       are the only options at this point.
        """
        self.save(filename, format='netcdf4')


    def save(self, filepath, format='netcdf4', variables={}):
        """
        Save the ugrid object as a netcdf file.

        :param filepath: path to file you want o save to.  An existing one
                         will be clobbered if it already exists.

        :param variables: dict of gridded.Variable objects to save to file

        Follows the convention established by the netcdf UGRID working group:

        http://ugrid-conventions.github.io/ugrid-conventions

        NOTE: Variables are saved here, because different conventions do it
              differently.
        """
        format_options = ('netcdf3', 'netcdf4')
        if format not in format_options:
            raise ValueError("format: {} not supported. Options are: {}".format(format, format_options))

        mesh_name = self.mesh_name


        nclocal = get_writable_dataset(filepath)