-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGEE-code
354 lines (296 loc) · 11.1 KB
/
GEE-code
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
## Google Earth Engine code for extracting enviromental variables for nightingale presence/absence records
// Create bounding box for West African region
var wa = ee.Geometry.BBox(-18.0416195374957, 2.99999216327873, 15, 17.5);
// Zoom to region of West Africa
Map.setCenter(0, 10, 4);
// Plot records
Map.addLayer(nigal, {}, 'Records');
// Bioclimate data
var clim = ee.ImageCollection('IDAHO_EPSCOR/TERRACLIMATE')
.filterDate('2001-01-01', '2020-12-31');
// ************************************ RAINFALL ********************************************** //
var prec = clim.select('pr');
// Look at precipiation in the region
// Set visualitation parameters
var prVis = {
min: 0,
max: 150,
palette: [
'#fff7fb','#ece7f2','#d0d1e6','#a6bddb','#74a9cf','#3690c0','#0570b0','#045a8d','#023858'
],
};
// Plot map
Map.addLayer(prec, prVis, 'Accumulated rainfall');
// Extract annual rainfall. This will give an indication of whether the location experienced higher
// or lower than average rainfall. Map through all records to match dates with images.
var precRes = nigal.map(function (f) {
var date = f.get('date'); // Date of record
// Create new property specifying the year of interest
var mn = ee.Number.parse(ee.String(date).slice(3,5));
var yr = ee.Number.parse(ee.String(date).slice(6,10));
// If the record is from Jan or Feb, we are interested in the year before
var nyr = ee.Algorithms.If(mn.lt(3),yr.subtract(1),yr);
return ee.Feature(f.geometry(),
// Select images years of interest and sum values of images
prec.filter(ee.Filter.calendarRange(nyr,nyr,'year')).
sum().
// Extract total rainfall for each record
reduceRegion({
reducer: ee.Reducer.first(),
geometry: f.geometry(),
scale: 4638.3,
})).set('ID', f.get('ID')); // Retain ID variable
});
// Extract monthly rainfall
// Use a function to iterate through months and create images of average monthly precipitation
var months = ee.List.sequence(1,12); // Create list of months
// Rather than doing this within the reduceRegions function, we want to export
// images because we will need them for making predictions later on
var prec_monthly = ee.ImageCollection.fromImages(
months.map(function (m) {
return ee.Image(prec.
filter(ee.Filter.calendarRange(m, m, 'month'))
.mean());
})).toBands(). // Convert to multi-band image
rename('JanRain','FebRain','MarchRain','AprRain','MayRain','JuneRain','JulyRain','AugRain','SepRain','OctRain','NovRain','DecRain');
print('Monthly precipitation:', prec_monthly.getInfo());
var monthlyPrecRes = prec_monthly.
reduceRegions({
reducer: ee.Reducer.first(),
collection: nigal,
scale: 4638.3,
});
// Check results
print('Precipitation results:', precRes.first());
print(monthlyPrecRes.first());
// Export
Export.table.toDrive({
collection: precRes,
description: 'nigal-prec',
folder: 'nigal GEE results'
});
Export.table.toDrive({
collection: monthlyPrecRes,
description: 'nigal-monthly-prec',
folder: 'nigal GEE results'
});
// ************************************ TEMPERATURE ********************************************** //
var tmp = clim.select(['tmmn','tmmx']);
// Plot temp
var tmpVis = {
min: -300.0,
max: 300.0,
palette: [
'1a3678', '2955bc', '5699ff', '8dbae9', 'acd1ff', 'caebff', 'e5f9ff',
'fdffb4', 'ffe6a2', 'ffc969', 'ffa12d', 'ff7c1f', 'ca531a', 'ff0000',
'ab0000'
],
};
Map.addLayer(tmp.select('tmmn'), tmpVis, 'Temperature');
// Iterate through months and select average minimum and max temperature
var tmp_min = ee.ImageCollection.fromImages(
months.map(function (m) {
return ee.Image(tmp.select('tmmn').
filter(ee.Filter.calendarRange(m, m, 'month'))
.mean());
})).min();
Map.addLayer(tmp_min, tmpVis, 'Minimum temperature');
var tmp_max = ee.ImageCollection.fromImages(
months.map(function (m) {
return ee.Image(tmp.select('tmmx').
filter(ee.Filter.calendarRange(m, m, 'month'))
.mean());
})).max();
// Extract temp for each record
var maxTmpRes = tmp_max.
reduceRegions({
reducer: ee.Reducer.first().setOutputs(["tmmx"]),
collection: nigal,
scale: 4638.3,
});
var minTmpRes = tmp_min.
reduceRegions({
reducer: ee.Reducer.first().setOutputs(["tmmn"]),
collection: nigal,
scale: 4638.3,
});
// Check results
print('Temperature results:', minTmpRes.first());
print(maxTmpRes.first());
// Export
Export.table.toDrive({
collection: minTmpRes,
description: 'nigal-min-temp',
folder: 'nigal GEE results'
});
Export.table.toDrive({
collection: maxTmpRes,
description: 'nigal-max-temp',
folder: 'nigal GEE results'
});
// ************************************ PALMER'S DROUGHT SEVERITY INDEX ********************************************** //
var pdsi = clim.select('pdsi');
// Visualise
var pdsiVis = {
min: -4000.0,
max: 3000.0,
palette: [
'1a3678', '2955bc', '5699ff', '8dbae9', 'acd1ff', 'caebff', 'e5f9ff',
'fdffb4', 'ffe6a2', 'ffc969', 'ffa12d', 'ff7c1f', 'ca531a', 'ff0000',
'ab0000'
],
};
Map.addLayer(pdsi, pdsiVis, 'PDSI');
// Repeat extraction of annual rainfall using PDSI
var pdsiRes = nigal.map(function (f) {
// What year is the record from?
var date = f.get('date');
// If the record is from Jan or Feb, we are interested in the year before
var mn = ee.Number.parse(ee.String(date).slice(3,5));
var yr = ee.Number.parse(ee.String(date).slice(6,10));
var nyr = ee.Algorithms.If(mn.lt(3),yr.subtract(1),yr);
return ee.Feature(f.geometry(),
// Select images years of interest and sum values of images
pdsi.filter(ee.Filter.calendarRange(nyr,nyr,'year')).
mean().
// Extract annual PDSI for each record
reduceRegion({
reducer: ee.Reducer.first(),
geometry: f.geometry(),
scale: 4638.3,
})).set('ID', f.get('ID')); // Retain ID variable;
});
// Check results
print('Drought results:', pdsiRes.first());
// Export
Export.table.toDrive({
collection: pdsiRes,
description: 'nigal-pdsi',
folder: 'nigal GEE results'
});
// ************************************ NDVI ********************************************** //
var terra = ee.ImageCollection('MODIS/061/MOD13Q1').select('NDVI');
var aqua = ee.ImageCollection('MODIS/061/MYD13Q1').select('NDVI');
// Merge image collections
var ndvi = terra.merge(aqua).
filterDate('2001-01-01', '2020-12-31'); // This time period covers out study period and a bit before
// Plot mean NDVI
var ndviVis = {
min:0,
max:9000,
palette: ['ffffff','ce7e45','df923d','f1b555','fcd163','99b718','74a901','66a000','529400',
'3e8601','207401','056201','004c00','023b01','012e01','011d01','011301'],
};
Map.addLayer(ndvi, ndviVis, 'NDVI');
// Use a function to iterate through months and create images of average monthly NDVI.
var ndvi_monthly = ee.ImageCollection.fromImages(
months.map(function (m) {
return ee.Image(ndvi.
filter(ee.Filter.calendarRange(m, m, 'month'))
.mean());
})).toBands(). // Convert to multi-band image
rename('JanNDVI','FebNDVI','MarchNDVI','AprNDVI','MayNDVI','JuneNDVI','JulyNDVI','AugNDVI','SepNDVI','OctNDVI','NovNDVI','DecNDVI');
// Explore monthly composites
print('NDVI bands:', ndvi_monthly.getInfo());
// Extract data underlying nightingale points
var ndviRes = ndvi_monthly.reduceRegions({
reducer: ee.Reducer.first(),
collection: nigal,
scale: 250,
});
// Check results
print('NDVI results:', ndviRes.first());
// Export
Export.table.toDrive({
collection: ndviRes,
description: 'nigal-ndvi',
folder: 'nigal GEE results'
});
// ************************************ ELEVATION ********************************************** //
var elev = ee.Image('CGIAR/SRTM90_V4').select('elevation');
Map.addLayer(elev, {min: 0, max: 60}, 'Elevation');
var elevRes = elev.
reduceRegions({
reducer: ee.Reducer.first().
setOutputs(['elevation']),
collection: nigal,
scale: 92.76624232,
});
// Check results
print('Elevation results:', elevRes.first());
// Export
Export.table.toDrive({
collection: elevRes,
description: 'nigal-elev',
folder: 'nigal GEE results'
});
// #### Export results #### //
// Combine properties in the different feature collections
// var res = precRes.map (function (f) {
// return f.copyProperties(avgprecRes.filter(ee.Filter.eq('ID', f.get('ID'))).first()).
// copyProperties(tmpRes.filter(ee.Filter.eq('ID', f.get('ID'))).first()).
// copyProperties(pdsiRes.filter(ee.Filter.eq('ID', f.get('ID'))).first()).
// copyProperties(ndviRes.filter(ee.Filter.eq('ID', f.get('ID'))).first()).
// copyProperties(elevRes.filter(ee.Filter.eq('ID', f.get('ID'))).first());
// });
// Check results
// print('Final results:', res.first());
// Export results
// Export.table.toDrive({
// collection: res
// description: 'nigal-GEE-results',
// folder: 'nigal GEE results'
// });
// Export rasters
var rastExp = prec_monthly.addBands(tmp_min).addBands(tmp_max).addBands(ndvi_monthly).addBands(pdsi.mean()).addBands(elev.double());
// Need to make elevation image into 64-bit float to match other images
print('Combined rasters:', rastExp);
Export.image.toDrive({
image: rastExp,
description: 'GEE-rasts',
// Crop by West African extent
region: wa,
folder: 'nigal GEE results',
maxPixels: 1e12,
scale: 4638 // Match lowest resolution of climate data
});
// Export rasters to look at change over time
var rast2020 = ee.ImageCollection.fromImages(
months.map(function (m) {
return ee.Image(prec
.filterDate('2020-01-01', '2020-12-31')
.filter(ee.Filter.calendarRange(m, m, 'month'))
.mean());
})).toBands(). // Convert to multi-band image
rename('JanRain','FebRain','MarchRain','AprRain','MayRain','JuneRain','JulyRain','AugRain','SepRain','OctRain','NovRain','DecRain')
.addBands(ee.ImageCollection.fromImages(
months.map(function (m) {
return ee.Image(ndvi
.filterDate('2020-01-01', '2020-12-31')
.filter(ee.Filter.calendarRange(m, m, 'month'))
.mean());
})).toBands(). // Convert to multi-band image
rename('JanNDVI','FebNDVI','MarchNDVI','AprNDVI','MayNDVI','JuneNDVI','JulyNDVI','AugNDVI','SepNDVI','OctNDVI','NovNDVI','DecNDVI'))
.addBands(ee.ImageCollection.fromImages(
months.map(function (m) {
return ee.Image(tmp.select('tmmx').filterDate('2020-01-01', '2020-12-31').
filter(ee.Filter.calendarRange(m, m, 'month'))
.mean());
})).max())
.addBands(ee.ImageCollection.fromImages(
months.map(function (m) {
return ee.Image(tmp.select('tmmn').filterDate('2020-01-01', '2020-12-31').
filter(ee.Filter.calendarRange(m, m, 'month'))
.mean());
})).min())
.addBands(pdsi.filterDate('2020-01-01', '2020-12-31').mean())
.addBands(elev.double());
Export.image.toDrive({
image: rast2020,
description: '2020-rasts',
// Crop by West African extent
region: wa,
folder: 'nigal GEE results',
maxPixels: 1e12,
scale: 4638 // Match lowest resolution of climate data
});