-
Notifications
You must be signed in to change notification settings - Fork 31
/
Water Balance NOAH vs NDVI.js
352 lines (325 loc) · 13.8 KB
/
Water Balance NOAH vs NDVI.js
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
////////////////////////////////////////////////////////////////////////////
// This script will compute the monthly NDVI from Landsat imagery and //
// water balance from NOAH Global Land Assimulation System. //
// It will display and export the correlation map of NDVI vs water //
// balance. Charts also considered lucc info and the average R and P value//
// over different vegetation type is computed and shown in GEE console. //
////////////////////////////////////////////////////////////////////////////
// This is part of a group work about drought analysis by MSc students in //
// Department of Earth Sciences, Uppsala University: //
// de Mendonça Fileni, Felipe; Erikson, Torbjörn-Johannes; Feng, Shunan // //
// Supervisor: Pettersson, Rickard; Winterdahl, Mattias //
// Contact: Shunan Feng (冯树楠): [email protected] //
////////////////////////////////////////////////////////////////////////////
// NOTE!
// some error spotted, remains to be solved
// note to myself:
// lucc class name on correlation chart needs to be corrected
// lucc class should be simplified
// SPEI could be uploaded once it is done and correlate it with NDVI
// Time lag and month gaps of ndvi
////////////////////////////////////////////////////////////////////////////
// Preparation //
////////////////////////////////////////////////////////////////////////////
// var worldmap = ee.FeatureCollection('ft:1tdSwUL7MVpOauSgRzqVTOwdfy17KDbw-1d9omPw');//world vector
var usstate = ee.FeatureCollection('ft:1fRY18cjsHzDgGiJiS2nnpUU3v9JPDc2HNaR7Xk8');//us state vector
// var worldmap = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017'); //not political right,
// var worldmap = ee.FeatureCollection('users/fsn1995/UIA_World_Countries_Boundaries');
var country = ['Spain'];//CHANGE the NAME of country here!
var state = ['California'];//CHANGE the NAME of us state here!
// var countryshape = worldmap.filter(ee.Filter.inList('Country', country));// country
var stateshape = usstate.filter(ee.Filter.inList('Name', state));// us state
// var roi = countryshape.geometry();// country
var roi = stateshape.geometry();// us state
var roiLayer = ui.Map.Layer(roi, {color: 'FF0000'}, 'roi');
// var roiCentroid = roi.centroid();
Map.layers().add(roiLayer);//display roi
// Map.setCenter(roiCentroid);
// study time range
var year_start = 2016;
var year_end = 2018;
var month_start = 1;
var month_end = 12;
var date_start = ee.Date.fromYMD(year_start, month_start, 1);
var date_end = ee.Date.fromYMD(year_end, month_end, 31);
var years = ee.List.sequence(year_start, year_end);// time range of years
var months = ee.List.sequence(month_start, month_end);// time range of months
// next step is to define the months of ndvi anomal calculation
// var month_anomaly = ee.List.sequence(3,5);// March to May
// var month_upper = 8;// April to June
// var month_lower = 4;
////////////////////////////////////////////////////////////////////////////
// NDVI //
////////////////////////////////////////////////////////////////////////////
// load landsat image
var surfaceReflectance4 = ee.ImageCollection('LANDSAT/LT04/C01/T1_SR')
.filterDate(date_start, date_end)
.filterBounds(roi);
var surfaceReflectance5 = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR')
.filterDate(date_start, date_end)
.filterBounds(roi);
var surfaceReflectance7 = ee.ImageCollection('LANDSAT/LE07/C01/T1_SR')
.filterDate(date_start, date_end)
.filterBounds(roi);
var surfaceReflectance8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.filterDate(date_start, date_end)
.filterBounds(roi);
var surfaceReflectance457 = surfaceReflectance4.merge(surfaceReflectance5).merge(surfaceReflectance7);
// // cloud/snow/water mask
// pixel_qa contains fmask information:
// bit 0: fill, bit 1: clear, bit 2: water,
// bit 3: cloud shadow, bit 4: snow/ice bit 5: cloud
// fmask for surfaceReflectance8
var fmaskL8sr = function(image) {
var cloudShadowBitmask = 1 << 3;
var cloudsBitMask = 1 << 5;
var waterBitmask = 1 << 2;
var snowBitmask = 1 << 4;
// QA band pixel value
var qa = image.select('pixel_qa');
// set cloud and shadows to 0
var mask = qa.bitwiseAnd(cloudShadowBitmask).eq(0)
.and(qa.bitwiseAnd(cloudsBitMask).eq(0))
.and(qa.bitwiseAnd(waterBitmask).eq(0))
.and(qa.bitwiseAnd(snowBitmask).eq(0));
return image.updateMask(mask);
};
// fmask for surfaceRflectance457
var fmaskL457 = function(image) {
var qa = image.select('pixel_qa');
// If the cloud bit (5) is set and the cloud confidence (7) is high
// or the cloud shadow bit is set (3), then it's a bad pixel. (GEE example)
var maskband = qa.bitwiseAnd(1 << 5)
.and(qa.bitwiseAnd(1 << 7))
.or(qa.bitwiseAnd(1 << 3))
.and(qa.bitwiseAnd(1 << 2))
.and(qa.bitwiseAnd(1 << 4));
// Remove edge pixels that don't occur in all bands
var mask2 = image.mask().reduce(ee.Reducer.min());
return image.updateMask(maskband.not()).updateMask(mask2);
};
// NDVI computation [-1 1]
var addNDVI457 = function(image) {
var ndvi457 = image.normalizedDifference(['B4', 'B3']).rename('NDVI');
return image.addBands(ndvi457);
};
var addNDVI8 = function(image) {
var ndvi8 = image.normalizedDifference(['B5', 'B4']).rename('NDVI');
return image.addBands(ndvi8);
};
// add cloud masked ndvi band
var L8ndvi = surfaceReflectance8
.filterBounds(roi)
.map(fmaskL8sr)
.map(addNDVI8);
var L457ndvi = surfaceReflectance457
.filterBounds(roi)
.map(fmaskL457)
.map(addNDVI457);
// merge L8 L457 NDVI
var landsatndvi = L8ndvi.merge(L457ndvi);
// var NDVI = landsatndvi.filterDate(date_start, date_end)
// .sort('system:time_start', false)
// .select('NDVI');
// // get the list of image dates
// var date_all = NDVI.map(function(image) {
// return image.set('date', image.date());
// });
// print(date_all);
// var datelist = date_all.aggregate_array('date');
// print(datelist);
// monthly average NDVI
// sytstem time is set as 1st of each month
var NDVI_monthlyave = ee.ImageCollection.fromImages(
years.map(function (y) {
return months.map(function(m) {
var vi = landsatndvi.filter(ee.Filter.calendarRange(y, y, 'year'))
.filter(ee.Filter.calendarRange(m, m, 'month'))
.mean()
.log()
.rename('NDVIm');
return vi.set('year', y)
.set('month', m)
.set('system:time_start', ee.Date.fromYMD(y, m, 1));
});
}).flatten()
);
// var NDVIfiltered = NDVI_monthlyave.filterMetadata('month','less_than',month_upper)
// .filterMetadata('month','greater_than',month_lower);
print(NDVI_monthlyave);
//////////////////////////////////////////////////////////////////////
//LUCC: This part will import and display the lucc info in the study//
//area //
//////////////////////////////////////////////////////////////////////
// load lucc
var lucc = ee.Image('ESA/GLOBCOVER_L4_200901_200912_V2_3').select('landcover').clip(roi);
Map.addLayer(lucc, {}, 'Landcover');
var lucc_pixelArea = ee.Image.pixelArea().addBands(lucc);
var lucc_group = lucc_pixelArea.reduceRegion({
reducer: ee.Reducer.sum().group({
groupField: 1,
groupName: 'landcover_class_value'
}),
geometry: roi,
scale: 300,// meters
bestEffort: true,
});
// print('reduction_results', lucc_group);
var lucc_names = ee.Dictionary.fromLists(
ee.List(lucc.get('landcover_class_values')).map(ee.String),
lucc.get('landcover_class_names')
);
print('lucc_names',lucc_names);
var lucc_palette = ee.Dictionary.fromLists(
ee.List(lucc.get('landcover_class_values')).map(ee.String),
lucc.get('landcover_class_palette')
);
// Chart functions
function createFeature(roi_class_stats) {
roi_class_stats = ee.Dictionary(roi_class_stats);
var class_number = roi_class_stats.get('landcover_class_value');
var result = {
lucc_class_number: class_number,
lucc_class_name: lucc_names.get(class_number),
lucc_class_palette: lucc_palette.get(class_number),
area_m2: roi_class_stats.get('sum')
};
return ee.Feature(null, result);
}
function createPieChartSliceDictionary(perc) {
return ee.List(perc.aggregate_array("lucc_class_palette"))
.map(function(p) { return {'color': p}; }).getInfo();
}
// pie chart of lucc summary
var roi_stats = ee.List(lucc_group.get('groups'));
var lucc_Pie = ee.FeatureCollection(roi_stats.map(createFeature));
var lucc_Piechart = ui.Chart.feature.byFeature({
features: lucc_Pie,
xProperty: 'lucc_class_name',
yProperties: ['area_m2', 'lucc_class_number']
})
.setChartType('PieChart')
.setOptions({
title: 'Land Cover Summary Chart',
slices: createPieChartSliceDictionary(lucc_Pie),
sliceVisibilityThreshold: 0
});
print('LUCC percentage', lucc_Piechart);
//////////////////////////////////////////////////////////////////////
// This part will prepare the water balance from NOAH Global //
// land assimulation system //
//////////////////////////////////////////////////////////////////////
var climate1 = ee.ImageCollection('NASA/GLDAS/V20/NOAH/G025/T3H')
.select(['Rainf_f_tavg','Evap_tavg'])
.filter(ee.Filter.date(date_start, date_end))
.filterBounds(roi);
var climate2 = ee.ImageCollection('NASA/GLDAS/V021/NOAH/G025/T3H')
.select(['Rainf_f_tavg','Evap_tavg'])
.filter(ee.Filter.date('2011-01-01', '2018-12-31'))
.filterBounds(roi);
var climate = climate1.merge(climate2)
.filter(ee.Filter.date(date_start, date_end));
// var rain = climate.select('Rainf_f_tavg');
// var evap = climate.select('Evap_tavg');
var addRain_mm = function(image) {
var Rain_mm = image.expression(
'b1 / 997 * 86400 / 8 * 100',// unit coversion
{
b1: image.select('Rainf_f_tavg')
}
).rename('Rain_mm');
return image.addBands(Rain_mm);
};
var addEvap_mm = function(image) {
var Evap_mm = image.expression(
'b1 / 997 * 86400 / 8 * 100',// unit coversion
{
b1: image.select('Evap_tavg')
}
).rename('Evap_mm');
return image.addBands(Evap_mm);
};
var rain = climate.map(addRain_mm);
var evap = climate.map(addEvap_mm);
var rain_monthlysum = ee.ImageCollection.fromImages(
years.map(function (y) {
return months.map(function(m) {
var vi = rain.select('Rain_mm')
.filter(ee.Filter.calendarRange(y, y, 'year'))
.filter(ee.Filter.calendarRange(m, m, 'month'))
.sum()
.rename('rainm');
return vi.set('year', y)
.set('month', m)
.set('system:time_start', ee.Date.fromYMD(y, m, 1));
});
}).flatten()
);
// print(rain_monthlysum);
var evap_monthlysum = ee.ImageCollection.fromImages(
years.map(function (y) {
return months.map(function(m) {
var vi = evap.select('Evap_mm')
.filter(ee.Filter.calendarRange(y, y, 'year'))
.filter(ee.Filter.calendarRange(m, m, 'month'))
.sum()
.rename('evapm');
return vi.set('year', y)
.set('month', m)
.set('system:time_start', ee.Date.fromYMD(y, m, 1));
});
}).flatten()
);
// print(evap_monthlysum);
var monthfilter = ee.Filter.equals({
leftField: 'month',
rightField: 'month',
});
var monthlink = ee.Join.saveFirst({
matchKey: 'match',
});
var climate_monthlink = ee.ImageCollection(monthlink.apply(evap_monthlysum,rain_monthlysum,monthfilter))
.map(function(image) {
return image.addBands(image.get('match'));
});
// var climate_filtered = climate_monthlink.filterMetadata('month','less_than',month_upper)
// .filterMetadata('month','greater_than',month_lower);
var waterbalance = climate_monthlink.map(function(image) {
return image.addBands(image.select('rainm')
.subtract(image.select('evapm'))
.rename('wb'));
});
//////////////////////////////////////////////////////////////////////
// Mapping and charting //
//////////////////////////////////////////////////////////////////////
var monthfilter = ee.Filter.equals({
leftField: 'system:time_start',
rightField: 'system:time_start',
});
var NDVI_WB = ee.ImageCollection(monthlink.apply(NDVI_monthlyave.select('NDVIm'),
waterbalance.select('wb'),monthfilter))
.map(function(image) {
return image.addBands(image.get('match'));
});
var corrmap = NDVI_WB.reduce(ee.Reducer.pearsonsCorrelation()).clip(roi);
// .addBands(lucc.select('landcover')
// .rename('lucc'));
var corrParams = {min: -1, max: 1, palette: ['red','white', 'green']};
Map.addLayer(corrmap.select('correlation'), corrParams, 'Correlation Map');
Export.image.toDrive({
image: corrmap,
description: 'Correlation map of monthly NDVI and water balance',
scale: 10000,
// region: roi
});
// var options = {
// // lineWidth: 1,
// // pointSize: 2,
// hAxis: {title: 'R and P value'},
// vAxis: {title: 'Correlation Coefficient'},
// title: 'Correlation map average'
// };
// var chart = ui.Chart.image.byClass(
// corrmap, 'lucc', roi, ee.Reducer.mean(), 1000, lucc.get('landcover_class_names')
// ).setOptions(options);
// print(chart);