-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNDWImeanchart.js
151 lines (125 loc) · 5.64 KB
/
NDWImeanchart.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
var fires = ee.FeatureCollection("projects/ee-dgodwin-rsfinal/assets/Rivercomplex");
//Use this to create the main collection.
//Each element of the collection represents the surface reflectance bands, NDVI, and NDWI between May 28 and August 28 of the years 1997-2017
//There is no data for 2012, as it represents a gap in coverage between Landsat 5 and 8, during which Landsat 7 produced poor quality data
// Declare years and dates of interest, this can be changed at will without changing any other code
var L5startYear = 2000;
var L5endYear = 2011; //Always skip 2012, as there is no data
var L8startYear = 2013; //Always skip 2012, as there is no data
var L8endYear = 2021;
var startMonth = 6;
var startDay = 1;
var endMonth = 7;
var endDay = 31;
var L5startDate = ee.Date.fromYMD(L5startYear, startMonth, startDay);
var L5endDate = ee.Date.fromYMD(L5endYear, endMonth, endDay);
var L8startDate = ee.Date.fromYMD(L8startYear, startMonth, startDay);
var L8endDate = ee.Date.fromYMD(L8endYear, endMonth, endDay);
//Set the region for data collection as overlapping the study area shapefiles
var region = ee.FeatureCollection(fires).geometry()
//First, we create the cloud masking function
// This example demonstrates the use of the Landsat 4, 5, 7 Collection 2,
// Level 2 QA_PIXEL band (CFMask) to mask unwanted pixels.
function maskL457sr(image) {
// Bit 0 - Fill
// Bit 1 - Dilated Cloud
// Bit 2 - Unused
// Bit 3 - Cloud
// Bit 4 - Cloud Shadow
var qaMask = image.select('QA_PIXEL').bitwiseAnd(parseInt('11111', 2)).eq(0);
var saturationMask = image.select('QA_RADSAT').eq(0);
// Apply the scaling factors to the appropriate bands.
var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
var thermalBand = image.select('ST_B6').multiply(0.00341802).add(149.0);
// Replace the original bands with the scaled ones and apply the masks.
return image.addBands(opticalBands, null, true)
.addBands(thermalBand, null, true)
.updateMask(qaMask)
.updateMask(saturationMask);
}
// Get all data in the desired range and apply the mask
var collection = ee.ImageCollection('LANDSAT/LT05/C02/T1_L2')
.filterDate(L5startDate, L5endDate)
.filterBounds(region)
.map(maskL457sr);
//Define the function to add NDVI and NDWI
var addNDVIandNDWI = function(image) {
var ndvi = image.normalizedDifference(['SR_B4', 'SR_B3']).rename('NDVI');
var ndwi = image.normalizedDifference(['SR_B2', 'SR_B4']).rename('NDWI');
return image.addBands(ndvi).addBands(ndwi);
};
//Add the NDVI and NDWI to the image
var collection = collection.map(addNDVIandNDWI)
var L5years = ee.List.sequence(L5startYear, L5endYear);
// Map a function to select data within the year and apply median reducer
var L5summermedian = ee.ImageCollection.fromImages(
L5years.map(function(year) {
var startDate = ee.Date.fromYMD(year, startMonth, startDay);
var endDate = ee.Date.fromYMD(year, endMonth, endDay);
var annual = collection
.filterDate(startDate, endDate)
.median();
return annual
.set('year', year)
.set('system:time_start', ee.Date.fromYMD(year, 1, 1).format("YYYY_MM_dd"))
})
);
function maskL8sr(image) {
// Bit 0 - Fill
// Bit 1 - Dilated Cloud
// Bit 2 - Cirrus
// Bit 3 - Cloud
// Bit 4 - Cloud Shadow
var qaMask = image.select('QA_PIXEL').bitwiseAnd(parseInt('11111', 2)).eq(0);
var saturationMask = image.select('QA_RADSAT').eq(0);
// Apply the scaling factors to the appropriate bands.
var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
var thermalBands = image.select('ST_B.*').multiply(0.00341802).add(149.0);
// Replace the original bands with the scaled ones and apply the masks.
return image.addBands(opticalBands, null, true)
.addBands(thermalBands, null, true)
.updateMask(qaMask)
.updateMask(saturationMask);
}
// Map the function over one year of data.
var collection = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
.filterDate(L8startDate, L8endDate)
.filterBounds(region)
.map(maskL8sr);
//Define the function to add NDVI and NDWI
var addNDVIandNDWI = function(image) {
var ndvi = image.normalizedDifference(['SR_B5', 'SR_B4']).rename('NDVI');
var ndwi = image.normalizedDifference(['SR_B3', 'SR_B5']).rename('NDWI');
return image.addBands(ndvi).addBands(ndwi);
};
//Add the NDVI and NDWI to the image
var collection = collection.map(addNDVIandNDWI)
var L8years = ee.List.sequence(L8startYear, L8endYear);
// Map a function to select data within the year and apply median reducer
var L8summermedian = ee.ImageCollection.fromImages(
L8years.map(function(year) {
var startDate = ee.Date.fromYMD(year, startMonth, startDay);
var endDate = ee.Date.fromYMD(year, endMonth, endDay);
var annual = collection
.filterDate(startDate, endDate)
.median();
return annual
.set('year', year)
.set('system:time_start', ee.Date.fromYMD(year, 1, 1).format("YYYY_MM_dd"))
})
);
var maincollection = L5summermedian.merge(L8summermedian);
var NDWIcollection = maincollection.select('NDWI')
var NDWIchart = ui.Chart.image.seriesByRegion({
imageCollection: NDWIcollection,
regions: region,
reducer: ee.Reducer.mean(), //type of reduction. See ee.Reducers for other kinds of reductions
scale: 30, //spatial scale of MODIS product
seriesProperty: 'NAME' //property of roi to display in map
})
.setOptions({
title: 'Region Mean NDWI in June and July',
vAxis: {title: 'NDWI', maxValue: 1, minValue: -1},
hAxis: {title: 'year', format: 'yy', gridlines: {count: 20}},
})
print(NDWIchart)