def __init__(self,
                 extent = None,
                 llcrnrlon = None,
                 llcrnrlat = None,
                 urcrnrlon = None,
                 urcrnrlat = None,
                 skippoly = False):
        self.proj4 = '+proj=lonlat +ellps=WGS84'
        self.crs = pyproj.CRS(self.proj4)
        self.skippoly = skippoly

        super (Reader, self).__init__ ()

        # Depth
        self.z = None

        if extent and (llcrnrlat or llcrnrlon or urcrnrlat or urcrnrlon):
            raise Exception ("'extent' cannot be given togheter with any of 'llcrnrlon', ..'")
        elif extent is None and (llcrnrlat or llcrnrlon or urcrnrlat or urcrnrlon):
            warnings.warn("llcrnrlon, llcrnrlat, et. al. is deprecated for the global landmask reader. Prefer 'extent' in stead.", DeprecationWarning)
            extent = [llcrnrlon, llcrnrlat, urcrnrlon, urcrnrlat]

        # Read and store min, max and step of x and y
        if extent is not None:
            self.xmin, self.ymin, self.xmax, self.ymax = extent
        else:
            self.xmin, self.ymin = -180, -90
            self.xmax, self.ymax = 180, 90

        self.xmin, self.ymin = self.lonlat2xy(self.xmin, self.ymin)
        self.xmax, self.ymax = self.lonlat2xy(self.xmax, self.ymax)

        # setup landmask
        self.mask = Landmask(extent, skippoly)
def test_setup_landmask_retry_weakref():
    delete_mask()
    os.makedirs(tmpdir, exist_ok=True)
    m = os.stat(tmpdir).st_mode
    try:
        print("setting permissions on tmpdir to 0o000:", tmpdir)
        os.chmod(tmpdir, 0o000)

        assert Landmask.__mask__() is None

        l = Landmask()

        assert Landmask.__mask__() is not None
        assert l.mask is not None

        # second landmask should also use the same memmap as previous
        l2 = Landmask()

        assert l2.mmapf == l.mmapf
        assert l2.mask is l.mask

        # TODO: does not seem to be collected in this case!
        # del l
        # del l2
        # gc.collect()
        # assert Landmask.__mask__() is None

    finally:
        os.chmod(tmpdir, m)
Esempio n. 3
0
    def __init__(self,
                 extent=None,
                 llcrnrlon=None,
                 llcrnrlat=None,
                 urcrnrlon=None,
                 urcrnrlat=None,
                 skippoly=False):
        """
        Initialize land mask using GSHHS dataset.

        Args:
            extent (array): minx, miny, maxx, maxy bounding box in source CRS for which to include
                geometries. Default None (all geometries).

            llcrnrlon (float): minx (Deprecated in favor of extent).

            llcrnrlat (float): miny (Deprecated in favor of extent).

            urcrnrlon (float): maxx (Deprecated in favor of extent).

            urcrnrlat (float): maxy (Deprecated in favor of extent).

            skippoly (bool): use only rasterized landmask to determine whether points are on land.
        """

        self.proj4 = '+proj=lonlat +ellps=WGS84'
        self.crs = pyproj.CRS(self.proj4)
        self.skippoly = skippoly

        super(Reader, self).__init__()

        # Depth
        self.z = None

        if extent and (llcrnrlat or llcrnrlon or urcrnrlat or urcrnrlon):
            raise Exception(
                "'extent' cannot be given togheter with any of 'llcrnrlon', ..'"
            )
        elif extent is None and (llcrnrlat or llcrnrlon or urcrnrlat
                                 or urcrnrlon):
            warnings.warn(
                "llcrnrlon, llcrnrlat, et. al. is deprecated for the global landmask reader. Prefer 'extent' in stead.",
                DeprecationWarning)
            extent = [llcrnrlon, llcrnrlat, urcrnrlon, urcrnrlat]

        # Read and store min, max and step of x and y
        if extent is not None:
            self.xmin, self.ymin, self.xmax, self.ymax = extent
        else:
            self.xmin, self.ymin = -180, -90
            self.xmax, self.ymax = 180, 90

        self.xmin, self.ymin = self.lonlat2xy(self.xmin, self.ymin)
        self.xmax, self.ymax = self.lonlat2xy(self.xmax, self.ymax)

        # setup landmask
        self.mask = Landmask(extent, skippoly)
def _f2(i, abort):
    print("launching instance:", i)
    if abort:
        try:
            l = Landmask(__concurrency_delay__=2.,
                         __concurrency_abort__=abort,
                         __no_retry__=True)
        except:
            pass
    else:
        l = Landmask(__concurrency_delay__=2.,
                     __concurrency_abort__=abort,
                     __no_retry__=True)
    print("instance", i, "done")
def get_mask(skippoly = False, extent = None):
    """
    Returns an instance of the landmask type and landmask. The mask data is
    usually shared between threads.
    """
    global __roaring_mask__

    try:
        if __roaring_mask__ is None:
            from roaring_landmask import RoaringLandmask
            logger.warning("using the experimental RoaringLandmask")
            __roaring_mask__ = RoaringLandmask.new()

        mask = __roaring_mask__
        mask_type = 1
        if skippoly or extent:
            logger.warning(
                'skippoly and extent is not supported with RoaringLandmask')

    except ImportError:
        from opendrift_landmask_data import Landmask
        mask_type = 0
        mask = Landmask(extent, skippoly)

    return mask_type, mask
def test_landmask_onocean_skippoly(benchmark):
    delete_mask()
    l = Landmask()

    onocean = (np.array([5.]), np.array([65.6]))
    c = benchmark(l.contains, onocean[0], onocean[1], True)
    assert not c
def test_landmask_onland_skippoly(benchmark):
    delete_mask()
    l = Landmask()

    onland = (np.array([15.]), np.array([65.6]))
    c = benchmark(l.contains, onland[0], onland[1], True)
    assert c
def test_setup_landmask_retry():
    delete_mask()
    os.makedirs(tmpdir, exist_ok=True)
    m = os.stat(tmpdir).st_mode
    try:
        print("setting permissions on tmpdir to 0o000:", tmpdir)
        os.chmod(tmpdir, 0o000)
        l = Landmask()
    finally:
        os.chmod(tmpdir, m)
def test_landmask_many(benchmark):
    delete_mask()
    l = Landmask()

    x = np.arange(-180, 180, .5)
    y = np.arange(-90, 90, .5)

    xx, yy = np.meshgrid(x, y)

    print("points:", len(xx.ravel()))
    benchmark(l.contains, xx.ravel(), yy.ravel())
def test_landmask_many_extent(benchmark):
    delete_mask()
    l = Landmask([50, 0, 65, 40])

    x = np.linspace(50.1, 64.9, 30000)
    y = np.linspace(0.1, 39.9, 10)

    xx, yy = np.meshgrid(x, y)

    print("points:", len(xx.ravel()))
    benchmark(l.contains, xx.ravel(), yy.ravel())
def test_tromsoe(tmpdir):
    delete_mask()

    # xmin, ymin, xmax, ymax
    extent = [18.64, 69.537, 19.37, 69.91]
    l = Landmask(extent)

    lon = np.arange(18.8, 18.969, .0003)
    lat = np.arange(69.59, 69.70, .0003)

    xx, yy = np.meshgrid(lon, lat)
    c = l.contains(xx.ravel(), yy.ravel(), True)
    c = c.reshape(xx.shape)

    print("c:", c)

    import cartopy
    import cartopy.crs as ccrs
    import matplotlib.pyplot as plt
    import os
    plt.figure(dpi=200)
    ax = plt.axes(projection=ccrs.PlateCarree())
    # ax.contourf(lon, lat, c, transform = ccrs.PlateCarree())
    ax.imshow(c,
              extent=[np.min(lon),
                      np.max(lon),
                      np.min(lat),
                      np.max(lat)],
              transform=ccrs.PlateCarree())

    reader = cartopy.feature.GSHHSFeature('f') \
             .intersecting_geometries([
               extent[0],
               extent[2],
               extent[1],
               extent[3]])
    ax.add_geometries(list(reader), ccrs.PlateCarree(), alpha=.5)

    ax.coastlines()
    # ax.set_global()
    plt.savefig(os.path.join(tmpdir, 'tromsoe.png'))
def test_generate_landmask_static():
    l = Landmask()  # make sure landmask is already generated
    assert Landmask.__mask__() is not None

    del l
    gc.collect()

    assert Landmask.__mask__() is None
    l = Landmask()

    assert Landmask.__mask__() is not None
    l2 = Landmask()

    assert l2.mask is not None
    assert l.mask is Landmask.__mask__()
    assert l2.mask is Landmask.__mask__()
def test_norway(tmpdir):
    delete_mask()
    l = Landmask(extent=[-1, 44, 41, 68])

    lon = np.arange(0, 40, .1)
    lat = np.arange(45, 67, .1)

    xx, yy = np.meshgrid(lon, lat)
    c = l.contains(xx.ravel(), yy.ravel())
    c = c.reshape(xx.shape)

    print("c:", c)

    import cartopy.crs as ccrs
    import matplotlib.pyplot as plt
    import os
    plt.figure(dpi=200)
    ax = plt.axes(projection=ccrs.PlateCarree())
    # ax.contourf(lon, lat, c, transform = ccrs.PlateCarree())
    ax.imshow(c, extent=[0, 40, 45, 67], transform=ccrs.PlateCarree())
    ax.coastlines()
    ax.set_global()
    plt.savefig(os.path.join(tmpdir, 'norway.png'))
def test_landmask_weakref():
    assert Landmask.__mask__() is None
    l = Landmask()
    assert Landmask.__mask__() is not None
    assert l.mask is not None

    l2 = Landmask()
    del l
    gc.collect()

    assert Landmask.__mask__() is not None
    assert l2.mask is not None

    del l2
    gc.collect()
    assert Landmask.__mask__() is None
def test_generate_landmask():
    delete_mask()
    l = Landmask(__no_retry__=True)

    assert os.path.exists(mmapf)
def test_landmask_contains():
    delete_mask()
    l = Landmask()

    onland = (np.array([15.]), np.array([65.6]))
    assert l.contains(onland[0], onland[1])

    onland = (np.array([10.]), np.array([60.0]))
    assert l.contains(onland[0], onland[1])

    onocean = (np.array([5.]), np.array([65.6]))
    assert not l.contains(onocean[0], onocean[1])

    l.contains([180], [90])
    l.contains([-180], [90])
    l.contains([180], [-90])
    l.contains([-180], [-90])
Esempio n. 17
0
class Reader(BaseReader):
    """
    The global landmask reader is based on the coastline data from
    GSHHG (https://www.ngdc.noaa.gov/mgg/shorelines/) optimized for
    checking against landmasks.

    Args:
        :param extent: minx, miny, maxx, maxy bounding box in source CRS for which to include
            geometries. Default None (all geometries).
        :type extent: array of floats, optional

        :param llcrnrlon: minx.
        :type llcrnrlon: float, optional, deprecated in favor of extent.

        :param llcrnrlat: miny.
        :type llcrnrlat: float, optional, deprecated in favor of extent.

        :param urcrnrlon: maxx.
        :type urcrnrlon: float, optional, deprecated in favor of extent.

        :param urcrnrlat: maxy.
        :type urcrnrlat: float, optional, deprecated in favor of extent.

        :param skippoly: use only rasterized landmask to determine whether points are on land.
        :type skippoly: bool, optional, default False.
    """
    name = 'global_landmask'
    return_block = False
    variables = ['land_binary_mask']
    proj4 = None
    crs   = None
    skippoly = False

    def __init__(self,
                 extent = None,
                 llcrnrlon = None,
                 llcrnrlat = None,
                 urcrnrlon = None,
                 urcrnrlat = None,
                 skippoly = False):
        self.proj4 = '+proj=lonlat +ellps=WGS84'
        self.crs = pyproj.CRS(self.proj4)
        self.skippoly = skippoly

        super (Reader, self).__init__ ()

        # Depth
        self.z = None

        if extent and (llcrnrlat or llcrnrlon or urcrnrlat or urcrnrlon):
            raise Exception ("'extent' cannot be given togheter with any of 'llcrnrlon', ..'")
        elif extent is None and (llcrnrlat or llcrnrlon or urcrnrlat or urcrnrlon):
            warnings.warn("llcrnrlon, llcrnrlat, et. al. is deprecated for the global landmask reader. Prefer 'extent' in stead.", DeprecationWarning)
            extent = [llcrnrlon, llcrnrlat, urcrnrlon, urcrnrlat]

        # Read and store min, max and step of x and y
        if extent is not None:
            self.xmin, self.ymin, self.xmax, self.ymax = extent
        else:
            self.xmin, self.ymin = -180, -90
            self.xmax, self.ymax = 180, 90

        self.xmin, self.ymin = self.lonlat2xy(self.xmin, self.ymin)
        self.xmax, self.ymax = self.lonlat2xy(self.xmax, self.ymax)

        # setup landmask
        self.mask = Landmask(extent, skippoly)

    def __on_land__(self, x, y):
        return self.mask.contains (x, y, skippoly = self.skippoly, checkextent = False)

    def get_variables(self, requestedVariables, time = None,
                      x = None, y = None, z = None, block = False):
        """
        Get binary mask of whether elements are on land or not.

        Args:
            x (deg[]): longitude (decimal degrees)
            y (deg[]): latitude (decimal degrees)
            ...

        x, y is given in reader local projection.

        Returns:
            Binary mask of point x, y on land.

        """

        self.check_arguments(requestedVariables, time, x, y, z)
        return { 'land_binary_mask': self.__on_land__(x,y) }
def test_setup_landmask_pregenerated(benchmark):
    delete_mask()
    l = Landmask(__no_retry__=True)

    l = benchmark(Landmask)
def test_setup_landmask_retry_open():
    delete_mask()
    l = Landmask(__retry_delete__=True)
 def f():
     delete_mask()
     return Landmask(__no_retry__=True)
def _f(i):
    print("launching instance:", i)
    l = Landmask(__concurrency_delay__=2., __no_retry__=True)
    print("instance", i, "done")