def test_read_write_tif_sanity_check(self):
     tif_array = file_io_tools.read_file(test_files.conus2_subsurface)
     tif_ref = file_io_tools.read_geotiff(test_files.conus2_subsurface)
     file_io_tools.write_array_to_geotiff('conus2_subsurface_tif_read_write_test.tif',
                                          tif_array, tif_ref.GetGeoTransform(), tif_ref.GetProjectionRef())
     tif_read_array = file_io_tools.read_file('conus2_subsurface_tif_read_write_test.tif')
     self.assertIsNone(np.testing.assert_array_equal(tif_array, tif_read_array),
                       'reading and writing a tif should not change the data')
     os.remove('conus2_subsurface_tif_read_write_test.tif')
 def test_write_tiff(self):
     ds_ref = gdal.Open(os.fspath(test_files.regression_truth_tif))
     file_io_tools.write_array_to_geotiff('test_write_tif_out.tif',
                                          file_io_tools.read_file(test_files.regression_truth_tif),
                                          ds_ref.GetGeoTransform(), ds_ref.GetProjection())
     data_array = file_io_tools.read_file('test_write_tif_out.tif')
     self.assertIsNone(np.testing.assert_array_equal(data_array,
                                                     file_io_tools.read_file(
                                                         test_files.regression_truth_tif)),
                       'writing and reading a tif gives back the same array values')
     os.remove('test_write_tif_out.tif')
 def test_write_pfb_to_tif_to_sa(self):
     pfb_array = file_io_tools.read_file(test_files.forcings_pfb)
     srs = osr.SpatialReference()
     srs.SetWellKnownGeogCS("WGS84")
     file_io_tools.write_array_to_geotiff(data=pfb_array, out_raster_path='tif_out_test_forcings_file.tif',
                                          geo_transform=[0, 1000, 0, 0, 0, -1000], projection=srs.ExportToWkt())
     sa_array = file_io_tools.read_file(test_files.forcings_sa)
     tif_array = file_io_tools.read_file('tif_out_test_forcings_file.tif')
     self.assertIsNone(np.testing.assert_array_equal(tif_array, pfb_array,
                                                     'Converting from multi-layer pfb to tif gives back same data'))
     self.assertIsNone(np.testing.assert_array_almost_equal(tif_array, sa_array, decimal=4))
     os.remove('tif_out_test_forcings_file.tif')
Beispiel #4
0
    def write_mask_to_tif(self, filename):
        """write the mask to a tif file on disk

        Parameters
        ----------
        filename : str
            path and filename to store geotif output

        Returns
        -------
        None
        """
        write_array_to_geotiff(filename, self.mask_array, self.mask_tif.GetGeoTransform(),
                               self.mask_tif.GetProjection(), no_data=self.no_data_value)
def clip_inputs(clipper,
                input_list,
                out_dir='.',
                pfb_outs=1,
                tif_outs=0,
                no_data=NO_DATA) -> None:
    """clip a list of files using a clipper object

    Parameters
    ----------
    clipper : Clipper
        clipper object prepared with full_dim_mask and reference dataset
    input_list : list
        list of data files (tif, pfb) to clip from
    out_dir : str, optional
        output directory (optional) (Default value = '.')
    pfb_outs : int, optional
        write pfb files as outputs (optional) (Default value = 1)
    tif_outs : int, optional
        write tif files as outputs (optional) (Default value = 0)
    no_data : int, optional
        no_data value for tifs (optional) (Default value = NO_DATA)

    Returns
    -------
    None
    """
    ref_proj = None
    if tif_outs:
        # identify projection
        ref_proj = clipper.subset_mask.mask_tif.GetProjection()

    # loop over and clip
    for data_file in input_list:
        filename = Path(data_file).stem
        return_arr, new_geom, _, _ = clipper.subset(
            file_io_tools.read_file(data_file))
        if pfb_outs:
            file_io_tools.write_pfb(
                return_arr, os.path.join(out_dir, f'{filename}_clip.pfb'))
        if tif_outs and new_geom is not None and ref_proj is not None:
            file_io_tools.write_array_to_geotiff(os.path.join(
                out_dir, f'{filename}_clip.tif'),
                                                 return_arr,
                                                 new_geom,
                                                 ref_proj,
                                                 no_data=no_data)
        del return_arr
Beispiel #6
0
    def test_subset_tif_conus2(self):
        data_array = file_io_tools.read_file(test_files.conus2_dem.as_posix())
        my_mask = SubsetMask(
            test_files.huc10190004.get('conus2_mask').as_posix())
        clipper = MaskClipper(subset_mask=my_mask, no_data_threshold=-1)
        return_arr, new_geom, new_mask, bbox = clipper.subset(data_array)
        file_io_tools.write_array_to_geotiff("conus_2_clip_dem_test.tif",
                                             return_arr, new_geom,
                                             my_mask.mask_tif.GetProjection())
        self.assertIsNone(
            np.testing.assert_array_equal(
                file_io_tools.read_file(
                    test_files.huc10190004.get('conus2_dem').as_posix()),
                file_io_tools.read_file('conus_2_clip_dem_test.tif')),
            'Clipping DEM matches reference')
        os.remove('conus_2_clip_dem_test.tif')

        file_io_tools.write_bbox(bbox, 'bbox_conus2_full.txt')
        self.assertSequenceEqual(
            file_io_tools.read_bbox('bbox_conus2_full.txt'),
            test_files.huc10190004.get('conus2_bbox'),
            'Subset writes correct bounding box file')
        os.remove('bbox_conus2_full.txt')