예제 #1
0
    def reproj_roi(self, out_path, lonw, lone, lats, latn):
        '''
        根据给定的经纬度方位裁剪AMSR2 L3 数据
        :param out_path:
        :param lonw:
        :param lone:
        :param lats:
        :param latn:
        :return:
        '''
        corner_lon = [lonw, lone, lone, lonw]
        corner_lat = [lats, lats, latn, latn]
        corner_xy = self.proj_to_nsidc_sea_ice_stere_n(corner_lon, corner_lat)
        min_x = corner_xy[0].min()
        max_x = corner_xy[0].max()
        min_y = corner_xy[1].min()
        max_y = corner_xy[1].max()
        start_x = min_x - self.lu_x
        end_x = max_x - self.lu_x
        start_x_pixel = int(start_x / self.gsd)
        end_x_pixel = int(np.ceil(end_x / self.gsd))
        if start_x_pixel < 0:
            start_x_pixel = 0
        if end_x_pixel >= self.data_shape[1]:
            end_x_pixel = self.data_shape[1] - 1
        start_y = self.lu_y - max_y
        end_y = self.lu_y - min_y
        start_y_pixel = int(start_y / self.gsd)
        end_y_pixel = int(np.ceil(end_y / self.gsd))
        if start_y_pixel < 0:
            start_y_pixel = 0
        if end_y_pixel >= self.data_shape[0]:
            end_y_pixel = self.data_shape[0] - 1

        # 获得区域内的亮温
        sub_nh_89h = self.nh_89h[start_y_pixel:end_y_pixel,
                                 start_x_pixel:end_x_pixel].copy()
        sub_nh_89v = self.nh_89v[start_y_pixel:end_y_pixel,
                                 start_x_pixel:end_x_pixel].copy()
        origin_x = start_x_pixel * self.gsd + self.lu_x
        origin_y = self.lu_y - start_y_pixel * self.gsd

        file_format = 'GTiff'
        driver = gdal.GetDriverByName(file_format)
        dst_ds = driver.Create(out_path,
                               xsize=sub_nh_89h.shape[1],
                               ysize=sub_nh_89h.shape[0],
                               bands=2,
                               eType=gdal.GDT_Int32)
        srs = NSR(3411)
        dst_ds.SetProjection(srs.ExportToWkt())
        dst_ds.SetGeoTransform([origin_x, self.gsd, 0, origin_y, 0, -self.gsd])

        band1 = dst_ds.GetRasterBand(1)
        band1.WriteArray(sub_nh_89h)
        band2 = dst_ds.GetRasterBand(2)
        band2.WriteArray(sub_nh_89v)
        dst_ds = None
예제 #2
0
    def test_init_from_EPSG(self):
        nsr = NSR(4326)

        self.assertEqual(type(nsr), NSR)
        self.assertEqual(nsr.Validate(), 0)
        self.assertTrue('4326' in nsr.ExportToWkt())
예제 #3
0
    def reproj_roi(self, out_path, gsd, lonw, lone, lats, latn, res='H'):
        '''

        :param out_path:
        :param gsd:
        :param lonw:
        :param lone:
        :param lats:
        :param latn:
        :return:
        '''
        corner_lon = [lonw, lone, lone, lonw]
        corner_lat = [lats, lats, latn, latn]
        corner_xy = self.proj_to_wgs84_nsidc_sea_ice_stere_n(
            corner_lon, corner_lat)
        min_x = corner_xy[0].min()
        max_x = corner_xy[0].max()
        min_y = corner_xy[1].min()
        max_y = corner_xy[1].max()
        img_width = int(np.ceil((max_x - min_x) / gsd))
        img_height = int(np.ceil((max_y - min_y) / gsd))
        img_pixel_crd_x = np.linspace(min_x, max_x, img_width)
        img_pixel_crd_y = np.linspace(max_y, min_y, img_height)
        grid_crd_x, grid_crd_y = np.meshgrid(img_pixel_crd_x, img_pixel_crd_y)
        grid_lonlat = self.proj_to_wgs84_nsidc_sea_ice_stere_n(
            grid_crd_x.flatten(), grid_crd_y.flatten(), True)
        grid_lon = grid_lonlat[0]
        grid_lat = grid_lonlat[1]
        grid_lon_idx = (grid_lon - self.min_lon) / self.mask_res
        grid_lat_idx = (grid_lat - self.min_lat) / self.mask_res
        grid_lon_idx = grid_lon_idx.astype(np.int)
        grid_lat_idx = grid_lat_idx.astype(np.int)
        gpi = self.mask[grid_lat_idx, grid_lon_idx]
        gpi = gpi.reshape(grid_crd_x.shape)

        if res == 'H':
            hr_xy = self.proj_to_wgs84_nsidc_sea_ice_stere_n(
                self.hr_lon.flatten(), self.hr_lat.flatten())
            hr_x = hr_xy[0].reshape(self.hr_shape)
            hr_y = hr_xy[1].reshape(self.hr_shape)
            hr_pts = np.asarray(list(zip(hr_x.flatten(), hr_y.flatten())))
            hr_value = self.bt_89h.flatten()
            dst_pts = np.asarray(
                list(zip(grid_crd_x.flatten(), grid_crd_y.flatten())))
            dst_value = interpolate.griddata(hr_pts,
                                             hr_value,
                                             dst_pts,
                                             method='linear',
                                             rescale=True)
            dst_value = dst_value.reshape(grid_crd_x.shape)

        else:
            lr_xy = self.proj_to_wgs84_nsidc_sea_ice_stere_n(
                self.lr_lon.flatten(), self.lr_lat.flatten())
            lr_x = lr_xy[0].reshape(self.lr_shape)
            lr_y = lr_xy[1].reshape(self.lr_shape)
            lr_pts = np.asarray(list(zip(lr_x.flatten(), lr_y.flatten())))
            lr_value = self.bt_36h.flatten()
            dst_pts = np.asarray(
                list(zip(grid_crd_x.flatten(), grid_crd_y.flatten())))
            dst_value = interpolate.griddata(lr_pts,
                                             lr_value,
                                             dst_pts,
                                             method='cubic',
                                             rescale=True)
            dst_value = dst_value.reshape(grid_crd_x.shape)

        dst_value[~gpi] = np.nan
        file_format = 'GTiff'
        driver = gdal.GetDriverByName(file_format)
        dst_ds = driver.Create(out_path,
                               xsize=img_width,
                               ysize=img_height,
                               bands=1,
                               eType=gdal.GDT_Byte)
        srs = NSR(3413)
        dst_ds.SetProjection(srs.ExportToWkt())
        dst_ds.SetGeoTransform([min_x, gsd, 0, max_y, 0, -gsd])
        img_uint8 = get_uint8_image(dst_value, None, None, 1, 99)
        # img_uint8 = unsharp_masking_sharp(img_uint8)
        # img_uint8 = laplcian_sharp(img_uint8)
        dst_ds.WriteRaster(0, 0, img_width, img_height, img_uint8.tostring())
        dst_ds = None