-
Notifications
You must be signed in to change notification settings - Fork 1
/
hanover_bands
105 lines (84 loc) · 3.55 KB
/
hanover_bands
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
import ee
import numpy as np
import pandas as pd
import shapefile
# Initialize the Earth Engine module
ee.Initialize()
# Load the shapefile
sf = shapefile.Reader("/Users/hydro3/Documents/USGSSites/shape_files/virginia/hanover.shp")
shapes = sf.shapes()
points = shapes[0].points
aoi = ee.Geometry.Polygon(points)
# Create date ranges
start = pd.date_range(start='2017-08-08', end='2024-06-22', freq='5d')
end = pd.date_range(start='2017-08-12', end='2024-06-26', freq='5d')
dates = pd.DataFrame({'start': start, 'end': end})
def pulldata(startDate, endDate):
# Define source data
image = (ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
.filterDate(startDate, endDate)
.filterMetadata('CLOUDY_PIXEL_PERCENTAGE', 'less_than', 10)
.select('B2', 'B3', 'B4', 'B5', 'B8', 'B11'))
# Create the mosaic
mosaic = image.median().reproject(crs='EPSG:32736', scale=10)
band_arrs = mosaic.sampleRectangle(region=aoi, defaultValue=0)
# Get individual band arrays
band_arr_b2 = band_arrs.get('B2')
band_arr_b3 = band_arrs.get('B3')
band_arr_b4 = band_arrs.get('B4')
band_arr_b5 = band_arrs.get('B5') # Corrected to B5
band_arr_b8 = band_arrs.get('B8')
band_arr_b11 = band_arrs.get('B11')
# Transfer the arrays from server to client and cast as np array
b2 = np.array(band_arr_b2.getInfo())
b3 = np.array(band_arr_b3.getInfo())
b4 = np.array(band_arr_b4.getInfo())
b5 = np.array(band_arr_b5.getInfo())
b8 = np.array(band_arr_b8.getInfo())
b11 = np.array(band_arr_b11.getInfo())
# Calculate NDWI
ndwiG = (b3 - b8) / (b3 + b8) # Gao
water = ndwiG > -0.02
# Initialize variables for calculations
results = {}
for band, arr in zip(['B2', 'B3', 'B4', 'B5', 'B8', 'B11'], [b2, b3, b4, b5, b8, b11]):
arr = arr * water # Apply water mask
results[band] = np.sum(arr) / np.sum(arr > 0) if np.sum(arr > 0) > 0 else np.nan
# Write results to file
with open("HanoverBands.txt", "a") as f:
f.write(f"{startDate}, {endDate}, {results['B2']}, {results['B3']}, "
f"{results['B4']}, {results['B5']}, {results['B8']}, {results['B11']}\n")
# Loop through each date range and pull data
for i in range(len(dates)):
pulldata(dates["start"][i], dates["end"][i])
#TESTING
ndwiG = (b3 - b8) / (b3 + b8)
plt.imshow(ndwiG, cmap='gray') # Use a colormap to visualize
plt.colorbar()
plt.show()
print(f"B2: {b2}")
print(f"B3: {b3}")
print(f"B8: {b8}")
def pulldata(startDate, endDate):
# Define source data
image = (ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
.filterDate(startDate, endDate)
.select('B2', 'B3', 'B4', 'B5', 'B8', 'B11'))
# Create the mosaic
mosaic = image.median().reproject(crs='EPSG:32736', scale=10)
band_arrs = mosaic.sampleRectangle(region=aoi, defaultValue=0)
# Get individual band arrays
band_arr_b2 = band_arrs.get('B2') # Blue
band_arr_b3 = band_arrs.get('B3') # Green
band_arr_b4 = band_arrs.get('B4') # Red
band_arr_b5 = band_arrs.get('B5') # Correctly fetching B5
band_arr_b8 = band_arrs.get('B8') # NIR
band_arr_b11 = band_arrs.get('B11') # SWIR
# Transfer the arrays from server to client and cast as np array
b2 = np.array(band_arr_b2.getInfo())
b3 = np.array(band_arr_b3.getInfo())
b4 = np.array(band_arr_b4.getInfo())
b5 = np.array(band_arr_b5.getInfo()) # Ensure this is defined
b8 = np.array(band_arr_b8.getInfo())
b11 = np.array(band_arr_b11.getInfo())
# Continue with the rest of your code...