def test_albedo(self): l8 = Landsat8(self.dirname_cloud) albedo = l8.albedo()[self.cell] # inputs for self.cell toa reflect b 2, 4, 5, 6, 7 l = [0.021763351, 0.065929502, 0.33231941, 0.2018306, 0.10294776] exp_alb = (0.356 * l[0] + 0.130 * l[1] + 0.373 * l[2] + 0.085 * l[3] + 0.072 * l[4] - 0.0018) / 1.014 self.assertAlmostEqual(exp_alb, albedo, delta=0.001)
def test_find_image_station(self): """ Test find closest agrimet station to Landsat image centroid. :return: """ l8 = Landsat8(self.dirname_image) agrimet = Agrimet(sat_image=l8) self.assertEqual(agrimet.station, self.fetch_site)
def download_cdl_over_path_row(path, row, year, image_directory): out_dir = os.path.join(image_directory, '_'.join([str(path), str(row), str(year)])) cdl_mask = os.path.join(out_dir, "cdl_mask.tif") if os.path.isfile(cdl_mask): print("cdl already downloaded for {} {} {}".format(path, row, year)) return sub_dirs = os.listdir(out_dir) if not len(sub_dirs): raise ValueError("images not downloaded for {} {} {}".format(path, row, year)) print(path, row, year) for r in sub_dirs: if os.path.isdir(os.path.join(out_dir, r)): if 'climate' not in r: random_landsat_dir = os.path.join(out_dir, r) break landsat = glob(os.path.join(random_landsat_dir, "*TIF"))[0] landsat_pic = landsat landsat = Landsat8(random_landsat_dir) try: polygon = landsat.get_tile_geometry() cdl = Cdl(year=year, target_profile=landsat.profile) cdl.get_mask(clip_geometry=polygon, out_file=cdl_mask) except Exception as e: print(e.args) print(landsat_pic)
def _get_landsat_image(self): l8 = Landsat8(self.landsat) bounds = RasterBounds( affine_transform=l8.rasterio_geometry['transform'], profile=l8.rasterio_geometry) proj_bounds = bounds.to_epsg(32611) clip = proj_bounds.get_shapely_polygon() return l8.rasterio_geometry, bounds, clip
def test_instantiate_Agrimet(self): """ Test object instantiation. :return: """ ag = Agrimet(start_date='2000-01-01', end_date='2000-12-31', station=self.fetch_site, sat_image=Landsat8(self.dirname_image)) self.assertIsInstance(ag, Agrimet)
def test_elevation(self): l8 = Landsat8(self.dir_name_LC8) polygon = l8.get_tile_geometry() bounds = RasterBounds(affine_transform=l8.rasterio_geometry['transform'], profile=l8.rasterio_geometry) gridmet = GridMet('elev', date=self.date, bbox=bounds, target_profile=l8.rasterio_geometry, clip_feature=polygon) gridmet.save_raster() gridmet.get_data_subset(os.path.join(self.grimet_raster_dir, 'elevation.tif'))
def test_toa_brightness(self): l8 = Landsat8(self.dirname_cloud) with rasterio.open(self.ex_bright, 'r') as src: ex_br = src.read(1) bright = l8.brightness_temp(10) self.assertEqual(bright.shape, ex_br.shape) self.assertAlmostEqual(ex_br[self.cell], bright[self.cell], delta=0.001)
def setUp(self): self.dir_name_LT8 = 'tests/data/ssebop_test/lc8/038_027/2014/LC80380272014227LGN01' self.l8 = Landsat8(self.dir_name_LT8) self.lst = self.l8.land_surface_temp() # self.lst_raster_path = os.path.join(self.dir_name_LT8, 'lst8_test.tif') self.lst_raster_path = '/data01/images/sandbox/lst8_test.tif' self.l8.save_array(self.lst, self.lst_raster_path) self.point_file = 'tests/data/points/038_027_US_Mj_points.shp' self.eef_lst_raster = os.path.join( 'tests/data/ssebop_test/lc8/038_027', '2014/LC80380272014227LGN01/LC80380272014227LGN01_LST_EEF.tif')
def test_instantiate_scene(self): l8 = Landsat8(self.dirname_cloud) self.assertEqual(l8.mtl['L1_METADATA_FILE']['PRODUCT_METADATA']['FILE_NAME_BAND_1'], 'LC80400282014193LGN00_B1.TIF') self.assertEqual(l8.utm_zone, 12) self.assertEqual(l8.rasterio_geometry['height'], 727) self.assertEqual(l8.rasterio_geometry['driver'], 'GTiff') self.assertEqual(l8.rasterio_geometry['dtype'], 'uint16') self.assertEqual(l8.rasterio_geometry['transform'], Affine(30.0, 0.0, 367035.0, 0.0, -30.0, 5082585.0))
def test_image_geo(self): for runspec in self.cfg.runspecs: paths.build(runspec.root) sseb = SSEBopModel(runspec) setattr(sseb, 'image', Landsat8(sseb.image_dir)) image_geo = SSEBopGeo(sseb.image_id, sseb.image_dir, sseb.image.get_tile_geometry(), sseb.image.transform, sseb.image.profile, sseb.image.rasterio_geometry) self.assertIsInstance(image_geo, SSEBopGeo) self.assertIsInstance(image_geo.image_dir, str)
def test_mask(self): l8 = Landsat8(self.dir_name_LC8) shape = 1, l8.rasterio_geometry['height'], l8.rasterio_geometry[ 'width'] polygon = l8.get_tile_geometry() cdl = Cdl(from_file=os.path.join(self.dir_name_LC8, 'cdl.tif')) arr = cdl.get_mask(polygon, out_file=os.path.join(self.dir_name_LC8, 'cdl_mask.tif')) self.assertEqual(shape, arr.shape) self.assertEqual(arr.max(), 1) self.assertEqual(arr.min(), 0)
def test_conforming_array(self): """ Test shape of Gridmet vs. Landsat image. :return: """ l8 = Landsat8(self.dir_name_LC8) polygon = l8.get_tile_geometry() bounds = RasterBounds(affine_transform=l8.rasterio_geometry['transform'], profile=l8.rasterio_geometry) gridmet = GridMet(self.var, date=self.date, bbox=bounds, target_profile=l8.rasterio_geometry, clip_feature=polygon) pet = gridmet.get_data_subset() shape = 1, l8.rasterio_geometry['height'], l8.rasterio_geometry['width'] self.assertEqual(pet.shape, shape)
def fmask(directory): dirs = [os.path.join(directory, x) for x in os.listdir(directory)] tif_name = 'cloud_mask.tif' for d in dirs: if tif_name in os.listdir(d): print('Looks like {} already has a {}'.format(d, tif_name)) else: print('Processing {}'.format(d)) l = Landsat8(d) f = Fmask(l) combo = f.cloud_mask(combined=True) f.save_array(combo, outfile=os.path.join(d, tif_name)) return None
def test_conforming_array(self): """ Test shape of Gridmet vs. Landsat image. :return: """ l8 = Landsat8(self.dir_name_LC8) shape = 1, l8.rasterio_geometry['height'], l8.rasterio_geometry[ 'width'] polygon = l8.get_tile_geometry() cdl = Cdl(year=self.year, target_profile=l8.profile, out_dir=self.dir_name_LC8) _ = cdl.get_conforming_data(polygon) with rasopen(os.path.join(self.dir_name_LC8, 'cdl.tif')) as dst: arr = dst.read() self.assertEqual(arr.shape, shape)
def test_conforming_array_to_native(self): """ Test confoming array to native Gridmet raster. Conforming array is what Thredds.Gridmet will build given geometry paramters derived from LandsatImage object. This test builds that array, and then compares it with several day's of native Gridmet netcdf data. The rasters can't align perfectly, as the grid has been resampled. This is built to look up 30 points, extract their location, get the native raster value (i.e., geo) at that location, and the (local) conforming array value. The ratio of the means from each raster must be w/in 0.5%. :return: """ l8 = Landsat8(self.dir_name_LC8) polygon = l8.get_tile_geometry() bounds = RasterBounds( affine_transform=l8.rasterio_geometry['transform'], profile=l8.rasterio_geometry, latlon=True) for day in rrule(DAILY, dtstart=self.start, until=self.end): gridmet = GridMet(self.var, date=day, bbox=bounds, target_profile=l8.rasterio_geometry, clip_feature=polygon) date_str = datetime.strftime(day, '%Y-%m-%d') met_arr = os.path.join(self.grimet_raster_dir, 'met_{}_{}.tif'.format(date_str, self.var)) met = gridmet.get_data_subset(out_filename=met_arr) native = os.path.join(self.grimet_raster_dir, '{}_pet.tif'.format(date_str)) points_dict = multi_raster_point_extract( local_raster=met_arr, geographic_raster=native, points=self.scene_points, image_profile=l8.rasterio_geometry) geo_list, local_list = [], [] for key, val in points_dict.items(): geo_list.append(val['geo_val']) local_list.append(val['local_val']) ratio = mean(geo_list) / mean(local_list) print('Ratio on {} of CONUSRaster:LocalRaster calculated is {}.'. format(datetime.strftime(day, '%Y-%m-%d'), ratio)) self.assertAlmostEqual(ratio, 1.0, delta=0.005) os.remove(met_arr)
def test_conforming_array_local(self): l8 = Landsat8(self.dir_name_LC8) polygon = l8.get_tile_geometry() bounds = RasterBounds( affine_transform=l8.rasterio_geometry['transform'], profile=l8.rasterio_geometry) gridmet = GridMet(self.var, date=self.date, bbox=bounds, target_profile=l8.rasterio_geometry, clip_feature=polygon) pet = gridmet.get_data_subset( file_url='/home/dgketchum/Downloads/{}_{}.nc'.format( self.var, self.date.year)) shape = 1, l8.rasterio_geometry['height'], l8.rasterio_geometry[ 'width'] self.assertEqual(pet.shape, shape)
def test_dem(self): l8 = Landsat8(self.dir_name_LC8) polygon = l8.get_tile_geometry() profile = l8.rasterio_geometry bb = RasterBounds(affine_transform=profile['affine'], profile=profile, latlon=True) dem = AwsDem(zoom=10, target_profile=profile, bounds=bb, clip_object=polygon) elev = dem.terrain( attribute='slope', out_file='/home/dgketchum/IrrigationGIS/tests/mapzen_' '{}_{}.tif'.format(l8.target_wrs_path, l8.target_wrs_row)) self.assertEqual(elev.shape, (1, 7429, 8163)) aspect = dem.terrain(attribute='aspect') self.assertEqual(aspect.shape, (7429, 8163)) slope = dem.terrain(attribute='slope') self.assertEqual(slope.shape, (1, 7429, 8163))
def test_toa_reflectance(self): l8 = Landsat8(self.dirname_cloud) with rasterio.open(self.ex_reflect, 'r') as src: expected_reflectance = src.read(1) reflectance = l8.reflectance(1)
def test_ndvi(self): l8 = Landsat8(self.dirname_cloud) ndvi = l8.ndvi()[self.cell] b5, b4 = l8.reflectance(5)[self.cell], l8.reflectance(4)[self.cell] ndvi_exp = (b5 - b4) / (b5 + b4) self.assertEqual(ndvi, ndvi_exp)
def test_ndsi(self): l8 = Landsat8(self.dirname_cloud) ndsi = l8.ndsi()[self.cell] b3, b6 = l8.reflectance(3)[self.cell], l8.reflectance(6)[self.cell] ndsi_exp = (b3 - b6) / (b3 + b6) self.assertEqual(ndsi, ndsi_exp)
def setUp(self): self.dirname_cloud = os.path.join(DATA, 'fmask_test', 'lc8_fmask') self.image = Landsat8(self.dirname_cloud)