-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNDVI.py
157 lines (107 loc) · 4.2 KB
/
NDVI.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
import os, rasterio, re
from datetime import datetime
class Raster:
# Raster class to associate the bands nir and red with their date
def __init__(self, date, band_4, band_8):
self.d = date
self.b4 = band_4
self.b8 = band_8
def date_check():
# Check the dates of the ndvi files present in the folder
folder_files.append(os.listdir(path_ndvi))
for file in folder_files[0]:
match = re.search(r"\d{2}-\d{2}-\d{2}", file)
date = datetime.strptime(match.group(), "%y-%m-%d").date().strftime("%y-%m-%d")
folder_dates.append(date)
return folder_dates
def directories_search(directory_path):
# Search for folders and subfolders within the folder
subfolders = [f.path for f in os.scandir(directory_path) if f.is_dir()]
for directory_path in list(subfolders):
subfolders.extend(directories_search(directory_path))
return subfolders
def bands_dn():
# Getting the dates of the bands and the name of the file
for directory in directories_list:
os.chdir(directory)
pictures = os.listdir()
for p in pictures:
match = re.search(r"\d{4}\d{2}\d{2}", p)
date = (
datetime.strptime(match.group(), "%Y%m%d").date().strftime("%y-%m-%d")
)
if "B08" in p:
bands_8.append(p)
sentinel_dates.append(date)
elif "B04" in p:
bands_4.append(p)
sentinel_dates.append(date)
return
def del_duplicates():
# Delete existing dates and bands in the NDVI folder
for i in duplicate_dates:
sentinel_dates.pop(i)
bands_8.pop(i)
bands_4.pop(i)
directories_list.pop(i)
return
def absolute_paths():
# Store of the absolute paths of the bands
if len(sentinel_dates) > len(duplicate_dates):
for i in range(0, len(directories_list)):
bands_8[i] = directories_list[i] + "\\" + bands_8[i]
bands_4[i] = directories_list[i] + "\\" + bands_4[i]
return bands_8, bands_4
def calc_ndvi(raster):
# Specify the output path of the NDVI output raster
output_path = path + "\\NDVI\\NDVI"
b4 = raster.b4
b8 = raster.b8
# we open the bands with rasterio
with rasterio.open(b4) as red:
RED = red.read()
with rasterio.open(b8) as nir:
NIR = nir.read()
# we calculate the NDVI
NDVI = (NIR.astype(float) - RED.astype(float)) / (NIR + RED)
profile = red.meta
profile.update(driver="GTiff")
profile.update(dtype=rasterio.float32)
with rasterio.open(output_path + "_" + raster.d + ".tif", "w", **profile) as dst:
dst.write(NDVI.astype(rasterio.float32))
return
# Start the script
path = os.getcwd()
path_ndvi = path + "\\NDVI"
folder_files = []
folder_dates = []
bands_8 = []
bands_4 = []
sentinel_dates = []
date_check() # Check the date of the processed files in the folder
# The names of the files and the directories where the bands to be processed are located
directories = directories_search(path)
directories_list = [d for d in directories if "R10m" in d]
bands_dn()
# Create a list with the dates of the downloaded files
sentinel_dates = list(set(sentinel_dates))
duplicate_dates = [
sentinel_dates.index(d) for d in sentinel_dates if d in folder_dates
] # List of duplicate date indexes
duplicate_dates = sorted(duplicate_dates, reverse=True)
# Create a list with the names to assign to the rasters
raster_names = ["raster_" + str(d) for d in range(1, len(sentinel_dates) + 1)]
raster_objects = []
if len(sentinel_dates) > len(duplicate_dates):
del_duplicates()
absolute_paths()
for k in range(0, len(raster_names)):
raster_objects.append(Raster(sentinel_dates[k], bands_4[k], bands_8[k]))
for raster in raster_objects:
calc_ndvi(raster)
if len(sentinel_dates) > 1:
print(str(len(sentinel_dates)) + " Images have been processed.")
else:
print(str(len(sentinel_dates)) + " Image has been processed.")
else:
print("There are no new images to process.")