I am looking to create a stack of cloud-free sentinel-2 imagery by day for a large portion of California for all of 2020 in the Descartes Lab platform. I realize that this is a very large amount of imagery and will require significant memory to store and so I'm looking for methods to mosaic imagery by day. I'm new to Descartes and I think using the groupby and mosaic methods could work but not sure how to implement the groupby function on a masked image collection.
First, I establish a small area of interest in Northern California and dates from January to March of 2020 (I am using a small area and small time range to test out the workflow):
import descarteslabs as dl
import numpy as np
# Define a bounding box in N CA, in a GeoJSON
aoi = {
"type": "Polygon",
"coordinates": [
[
[-121.8430, 38.9676],
[-121.6991, 38.9676],
[-121.6991, 38.8590],
[-121.8430, 38.8590],
]
],
}
start_datetime = "2019-01-01"
end_datetime = "2019-03-30"
Next, I create a Sentinel-2 scene stack with desired bands:
sentinel_scenes, sentinel_ctx = dl.scenes.search(
aoi,
products="sentinel-2:L1C",
start_datetime = start_datetime,
end_datetime = end_datetime,
# cloud_fraction=0.7,
limit = 500,
)
sentinel_all_stack = sentinel_scenes.stack('red green blue red-edge nir swir1 derived:ndvi',
sentinel_ctx,
processing_level='surface')
Then I create a Descartes Labs Sentinel-2 cloudmask stack for masking:
dlcloud_scenes, dlcloud_ctx = dl.scenes.search(
aoi,
products=["sentinel-2:L1C:dlcloud:v1"],
start_datetime=start_datetime, end_datetime=end_datetime,
limit=None
)
dlcloud_valid_cloudfree_stack = dlcloud_scenes.stack('valid_cloudfree',
sentinel_ctx ,
data_type='Byte')
Next, I mask the Sentinel-2 collection with the Descartes Labs cloudmask:
dlcloud_valid_cloudfree_stack_rep = np.repeat(a=dlcloud_valid_cloudfree_stack,
repeats=7,
axis=1)
# Mask the pixel values where valid_cloud_free is 0
cloudfree_stack = np.ma.masked_where(dlcloud_valid_cloudfree_stack_rep==0, sentinel_all_stack)
From here, I would like to group the imagery by day. I tried:
for (year, month, day), day_scenes in imagery.groupby(
"properties.date.year", "properties.date.month", "properties.date.day"):
print("{}: {} scenes".format(month, np.array(day_scenes.stack("blue green red red-edge nir swir1 derived:ndvi", ctx)).shape))
And I get the error: 'MaskedArray' object has no attribute 'groupby'
I would like to mosaic all images grouped by same day afterwards. All ideas are very welcome!