-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
212 lines (154 loc) · 8.09 KB
/
utils.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
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function bulkRenameBands(bandNames, type, phase) {
if (type == 'sentinel2') {
return bandNames.map(function (bandName) {
var splitBandName = ee.String(bandName).split('_');
return ee.String(splitBandName.get(1)).cat('_').cat(ee.String(splitBandName.get(2))).cat('_').cat(ee.String(splitBandName.get(0)));
});
} else if (type == 'sentinel1') {
return bandNames.map(function (bandName) {
var splitBandName = ee.String(bandName).split('_');
return ee.String(phase).cat('_').cat(ee.String(splitBandName.get(1))).cat('_').cat(ee.String(splitBandName.get(0)));
});
} else {
return bandNames.map(function (bandName) {
var splitBandName = ee.String(bandName).split('_');
return ee.String(splitBandName.get(1)).cat('_').cat(ee.String(splitBandName.get(0)));
});
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function timePeriodSelector (ImageCollection, monthsList, yearsList, ROI) {
var imageCollection = yearsList.map(function (y) {
var list_ic = monthsList.map(function (m) {
var startDate = ee.Date.fromYMD(y, m, 1);
var endDate = startDate.advance(1, 'month').advance(-1, 'day');
var xic = ImageCollection.filterBounds(ROI).filter(
ee.Filter.date(startDate, endDate)
);
// return xic.toList(xic.size());
var x = ee.Algorithms.If(xic.size().eq(0), ee.List([]), xic.toList(xic.size()));
return x;
});
return ee.List(list_ic).flatten();
});
return ee.List(imageCollection).flatten();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function getMedianCompositePerMonth(imageCollection, monthsList, yearsList, ROI) {
var imageCollectionComposites = yearsList.map(function (y) {
var list_ic = monthsList.map(function (m) {
var startDate = ee.Date.fromYMD(y, m, 1);
var endDate = startDate.advance(1, 'month').advance(-1, 'day');
var xic = imageCollection.filterBounds(ROI).filter(ee.Filter.date(startDate, endDate));
// var x = ee.Algorithms.If(xic.size().eq(0), ee.List([]), xic.toList(xic.size()));
// var x = ee.Algorithms.If(xic.size().eq(0), ee.List([]), ee.List([xic.median()]));
return xic.median().set('system:time_start', startDate).set('system:time_end', endDate);
});
return ee.List(list_ic).flatten();
});
return ee.List(imageCollectionComposites).flatten();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function dateSplitter (ImageCollection, MonthRange1, MonthRange2, YearRange1, YearRange2, ROI){
return ee.ImageCollection(
ImageCollection.filter(ee.Filter.calendarRange(MonthRange1, MonthRange2, 'month'))
.filter(ee.Filter.calendarRange(YearRange1, YearRange2, 'year'))
).filterBounds(ROI);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function exportImageAsset (image, description, region, scale, assetId) {
Export.image.toAsset({
image: image,
description: description,
scale: scale || 30,
maxPixels: 1E13,
region: region,
assetId: assetId
});
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function statisticsForImage (img, ROI, scale) {
var reducers = ee.Reducer.mean()
.combine({reducer2: ee.Reducer.min(), sharedInputs: true})
.combine({reducer2: ee.Reducer.max(), sharedInputs: true})
.combine({reducer2: ee.Reducer.stdDev(), sharedInputs: true})
.combine({reducer2: ee.Reducer.percentile([25, 50, 75], ['Q1', 'Q2', 'Q3']), sharedInputs: true});
return img.reduceRegion({
reducer: reducers,
geometry: ROI,
scale: scale,
maxPixels: 1E13
});
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function statisticsFromImageCollection (imageCollection, monthsList, yearsList, ROI) {
var reducers = ee.Reducer.mean()
.combine({reducer2: ee.Reducer.min(), sharedInputs: true})
.combine({reducer2: ee.Reducer.max(), sharedInputs: true})
.combine({reducer2: ee.Reducer.stdDev(), sharedInputs: true})
.combine({reducer2: ee.Reducer.percentile([25, 50, 75], ['Q1', 'Q2', 'Q3']), sharedInputs: true});
var imageCollectionReduced = yearsList.map(function (y) {
var list_ic = monthsList.map(function (m) {
var startDate = ee.Date.fromYMD(y, m, 1);
var endDate = startDate.advance(1, 'month').advance(-1, 'day');
var xic = imageCollection.filterBounds(ROI).filter(ee.Filter.date(startDate, endDate));
var ic = ee.ImageCollection(xic);
var reducer = ic.reduce({reducer: reducers});
return reducer.float().set('system:time_start', startDate).set('system:time_end', endDate);
});
return ee.List(list_ic).flatten();
});
return ee.List(imageCollectionReduced).flatten();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function statisticsFromImageCollection2 (imageCollection) {
var reducers = ee.Reducer.mean()
.combine({reducer2: ee.Reducer.min(), sharedInputs: true})
.combine({reducer2: ee.Reducer.max(), sharedInputs: true})
.combine({reducer2: ee.Reducer.stdDev(), sharedInputs: true})
.combine({reducer2: ee.Reducer.percentile([25, 50, 75], ['Q1', 'Q2', 'Q3']), sharedInputs: true});
var reducer = imageCollection.reduce({reducer: reducers});
return reducer.float();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function regrid (number, ImageCollection, string) {
// Choose the grid size and projection
var number = number; ////100
var gridProjection = ee.Projection('EPSG:3857')
.atScale(number);
// Create a stacked image
// We assemble a composite with all the bands
var stacked = ImageCollection
var stacked = stacked.resample(string);
// Aggregate pixels with 'mean' statistics
var stackedResampled = stacked
.reduceResolution({
reducer: ee.Reducer.mean(),
maxPixels: 1024
})
.reproject({
crs: gridProjection
});
return stacked
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
exports.bulkRenameBands = bulkRenameBands;
exports.timePeriodSelector = timePeriodSelector;
exports.dateSplitter = dateSplitter;
exports.exportImageAsset = exportImageAsset;
exports.getMedianCompositePerMonth = getMedianCompositePerMonth;
exports.statisticsFromImageCollection = statisticsFromImageCollection;
exports.statisticsFromImageCollection2 = statisticsFromImageCollection2;
exports.statisticsForImage = statisticsForImage;
exports.regrid=regrid;