|
| 1 | +// Example script showing how to create composite images |
| 2 | +// at regular intervals |
| 3 | + |
| 4 | +// Let's create 15-day composites |
| 5 | +// Change the parameters below for custom intervals |
| 6 | +// i.e. For monthly composites, use interval=1 and unit='month' |
| 7 | + |
| 8 | +// Define the interval |
| 9 | +var interval = 15; |
| 10 | + |
| 11 | +// Define the unit of interval |
| 12 | +// 'year', 'month' 'week', 'day', 'hour', 'minute', or 'second'. |
| 13 | +var unit = 'day'; |
| 14 | + |
| 15 | +// Define the period |
| 16 | +var startDate = ee.Date.fromYMD(2023, 1, 1); |
| 17 | +var endDate = ee.Date.fromYMD(2024, 1, 1); |
| 18 | + |
| 19 | +// Get the total units in the period |
| 20 | +var totalUnits = endDate.difference(startDate, unit); |
| 21 | +print('Total ' + unit, totalUnits); |
| 22 | + |
| 23 | +// Create a list of dates at start of each interval |
| 24 | +var intervals = ee.List.sequence(0, totalUnits, interval); |
| 25 | + |
| 26 | +var startDates = intervals.map(function(interval) { |
| 27 | + var intervalStart = startDate.advance(interval, unit); |
| 28 | + return intervalStart; |
| 29 | +}); |
| 30 | + |
| 31 | +print('Start dates for each interval', startDates); |
| 32 | + |
| 33 | +// Now we create the composites |
| 34 | + |
| 35 | +// Define collection and apply filters |
| 36 | +var s2 = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED") |
| 37 | +var basin = ee.FeatureCollection("WWF/HydroSHEDS/v1/Basins/hybas_7") |
| 38 | +var arkavathy = basin.filter(ee.Filter.eq('HYBAS_ID', 4071139640)) |
| 39 | +var geometry = arkavathy.geometry() |
| 40 | +Map.centerObject(geometry, 11) |
| 41 | + |
| 42 | +// Function to remove cloud pixels from Sentinel-2 SR image |
| 43 | +function maskCloudAndShadowsSR(image) { |
| 44 | + var cloudProb = image.select('MSK_CLDPRB'); |
| 45 | + var snowProb = image.select('MSK_SNWPRB'); |
| 46 | + var cloud = cloudProb.lt(10); |
| 47 | + var scl = image.select('SCL'); |
| 48 | + var shadow = scl.eq(3); // 3 = cloud shadow |
| 49 | + var cirrus = scl.eq(10); // 10 = cirrus |
| 50 | + // Cloud probability less than 10% or cloud shadow classification |
| 51 | + var mask = cloud.and(cirrus.neq(1)).and(shadow.neq(1)); |
| 52 | + return image.updateMask(mask).divide(10000) |
| 53 | + .copyProperties(image, ['system:time_start']); |
| 54 | +} |
| 55 | + |
| 56 | +var filtered = s2 |
| 57 | + .filter(ee.Filter.bounds(geometry)) |
| 58 | + .map(maskCloudAndShadowsSR) |
| 59 | + |
| 60 | +// We map() a function that takes each date from the startDates |
| 61 | +// and applies a filter for images in that interval |
| 62 | +var compositeImages = startDates.map(function(startDate) { |
| 63 | + var intervalStartDate = ee.Date(startDate); |
| 64 | + var intervalEndDate = intervalStartDate.advance(interval, unit); |
| 65 | + // Remember that end dates are not included in EE Filters |
| 66 | + // So we get images upto the end date. |
| 67 | + var intervalFiltered = filtered.filter( |
| 68 | + ee.Filter.date(intervalStartDate, intervalEndDate)); |
| 69 | + // Count the number of images |
| 70 | + // This will be used later to filter out |
| 71 | + // composites with no matching images |
| 72 | + var intervalImageCount = intervalFiltered.size(); |
| 73 | + var composite = intervalFiltered.median(); |
| 74 | + return composite.set({ |
| 75 | + 'system:time_start': intervalStartDate.millis(), |
| 76 | + 'system:time_end': intervalEndDate.millis(), |
| 77 | + 'start_date': intervalStartDate.format('YYYY-MM-dd'), |
| 78 | + 'end_date': intervalEndDate.format('YYYY-MM-dd'), |
| 79 | + 'image_count': intervalImageCount |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +var compositeCol = ee.ImageCollection.fromImages(compositeImages); |
| 84 | + |
| 85 | +var compositeColFiltered = compositeCol.filter( |
| 86 | + ee.Filter.neq('image_count', 0)); |
| 87 | + |
| 88 | +print('Composites at ' + interval + ' ' + unit + ' intervals', |
| 89 | + compositeColFiltered); |
0 commit comments