55import time
66from efast import eoprofiling
77
8- from efast .binning import BBox , SynProduct , bin_to_grid , create_geogrid , get_reflectance_filename , bin_to_grid_numpy
8+ from efast .binning import FILL_VALUE , BBox , SynProduct , bin_to_grid , create_geogrid , get_reflectance_filename , bin_to_grid_numpy
9+ from efast .constants import S3L2SYNClassificationAerosolFlags , S3L2SYNCloudFlags
910
1011SYN_PRODUCT_PATH = (
1112 Path (__file__ ).parent .parent
@@ -31,22 +32,37 @@ def test_create_geogrid():
3132 lat , lon = create_geogrid (np .array ([], dtype = np .float32 ))
3233 return True
3334
35+ CLOUD_FLAGS_COMBINED = (
36+ S3L2SYNCloudFlags .CLOUD
37+ | S3L2SYNCloudFlags .CLOUD_AMBIGUOUS
38+ | S3L2SYNCloudFlags .CLOUD_MARGIN
39+ )
3440
3541def test_bin_to_grid_dev ():
3642 reflectance_bands = [get_reflectance_filename (i ) for i in (4 , 6 , 8 , 17 )]
37- band_names = [* reflectance_bands , "CLOUD_flags" ]
43+ band_names = [* reflectance_bands , "CLOUD_flags" , "SYN_flags" ]
3844 prod = SynProduct (SYN_PRODUCT_PATH , band_names = band_names )
3945 ds = prod .read_bands ()
4046 bbox = BBox .from_wkt (REGION )
4147 grid = create_geogrid (bbox , num_rows = 66792 )
4248
43- #binned = bin_to_grid_numpy(ds, reflectance_bands, grid, super_sampling=2)
49+ cloud_no_land_mask = ((ds ["CLOUD_flags" ] & CLOUD_FLAGS_COMBINED .value ) != 0 ) & (ds ["SYN_flags" ] & S3L2SYNClassificationAerosolFlags .SYN_land .value > 0 )
50+
51+ for reflectance_band in reflectance_bands :
52+ band_values = ds [reflectance_band ].data
53+ band_values [cloud_no_land_mask ] = - 10000
54+ ds [reflectance_band ] = (["lat" , "lon" ], band_values )
55+
56+
4457 binned = bin_to_grid_numpy (ds , reflectance_bands , grid , super_sampling = 2 )
4558 pixel_size = grid .lat [1 ] - grid .lat [0 ]
46- # TODO why last lat?
59+
60+ # TODO
61+ binned [binned < 0 ] = np .nan
62+
4763 transform = Affine .translation (grid .lon [0 ], grid .lat [- 1 ]) * Affine .scale (pixel_size , pixel_size )
4864
49- with rasterio .open (TEST_DATA_OUTPUT_PATH / "binned.tif" , "w" , driver = "GTiff" , height = binned .shape [1 ], width = binned .shape [2 ], count = len (reflectance_bands ), dtype = binned .dtype , crs = "+proj=latlong" , transform = transform ) as ds :
65+ with rasterio .open (TEST_DATA_OUTPUT_PATH / "binned.tif" , "w" , driver = "GTiff" , height = binned .shape [1 ], width = binned .shape [2 ], count = len (reflectance_bands ), dtype = binned .dtype , crs = "+proj=latlong" , transform = transform , nodata = np . nan ) as ds :
5066 ds .write (binned [:, ::- 1 , :])
5167
5268 return True
0 commit comments