-
Notifications
You must be signed in to change notification settings - Fork 3
/
Landsat_Median_Compozite.js
146 lines (123 loc) · 4.19 KB
/
Landsat_Median_Compozite.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
Map.setOptions("HYBRID");
//////////////////////////////////////////////
/////SET NUMBER of DAY and INTERVAL///////////
var StartYEAR = 2014
var EndYEAR = 2018
// var YEAR=2018;
var STARTDAY=229;///julian number of start day
var ENDDAY=258;
var INTERVAL = ENDDAY - STARTDAY; //days
var cloud_treshold=80;
print(INTERVAL);
///////////////end////////////////////////////
var geometry = /* color: #d63000 */ee.Geometry.Polygon(
[ [ [ 33.8109352, 50.226703 ],
[ 33.8109352, 50.3316725 ],
[ 33.6190876, 50.3316725 ],
[ 33.6190876, 50.226703 ],
[ 33.8109352, 50.226703 ] ] ] );
var AOI = ee.FeatureCollection([ee.Feature(geometry, {name: 'my area'})]);
/*
//export aoi to google drive
Export.table.toDrive({
collection: AOI,
description:'AOI',
fileFormat: 'kml'
});
*/
//
Map.centerObject(geometry, 12);
///////////////////////////////////////////////////////
//////////////////MEDIANS AND FILTERS//////////////////
///////////////////////////////////////////////////////
//Select only 6 bands to reduce size
function selectBandsL8(img){
//select only 6 bands
return img.expression('b("B2","B3","B4","B5","B6","B7")').rename('B1','B2','B3','B4','B5','B6').uint16();
}
function selectBandsL57(img){
//select only 6 bands
return img.expression('b("B1","B2","B3","B4","B5","B7")').rename('B1','B2','B3','B4','B5','B6').uint16();
}
//Bit selection fo SR
var nocloudbit = 1;// 0x400
var nocloudBitMask = Math.pow(2,nocloudbit);
function maskSRclouds(image) {
var qa = image.select('pixel_qa');
var mask = qa.bitwiseAnd(nocloudBitMask);// land;
return image.updateMask(mask);
}
// A function to compute NDVI.
var NDVI = function(image) {
return image.expression('float(b("B4") - b("B3")) / (b("B4") + b("B3"))');
};
//Create medians
function createMedians(AOI, year, beginDay, endDay, period){
var imgList = [];
var collectionL8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR');
var collectionL5 = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR');
var begin = beginDay;
while (begin < endDay){
var filterParamsL8 = collectionL8
.filterBounds(AOI)
.filter(ee.Filter.calendarRange(year, year, 'year'))
.filter(ee.Filter.dayOfYear(begin, begin + period))
.filterMetadata('CLOUD_COVER','less_than', cloud_treshold)
.map(maskSRclouds)
.map(selectBandsL8);
var filterParamsL5 = collectionL5
.filterBounds(AOI)
.filter(ee.Filter.calendarRange(year, year, 'year'))
.filter(ee.Filter.dayOfYear(begin, begin + period))
.filterMetadata('CLOUD_COVER','less_than', cloud_treshold)
.map(maskSRclouds)
.map(selectBandsL57);
print(year);
print(filterParamsL5,filterParamsL8);
var collection = ee.ImageCollection(filterParamsL5.merge(filterParamsL8));
print('Number of Scenes', collection.size());
var median = collection.median();
var comp = median.set('id','L78'+ year+'_'+(begin + period));
var clippedMedian = comp.clip(AOI);
imgList.push(clippedMedian);
//imgList.push(mask);
begin += period;
}
// var result = ee.ImageCollection(imgList)
return (ee.Image(ee.ImageCollection(imgList).first()));
}
///////////////////////////////////////////////////////////
/////////////CALCULATE MEDIANS/////////////////////////////
// var med = createMedians(AOI, YEAR, STARTDAY, ENDDAY, INTERVAL);
/// Calculate Medians by years
function createMediansByYears(AOI, beginYear, endYear, STARTDAY, ENDDAY, INTERVAL){
// var imgList = [];
var begin = beginYear;
while (begin <= endYear){
var med = createMedians(AOI, begin, STARTDAY, ENDDAY, INTERVAL);
print(med);
Map.addLayer(med, {bands: ['B5', 'B4', 'B3'], min: 0.0, max: 10000.0, gamma: 1.4,}, 'med_'+begin+'_'+(STARTDAY)+'_'+(STARTDAY+INTERVAL), false);
Export.image.toDrive({
image: med,
description: 'My_AOI_'+begin+'_'+(STARTDAY)+'_'+(ENDDAY),
folder: 'GEE data',
scale: 30,
region: geometry,
fileFormat: 'GeoTIFF',
formatOptions: {
cloudOptimized: true
}
});
/*
Export.image.toAsset({
image: med,
description: 'My_AOI_'+begin+'_'+(STARTDAY)+'_'+(ENDDAY),
scale: 30,
region: geometry
});
*/
begin += 1
}
}
var medbyyears = createMediansByYears(AOI, StartYEAR, EndYEAR, STARTDAY, ENDDAY, INTERVAL);
print(medbyyears);