-
Notifications
You must be signed in to change notification settings - Fork 3
/
ChesapeakeBayAgLandsat5Calibration.txt
167 lines (136 loc) · 6.49 KB
/
ChesapeakeBayAgLandsat5Calibration.txt
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
//2017 Spring GSFC Chesapeake Bay Agriculture
//NASA DEVELOP | Goddard Space Flight Center
//Script Primary Author: Alison Thieme, [email protected]
//Team Members: John Fitz, Sean McCartney, Perry Oddo, Alison Thieme, Sunita Yadav (Team Leader)
//Most Recent Update: April 10, 2018
//
//This script is designed to extract Landsat 5 Surface Reflectance NDVI averages from field boundaries
//and export the values as a table. The date of the image is found in the header of the table and the
//NDVI averages as as the cells in the column corresponding to the date. Here CLUID is used as the
//unique identifier for each field. Originally this was used to match both field data and calibration points.
//
//Complete top section in order to adjust script for fields and year as needed.
//___________________________________________________________________________________________________________
//Run python script using ArcMap to buffer field polygons 15m inward and separate into 4 shapefiles
//based on county.
//Upload shapefiles as Fusion Table or as a table Asset in Google Earth Engine.
//Shapefiles must be in WGS 1984 as projection
//Import these Fusion Table containing buffered field data for one year
//Below are the variables for 2014-2015 fields in 4 counties:
//Queen Annes County and Talbot Counties are combined into one region.
//replace XXXXX with Fusion Table ID or import from Assets and name var = region
//var region = ee.FeatureCollection('ft:XXXXXXXXX', 'geometry'); //calibration buffer points
//To add a different fusion table, change the ID found after ft: to the one matching your fusion table
//ID found under File>About This Table
//Specify year when Winter Cover Crop planting took place
//Landsat 5 imagery is available from 1984-2011.
var year = ee.Number(2003);
//When steps above are completed click Run.
//When completed, under Console tab, select location to output data and hit Run.
//This should appear as Winter (December 15-January 31) and Spring (March 1-April 15) tables.
//___________________________________________________________________________________________________________
//******************************DROP HTML DESCRIPTION FIELD***************************************
region = region.map(
function(feature) {
return feature.select(['CLUID']);
});
print('Field Boundaries', region.first());
//******************************CREATE DATE RANGES BASED ON YEAR INPUT ABOVE**********************
var start_date = ee.Date.fromYMD(year, 1, 1)
var advanceyear = start_date.advance(1, 'year');
var nextyear = advanceyear.get('year');
var start_Winter = ee.Date.fromYMD(year, 12, 15);
var end_Winter = ee.Date.fromYMD(nextyear, 1, 31);
var start_Spring = ee.Date.fromYMD(nextyear, 3, 1);
var end_Spring = ee.Date.fromYMD(nextyear, 4, 15);
//******************************IMPORT LANDSAT 5 SURFACE REFLECTANCE COLLECTION*******************
// Load a Landsat 5 ImageCollection for a single path-row for Winter
var Wcollection = ee.ImageCollection("LANDSAT/LT05/C01/T1_SR")
.filterBounds(region)
.filterDate(start_Winter, end_Winter);
print('Collection: ', Wcollection);
// Load a Landsat 5 ImageCollection for a single path-row for Spring
var Scollection = ee.ImageCollection("LANDSAT/LT05/C01/T1_SR")
.filterBounds(region)
.filterDate(start_Spring, end_Spring);
print('Collection: ', Scollection);
Map.addLayer(Wcollection.filterBounds(region),
{'bands': ['B3', 'B2', 'B1'], 'min': 1000, 'max': 9500}, 'LS5');
//******************************ADD DATE**********************************************************
var addDate = function(image) {
var date = ee.Date(image.get('system:time_start'));
var dateString = date.format('YYYY-MM-dd');
return image.set('date', dateString);
};
var W_dated = Wcollection.map(addDate);
var S_dated = Scollection.map(addDate);
//******************************CLOUD MASKING*****************************************************
function maskCloud(img) {
//var cloud = img.select('cfmask').eq(0);
var clear = img.select('pixel_qa').bitwiseAnd(2).neq(0);
//clear = clear.updateMask(clear);
return img.updateMask(clear);}
var W_cloudmasked = Wcollection.map(maskCloud);
var S_cloudmasked = Scollection.map(maskCloud);
//******************************FUNCTION TO GET NDVI FROM IMAGE***********************************
var LS5_getNDVI = function(image) {
return image.addBands(image.normalizedDifference(['B4', 'B3']));
};
var NDVI_W = W_cloudmasked.map(LS5_getNDVI);
var NDVI_S = S_cloudmasked.map(LS5_getNDVI);
Map.setCenter(-76.575367, 39.393800, 8);
Map.addLayer(NDVI_S, {bands: ['nd'], palette: '000000, 00FF00', min: 0, max: 0.7}, 'NDVI');
//******************************RENAME NDVI TO DATE***********************************************
NDVI_W = NDVI_W.map(function(i) {
return ee.Image(i)
.select(['nd'],[ee.Date(ee.Image(i).get('system:time_start')).format('YYYY-MM-dd')])});
NDVI_S = NDVI_S.map(function(i) {
return ee.Image(i)
.select(['nd'],[ee.Date(ee.Image(i).get('system:time_start')).format('YYYY-MM-dd')])});
//******************************EXTRACT MEAN NDVI FOR ALL FIELDS**********************************
var stack = function (i1, i2) {
return ee.Image(i2).addBands(ee.Image(i1));
};
var W_STACK = NDVI_W.iterate(stack, ee.Image());
var S_STACK = NDVI_S.iterate(stack, ee.Image());
var ww = function(feature) {
return feature.set(ee.Image(W_STACK)
.slice(1)
.reduceRegion({
reducer: 'mean',
geometry: feature.geometry(),
scale: 30,
maxPixels: 1e9
}).map(function(key, value) {
return ee.Algorithms.If(value, value, 0)
}))
}
var Wresults = region.map(ww);
print('WResults', Wresults.limit(10));
var ss = function(feature) {
return feature.set(ee.Image(S_STACK)
.slice(1)
.reduceRegion({
reducer: 'mean',
geometry: feature.geometry(),
scale: 30,
maxPixels: 1e9
}).map(function(key, value) {
return ee.Algorithms.If(value, value, 0)
}))
}
var Sresults = region.map(ss);
print('SResults', Sresults.limit(10));
//******************************EXPORT AS CSV******************************************************
Export.table.toDrive({
collection: Wresults,
description: 'Winter_NDVI',
fileNamePrefix: 'Winter_NDVI',
fileFormat: 'CSV'
});
Export.table.toDrive({
collection: Sresults,
description: 'Spring_NDVI',
fileNamePrefix: 'Spring_NDVI',
fileFormat: 'CSV'
});