-
Notifications
You must be signed in to change notification settings - Fork 4
/
GSSM1km generation
527 lines (501 loc) · 24.6 KB
/
GSSM1km generation
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
var MERIT = ee.Image("MERIT/Hydro/v1_0_1"),
MOD13A2 = ee.ImageCollection("MODIS/006/MOD13A2"),
table = ee.FeatureCollection("users/qianrswaterr/globalBoundary/World_Continents"),
ERA5Land = ee.ImageCollection("ECMWF/ERA5_LAND/HOURLY"),
imageCollection = ee.ImageCollection("users/qianrswaterr/WTD"),
DTB = ee.Image("users/qianrswaterr/predictors/BDTICM_M_1km_ll"),
trainTest = ee.FeatureCollection("users/qianrswaterr/GlobalSSM2022/trainTestFinal2022-0509coor"),
valiEva = ee.FeatureCollection("users/qianrswaterr/GlobalSSM2022/valiEvaFinal2022-0509coor"),
NLsamples = ee.FeatureCollection("users/qianrswaterr/NLsamples/trainTestNL2022-0509coor"),
TIele = ee.Image("users/qianrswaterr/GlobalSSM/TIele1000resample0709");
var WTD = imageCollection.mosaic().reproject("EPSG:4326",null,1000).rename('WTD');
DTB = DTB.reproject("EPSG:4326",null,1000).rename('DTB')
var EuropeBoundary=table.filterMetadata("CONTINENT","equals","Europe");
///////////////NDVI & EVI
var modis = MOD13A2
var oeel=require('users/OEEL/lib:loadAll');
var firstYear = 2019
var firstDaymodis = ee.String(ee.Number(firstYear).subtract(1)).cat('-12-13');
var lastDaymodis = ee.String(ee.Number(firstYear).add(1)).cat('-01-18');
// 26 images in the current year
modis=modis.filterDate(firstDaymodis,lastDaymodis).select(["NDVI","EVI"]);
print("originalMODIS",modis);
Map.addLayer(modis.select(["NDVI","EVI"]),{},"original NDVI&EVI")
// SG filter
var s=oeel.ImageCollection.SavatskyGolayFilter(modis,
ee.Filter.maxDifference(1000*3600*24*48, 'system:time_start', null, 'system:time_start'),
function(infromedImage,estimationImage){
return ee.Image.constant(ee.Number(infromedImage.get('system:time_start'))
.subtract(ee.Number(estimationImage.get('system:time_start'))));},
3,["NDVI","EVI"],modis);
//print("SG NDVI & EVI in the current year and 12/19(or 12/18) of the previous year, 1/17 of the next year)",s)
///////////////////////////Linear interpolation
////24 images, filter 12/19(or 12/18) and 1/17 which have masked values
s=ee.ImageCollection(s.toList(26).slice(1,25)).select(["d_0_NDVI","d_0_EVI"])
.map(function(img){
return img//.unmask(0)
})
//print('SG NDVI & EVI only in the current year',s)
//Map.addLayer(s.select(["d_0_NDVI","d_0_EVI"]),{},'SG NDVI&EVI')
// Yr start date
var ddStart = ee.Number(s.aggregate_min('system:time_start'));
// Yr end date
var ddEnd = ee.Number(s.aggregate_max('system:time_start'));
// DD list
var ddList = ee.List.sequence(ddStart, ddEnd, (24*60*60*1000));
// DD list to img collection
var ddCll = ee.ImageCollection.fromImages(
ddList.map(function (tuple){return ee.Image.constant(tuple).toInt64()
.set('system:time_start',ee.Number(tuple))
.select(['constant'],['time']);
})
);
// Original NDVI copier
function copyValue (img){
var time = img.metadata('system:time_start');
function mask (val){
var timeOrig = val.metadata('system:time_start');
var masked = timeOrig.eq(time);
return val.mask(masked);
}
var ndviCll = s.map(mask);
return img.addBands(ndviCll.max());
}
var filledDate = ddCll.map(copyValue);
//print('filledDate',filledDate)
var day = (24*60*60*1000);
var timeDelta = (16*day);
// when 12/19, it is 13 days
// when 12/18, it is 14 days
var timeDelta1= (13*day);
// Create a list of original calculated dates
var startDayList = s.toList(24).map(function(ele){
ele=ee.Image(ele)
return ele.get("system:time_start")
}).slice(0,23)
// convert it in an image collection
function toImage(tuple){
return ee.Image.constant(ee.Number(tuple)).set('system:time_start',tuple);
}
var imgCll = ee.ImageCollection.fromImages(startDayList.map(toImage));
///////////////*******************************//////////////////////
/////////////The function to interpolate EVI
function NDVIinterpolator(img){
img=ee.Image(img)
// get the first day of the subsample
var begin = ee.Number(img.get('system:time_start'));
// get the end day of the subsample
//var end = begin.add(ee.Number(timeDelta));
var difference = ee.String(ee.Date(ee.Number(img.get('system:time_start'))).format('YYYY-MM-dd')).slice(5).compareTo("12-18").neq(0)
.and(ee.String(ee.Date(ee.Number(img.get('system:time_start'))).format('YYYY-MM-dd')).slice(5).compareTo("12-19").neq(0))
var end = ee.Algorithms.If(difference,
begin.add(ee.Number(timeDelta)),
begin.add(ee.Number(timeDelta1))
)
//convert to an image
var minDD = ee.Image(filledDate.filterDate(begin).first());
var maxDD = ee.Image(filledDate.filterDate(end).first());
//calculate the coefficent
var angularCoeff = ee.Algorithms.If(difference,
(maxDD.select('d_0_NDVI').subtract(minDD.select('d_0_NDVI'))).divide(timeDelta),
(maxDD.select('d_0_NDVI').subtract(minDD.select('d_0_NDVI'))).divide(timeDelta1)
)
var q = ee.Algorithms.If(difference,
((maxDD.select('time').multiply(minDD.select('d_0_NDVI'))).subtract(minDD.select('time').multiply(maxDD.select('d_0_NDVI'))))
.divide(timeDelta),
((maxDD.select('time').multiply(minDD.select('d_0_NDVI'))).subtract(minDD.select('time').multiply(maxDD.select('d_0_NDVI'))))
.divide(timeDelta1)
)
//refilter the out collection
var mnth = filledDate.filterDate(begin,end);
// interpolate the values
function ndviInterpolator(img){
var NDVI = (img.select('time').multiply(angularCoeff)).add(q);
var result = img.select('d_0_NDVI').unmask(NDVI);
return result;
}
var filledMth = mnth.map(ndviInterpolator);
return filledMth.cast({"d_0_NDVI": "float"}, ["d_0_NDVI"]);
}
///////////////*******************************//////////////////////
/////////////The function to interpolate EVI
function EVIinterpolator(img){
img=ee.Image(img)
// get the first day of the subsample
var begin = ee.Number(img.get('system:time_start'));
// get the end day of the subsample
//var end = begin.add(ee.Number(timeDelta));
var difference = ee.String(ee.Date(ee.Number(img.get('system:time_start'))).format('YYYY-MM-dd')).slice(5).compareTo("12-18").neq(0)
.and(ee.String(ee.Date(ee.Number(img.get('system:time_start'))).format('YYYY-MM-dd')).slice(5).compareTo("12-19").neq(0))
var end = ee.Algorithms.If(difference,
begin.add(ee.Number(timeDelta)),
begin.add(ee.Number(timeDelta1))
)
//convert to an image
var minDD = ee.Image(filledDate.filterDate(begin).first());
var maxDD = ee.Image(filledDate.filterDate(end).first());
//calculate the coefficent
var angularCoeff = ee.Algorithms.If(difference,
(maxDD.select('d_0_EVI').subtract(minDD.select('d_0_EVI'))).divide(timeDelta),
(maxDD.select('d_0_EVI').subtract(minDD.select('d_0_EVI'))).divide(timeDelta1)
)
var q = ee.Algorithms.If(difference,
((maxDD.select('time').multiply(minDD.select('d_0_EVI'))).subtract(minDD.select('time').multiply(maxDD.select('d_0_EVI'))))
.divide(timeDelta),
((maxDD.select('time').multiply(minDD.select('d_0_EVI'))).subtract(minDD.select('time').multiply(maxDD.select('d_0_EVI'))))
.divide(timeDelta1)
)
//refilter the out collection
var mnth = filledDate.filterDate(begin,end);
// interpolate the values
function ndviInterpolator(img){
var NDVI = (img.select('time').multiply(angularCoeff)).add(q);
var result = img.select('d_0_EVI').unmask(NDVI);
return result;
}
var filledMth = mnth.map(ndviInterpolator);
return filledMth.cast({"d_0_EVI": "float"}, ["d_0_EVI"]);
}
///////////////*******************************//////////////////////
//apply the interpolation function to NDVI
var sgliNDVICollection = ee.ImageCollection(imgCll.map(NDVIinterpolator).flatten().toList(3000)).map(function(img){
//.divide(10000) why the "system: time_Start" disappear after divide?
return img.clip(EuropeBoundary)//.reproject("EPSG:4326",null,1000);
});
var NDVI=sgliNDVICollection;
print("NDVI",NDVI)
Map.addLayer(NDVI,{},"NDVI")
//apply the interpolation function to NDVI
var sgliEVICollection = ee.ImageCollection(imgCll.map(EVIinterpolator).flatten().toList(3000)).map(function(img){
return img.clip(EuropeBoundary)//.reproject("EPSG:4326",null,1000);
});
var EVI=sgliEVICollection;
print("EVI",EVI)
Map.addLayer(EVI,{},"EVI")
/////////////////////////**********************************
//*******API************
//1979-01-02T00:00:00 - 2020-07-09T00:00:00
///////filter ERA5 collection according to Date
//because t=34, so we need to use 34 days data before 2018-01-01
//And drop them after API calculation
var firstDayPreci = ee.String(ee.Number(firstYear).subtract(1)).cat('-11-28');
var firstDay = ee.String(firstYear.toString()).cat('-01-01');
var lastDay = ee.String(ee.Number(firstYear).add(1)).cat('-01-01');
var lastDayExtra1 = ee.String(ee.Number(firstYear).add(1)).cat('-01-02');
var ERA5LandPre = ERA5Land.filterDate(firstDayPreci,lastDayExtra1).map(function(img){
return img.select(["total_precipitation",'total_evaporation']).clip(EuropeBoundary)
// 20151129T00 represents total precipitation of 20151128 (T01-T00), so one hour shift earlier
.set("system:time_start",ee.Number(img.get("system:time_start")).subtract(86400000))
})
.filterDate(firstDayPreci,lastDay)
.filterMetadata("hour","equals",00)
print("ERA5LandPre",ERA5LandPre)
print("ERA5LandPresize",ERA5LandPre.size())
Map.addLayer(ERA5LandPre,{min:0,max:0.05},'ERA5LandPre')
ERA5LandPre = ERA5LandPre.map(function(img){
return img.select("total_precipitation").add(img.select('total_evaporation'))
.set("system:time_start",ee.Number(img.get("system:time_start")))
.set('hour',img.get('hour'))
})
//t=34(0-33) k=0.91
var lagRange = 33;
// Looks for all images up to 'lagRange' days away from the current image.
var maxDiffFilter = ee.Filter([
ee.Filter.maxDifference({
difference: lagRange * 24 * 60 * 60 * 1000,
leftField: 'system:time_start',
rightField: 'system:time_start'
})]);
//Images before, sorted in ascending order (so closest is last).
//here we cannot remove the equals, otherwise the timeseries will lost first element
var FilterBefore = ee.Filter.and(maxDiffFilter, ee.Filter.greaterThanOrEquals('system:time_start', null, 'system:time_start'))
var ERA5LandPre_BeforeJoinedCols = ee.Join.saveAll('before', 'system:time_start', true).apply(ERA5LandPre, ERA5LandPre, FilterBefore)
print('ERA5LandPre_BeforeJoinedCols',ERA5LandPre_BeforeJoinedCols);
////////////calculate apiCollection over all precipitation datasets
var apiLandCollection = ERA5LandPre_BeforeJoinedCols.map(function(image1) {
image1 = ee.Image(image1);
var beforeImages=ee.List(image1.get('before'))
beforeImages=beforeImages.map(function(image2){
image2=ee.Image(image2)
var startTime=ee.Number(image1.get('system:time_start'))
var id=startTime.subtract(ee.Number(image2.get('system:time_start'))).divide(86400000);
return ee.Image(image2).set('id',id)
})
beforeImages=ee.ImageCollection(beforeImages)//.filterMetadata("id","not_equals",0)
var k=ee.Image(0.91)
var apiItem=beforeImages.map(function(image3){
image3=ee.Image(image3)
var id=ee.Image(ee.Number(image3.get('id')));
var api=image3.multiply(k.pow(id))
return api
})
var api=ee.ImageCollection(apiItem).sum()
return api.rename([ee.String('band').cat(ee.String('_')).cat(image1.get('system:index')).cat("_APEI")])
.set('system:index1',ee.String(image1.get('system:index')).slice(0,8))
.set("system:time_start",image1.get("system:time_start"))
})
print('APILand',apiLandCollection);
apiLandCollection=ee.ImageCollection(apiLandCollection
.filterMetadata("system:index","not_less_than",ee.String(ee.Number(firstYear)).cat("0102T00")))
print('APILand',apiLandCollection);
var APILand = apiLandCollection
Map.addLayer(APILand.toBands(),{min:0,max:0.04},"APILand")
/////////////////////*****************************
//air temperature
var preImgCol = ERA5Land.filterDate(firstDay.cat("T01"),lastDay.cat("T01")).select("temperature_2m").map(function(col){
//0102T00(equals to 0101T24) -> 0101T23
//0101T01+0101T02+...+0102T00(equals to 0101T24)
var system_time_start = ee.Number(col.get('system:time_start')).subtract(3600000)
var system_time_end = ee.Number(col.get('system:time_end')).subtract(3600000)
var date=ee.Date(ee.Number(col.get('system:time_start')).subtract(3600000)).format("YYYY-MM-dd")
var preImgCol=col.set('date',date).set('system:time_start',system_time_start).set('system:time_end',system_time_end);
return preImgCol
})
print(preImgCol.size())
print("preImgCol Tair",ee.ImageCollection(preImgCol.toList(8784).slice(8000,8784)))
// //merge data from hourly into daily with join function
var join = ee.Join.saveAll("matches");
var filter = ee.Filter.equals({
leftField: "date",
rightField: "date"
});
var joinImgs = join.apply(preImgCol.filterMetadata("hour","equals",1), preImgCol, filter);
print("joinImgs",joinImgs.first())
var TairCollection = joinImgs.map(function(image) {
var _imgList = ee.List(image.get("matches"));
var _tempCol = ee.ImageCollection.fromImages(_imgList);
//due to it is hourly, so we need to calculate the average of everyday
//Temperature measured in kelvin can be converted to degrees Celsius (°C) by subtracting 273.15.
var _dayImg = _tempCol.mean().subtract(273.15);
var _date = image.get("date");
_dayImg = _dayImg.set("system:time_start", ee.Date.parse("yyyy-MM-dd", _date).millis())
.set('date',_date)
return _dayImg.rename("Tair")//.reproject("EPSG:4326",null,1000);
});
TairCollection = ee.ImageCollection(TairCollection)
print("TairCollection",TairCollection)
Map.addLayer(ee.ImageCollection(TairCollection),{min:0,max:0.05},'TairCollection')
////evaporation
var evapoCollection = ERA5Land.filterDate(firstDay,lastDayExtra1)
.map(function(img){
//var date = ee.Date(img.get('system:time_start')).format("YYYY-MM-dd")
return img.select("total_evaporation").rename("Evapo")//.reproject("EPSG:4326",null,1000)
.set("system:time_start",ee.Number(img.get("system:time_start")).subtract(86400000))
//.set("date",date)
})
.filterDate(firstDay,lastDay).filterMetadata("hour","equals",00)
print("evapoCollection",evapoCollection)
//////////////////////////*************************************
///////////////////////////***********************************
//LST
var LST=ee.ImageCollection("users/qianrswaterAmerica/LSTEuropeMOD11A1")
var dayLSTfilter = ee.String("MODIS_LST_Blended_Day_Europe").cat(ee.String(ee.Number(firstYear)))
var nightLSTfilter = ee.String("MODIS_LST_Blended_Night_Europe").cat(ee.String(ee.Number(firstYear)))
var dayLST = LST.filterMetadata("system:index","equals",dayLSTfilter).first().divide(100);
var nightLST = LST.filterMetadata("system:index","equals",nightLSTfilter).first().divide(100);
print("dayLST",dayLST)
print("nightLST",nightLST)
function batchRename_dailyLST(image){
var rename=image.bandNames().map(function(name){
return ee.String("band_").cat(ee.String(name).slice(-10)).cat(ee.String("_dailyLST"));
})
return image.rename(rename);
}
var dailyLST0=batchRename_dailyLST(dayLST.add(nightLST).divide(ee.Number(2)))
.reproject("EPSG:4326",null,1000);
print("dailyLST0",dailyLST0)
function batchRename_dailyLSTDiff(image){
var rename=image.bandNames().map(function(name){
return ee.String("band_").cat(ee.String(name).slice(-10)).cat(ee.String("_dailyLSTDiff"));
})
return image.rename(rename);
}
var dailyLSTDiff0=batchRename_dailyLSTDiff(dayLST.subtract(nightLST))
.reproject("EPSG:4326",null,1000);
print("dailyLSTDiff0",dailyLSTDiff0)
///dailyLST
var bandNamesdailyLST=dailyLST0.bandNames();
var dailyLST=ee.ImageCollection(bandNamesdailyLST.map(function(BandNameElement){
var stringLength=ee.String(BandNameElement).length();
var stryearBegin=ee.String(BandNameElement).slice(-19,-9)
var startIndex=ee.String(BandNameElement).rindex(stryearBegin)
var DateString=ee.String(BandNameElement).slice(startIndex,startIndex.add(10))
var yearStr=ee.Number.parse(DateString.slice(0,4));
var monthStr=ee.Number.parse(DateString.slice(5,7));
var DayStr=ee.Number.parse(DateString.slice(8,10));
return ee.Image(dailyLST0.select([BandNameElement])).rename(['dailyLST']).cast({"dailyLST": "double"}, ["dailyLST"])
.set('system:time_start', ee.Date.fromYMD(yearStr.int(), monthStr.int(), DayStr.int()).millis())
.set('bandName',BandNameElement)
.set("system:index",stryearBegin)
}))
print("dailyLST",dailyLST)
Map.addLayer(dailyLST,{},"dailyLST")
////dailyLSTDiff
var bandNamesdailyLSTDiff=dailyLSTDiff0.bandNames();
var dailyLSTDiff=ee.ImageCollection(bandNamesdailyLSTDiff.map(function(BandNameElement){
var stringLength=ee.String(BandNameElement).length();
var stryearBegin=ee.String(BandNameElement).slice(-23,-13)
var startIndex=ee.String(BandNameElement).rindex(stryearBegin)
var DateString=ee.String(BandNameElement).slice(startIndex,startIndex.add(10))
var yearStr=ee.Number.parse(DateString.slice(0,4));
var monthStr=ee.Number.parse(DateString.slice(5,7));
var DayStr=ee.Number.parse(DateString.slice(8,10));
return ee.Image(dailyLSTDiff0.select([BandNameElement])).rename(['dailyLSTDiff']).cast({"dailyLSTDiff": "double"}, ["dailyLSTDiff"])
.set('system:time_start', ee.Date.fromYMD(yearStr.int(), monthStr.int(), DayStr.int()).millis())
.set('bandName',BandNameElement)
.set("system:index",stryearBegin)
}))
print("dailyLSTDiff",dailyLSTDiff)
Map.addLayer(dailyLSTDiff,{},"dailyLSTDiff")
////////////////////////////////////////
///////////////
var modis = MOD13A2.first().reproject("EPSG:4326",null,1000)
// Get information about the MODIS projection.
var modisProjection = modis.projection();
print('MODIS projection:', modisProjection);
//soilTexture
//print(ee.Image("projects/soilgrids-isric/clay_mean").projection().nominalScale())
// divide 10, convert "g/kg" to "g/100g (%)"
var clayFraction =ee.Image("projects/soilgrids-isric/clay_mean").select("clay_0-5cm_mean")
.rename("clay").divide(10).reproject("EPSG:4326",null,250)
var sandFraction =ee.Image("projects/soilgrids-isric/sand_mean").select("sand_0-5cm_mean")
.rename("sand").divide(10).reproject("EPSG:4326",null,250)
var siltFraction =ee.Image("projects/soilgrids-isric/silt_mean").select("silt_0-5cm_mean")
.rename("silt").divide(10).reproject("EPSG:4326",null,250)
//porosity
//divide 100, convert "cg/cm³" to "kg/dm³", which is same as "g/cm³"
var bulkDensity=ee.Image("projects/soilgrids-isric/bdod_mean").select("bdod_0-5cm_mean")
.divide(100).reproject("EPSG:4326",null,250)
var porosity = ee.Image(1).subtract(bulkDensity.divide(ee.Image(2.65)))
.rename("porosity").reproject("EPSG:4326",null,250)
//organic matter content
var soc = ee.Image("projects/soilgrids-isric/soc_mean");
//divide 10, convert "dg/kg" to "g/kg", then divide 10, convert "g/kg" to "%"
var omc = soc.select("soc_0-5cm_mean").multiply(0.01).multiply(1.724).reproject("EPSG:4326",null,250).rename("omc")
var soilProper = clayFraction.addBands(sandFraction).addBands(siltFraction).addBands(porosity).addBands(omc)
Map.addLayer(soilProper,{min:0,max:100},"soilProper250")
var resample = function(image) {
return image.resample('bilinear')
.reproject({
crs: modisProjection,
scale: 1000})
};
soilProper = resample(soilProper)
Map.addLayer(soilProper,{min:0,max:100},"soilProper1000")
////////////////////////
var longitude = ee.Image.pixelLonLat().select("longitude").reproject("EPSG:4326",null,1000).rename("lon")
//Map.addLayer(longitude,{},"longitude")
var latitude = ee.Image.pixelLonLat().select("latitude").reproject("EPSG:4326",null,1000).rename("lat")
//Map.addLayer(latitude,{},"latitude")
var elevation=TIele.select("elevation").reproject("EPSG:4326",null,1000)
var TI=TIele.select("TI").reproject("EPSG:4326",null,1000)
////////////////////////combine all predictors
var predictors=dailyLST.map(function(img){
var time=img.get("system:time_start")
var dailyLSTDiff1=dailyLSTDiff.filterMetadata("system:time_start","equals",time).first().rename("LST_Diff")
var NDVI1=NDVI.filterMetadata("system:time_start","equals",time).first().rename("NDVI_SG_linear").divide(10000)
var EVI1=EVI.filterMetadata("system:time_start","equals",time).first().rename("EVI_SG_linear").divide(10000)
var Preci1=ERA5LandPre.filterMetadata("system:time_start","equals",time).first().rename("Preci").multiply(1000)
var APILand1=APILand.filterMetadata("system:time_start","equals",time).first().rename("apei").multiply(1000)
var Tair1 = TairCollection.filterMetadata("system:time_start","equals",time).first().rename("Tair")
var Evapo1 = evapoCollection.filterMetadata("system:time_start","equals",time).first().rename("Evapo").multiply(-1000)
return img.rename("LST_DAILY").addBands(dailyLSTDiff1)
.addBands(Preci1).addBands(APILand1)
.addBands(Tair1).addBands(Evapo1)
.addBands(NDVI1).addBands(EVI1).addBands(TI)
.addBands(soilProper.select("porosity")).addBands(soilProper.select("omc"))
.addBands(soilProper.select("clay")).addBands(soilProper.select("sand")).addBands(soilProper.select("silt"))
.addBands(longitude).addBands(latitude).addBands(elevation)
.addBands(WTD)
.addBands(DTB)
})
.map(function(img){
return img.clip(EuropeBoundary).reproject("EPSG:4326",null,1000)
})
print("predictors",predictors.first())
Map.addLayer(predictors.first(),{},"predictors",false)
////////////
print("///// training /////////////////////")
Map.addLayer(trainTest,{},'trainTest',false)
print(trainTest.first())
/////////select a certain number of training and testing samples
var sample=trainTest;
var station=sample.toList(556443).map(function(a){
return ee.Feature(a).get('station')
})
sample=sample.randomColumn();
var sampleSplit = 0.5;
sample=sample.filter(ee.Filter.lt('random', sampleSplit));
print('sampleFirst',sample.first())
print('sampleSize',sample.size())
//Map.addLayer(sample,{},'samples')
var station=sample.toList(556443).map(function(a){
return ee.Feature(a).get('station')
})
print("station size",station.distinct())
NLsamples = NLsamples.randomColumn();
var trainingNL = NLsamples.filter(ee.Filter.lt('random', 0.75));
var testingNL = NLsamples.filter(ee.Filter.gte('random', 0.75));
print("trainingNL",trainingNL)
print("testingNL",testingNL)
sample = sample.merge(trainingNL)
print('sampleSize',sample.size())
// //////////split training and testing samples
sample = sample.randomColumn();
print('sampleSize',sample.size())
var split = 0.75; // Roughly 75% training, 25% testing.
var training = sample.filter(ee.Filter.lt('random', split));
print('trainingSize',training.size())
var testing = sample.filter(ee.Filter.gte('random', split));
//print('testingSize',testing.size())
// Make a Random Forest classifier and train it
var classifier = ee.Classifier.smileRandomForest({
numberOfTrees:20,
minLeafPopulation:1,
bagFraction:0.5,
seed:0
}).setOutputMode('REGRESSION')
.train({
features: training,
classProperty: 'soil moisture',
inputProperties: [
"apei",
//"Preci",
'Tair','Evapo',
'LST_DAILY','LST_Diff',
'EVI_SG_linear','NDVI_SG_linear',
'clay','sand','silt',
'TI','elevation',
"lat","lon",
'porosity',
"omc"
,'WTD'
,'DTB'
]
});
//calculate the importance of every land surface feature
var importance=classifier.explain();
print('importance',importance)
var SM=ee.ImageCollection(predictors.toList(366).slice(0,366)).map(function(img){
return img.classify(classifier).multiply(1000).round().toUint16()
})
print("SM",SM.size())
//print("spatial resolution of SM",SM.first().projection().nominalScale().getInfo())
function batchRename(image){
var rename=image.bandNames().map(function(name){
return ee.String("band_").cat(ee.String(name));
})
return image.rename(rename);
}
SM=batchRename(SM.toBands())
Export.image.toAsset({
image: SM,
description:ee.String("SM").cat(ee.String(ee.Number(firstYear))).cat("Europe1km").getInfo(),
scale: 1000,
region: EuropeBoundary,
crs:"EPSG:4326",
assetId:ee.String("GlobalSSM1km0509/SM").cat(ee.String(ee.Number(firstYear))).cat("Europe1km").getInfo(),
maxPixels: 1e13,
pyramidingPolicy: {'.default': 'sample'}
});
Map.addLayer(EuropeBoundary,{},"EuropeBoundary")