-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04 Calculate Heatwave
310 lines (243 loc) · 11.1 KB
/
04 Calculate Heatwave
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
var image2041 = ee.Image("projects/heatwave0214/assets/TM_SSP245_HWF/Interpolation_TM_SSP245_HWF_2041")
var geometry =ee.FeatureCollection("projects/heatwave0214/assets/basicpolygon/Chinaregion_NOTW")
// .filter(ee.Filter.eq('zone', 'Central China'))
.geometry();
var InnerJoin = require('users/geogismx/mountain_solar:join.js');
var i;
for (i = 2041; i < 2042; i++) {
function InnerJoin(primary, secondary, filter, join) {
join = typeof join !== 'undefined' ? join : ee.Join.inner();
var JoinedImgCol_raw = join.apply(primary, secondary, filter);
var joinedImgCol = JoinedImgCol_raw.map(joinfea);
return ee.ImageCollection(joinedImgCol);
}
function functocol(year){
// Original string
var str = 'projects/heatwave0214/assets/CN_GDDP_EHF_SSP245_TM/Interpolation_EHF_ssp245_TM_10km_';
// Joining the strings together
var stryear = year.toString();
var asset = str.concat(stryear); //image name string
var img = ee.Image(asset);
// print("img",img)
var meantemp = multibandstoImageCollection(img);
var newcoll = meantemp.map(function(image){
var start = stryear.concat('-05-01');
start = ee.Date(start);
var dateindex = image.get('system:index');
//var dateofimage = dateList(dateindex);
var index = ee.Number.parse(dateindex);
//var len = parseInt(index);
var date = start.advance(index,'day');
var month = date.get('month');
//var date = ee.Date(daily_coll.get(len));
return ee.Image(image).setMulti({
'system:time_start' : date,
'system:time_end' : date,
'month' : month
});
});
return newcoll;
}
function multibandstoImageCollection(image) {
function selectBand(image) {
return function(bandName) {
return image.select([bandName]).rename('EHF');
}
}
var bandNames = image.bandNames()
return ee.ImageCollection.fromImages(bandNames.map(selectBand(image)))
}
function heatwave(imgcoll,maxThresh){
var dataset = imgcoll.map(function(img){
return img.addBands(ee.Image.constant(0).toDouble().rename('counter'));
}).sort('system:time_start');
function heatSpells(img, list){
var prev = ee.Image(ee.List(list).get(-1));
var hot = img.select([0]).gt(maxThresh);
var accum = prev.select('counter').add(hot).rename('counter');
var out = img.select([0]).addBands(img.select('counter').where(hot.eq(1),accum));
return ee.List(list).add(out);
}
// create first image for iteration
var first = ee.List([ee.Image(dataset.first())]);
// apply dry speall iteration function
var lists = dataset.iterate(heatSpells,first); // get the max value
lists = ee.List(lists).slice(1,null);
return ee.ImageCollection.fromImages(lists);
}
// HWN mean how many discrete heatwave events for the selected season.
// HWM means the sum of EHF on all days classed as heatwave days, divide by the number of such days.
// HWA refers to the hottest day of the hottest heatwave of the year.
// The hottest heatwave is the one with the highest average EHF across all days of the heatwave
// The hottest day of this heatwave is also selected, based on the day within the heatwave with the highest EHF.
// HWF the number of days in the sum of the duration of all four events.
// HWF is the sum of days that belong to a period of at least 3 consecutive days where EHF is positive
function itt1(img, list){
// get last image
var prev = ee.Image(ee.List(list).get(-1));
// find areas gt 1 threshold (gt==1, lt==0)
var hot = prev.select('subwave1').eq(1);
// add previous day counter to today's counter
var accum = img.select('subwave1').add(hot).rename('xcounter');
// create a result image for iteration
var out = img.addBands(accum);
return ee.List(list).add(out);
}
function itt2(img, list){
// get last image
var prev = ee.Image(ee.List(list).get(-1));
// find areas eq 1 and eq 0
var hot = img.select('xcounter').eq(0).and(prev.select('xcounter').eq(1));
// add previous day counter to today's counter
var accum = img.select('xcounter').add(hot).rename('xcounter1');
// create a result image for iteration
var out = img.addBands(accum);
return ee.List(list).add(out);
}
function itt3(img, list){
// get last image
var prev = ee.Image(ee.List(list).get(-1));
// find areas eq 1 and eq 0
var hot = img.select('heat_identity').eq(1).and(prev.select('heat_identity').eq(0));
// add previous day counter to today's counter
var accum = img.select('newcounter').add(hot).rename('accumpoint');
// create a result image for iteration
var out = img.addBands(accum);
return ee.List(list).add(out);
}
function itt4(img, list){
var prev = ee.Image(ee.List(list).get(-1));
var hot = img.select('counter').gt(0);
//var accumEHF = prev.select('mEHF').multiply(hot).add(img.select('mEHF')).rename('accumEHF')
//var accumEHF = part1;
//var prevs = prev.select('mEHF').rename('accumEHF')
img = img.select('accumEHF').where(hot.eq(1),prev.select('accumEHF').add(img.select('accumEHF')))
//var accum = img.select('mEHF').multiply(hot);
//var accum = prev.select('mEHF').multiply(hot).add(img.select('mEHF')).rename('tEHF');
var out = img;
//var accumEHF = img.select('tEHF').where(hot.eq(1),accum);
//var out = img.addBands(accumEHF.toDouble());
return ee.List(list).add(out);
}
//print(HWFcol)
// create first image for iteration
function silotemp(year){
var stryear = year.toString();
var start = stryear.concat('-05-01');
start = ee.Date(start);
var end = start.advance(5,'month');
var yearofsilo = ee.ImageCollection(silo).filterDate(start,end);
//print(yearofsilo)
var newsilo = yearofsilo.map(function(image){
var maxtemp = image.select('max_temp');
var mintemp = image.select('min_temp');
var meantemp = maxtemp.add(mintemp).divide(2);
meantemp = meantemp.rename('Tmean');
//#############################//
//Before 2017, 'date' property name is 'Date'
//After 2017, 'date' property name is 'date'
//#############################//
var date = image.get('system:time_start');
date = ee.Date(date);
var month = date.get('month');
return meantemp.setMulti({
'system:time_start' :date,
'system:time_end' :date,
'month' :month
});
});
return newsilo;
}
// var i = 2018;
//var tempcol = silotemp(i);
var EHF_Dataset = functocol(i);
// print("EHF_Dataset",EHF_Dataset)
var HWFdataset = heatwave(EHF_Dataset,0).map(function(img){
return img.addBands(ee.Image.constant(0).toDouble().rename('newcounter'));
}).sort('system:time_start');
//创建一个名为 "HWFcol" 的图像集合中包含 "subwave1" 和 "subwave2" 这两个波段
//它们分别表示短暂的热浪和较长时间的热浪
var HWFcol = HWFdataset.map(function(img){
var newcounter = img.select('newcounter');
var subcount1 = newcounter.where(img.select('counter').eq(3),1);
var subcount2 = newcounter.where(img.select('counter').gt(3),1);
return img.addBands(subcount1.rename('subwave1')).addBands(subcount2.rename('subwave2'));
});
var first1 = ee.List([ee.Image(HWFcol.first())]);
// reverse the imglist
var n = HWFcol.size();
var HWFcols = ee.List(HWFcol.toList(n)).reverse();
// make it into imgcoll
HWFcols = ee.ImageCollection.fromImages(HWFcols);
var list1 = HWFcols.iterate(itt1,first1);
list1 = ee.List(list1).slice(1,null);
var HWFcoln = ee.ImageCollection.fromImages(list1);
// print(HWFcoln);
HWFcoln = HWFcoln.select([0,1,2,4,5])
var first2 = ee.List([ee.Image(HWFcoln.first())]);
var list2 = HWFcoln.iterate(itt2,first2);
list2 = ee.List(list2).slice(1,null).reverse();
var HWFcolns = ee.ImageCollection.fromImages(list2);
//print(HWFcolns)
HWFcolns = HWFcolns.map(function(img){
var EHF = img.select('EHF');
var subwave2 = img.select('subwave2');
var xcounter1 = img.select('xcounter1');
var nheat = subwave2.add(xcounter1);
var mEHF = nheat.multiply(EHF);
var accumEHF = mEHF;
return img.addBands(nheat.rename('heat_identity')).addBands(mEHF.rename('mEHF')).addBands(accumEHF.rename('accumEHF'));
});
//print(HWFcolns)
HWFcolns = HWFcolns.select([0,1,2,6,7,8])
var first3 = ee.List([ee.Image(HWFcolns.first())]);
HWFcolns = ee.List(HWFcolns.toList(n)).reverse();
HWFcolns = ee.ImageCollection.fromImages(HWFcolns);
var list3 = HWFcolns.iterate(itt3,first3);
list3 = ee.List(list3).slice(1,null).reverse();
HWFcolns = ee.ImageCollection.fromImages(list3);
//print(HWFcolns)
HWFcolnt = HWFcolns.select([1,6]);
var first4 = ee.List([ee.Image(HWFcolns.first())]);
// apply EHF iteration function
var list4 = HWFcolns.iterate(itt4,first4); // get the max value
list4 = ee.List(list4).slice(1,null);
var HWFcolnt = ee.ImageCollection.fromImages(list4);
// print(HWFcolnt);
//
var EHFcollection = InnerJoin.InnerJoin(HWFcolns.select([0,1,3,4,6]), HWFcolnt, InnerJoin.filterTimeEq);
// print("EHFcollection", EHFcollection)
EHFcollection = EHFcollection.map(function(img){
var accumEHF = img.select('accumEHF').multiply(img.select('accumpoint')).rename('accumEHF');
var accumcounter = img.select('counter').multiply(img.select('accumpoint')).rename('maxlens');
var meanEHF = accumEHF.divide(accumcounter);
var mEHF = img.select('mEHF');
return mEHF.addBands(meanEHF.rename('meanEHF')).addBands(img.select('accumpoint')).addBands(accumcounter).addBands(accumEHF).copyProperties(img,img.propertyNames());
});
var HWD = EHFcollection.select('maxlens').reduce(ee.Reducer.max());
HWD = HWD.rename("HWD");
var HWA = EHFcollection.select('mEHF').reduce(ee.Reducer.max());
HWA = HWA.rename("HWA");
var HWF = EHFcollection.select('maxlens').reduce(ee.Reducer.sum());
HWF = HWF.rename("HWF");
var HWA_Sum = EHFcollection.select('meanEHF').reduce(ee.Reducer.sum());
var HWN = EHFcollection.select('accumpoint').reduce(ee.Reducer.sum());
HWN = HWN.rename("HWN");
var HWM = HWA_Sum.divide(HWN);
HWM = HWM.rename("HWM");
// var HWF = HWFcolf.select('subwave1').reduce(ee.Reducer.sum());
// Map.addLayer(HWF.clip(geometry),{min:0,max:150,palette:'#9ecae1,#ffffff,#ffeda0,#feb24c,#f03b20'},'Total heatwave days');
// print("HWF",HWF)
// Map.addLayer(image2041,{min:0,max:150,palette:'#9ecae1,#ffffff,#ffeda0,#feb24c,#f03b20'},'image2041');
// print("image2041",image2041)
var task = 'Interpolation_TM_SSP245_HWF_'+i.toString()
var folder = 'projects/heatwave0214/assets/TM_SSP245_HWF'
Export.image.toAsset({
image: HWF,
description: task,
assetId:folder.concat('/').concat(task),
region:geometry.bounds(),
scale: 11132,
crs: 'EPSG:4326'
})
}