-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemapping.py
More file actions
369 lines (300 loc) · 14.2 KB
/
Remapping.py
File metadata and controls
369 lines (300 loc) · 14.2 KB
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
import os
import matplotlib
import matplotlib.pyplot as plt
from matplotlib import cm
import cartopy
import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader
import numpy as np
from numpy import savetxt
import colorsys
from osgeo import gdal
from osgeo import osr
import datetime
import time
from PIL import Image
# Force matplotlib to not use any Xwindows backend.
matplotlib.use('Agg')
gdal.PushErrorHandler('CPLQuietErrorHandler') # Ignore GDAL warnings
dir_in = f'nc_files_directory_path'
dir_main = f'main_directory'
dir_out = dir_main + "output\\"
dir_colortables = dir_main + "colortables\\"
v_extent_cloud = 'cloud'
v_extent_vegetation = 'vegetation'
v_extent_x = 'random' # Validation
v_extent_water = 'water'
v_extent_city = 'city'
v_extent_rs = 'rs'
v_extent_mask = 'mask'
def load_cpt(path):
try:
f = open(path)
except:
print("File ", path, "not found")
return None
lines = f.readlines()
f.close()
x = np.array([])
r = np.array([])
g = np.array([])
b = np.array([])
colorModel = 'RGB'
for l in lines:
ls = l.split()
if l[0] == '#':
if ls[-1] == 'HSV':
colorModel = 'HSV'
continue
else:
continue
if ls[0] == 'B' or ls[0] == 'F' or ls[0] == 'N':
pass
else:
x = np.append(x, float(ls[0]))
r = np.append(r, float(ls[1]))
g = np.append(g, float(ls[2]))
b = np.append(b, float(ls[3]))
xtemp = float(ls[4])
rtemp = float(ls[5])
gtemp = float(ls[6])
btemp = float(ls[7])
x = np.append(x, xtemp)
r = np.append(r, rtemp)
g = np.append(g, gtemp)
b = np.append(b, btemp)
if colorModel == 'HSV':
for i in range(r.shape[0]):
rr, gg, bb = colorsys.hsv_to_rgb(r[i] / 360., g[i], b[i])
r[i] = rr;
g[i] = gg;
b[i] = bb
if colorModel == 'RGB':
r = r / 255.0
g = g / 255.0
b = b / 255.0
xNorm = (x - x[0]) / (x[-1] - x[0])
red = []
blue = []
green = []
for i in range(len(x)):
red.append([xNorm[i], r[i], r[i]])
green.append([xNorm[i], g[i], g[i]])
blue.append([xNorm[i], b[i], b[i]])
colorDict = {'red': red, 'green': green, 'blue': blue}
return colorDict
def reproject(reproj_file, reproj_var, reproj_extent, reproj_resolution, coordinates):
global dir_in
def get_geot(ex, nlines, ncols):
# Compute resolution based on data dimension
resx = (ex[2] - ex[0]) / ncols
resy = (ex[3] - ex[1]) / nlines
return [ex[0], resx, 0, ex[3], 0, -resy]
if reproj_extent == 'br':
# Brazil
r_extent = [-90.0, -40.0, -20.0, 10.0] # Min lon, Min lat, Max lon, Max lat
elif reproj_extent == 'sp':
# São Paulo
r_extent = [-53.25, -26.0, -44.0, -19.5] # Min lon, Min lat, Max lon, Max lat
else:
r_extent = coordinates # Min lon, Min lat, Max lon, Max lat
# GOES-16 Spatial Reference System
source_prj = osr.SpatialReference()
source_prj.ImportFromProj4(
'+proj=geos +h=35786023.0 +a=6378137.0 +b=6356752.31414 +f=0.00335281068119356027 +lat_0=0.0 +lon_0=-75 +sweep=x +no_defs')
# Lat/lon WSG84 Spatial Reference System
target_prj = osr.SpatialReference()
target_prj.ImportFromProj4('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
# Opening image with the GDAL library
raw = gdal.Open(f'NETCDF:{reproj_file}:' + reproj_var, gdal.GA_ReadOnly)
# Reading header metadata
metadata = raw.GetMetadata()
try:
scale = float(metadata.get(reproj_var + '#scale_factor'))
except:
scale = 1
try:
offset = float(metadata.get(reproj_var + '#add_offset'))
except:
offset = 0.0
undef = float(metadata.get(reproj_var + '#_FillValue'))
file_dtime = metadata.get('NC_GLOBAL#time_coverage_start')
file_satellite = metadata.get('NC_GLOBAL#platform_ID')[1:3]
# Setup projection and geo-transformation
raw.SetProjection(source_prj.ExportToWkt())
GOES16_EXTENT = [-5434894.885056, -5434894.885056, 5434894.885056, 5434894.885056]
raw.SetGeoTransform(get_geot(GOES16_EXTENT, raw.RasterYSize, raw.RasterXSize))
# Compute grid dimension
KM_PER_DEGREE = 111.32
sizex = int(((r_extent[2] - r_extent[0]) * KM_PER_DEGREE) / reproj_resolution)
sizey = int(((r_extent[3] - r_extent[1]) * KM_PER_DEGREE) / reproj_resolution)
# Get memory driver
driver = gdal.GetDriverByName('MEM')
# Create grid
grid = driver.Create('grid', sizex, sizey, 1, gdal.GDT_Float32)
# Setup projection and geo-transformation
grid.SetProjection(target_prj.ExportToWkt())
grid.SetGeoTransform(get_geot(r_extent, grid.RasterYSize, grid.RasterXSize))
# Perform the projection/resampling
gdal.ReprojectImage(raw, grid, source_prj.ExportToWkt(), target_prj.ExportToWkt(), gdal.GRA_NearestNeighbour,
options=['NUM_THREADS=ALL_CPUS'])
# Perform the projection/resampling
gdal.ReprojectImage(raw, grid, source_prj.ExportToWkt(), target_prj.ExportToWkt(), gdal.GRA_NearestNeighbour,
options=['NUM_THREADS=ALL_CPUS'])
# Close file
raw = None
del raw
# Read grid data
array = grid.ReadAsArray()
# Mask fill values (i.e. invalid values)
np.ma.masked_where(array, array == -1, False)
# Applying scale, offset
array = array * scale + offset
grid.GetRasterBand(1).SetNoDataValue(-1)
grid.GetRasterBand(1).WriteArray(array)
# Define the parameters of the output file
kwargs = {'format': 'netCDF',
'dstSRS': target_prj,
'outputBounds': (r_extent[0], r_extent[3], r_extent[2], r_extent[1]),
'outputBoundsSRS': target_prj,
'outputType': gdal.GDT_Float32,
'srcNodata': undef,
'dstNodata': 'nan',
'resampleAlg': gdal.GRA_NearestNeighbour}
reproj_file = reproj_file.split('\\')
reproj_file.reverse()
r_file = reproj_file[0].replace('.nc', f'_reproj_{reproj_extent}.nc')
gdal.Warp(f'{dir_in}{reproj_file[1]}\\{r_file}', grid, **kwargs)
return file_dtime, file_satellite, grid
def loop_remap_plot(v_extent, coordinates):
for file in os.listdir(dir_in):
ch = (file[file.find("CMIPF-M6C") + 9:file.find("_G16_s")])
file_var = 'CMI'
# Captures the time to count image processing time
processing_start_time = time.time()
# Area of interest for cropping
if v_extent == 'br':
# Brasil
extent = [-90.0, -40.0, -20.0, 10.0] # Min lon, Min lat, Max lon, Max lat
# Choose the image resolution (the higher the number the faster the processing is)
resolution = 4.0
elif v_extent == 'sp':
# São Paulo
extent = [-53.25, -26.0, -44.0, -19.5] # Min lon, Min lat, Max lon, Max lat
# Choose the image resolution (the higher the number the faster the processing is)
resolution = 1.0
else:
extent = coordinates # Min lon, Min lat, Max lon, Max lat
resolution = 1.0
# Reprojecting CMI image and receiving image date/time, satellite and absolute path of the redesigned file
dtime, satellite, reproject_band = reproject(dir_in + file, file_var, v_extent, resolution, extent)
if 1 <= int(ch) <= 6:
data = reproject_band.ReadAsArray()
else:
data = reproject_band.ReadAsArray() - 273.15
reproject_band = None
del reproject_band
# ABI channel wavelengths
wavelenghts = ['[]', '[0.47 μm]', '[0.64 μm]', '[0.865 μm]', '[1.378 μm]', '[1.61 μm]', '[2.25 μm]',
'[3.90 μm]', '[6.19 μm]',
'[6.95 μm]', '[7.34 μm]', '[8.50 μm]', '[9.61 μm]', '[10.35 μm]', '[11.20 μm]', '[12.30 μm]',
'[13.30 μm]']
# Formatting date to plot on the image and save the file
date = (datetime.datetime.strptime(dtime, '%Y-%m-%dT%H:%M:%S.%fZ'))
date_img = date.strftime('%d-%b-%Y %H:%M UTC')
date_file = date.strftime('%Y%m%d_%H%M%S')
# Defining image measurement unit according to channel
if 1 <= int(ch) <= 6:
unit = "Albedo (%)"
else:
unit = "Brightness Temperature [°C]"
# Defining output image size
d_p_i = 150
fig = plt.figure(figsize=(2000 / float(d_p_i), 2000 / float(d_p_i)), frameon=True, dpi=d_p_i, edgecolor='black',
facecolor='black')
# Using geostationary projection in cartopy
ax = plt.axes(projection=ccrs.PlateCarree())
gl = ax.gridlines(crs=ccrs.PlateCarree(), color='white', alpha=0.7, linestyle='--', linewidth=0.2,
xlocs=np.arange(-180, 180, 5), ylocs=np.arange(-90, 90, 5))
gl.top_labels = False
gl.right_labels = False
# Defining the image color palette according to the channel
# Converting a CPT file to be used in Python using the loadCPT function
# CPT archive: http://soliton.vm.bytemark.co.uk/pub/cpt-city/
if 1 <= int(ch) <= 6:
cpt = load_cpt(dir_colortables + 'Square Root Visible Enhancement.cpt')
elif int(ch) == 7:
cpt = load_cpt(dir_colortables + 'SVGAIR2_TEMP.cpt')
elif 8 <= int(ch) <= 10:
cpt = load_cpt(dir_colortables + 'SVGAWVX_TEMP.cpt')
else:
cpt = load_cpt(dir_colortables + 'IR4AVHRR6.cpt')
my_cmap = cm.colors.LinearSegmentedColormap('cpt', cpt) # Creating a custom color palette
# Formatting the image extension, modifying order of minimum and maximum longitude and latitude
img_extent = [extent[0], extent[2], extent[1], extent[3]] # Min lon, Max lon, Min lat, Max lat
# Plotting the image
# Defining the maximum and minimum values of the image according to the channel and color palette used
if 1 <= int(ch) <= 6:
img = ax.imshow(data, origin='upper', vmin=0, vmax=1, cmap=my_cmap, extent=img_extent)
elif 7 <= int(ch) <= 10:
img = ax.imshow(data, origin='upper', vmin=-112.15, vmax=56.85, cmap=my_cmap, extent=img_extent)
else:
img = ax.imshow(data, origin='upper', vmin=-103, vmax=84, cmap=my_cmap, extent=img_extent)
plt.savefig(f'{dir_out}band{ch}_{date_file}_{v_extent}.png', bbox_inches='tight', pad_inches=0, dpi=d_p_i)
plt.close()
# Logs the calculation of image processing time
print(f'{file} - {v_extent} - {str(round(time.time() - processing_start_time, 4))} seconds')
def loop_remap_plot_mask(v_extent, coordinates):
for file in os.listdir(dir_in):
file_var = 'BCM'
# Captures the time to count image processing time
processing_start_time = time.time()
# Area of interest for cropping
if v_extent == 'br':
# Brasil
extent = [-90.0, -40.0, -20.0, 10.0] # Min lon, Min lat, Max lon, Max lat
# Choose the image resolution (the higher the number the faster the processing is)
resolution = 4.0
elif v_extent == 'sp':
# São Paulo
extent = [-53.25, -26.0, -44.0, -19.5] # Min lon, Min lat, Max lon, Max lat
# Choose the image resolution (the higher the number the faster the processing is)
resolution = 1.0
else:
extent = coordinates # Min lon, Min lat, Max lon, Max lat
resolution = 1.0
# Reprojecting CMI image and receiving image date/time, satellite and absolute path of the redesigned file
dtime, satellite, reproject_band = reproject(dir_in + file, file_var, v_extent, resolution, extent)
data = reproject_band.ReadAsArray()
del reproject_band
print(data.shape)
# Formatting date to plot on the image and save the file
date = (datetime.datetime.strptime(dtime, '%Y-%m-%dT%H:%M:%S.%fZ'))
date_file = date.strftime('%Y%m%d_%H%M%S')
# Defining output image size
d_p_i = 150
fig = plt.figure(figsize=(2000 / float(d_p_i), 2000 / float(d_p_i)), frameon=True, dpi=d_p_i, edgecolor='black',
facecolor='black')
# Using geostationary projection in cartopy
ax = plt.axes(projection=ccrs.PlateCarree())
gl = ax.gridlines(crs=ccrs.PlateCarree(), color='white', alpha=0.7, linestyle='--', linewidth=0.2,
xlocs=np.arange(-180, 180, 5), ylocs=np.arange(-90, 90, 5))
gl.top_labels = False
gl.right_labels = False
# Defining the image color palette according to the channel
# Converting a CPT file to be used in Python using the loadCPT function
# CPT archive: http://soliton.vm.bytemark.co.uk/pub/cpt-city/
cpt = load_cpt(dir_colortables + 'Square Root Visible Enhancement.cpt')
my_cmap = cm.colors.LinearSegmentedColormap('cpt', cpt) # Creating a custom color palette
# Formatting the image extension, modifying order of minimum and maximum longitude and latitude
img_extent = [extent[0], extent[2], extent[1], extent[3]] # Min lon, Max lon, Min lat, Max lat
# Plotting the image
# Defining the maximum and minimum values of the image according to the channel and color palette used
img = ax.imshow(data, origin='upper', vmin=0, vmax=1, cmap=my_cmap, extent=img_extent)
plt.savefig(f'{dir_out}mask_{date_file}_{v_extent}.png', bbox_inches='tight', pad_inches=0, dpi=d_p_i)
plt.close()
# Logs the calculation of image processing time
print(f'{file} - {v_extent} - {str(round(time.time() - processing_start_time, 4))} seconds')
# Remaps the desired area with desired extent
coordinates = [left, bottom, right, top]
loop_remap_plot(v_extent, coordinates)