Esempio n. 1
0
def _get_swathsegment(filelist, time_start, time_end=None, area=None):
    """
    Return only the granule files for the time interval or area.
    """
    if area is not None:
        from trollsched.spherical import SphPolygon
        from trollsched.boundary import AreaBoundary

        lons, lats = area.get_boundary_lonlats()
        area_boundary = AreaBoundary((lons.side1, lats.side1),
                                     (lons.side2, lats.side2),
                                     (lons.side3, lats.side3),
                                     (lons.side4, lats.side4))
        area_boundary.decimate(500)
        contour_poly = area_boundary.contour_poly

    segment_files = []
    for filename in filelist:

        timetup = _get_times_from_npp(filename)

        # Search for multiple granules using an area
        if area is not None:
            md = NPPMetaData(filename)
            md.read()
            coords = np.vstack(md.get_ring_lonlats())
            poly = SphPolygon(np.deg2rad(coords))
            if poly.intersection(contour_poly) is not None:
                segment_files.append(filename)
            continue

        # Search for single granule using time start
        if time_end is None:
            if time_start >= timetup[0] and time_start <= timetup[1]:
                segment_files.append(filename)
                continue

        # search for multiple granules
        else:
            # check that granule start time is inside interval
            if timetup[0] >= time_start and timetup[0] <= time_end:
                segment_files.append(filename)
                continue

            # check that granule end time is inside interval
            if timetup[1] >= time_start and timetup[1] <= time_end:
                segment_files.append(filename)
                continue

    segment_files.sort()
    return segment_files
Esempio n. 2
0
def _get_swathsegment(filelist, time_start, time_end=None, area=None):
    """
    Return only the granule files for the time interval or area.
    """
    if area is not None:
        from trollsched.spherical import SphPolygon
        from trollsched.boundary import AreaBoundary

        lons, lats = area.get_boundary_lonlats()
        area_boundary = AreaBoundary(
            (lons.side1, lats.side1), (lons.side2, lats.side2),
            (lons.side3, lats.side3), (lons.side4, lats.side4))
        area_boundary.decimate(500)
        contour_poly = area_boundary.contour_poly

    segment_files = []
    for filename in filelist:

        timetup = _get_times_from_npp(filename)

        # Search for multiple granules using an area
        if area is not None:
            md = NPPMetaData(filename)
            md.read()
            coords = np.vstack(md.get_ring_lonlats())
            poly = SphPolygon(np.deg2rad(coords))
            if poly.intersection(contour_poly) is not None:
                segment_files.append(filename)
            continue

        # Search for single granule using time start
        if time_end is None:
            if time_start >= timetup[0] and time_start <= timetup[1]:
                segment_files.append(filename)
                continue

        # search for multiple granules
        else:
            # check that granule start time is inside interval
            if timetup[0] >= time_start and timetup[0] <= time_end:
                segment_files.append(filename)
                continue

            # check that granule end time is inside interval
            if timetup[1] >= time_start and timetup[1] <= time_end:
                segment_files.append(filename)
                continue

    segment_files.sort()
    return segment_files
    def test_bool(self):
        """Test the intersection and union functions.
        """
        vertices = np.array([[180, 90, 0, -90],
                             [89, 89, 89, 89]]).T
        poly1 = SphPolygon(np.deg2rad(vertices))
        vertices = np.array([[-45, -135, 135, 45],
                             [89, 89, 89, 89]]).T
        poly2 = SphPolygon(np.deg2rad(vertices))

        uni = np.array([[157.5,   89.23460094],
                        [-225.,   89.],
                        [112.5,   89.23460094],
                        [90.,   89.],
                        [67.5,   89.23460094],
                        [45.,   89.],
                        [22.5,   89.23460094],
                        [0.,   89.],
                        [-22.5,   89.23460094],
                        [-45.,   89.],
                        [-67.5,   89.23460094],
                        [-90.,   89.],
                        [-112.5,   89.23460094],
                        [-135.,   89.],
                        [-157.5,   89.23460094],
                        [-180.,   89.]])
        inter = np.array([[157.5,   89.23460094],
                          [112.5,   89.23460094],
                          [67.5,   89.23460094],
                          [22.5,   89.23460094],
                          [-22.5,   89.23460094],
                          [-67.5,   89.23460094],
                          [-112.5,   89.23460094],
                          [-157.5,   89.23460094]])
        poly_inter = poly1.intersection(poly2)
        poly_union = poly1.union(poly2)

        self.assertTrue(poly_inter.area() <= poly_union.area())

        self.assertTrue(np.allclose(poly_inter.vertices,
                                    np.deg2rad(inter)))
        self.assertTrue(np.allclose(poly_union.vertices,
                                    np.deg2rad(uni)))

        # Test 2 polygons sharing 2 contiguous edges.

        vertices1 = np.array([[-10,  10],
                              [-5,  10],
                              [0,  10],
                              [5,  10],
                              [10,  10],
                              [10, -10],
                              [-10, -10]])

        vertices2 = np.array([[-5,  10],
                              [0,  10],
                              [5,  10],
                              [5,  -5],
                              [-5,  -5]])

        vertices3 = np.array([[5,  10],
                              [5,  -5],
                              [-5,  -5],
                              [-5,  10],
                              [0,  10]])

        poly1 = SphPolygon(np.deg2rad(vertices1))
        poly2 = SphPolygon(np.deg2rad(vertices2))
        poly_inter = poly1.intersection(poly2)

        self.assertTrue(np.allclose(poly_inter.vertices,
                                    np.deg2rad(vertices3)))

        # Test when last node of the intersection is the last vertice of the
        # second polygon.

        swath_vertices = np.array([[-115.32268301,   66.32946139],
                                   [-61.48397172,  58.56799254],
                                   [-60.25004314, 58.00754686],
                                   [-71.35057076,   49.60229517],
                                   [-113.746486,  56.03008985]])
        area_vertices = np.array([[-68.32812107,  52.3480829],
                                  [-67.84993896,  53.07015692],
                                  [-55.54651296,  64.9254637],
                                  [-24.63341856,  74.24628796],
                                  [-31.8996363,  27.99907764],
                                  [-39.581043,  37.0639821],
                                  [-50.90185988,  45.56296169],
                                  [-67.43022017,  52.12399581]])

        res = np.array([[-62.77837918,   59.12607053],
                        [-61.48397172,   58.56799254],
                        [-60.25004314,   58.00754686],
                        [-71.35057076,   49.60229517],
                        [-113.746486,     56.03008985],
                        [-115.32268301,   66.32946139]])

        poly1 = SphPolygon(np.deg2rad(swath_vertices))
        poly2 = SphPolygon(np.deg2rad(area_vertices))

        poly_inter = poly1.intersection(poly2)
        self.assertTrue(np.allclose(poly_inter.vertices,
                                    np.deg2rad(res)))

        poly_inter = poly2.intersection(poly1)
        self.assertTrue(np.allclose(poly_inter.vertices,
                                    np.deg2rad(res)))
    def test_bool(self):
        """Test the intersection and union functions.
        """
        vertices = np.array([[180, 90, 0, -90], [89, 89, 89, 89]]).T
        poly1 = SphPolygon(np.deg2rad(vertices))
        vertices = np.array([[-45, -135, 135, 45], [89, 89, 89, 89]]).T
        poly2 = SphPolygon(np.deg2rad(vertices))

        uni = np.array([[157.5, 89.23460094], [-225.,
                                               89.], [112.5, 89.23460094],
                        [90., 89.], [67.5, 89.23460094], [45., 89.],
                        [22.5, 89.23460094], [0., 89.], [-22.5, 89.23460094],
                        [-45., 89.], [-67.5, 89.23460094], [-90., 89.],
                        [-112.5, 89.23460094], [-135., 89.],
                        [-157.5, 89.23460094], [-180., 89.]])
        inter = np.array([[157.5, 89.23460094], [112.5, 89.23460094],
                          [67.5, 89.23460094], [22.5, 89.23460094],
                          [-22.5, 89.23460094], [-67.5, 89.23460094],
                          [-112.5, 89.23460094], [-157.5, 89.23460094]])
        poly_inter = poly1.intersection(poly2)
        poly_union = poly1.union(poly2)

        self.assertTrue(poly_inter.area() <= poly_union.area())

        self.assertTrue(np.allclose(poly_inter.vertices, np.deg2rad(inter)))
        self.assertTrue(np.allclose(poly_union.vertices, np.deg2rad(uni)))

        # Test 2 polygons sharing 2 contiguous edges.

        vertices1 = np.array([[-10, 10], [-5, 10], [0, 10], [5, 10], [10, 10],
                              [10, -10], [-10, -10]])

        vertices2 = np.array([[-5, 10], [0, 10], [5, 10], [5, -5], [-5, -5]])

        vertices3 = np.array([[5, 10], [5, -5], [-5, -5], [-5, 10], [0, 10]])

        poly1 = SphPolygon(np.deg2rad(vertices1))
        poly2 = SphPolygon(np.deg2rad(vertices2))
        poly_inter = poly1.intersection(poly2)

        self.assertTrue(np.allclose(poly_inter.vertices,
                                    np.deg2rad(vertices3)))

        # Test when last node of the intersection is the last vertice of the
        # second polygon.

        swath_vertices = np.array([[-115.32268301, 66.32946139],
                                   [-61.48397172, 58.56799254],
                                   [-60.25004314, 58.00754686],
                                   [-71.35057076, 49.60229517],
                                   [-113.746486, 56.03008985]])
        area_vertices = np.array([[-68.32812107, 52.3480829],
                                  [-67.84993896, 53.07015692],
                                  [-55.54651296, 64.9254637],
                                  [-24.63341856, 74.24628796],
                                  [-31.8996363, 27.99907764],
                                  [-39.581043, 37.0639821],
                                  [-50.90185988, 45.56296169],
                                  [-67.43022017, 52.12399581]])

        res = np.array([[-62.77837918,
                         59.12607053], [-61.48397172, 58.56799254],
                        [-60.25004314,
                         58.00754686], [-71.35057076, 49.60229517],
                        [-113.746486, 56.03008985],
                        [-115.32268301, 66.32946139]])

        poly1 = SphPolygon(np.deg2rad(swath_vertices))
        poly2 = SphPolygon(np.deg2rad(area_vertices))

        poly_inter = poly1.intersection(poly2)
        self.assertTrue(np.allclose(poly_inter.vertices, np.deg2rad(res)))

        poly_inter = poly2.intersection(poly1)
        self.assertTrue(np.allclose(poly_inter.vertices, np.deg2rad(res)))