-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathS2_baresoil_GEE.txt
461 lines (300 loc) · 20.9 KB
/
S2_baresoil_GEE.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sentine-2-processing
/// 1. Cloud-masking
/// 3. Additional processing of collection:
//// -> percentile-masking
//// -> add timeStamp-bands
//// -> add spectral index-bands
//// -> select/order bands
/// 4. Frequency composite images
//// -> total pixel-frequency (freqTotal)
//// -> baresoil pixel-frequency (freqBS)
//// -> vegetation pixel-frequency (freqVeg)
/// 5. Reflectance composite images
//// -> mean of annual GreenestPixel (bands:B2-B8,B8A,B9-B12,ndvi,nbr2)
//// -> mean of annual LeastGreenestPixel (bands:B2-B8,B8A,B9-B12,ndvi,nbr2)
//// -> mean of annual Median (bands:B2-B8,B8A,B9-B12,ndvi,nbr2)
/// 6. Soil reflectance composite images
//// -> mean of annual barest pixel ((bands:B2-B8,B8A,B9-B12,ndvi,nbr2))
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//==========================================================================================================================================================================================//
//==========================================================================================================================================================================================//
// Set Globals
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
// Globals: Location and Period for S2-collection
//--------------------------------------------------------//
var aoi = table; // add any point or polygon geometry by clicking into the map
var aoi_forClip = table;// add any point or polygon geometry by clicking into the map
// Loop through years
for (var year = 2017; year <= 2023; year++) {
// Update startYear and endYear
var startYear = ee.Number(year);
var endYear = ee.Number(year);
// Update startDate and endDate
var startDate = ee.String(ee.Number(year).format('%d')).cat('-01-01');
var endDate = ee.String(ee.Number(year).format('%d')).cat('-12-31');
var startMonth= ee.Number(1);
var endMonth= ee.Number(12);
/*
var startDate = '2017-01-01';
var endDate = '2017-12-31';
var startMonth= ee.Number(1);
var endMonth= ee.Number(12);
var startYear = ee.Number(2017)
var endYear = ee.Number(2017)
*/
// Globals: Cloud and cloud shadow detection (s2cloudless-parameter)
//--------------------------------------------------------//
var cloudFilter = 50; // maximum allowed cloud cover per image in %
var cloudProbThr = 10; // Cloud probability threshold per pixel in %; greater values are considered as cloud
var nirDarknessThr = 0.15; // cloud shadow threshold based on nir-reflectance; lesser values are considered as cloud shadow
var cloudProjDist = 2; // maximum distance to search for cloud shadows from cloud edge in pixels
var cloudBuffer = 50; // distance (m) to extent the edge of cloud objects
// Globals: Visualization
//--------------------------------------------------------//
var visualization = {
min: 0.0,
max: 3000,
bands: ['B4', 'B3', 'B2'],
};
// Get SRTM-dem (30m x 30m)
//--------------------------------------------------------//
var dem = ee.Image("USGS/SRTMGL1_003"); // needed for topographic correction
//==========================================================================================================================================================================================//
//==========================================================================================================================================================================================//
//==========================================================================================================================================================================================//
//==========================================================================================================================================================================================//
// Cloud-masked Sentinel-2-collection
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
// Function: Get S2_SR collection with s2cloudless-info, filtered by aoi,period, relevant bands and a cloudPrefilter
//--------------------------------------------------------//
var FUN_s2_collectionPrefiltered = function(aoi, startDate,endDate,startMonth,endMonth) {
var s2_sr = ee.ImageCollection('COPERNICUS/S2_SR')
.filterDate(startDate, endDate)
.filter(ee.Filter.calendarRange(startMonth,endMonth,'month'))
.filter(ee.Filter.bounds(aoi))
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',cloudFilter)) // => prefilter derived from raw metadata
.sort('system:time_start',true) // if false collection is sorted descending
.select(['B1','B2', 'B3','B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B11', 'B12',
'SCL']);
var s2_cloudProb = ee.ImageCollection('COPERNICUS/S2_CLOUD_PROBABILITY')
.filterDate(startDate, endDate)
.filter(ee.Filter.calendarRange(startMonth,endMonth,'month'))
.filter(ee.Filter.bounds(aoi))
.sort('system:time_start',true) // if false collection is sorted descending
.select(['probability']);
var filter_sysIndex = ee.Filter.equals({'leftField':'system:index',
'rightField': 'system:index'
});
return ee.ImageCollection(ee.Join.saveFirst('s2cloudless').apply({'primary':s2_sr,
'secondary':s2_cloudProb,
'condition':filter_sysIndex,
}))};
// Function: Add bands -> add 2cloudless-prob and cloud mask to S2_SR image
//--------------------------------------------------------//
var FUN_addCloudBands = function(img){
var cloudProb = ee.Image(img.get('s2cloudless')).select('probability'); // get s2cloudless-probability band as image
var isCloud = cloudProb.gt(cloudProbThr).rename('clouds'); // get mask according to probability threshold
return img.addBands(ee.Image([cloudProb,isCloud]))}; // add s2cloudless-probability and probability mask as image-bands
// Function: Add bands -> add masks for dark pixels, cloud projection and shadows to S2_SR image
//--------------------------------------------------------//
var FUN_addShadowBands = function(img){
var not_water = img.select('SCL').neq(6) // get water, snow, cloud mask from the SCL-band
.and(img.select('SCL').neq(3))
.and(img.select('SCL').neq(8))
.and(img.select('SCL').neq(9))
.and(img.select('SCL').neq(10))
.and(img.select('SCL').neq(11));
var SR_BAND_SCALE = 1e4; // rescale B8-band to get dark pixel mask from based on dark pixel-threshold
var dark_pixels = img.select('B8').lt(nirDarknessThr*SR_BAND_SCALE).multiply(not_water).rename('dark_pixels');
var shadow_azimuth = ee.Number(90).subtract(ee.Number(img.get('MEAN_SOLAR_AZIMUTH_ANGLE'))); // determine the direction of shadows from clouds (assumes UTM projection)
var cld_proj = (img.select('clouds').directionalDistanceTransform(shadow_azimuth, cloudProjDist*10) // extent shadows from clouds based on cloudProjDist-threshold
.reproject({'crs': img.select(0).projection(), 'scale': 100})
.select('distance')
.mask()
.rename('cloud_transform'));
var shadows = cld_proj.multiply(dark_pixels).rename('shadows'); // get mask of combined darkpixel-band and shadow-band
return img.addBands(ee.Image([dark_pixels, cld_proj, shadows]))}; //add masks for darkpixels,shadow extent and shadows as image-bands
// Function: Add band -> Assemble cloud and shadow components and add final cloud-shadow-mask to to S2_SR image
//--------------------------------------------------------//
var FUN_addCloudShadowMask = function(img){
var img_cloud = FUN_addCloudBands(img); // add previously processed cloud bands
var img_cloud_shadow = FUN_addShadowBands(img_cloud); // add previously processed shadow bands
var is_cld_shdw1 = img_cloud_shadow.select('clouds').add(img_cloud_shadow.select('shadows')).gt(0); // combine cloud and shadow mask; cloud and shadow = 1, else 0
var is_cld_shdw = (is_cld_shdw1.focal_min(2).focal_max(cloudBuffer*2/20) // remove small cloud shadow patches and extent remaining pixels by cloudBuffer-threshold
.reproject({'crs': img.select([0]).projection(), 'scale': 20}) // 20m-scale assumes adequate precision and causes accelerated processing
.rename('cloudmask'));
return img.addBands(is_cld_shdw)}; // add cloud-shadow-mask as band to the original S2-bands; if you want to keep all processed bands, replace with "return img_cloud_shadow.addBands(is_cld_shdw)"
// Function: apply the cloudmask to each image in a s2-collection
//--------------------------------------------------------//
var FUN_applyCloudShadowMask = function (img){
var not_cld_shdw = img.select('cloudmask').not(); // get cloudmask-band and invert: cloud/shadow=0, else 1
return img.select('B.*').updateMask(not_cld_shdw)}; // get reflectenace bands masked by cloudShadowMask
// Get s2-collection filtered by space-time and atmospherically corrected
//--------------------------------------------------------//
var S2_coll = FUN_s2_collectionPrefiltered(aoi, startDate,endDate,startMonth,endMonth);
var S2_coll_AtmoCorrected = S2_coll.map(FUN_addCloudShadowMask)
.map(FUN_applyCloudShadowMask);
// Function to mask out snowy pixels
var FUN_maskSnow = function(image) {
// Use appropriate bands for snow detection based on your specific requirements
var snowMask = image.select('B4').lt(1000); // Modify this condition based on your data
// Update the mask property of the image
return image.updateMask(snowMask);
};
// Apply snow masking function to the collection
var S2_coll_SnowMasked = S2_coll_AtmoCorrected.map(FUN_maskSnow);
var S2 = S2_coll_SnowMasked; // rename for further processing
//==========================================================================================================================================================================================//
//==========================================================================================================================================================================================//
//==========================================================================================================================================================================================//
//==========================================================================================================================================================================================//
// Additional processing of collection
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
// Mask pixels if one band greater and below absolute threshold values
//--------------------------------------------------------//
var thr_top = 10000;
var S2 = S2.map(function(image) {
var maskTop = image.lt(thr_top);
return image.updateMask(maskTop);
});
var thr_bottom = 1;
var S2 = S2.map(function(image) {
var maskBottom = image.gt(thr_bottom);
return image.updateMask(maskBottom);
});
// Mask pixels by bandwise top- and bottom-percentiles as threshold
//--------------------------------------------------------//
/// Mask by top-percentile threshold
var thr_percentileTop = 99;
var mask_percentileTHR_top = S2.reduce(ee.Reducer.percentile([thr_percentileTop]));
var S2 = S2.map(function(image) {
return image.updateMask(image.lt(mask_percentileTHR_top));
});
/// Mask by bottom-percentile threshold
var thr_percentileBottom = 1;
var mask_percentileTHR_bottom = S2.reduce(ee.Reducer.percentile([thr_percentileBottom]));
var S2 = S2.map(function(image) {
return image.updateMask(image.gt(mask_percentileTHR_bottom));
});
// Add timestamp-band 'day of the year' (doy) to the collection
//--------------------------------------------------------//
var FUN_addDOY = function(image){
var doy = image.date().getRelative('day', 'year');
var doyBand = ee.Image.constant(doy).uint16().rename('doy');
return image.addBands(doyBand);
};
var S2 = S2.map(FUN_addDOY);
// Add timestamp-band 'year' to the collection
//--------------------------------------------------------//
var FUN_addYear = function(image){
var year = ee.Date(image.get('system:time_start')).format('y');
var num = ee.Number.parse(year);
var banddate = image.select('B1').multiply(0).eq(0).multiply(num).toUint16().rename('year');
return image.addBands(banddate);
};
var S2 = S2.map(FUN_addYear);
// Add spectral indices to the collection
//--------------------------------------------------------//
var FUN_addIndices = function(image) {
var ndvi = image.expression('(nir - red) / (nir + red)',
{'red': image.select('B4'), 'nir': image.select('B8')})
.rename('ndvi').float(); // NDVI: Normalized Difference Vegetation Index (Rouse et al., 1974)
var ndviNeg = image.expression('((nir - red) / (nir + red))*(-1)',
{'red': image.select('B4'), 'nir': image.select('B8')})
.rename('ndviNeg').float(); // negated NDVI for later use with "qualityMosaic" and "LeastGreenestPixel"
var nbr2 = image.expression('(swirS - swirL) / (swirS + swirL)',
{'swirS': image.select('B11'), 'swirL': image.select('B12')})
.rename('nbr2').float(); // NBR2: Normalized Burned Ratio (Garcia and Caselles, 1991)
var constant = ee.Image.constant(1).rename('constant'); // add a constant band = 1
return image.addBands([ndvi,ndviNeg,nbr2,constant]);
};
var S2 = S2.map(FUN_addIndices);
//==========================================================================================================================================================================================//
//==========================================================================================================================================================================================//
// Frequency composite images
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
// Get baresoil pixel-frequency image
/// band: freqBS
//--------------------------------------------------------//
var FUN_BSfilter = function(image) {
var ndvi = image.select('ndvi');
var nbr2 = image.select('nbr2');
var BSmask = ndvi.gte(0).and(ndvi.lte(0.25)).and(nbr2.lte(0.075)); // ndvi 0-0.25 => baresoil; nbr2 > 0.075 => straw. Also mask out snow
return image.updateMask(BSmask);
};
var img_freqBS = S2.map(FUN_BSfilter)
.select('B1') // take any band
.count()
.rename('freqBS');
// Function: Extract date from image
var FUN_getDate = function(image) {
return image.date().format('YYYY-MM-dd');
};
//==========================================================================================================================================================================================//
//==========================================================================================================================================================================================//
// Soil reflectance composite images
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
// Mean of annual barest pixel image
/// -> bands:B2-B8,B8A,B9-B12,ndvi,nbr2
//--------------------------------------------------------//
/// Get S2-collection with only baresoil-pixels
var FUN_BSfilter = function(image) {
var ndvi = image.select('ndvi');
var nbr2 = image.select('nbr2');
var BSmask = ndvi.gte(0).and(ndvi.lte(0.25)).and(nbr2.lte(0.075)); // ndvi 0-0.25 => baresoil; nbr2 > 0.075 => straw
return image.updateMask(BSmask);
};
var S2_BS = S2.select(['B1', 'B2', 'B3','B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B11', 'B12','ndvi','nbr2','ndviNeg','doy','year'])
.map(FUN_BSfilter);
print('S2_BS',S2_BS);
/// Mean of annual barest pixel
var years = ee.List.sequence(startYear, endYear);
var S2_annualBS = ee.ImageCollection.fromImages(
years.map(function (m) {
return S2_BS.filter(ee.Filter.calendarRange(m, m, 'year'))
.select(['B1', 'B2', 'B3','B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B11', 'B12','ndvi','nbr2','ndviNeg'])
.qualityMosaic('ndviNeg');
}));
var img_MeanBS = S2_annualBS.select(['B1', 'B2', 'B3','B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B11', 'B12','ndvi','nbr2'])
.reduce(ee.Reducer.mean())
.rename(['B1_MeanBS','B2_MeanBS','B3_MeanBS',
'B4_MeanBS', 'B5_MeanBS',
'B6_MeanBS', 'B7_MeanBS',
'B8_MeanBS','B8A_MeanBS',
'B9_MeanBS','B11_MeanBS',
'B12_MeanBS','ndvi_MeanBS',
'nbr2_MeanBS']);
Map.centerObject(aoi, 15);
Map.addLayer(img_MeanBS.clip(aoi_forClip), {min:1,max:3000,bands: ['B4_MeanBS', 'B3_MeanBS', 'B2_MeanBS']}, 'img_MeanBS_RGB');
//==========================================================================================================================================================================================//
//==========================================================================================================================================================================================//
//==========================================================================================================================================================================================//
//==========================================================================================================================================================================================//
// Export Sentinel composite images - as example
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
// Soil Reflectance Composite (SRC): Mean of annual barest pixel across 2017-2020
/// -> bands:'B2_MeanBS','B3_MeanBS','B4_MeanBS', 'B5_MeanBS','B6_MeanBS', 'B7_MeanBS',
/// -> 'B8_MeanBS','B8A_MeanBS','B9_MeanBS','B11_MeanBS','B12_MeanBS','ndvi_MeanBS',
/// -> 'nbr2_MeanBS'
//--------------------------------------------------------//
// Export Sentinel composite images for each year
Export.image.toDrive({
image: img_MeanBS.clip(aoi_forClip).toFloat(),
region: aoi,
description: 'S2_BS_' + year + '_32TGM_p3',
scale: 10,
crs: 'EPSG:2056',
maxPixels: 90000000,
folder: 'S2_baresoil_GEE',
fileFormat: 'GeoTIFF'
});
}