-
Notifications
You must be signed in to change notification settings - Fork 15
/
Continuous LST Daily Export
227 lines (196 loc) · 9.4 KB
/
Continuous LST Daily Export
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
// Change the path to your own TFA images
var CFSV2_TFA = ee.Image("users/shilosh/CFSv2_Daily_LST_TFA"),
MODIS_TFA = ee.Image("users/shilosh/MODIS_Daily_LST_TFA");
var firstDay = '2019-02-27';
var lastDay = '2019-03-01';
var Assets_path = 'users/shilosh/';
var FileName = 'MODIS_Continuous_LST_Daily_2018';
var Temperature_Band = 'Maximum_temperature_height_above_ground_6_Hour_Interval';
var Day_Temperature_Band = 'LST_Day_1km';
var Night_Temperature_Band = 'LST_Night_1km';
var collection = 'NOAA/CFSV2/FOR6H';
// Reverse the images into imageCollections
var MODIS_TFA_ic = ee.ImageCollection(MODIS_TFA.bandNames().map(function(name) {
return MODIS_TFA.select([ee.Algorithms.String(name)],['mod']).set('system:DOY', ee.Number.parse(ee.Algorithms.String(name).replace('TFA','0').replace('_','')).add(1)) }))
var CFSV2_TFA_ic = ee.ImageCollection(CFSV2_TFA.bandNames().map(function(name) {
return CFSV2_TFA.select([ee.Algorithms.String(name)],['cfs']).set('system:DOY', ee.Number.parse(ee.Algorithms.String(name).replace('TFA','0').replace('_','')).add(1)) }))
var geometry = ee.Geometry.Rectangle([33.2,29.0,36.6,34.0]);
var geometry_json = geometry.toGeoJSON();
// Map.addLayer(geometry)
var modisProjection = MODIS_TFA.projection().crs().getInfo()
var scale = ee.Image(MODIS_TFA).projection().nominalScale().getInfo();
// Get the CFSv2 data at MODIS scale and projection.
var resample = function(image) {
return image.resample('bilinear')
.reproject({
crs: modisProjection,
scale: scale})
.set('system:DOY', image.get('system:DOY'))
.set('system:time_start', image.get('system:time_start'));
};
//convert Kelvin to Celsius
var k2celsius = function(image) {
return image.subtract(ee.Image(273.15))
.clip(geometry)
.set('system:DOY', image.get('system:DOY'))
.set('system:time_start', image.get('system:time_start'));};
// Add a property with doy to the colection.
function createDoyBand(img) {
var d = ee.Date(img.get('system:time_start'))
.getRelative('day', 'year')
.add(1);
img=img.set('system:DOY', d);
return img;
}
// Construct image date from 'system:index' and add it to a new 'date' property
var addTimeStampToCFSv2 = function(image) {
var start = ee.String(image.get('system:index'));
var y = start.slice(0,4);
var m = start.slice(4,6);
var d = start.slice(6,8);
var date = y.cat('-').cat(m).cat('-').cat(d);
return image.set({'system:time_start': date});
};
// Construct image date from 'system:index' and add it to a new 'date' property
var addTimeStampToMODIS = function(image) {
var start = ee.String(image.get('system:index'));
// var date = start.replace(/_/g, '-');
start = start.replace('_', '-');
var date = start.replace('_', '-');
return image.set({'system:time_start': ee.String(date)});
};
// Calculate the daily mean of the 4 images (00, 06, 12, 18)
var daily_mean = function(image) {
return image.reduce(ee.Reducer.mean())
.set('system:DOY', image.get('system:DOY'))
.set('system:time_start', image.get('system:time_start'));
};
CFSV2_TFA_ic = CFSV2_TFA_ic.map(resample);
// Convert the date string into milliseconds integer
var dayMillis = 86400000 // 86400000 is 1 day in milliseconds
var intFirstDay = ee.Date(firstDay).millis()
var intLastDay = ee.Date(lastDay).millis().subtract(dayMillis)
// Collect all 4 images of each day and create imageCollection from the daily mean.
var CFSV2 = ee.ImageCollection(ee.List.sequence(intFirstDay, intLastDay, dayMillis).map(function (day){
return ee.ImageCollection('NOAA/CFSV2/FOR6H')
.select('Maximum_temperature_height_above_ground_6_Hour_Interval')
.filterDate(day, ee.Number(day).add(dayMillis))
// .filter(ee.Filter.calendarRange(doy, doy, 'day_of_year'))
.map(resample)
.map(k2celsius)
.mean()
.set({'system:DOY': ee.Date(day).getRelative('day', 'year').add(1)})
.set({'system:time_start': ee.Date(day)})
}))
// Use an equals filter to specify how the collections match.
var Filter = ee.Filter.equals({
leftField: 'system:DOY',
rightField: 'system:DOY'
});
// Define the join.
var innerJoin = ee.Join.inner('primary', 'secondary');
// Join CFSV2 with CFSV2_TFA_ic by DOY
// Apply the join.
var CFSV2_JoinInner = innerJoin.apply(CFSV2, CFSV2_TFA_ic, Filter);
// Calculate CFSv2 anomalies
var CFSV2_Anomalies = CFSV2_JoinInner.map(function(f) {
var tfa = ee.Image(f.get('secondary'));
var actual = ee.Image(f.get('primary'));
return actual.subtract(tfa)
.set('system:time_start', actual.get('system:time_start'))
.set('system:DOY', actual.get('system:DOY'));
})//.map(addTimeStampToCFSv2)
//.map(createDoyBand);
// Join MODIS_TFA_ic with CFSV2_Anomalies by DOY
// Apply the join.
var MODIS_JoinInner = innerJoin.apply(CFSV2_Anomalies, MODIS_TFA_ic, Filter);
// print('MODIS_JoinInner = ' ,MODIS_JoinInner)
// Calculate MODIS TFA Plus CFSv2 anomalies
var MODIS_Continuous = MODIS_JoinInner.map(function(f) {
var anomalies = ee.Image(f.get('primary'));
var tfa = ee.Image(f.get('secondary'));
// Anomalies at night do not conribute to the TFA only prediction,
// therefor because we are trying to predict daily mean LST, we only add half of the daily anomalies
return (anomalies.divide(ee.Image(2))).add(tfa)//.subtract(anomalies);
.set('system:time_start', anomalies.get('system:time_start'))
.set('system:DOY', anomalies.get('system:DOY'));
})//.map(addTimeStampToCFSv2)
//.map(createDoyBand);
// print('MODIS_Continuous = ' ,MODIS_Continuous)
var Temperature_Band = 'LST_Day_1km';
var collection = 'MODIS/006/MYD11A1';
//convert Kelvin to Celsius
var modis_k2celsius = function(image) {
return image.updateMask(image.select(Day_Temperature_Band))
.updateMask(image.select(Night_Temperature_Band))
.reduce( ee.Reducer.mean()).rename(Temperature_Band)
.multiply(ee.Image(0.02))
.subtract(ee.Image(273.15))
.clip(geometry)
.set('system:time_start', ee.Date(image.get('system:time_start')))
.rename([ee.String('daily_').cat(image.get('system:time_start'))]);
};
var MODIS_LST = ee.ImageCollection(collection)
.filterDate(firstDay, lastDay)
.select(Day_Temperature_Band, Night_Temperature_Band)
//.map(function (image){return image.reduce(ee.Reducer.mean())})
.map(addTimeStampToMODIS)
.map(modis_k2celsius)
print('MODIS_LST = ' ,MODIS_LST)
// Function to calculate the daily mean value of each pixel
// MODIS_LST = MODIS_LST.map(function (image){
// return (image.select(0).add(image.select(1))).divide(ee.Image(2))
// .clip(geometry)
// // .reduce(ee.Reducer.mean())
// .set({'system:DOY': image.get('system:DOY')})
// //.rename('MODIS_LST')
// });
// Use an equals filter to specify how the collections match.
Filter = ee.Filter.equals({
leftField: 'system:time_start',
rightField: 'system:time_start'
});
// Join MODIS_LST with MODIS_TFA_plus_CFSV2_Anomalies by DOY
// Apply the join.
var MODIS_Blended_JoinInner = innerJoin.apply(MODIS_LST, MODIS_Continuous, Filter);
// Blend the results to fill LST gaps
var MODIS_LST_Blended = MODIS_Blended_JoinInner.map(function(f) {
var prediction = ee.Image(f.get('secondary'));
var lst = ee.Image(f.get('primary'));
return prediction.blend(lst);
})
// print(MODIS_LST_Blended)
// print(MODIS_LST)
// print('MODIS_LST_Blended = ' ,MODIS_LST_Blended)
// print('MODIS_Continuous = ' ,MODIS_Continuous)
// Map.addLayer(ee.Image(MODIS_LST.first()),{},'MODIS_LST')
// Map.addLayer(ee.Image(MODIS_Continuous.first()),{},'MODIS_Continuous')
// Map.addLayer(ee.Image(MODIS_LST_Blended.first()),{},'MODIS_LST_Blended')
// // Map.addLayer(ee.ImageCollection(MODIS_LST_Blended).mean())
//Iterating over the image collection using this function....
var LST_Images = MODIS_LST_Blended//.select(Temperature_Band)
.iterate(function(img, all) {
return ee.Image(all).addBands(img);
}, ee.Image().select());
var LST_ic = ee.ImageCollection(ee.Image(LST_Images).bandNames().map(function(name) {
return ee.Image(LST_Images).select([ee.Algorithms.String(name)],[Temperature_Band]).set('system:index', name) }))
// Map.addLayer(ee.Image(LST_ic.first()),{min:5, max:35},'LST_continuous')
// Export.image.toAsset({
// image: ee.Image(LST_Images),
// assetId: Assets_path + FileName,
// region: geometry_json,
// crs: ee.Image(LST_Images).projection().crs().getInfo(), // from the projection output
// crsTransform: [926.625433056,0,-20015109.354,0,-926.625433055,10007554.677], // from the projection output
// description: 'exportToAsset-MODIS-LST-TS',
// pyramidingPolicy: {'.default': 'sample'}
// });
// Enable batch download to Drive
var batch = require('users/fitoprincipe/geetools:batch')
var scale = ee.Image(MODIS_LST_Blended.first()).select([0]).projection().nominalScale().getInfo()
// export collection to google drive
batch.Download.ImageCollection.toDrive(ee.ImageCollection(LST_ic), 'LST_cont', {
// name: {system:id},
scale: scale,
crs: "EPSG:4326",
region: geometry.getInfo() // or geometry.getInfo()
})