forked from abatz/WebInterface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollectionMethods.py
417 lines (388 loc) · 17.4 KB
/
collectionMethods.py
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
import logging
import ee
#===========================================
# GET_COLLECTION
#===========================================
def get_collection(product, variable):
"""Return an EarthEngine image collection for given product and variable
Args:
product: single character indicating the product (G, M, 8, or 5)
variable: string indicating the variable/band to return
(i.e. NDVI, EVI, pet, tmmn)
Returns:
Output from product collection functions
Tuple of the following:
EarthEngine image collection object
String of the collection name
String of the collection description
String of the variable description (may be the input variable)
String of additional notes about the collection
"""
if product == 'G':
return get_gridmet_collection(variable)
elif product == 'M':
return get_modis_collection(variable)
elif product == '8':
return get_landsat8_daily_collection(variable)
elif product == '5':
return get_landsat5_daily_collection(variable)
##return get_landsat457_daily_collection(variable)
## How should this function fail gracefully if the inputs are bad?
## Should it return an exception?
##else:
## pass
#===========================================
# LANDSAT457 Daily
#===========================================
def get_landsat457_daily_collection(variable):
"""Return the daily merged image collection for Landsat 4, 5, and 7
Args:
variable: string indicating the variable/band to return
(NDVI, NDSI, NDWI, or EVI)
Returns:
EarthEngine image collection object
String of the collection name
String of the collection description
String of the input variable
String of additional notes about the collection
"""
## This string could/should be built based on the date range or looking at
## or looking atthe images in the collection
coll_name = 'LT4_L1T_TOA,LT5_L1T_TOA,LE7_L1T_TOA'
coll_desc = 'Landsat 4/5/7 Daily {0} (cloud mask applied)'.format(variable)
var_desc = variable
## Select variable after calculating index
collection = ee.ImageCollection([])
collection = collection.merge(ee.ImageCollection('LT4_L1T_TOA'))
collection = collection.merge(ee.ImageCollection('LT5_L1T_TOA'))
collection = collection.merge(ee.ImageCollection('LE7_L1T_TOA'))
## Can this be done on the merged collection?
collection = collection.map(landsat457_cloud_mask_func)
if variable == 'NDVI':
notes = "NDSI calculated from Norm. Diff. of Near-IR and Red bands"
collection = collection.map(landsat457_ndvi_func)
elif variable == 'NDSI':
notes = "NDSI calculated from Norm. Diff. of Green and mid-IR bands"
collection = collection.map(landsat457_ndsi_func)
elif variable == 'NDWI':
notes = "NDWI calculated from Norm. Diff. of near-IR and mid-IR bands"
collection = collection.map(landsat457_ndwi_func)
elif variable == 'EVI':
notes = "EVI calculated from Near-IR, Red and Blue bands"
collection = collection.map(landsat457_evi_func)
## How should this function fail gracefully if the inputs are bad?
## Should it return an exception?
else:
notes = ''
collection = collection.select(variable)
return collection, coll_name, coll_desc, var_desc, notes
#===========================================
# LANDSAT5 Daily
#===========================================
def get_landsat5_daily_collection(variable):
"""Return the daily image collection for only Landsat 5
Args:
variable: string indicating the variable/band to return
(NDVI, NDSI, NDWI, or EVI)
Returns:
EarthEngine image collection object
String of the collection name
String of the collection description
String of the input variable
String of additional notes about the collection
"""
coll_name = 'LT5_L1T_TOA'
coll_desc = 'Landsat 5, daily {0} (cloud mask applied)'.format(variable)
var_desc = variable
## Select variable after calculating index
collection = ee.ImageCollection(coll_name).map(landsat457_cloud_mask_func)
if variable == 'NDVI':
notes = "NDSI calculated from Norm. Diff. of Near-IR and Red bands"
collection = collection.map(landsat457_ndvi_func)
elif variable == 'NDSI':
notes = "NDSI calculated from Norm. Diff. of Green and mid-IR bands"
collection = collection.map(landsat457_ndsi_func)
elif variable == 'NDWI':
notes = "NDWI calculated from Norm. Diff. of near-IR and mid-IR bands"
collection = collection.map(landsat457_ndwi_func)
elif variable == 'EVI':
notes = "EVI calculated from Near-IR, Red and Blue bands"
collection = collection.map(landsat457_evi_func)
## How should this function fail gracefully if the inputs are bad?
## Should it return an exception?
else:
notes = ''
collection = collection.select(variable)
return collection, coll_name, coll_desc, var_desc, notes
#===========================================
# LANDSAT8 Daily
#===========================================
def get_landsat8_daily_collection(variable):
"""Return the daily image collection for only Landsat 8
Args:
variable: string indicating the variable/band to return
(NDVI, NDSI, NDWI, or EVI)
Returns:
EarthEngine image collection object
String of the collection name
String of the collection description
String of the input variable
String of additional notes about the collection
"""
coll_name = 'LC8_L1T_TOA'
coll_desc = 'Landsat 8, daily {0} (cloud mask applied)'.format(variable)
var_desc = variable
## Select variable after calculating index
collection = ee.ImageCollection(coll_name)
## Need to code in Landsat 8 cloud masking
##collection = ee.ImageCollection(coll_name).map(landsat8_cloud_mask_func)
if variable == 'NDVI':
notes = "NDSI calculated from Norm. Diff. of Near-IR and Red bands"
collection = collection.map(landsat8_ndvi_func)
elif variable == 'NDSI':
notes = "NDSI calculated from Norm. Diff. of Green and mid-IR bands"
collection = collection.map(landsat8_ndsi_func)
elif variable == 'NDWI':
notes = "NDWI calculated from Norm. Diff. of near-IR and mid-IR bands"
collection = collection.map(landsat8_ndwi_func)
elif variable == 'EVI':
notes = "EVI calculated from Near-IR, Red and Blue bands"
collection = collection.map(landsat8_evi_func)
## How should this function fail gracefully if the inputs are bad?
## Should it return an exception?
else:
notes = ''
collection = collection.select(variable)
return collection, coll_name, coll_desc, var_desc, notes
#===========================================
# LANDSAT8 8-day
#===========================================
## Landsat 8 Day collection functions
##def get_landsat457_8day_collection(variable):
## """Return the merged 8 day composite image coll. for Landsats 4, 5, and 7
##
## Args:
## variable: string indicating the variable/band to return
## (NDVI, NDSI, NDWI, or EVI)
## Returns:
## EarthEngine image collection
## String of the collection name object
## String of the collection description
## String of the input variable
## String of additional notes about the collection
## """
## coll_name = 'LT4_L1T_8DAY_{0},LT5_L1T_8DAY_{0},LE7_L1T_8DAY_{0}'.format(variable)
## coll_desc = 'Landsat 4/5/7 8-day {0} Composite'.format(variable)
## collection4 = ee.ImageCollection('LT4_L1T_8DAY_{0}'.format(variable))
## collection5 = ee.ImageCollection('LT5_L1T_8DAY_{0}'.format(variable))
## collection7 = ee.ImageCollection('LE7_L1T_8DAY_{0}'.format(variable))
## collection = ee.ImageCollection(collection4.merge(collection5).merge(collection7))
## collection = collection.select(variable)
## if variable == 'NDVI':
## notes = "NDVI calculated from Norm. Diff. of Near-IR and Red bands"
## elif variable == 'NDSI':
## notes = "NDSI calculated from Norm. Diff. of Green and mid-IR bands"
## elif variable == 'NDWI':
## notes = "NDWI calculated from Norm. Diff. of near-IR and mid-IR bands"
## elif variable == 'EVI':
## notes = "EVI calculated from Near-IR, Red and Blue bands"
## ## How should this function fail gracefully if the inputs are bad?
## ## Should it return an exception?
## else:
## notes = ''
## return collection, coll_name, coll_desc, product, variable, notes
##
##def get_landsat8_8day_collection(variable):
## """Return the 8 day composite image collection for only Landsat 8
##
## Args:
## variable: string indicating the variable/band to return
## (NDVI, NDSI, NDWI, or EVI)
## Returns:
## EarthEngine image collection object
## String of the collection name
## String of the collection description
## String of the input variable
## String of additional notes about the collection
## """
## coll_name = 'LT5_L1T_8DAY_{0}'.format(variable)
## coll_desc = 'Landsat 5, 8-day {0} Composite'.format(variable)
## collection = ee.ImageCollection('LT5_L1T_8DAY_{0}'.format(variable)).select(variable)
## if variable == 'NDVI':
## notes = "NDSI calculated from Norm. Diff. of Near-IR and Red bands"
## elif variable == 'NDSI':
## notes = "NDSI calculated from Norm. Diff. of Green and mid-IR bands"
## elif variable == 'NDWI':
## notes = "NDWI calculated from Norm. Diff. of near-IR and mid-IR bands"
## elif variable == 'EVI':
## notes = "EVI calculated from Near-IR,Red and Blue bands"
## ## How should this function fail gracefully if the inputs are bad?
## ## Should it return an exception?
## else:
## notes = ''
## return collection, coll_name, coll_desc, variable, notes
#===========================================
# MODIS
#===========================================
def get_modis_collection(variable):
"""Return the 8 or 16 day composite image collection for MODIS
Args:
variable: string indicating the variable/band to return
(LST_Day_1km, NDVI, NDSI, NDWI, or EVI)
Returns:
EarthEngine image collection object
String of the collection name
String of the collection description
String of the input variable
String of additional notes about the collection
"""
coll_name = 'MCD43A4_{0}'.format(variable)
coll_desc = 'MODIS 16-day {0}'.format(variable)
var_desc = variable
if variable == 'NDVI':
notes = "NDSI calculated from Norm. Diff. of Near-IR and Red bands"
elif variable == 'NDSI':
notes = "NDSI calculated from Norm. Diff. of Green and mid-IR bands"
elif variable == 'NDWI':
notes = "NDWI calculated from Norm. Diff. of near-IR and mid-IR bands"
elif variable == 'EVI':
notes = "EVI calculated from Near-IR,Red and Blue bands"
elif variable == 'LST_Day_1km':
notes = "Level 2 LST projected in a Sinusoidal Grid by mapping to 1-km grid"
coll_name = 'MOD11A2'
coll_desc = 'MODIS 8-day {0}'.format(variable)
var_desc = 'Land Surface Temperature during Day'
## How should this function fail gracefully if the inputs are bad?
## Should it return an exception?
else:
notes = ''
collection = ee.ImageCollection(coll_name).select(variable)
return collection, coll_name, coll_desc, var_desc, notes
#===========================================
# GRIDMET
#===========================================
def get_gridmet_collection(variable):
"""Return the daily image collection for GRIDMET
Args:
variable: string indicating the variable/band to return
(i.e. pr, tmmx, rmin, vs, pet, etc.)
Returns:
EarthEngine image collection object
String of the collection name
String of the collection description
String of the variable description
String of additional notes about the collection
"""
coll_name = 'IDAHO_EPSCOR/GRIDMET'
coll_desc = 'gridMET 4-km observational dataset(University of Idaho)'
# Don't select variable here since Tmean or WB need to be mapped/calculated first
collection = ee.ImageCollection(coll_name)
notes = ""
if variable == 'pr':
var_desc = 'Precipitation'
elif variable == 'tmmx':
var_desc = 'Maximum Temperature'
elif variable == 'tmmn':
var_desc = 'Minimum Temperature'
elif variable == 'rmin':
var_desc = 'Minimum Relative Humidity'
elif variable == 'rmax':
var_desc = 'Maximum Relative Humidity'
elif variable == 'srad':
var_desc = 'Downwelling Shortwave Radiation'
elif variable == 'vs':
var_desc = 'Wind Speed Near Surface'
elif variable == 'sph':
var_desc = 'Specific Humidity'
elif variable == 'erc':
var_desc = 'Energy Release Component'
elif variable == 'pet':
notes = "ASCE Standardized Reference ET, estimated using the Penmann Monteith method. See Equation 1 in http://www.kimberly.uidaho.edu/water/asceewri/ascestzdetmain2005.pdf"
var_desc = 'Reference Evapotranspiration'
elif variable == 'tmean':
collection = collection.map(gridmet_tmean_func)
notes = "Calculated as Average of Min/Max Daily Temperature"
var_desc = 'Average Temperature'
elif variable == 'wb':
collection = collection.map(gridmet_wb_func)
notes = "Calculated as the difference between precipitation and reference evapotranspiration"
var_desc = 'Water Balance (PPT-PET)'
elif variable == 'pdsi':
## Rebuild PDSI collection since it isn't technically in the GRIDMET coll.
coll_name = 'IDAHO_EPSCOR/PDSI'
collection = ee.ImageCollection(coll_name)
notes = ""
var_desc = 'Palmer Drought Severity Index (PDSI)'
## How should this function fail gracefully if the inputs are bad?
## Should it return an exception?
else:
var_desc = ""
collection = collection.select(variable)
return collection, coll_name, coll_desc, var_desc, notes
#===========================================
# Collection Functions
#===========================================
property_list = ['system:index','system:time_start', 'system:time_end']
def landsat457_cloud_mask_func(img):
"""Apply basic ACCA cloud mask to a daily Landsat 4, 5, or 7 image"""
cloud_mask = ee.Algorithms.Landsat.simpleCloudScore(img).\
select(['cloud']).lt(ee.Image.constant(50))
return img.mask(cloud_mask.mask(cloud_mask))
def landsat457_ndvi_func(img):
"""Calculate NDVI for a daily Landsat 4, 5, or 7 image"""
## Remove .clamp(-0.1, 1)
return img.normalizedDifference(["B4","B3"]).select([0], ['NDVI'])\
.copyProperties(img, property_list)
def landsat457_ndsi_func(img):
"""Calculate NDSI for a daily Landsat 4, 5, or 7 image"""
## Removed .clamp(-0.1, 1)
return img.normalizedDifference(["B2", "B5"]).select([0], ['NDSI'])\
.copyProperties(img, property_list)
def landsat457_ndwi_func(img):
"""Calculate NDWI (Gao 1996 formulation) for a daily Landsat 4, 5, or 7 image"""
## Removed .clamp(-0.1, 1)
return img.normalizedDifference(["B4", "B5"]).select([0], ['NDWI'])\
.copyProperties(img, property_list)
def landsat457_evi_func(img):
"""Calculate EVI for a daily Landsat 4, 5, or 7 image"""
return img.expression('(2.5 * (b("B4") - b("B3"))) / (b("B4") + 6 * b("B3") - 7.5 * b("B1") + 1)')\
.select([0], ['EVI']).copyProperties(img, property_list)
## DEADBEEF - Need to code in Landsat 8 cloud masking
def landsat8_cloud_mask_func(img):
return img
def landsat8_ndvi_func(img):
"""Calculate NDVI for a daily Landsat 8 image"""
## Removed .clamp(-0.1, 1)
return img.normalizedDifference(["B5","B4"]).select([0], ['NDVI'])\
.copyProperties(img, property_list)
def landsat8_ndsi_func(img):
"""Calculate NDSI for a daily Landsat 8 image"""
## Removed .clamp(-0.1, 1)
return img.normalizedDifference(["B3","B6"]).select([0], ['NDSI'])\
.copyProperties(img, property_list)
def landsat8_ndwi_func(img):
"""Calculate NDWI for a daily Landsat 8 image"""
## Removed .clamp(-0.1, 1)
return img.normalizedDifference(["B6","B5"]).select([0], ['NDWI'])\
.copyProperties(img, property_list)
def landsat8_evi_func(img):
"""Calculate EVI for a daily Landsat 8 image"""
##This formulation should be double checked
return img.expression('(2.5 * (b("B5") - b("B4"))) / (b("B5") + 6 * b("B4") - 7.5 * b("B2") + 1)')\
.select([0], ['EVI']).copyProperties(img, property_list)
def gridmet_wb_func(img):
"""Calculate water balance from precip and PET for GRIDMET collection"""
##return img.expression("b('pr') - b('pet')")\
## .select([0], ['wb']).copyProperties(img, property_list)
pr_img = img.select('pr')
pet_img = img.select('pet')
return pr_img.subtract(pet_img).select([0], ['wb'])\
.copyProperties(img, property_list)
def gridmet_tmean_func(img):
"""Calculate Tmean image from Tmin and Tmax for GRIDMET collection"""
##return img.expression("0.5 * (b('tmmx') + b('tmmx'))")\
## .select([0],['tmean']).copyProperties(img, property_list)
tmax_img = img.select('tmmx')
tmin_img = img.select('tmmn')
return tmax_img.add(tmin_img).multiply(0.5).select([0],['tmean'])\
.copyProperties(img, property_list)