From the public bucket
Read Sentinel-2 files from public bucket¶
Set the env variables to be able to read from the Google bucket. This is needed to cover reading costs
!pip install georeader-spaceml fsspec gcsfs
import os
from georeader.readers import S2_SAFE_reader
os.environ["GS_NO_SIGN_REQUEST"] = "YES"
# Donwload key from next line link to access the buckets and requester pays requests to public bucket (this is needed to query Sentinel-2 data)
# This is required to do advaced operations in the GCP bucket
# os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path/to/file.json"
# os.environ["GS_USER_PROJECT"] = "project-name"
# S2_SAFE_reader.DEFAULT_REQUESTER_PAYS=True
%%time
safe_file = "S2B_MSIL1C_20220527T030539_N0400_R075_T49SGV_20220527T051042.SAFE"
s2_safe_folder = S2_SAFE_reader.s2_public_bucket_path(safe_file, check_exists=False)
print(f"File is located at: {s2_safe_folder}")
s2obj = S2_SAFE_reader.s2loader(s2_safe_folder, out_res=10)
os.makedirs("deleteme",exist_ok=True)
s2obj = s2obj.cache_product_to_local_dir("deleteme")
s2obj
Select the bands and bounds to read and trigger load
(to read the data in memory)
%%time
from georeader import read
bounds_read = (759760.0, 3940220.0, 799760.0, 3960220.0)
s2obj_3bands = s2obj.read_from_band_names(["B04", "B03", "B02"])
data = read.read_from_bounds(s2obj_3bands, bounds_read) # This does not trigger the download of the data
data_memory = data.load() # this triggers download only for the selected bands and within the bounds
data_memory
s2obj.radio_add_offsets()
import numpy as np
cosa = np.array([1001,10002,1000], dtype=np.uint16)
cosa - 1006
The numpy.array
is stored in the values
property.
%%time
data_memory.values
Plot the data that we have read
%%time
import rasterio.plot as rstplt
import numpy as np
# From processing baseline PB04.00 values have an offset of 1_000.
# This is handled automatically by the load function
rstplt.show(np.clip(data_memory.values/3_000,0,1), transform=data_memory.transform)
# import shutil
# if os.path.exists("deleteme"):
# shutil.rmtree("deleteme")
Licence¶
The georeader package is published under a GNU Lesser GPL v3 licence
If you find this work useful please cite:
@article{portales-julia_global_2023,
title = {Global flood extent segmentation in optical satellite images},
volume = {13},
issn = {2045-2322},
doi = {10.1038/s41598-023-47595-7},
number = {1},
urldate = {2023-11-30},
journal = {Scientific Reports},
author = {Portalés-Julià, Enrique and Mateo-García, Gonzalo and Purcell, Cormac and Gómez-Chova, Luis},
month = nov,
year = {2023},
pages = {20316},
}