forked from shilosh/ContinuousLST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Continuous LST Night Export
187 lines (159 loc) · 7.02 KB
/
Continuous LST Night 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
var MODIS_TFA = ee.Image("users/shilosh/MODIS_LST_TFA_Night"),
CFSV2_TFA = ee.Image("users/shilosh/CFSv2_LST_TFA_Night");
// 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 firstDay = '2013-01-01';
var lastDay = '2016-01-01';
var Temperature_Band = 'Temperature_height_above_ground';
var collection = 'NOAA/CFSV2/FOR6H';
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: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)});
};
CFSV2_TFA_ic = CFSV2_TFA_ic.map(resample)
var CFSV2 = ee.ImageCollection(collection)
.filterDate(firstDay, lastDay)
.filter(ee.Filter.stringEndsWith('system:index', '00'))
.map(resample)
.select(Temperature_Band)
.map(k2celsius)
.map(createDoyBand)
// 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);
}).map(addTimeStampToCFSv2)
.map(createDoyBand);
print('MODIS_TFA_ic = ' ,MODIS_TFA_ic)
print('CFSV2_Anomalies = ' ,CFSV2_Anomalies)
// 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'));
return anomalies.add(tfa)//.subtract(anomalies);
}).map(addTimeStampToCFSv2)
.map(createDoyBand);
// print('MODIS_Continuous = ' ,MODIS_Continuous)
var Temperature_Band = 'LST_Night_1km';
var collection = 'MODIS/006/MYD11A1';
//convert Kelvin to Celsius
var modis_k2celsius = function(image) {
return image.multiply(ee.Image(0.02))
.subtract(ee.Image(273.15))
.clip(geometry)
.set('system:time_start', image.get('system:time_start'))
.rename([ee.String('night_').cat(image.get('system:time_start'))]);
};
var MODIS_LST = ee.ImageCollection(collection)
.filterDate(firstDay, lastDay)
.select(Temperature_Band)
.map(addTimeStampToMODIS)
.map(modis_k2celsius)
// 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);
})
// 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());
print(LST_Images)
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) }))
print(LST_ic)
Map.addLayer(ee.Image(LST_ic.first()))
print('Projection = ', ee.Image(LST_Images).projection())
// Export.image.toAsset({
// image: LST_Images,
// assetId: 'users/shilosh/MODIS_Continuous_LST_Night_2013_2015',
// region: geometry_json,
// // scale: scale,
// 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()
})