forked from diegormsouza/geo-sat-python-mar-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
314 lines (258 loc) · 11.8 KB
/
utilities.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
# Training: Python and GOES-R Imagery: Script 8 - Functions for download data from AWS
#-----------------------------------------------------------------------------------------------------------
# Required modules
import os # Miscellaneous operating system interfaces
import numpy as np # Import the Numpy package
import colorsys # To make convertion of colormaps
import boto3 # Amazon Web Services (AWS) SDK for Python
from botocore import UNSIGNED # boto3 config
from botocore.config import Config # boto3 config
import math # Mathematical functions
from datetime import datetime # Basic Dates and time types
from osgeo import osr # Python bindings for GDAL
from osgeo import gdal # Python bindings for GDAL
#from netCDF4 import Dataset # Read / Write NetCDF4 files
#import matplotlib.pyplot as plt # Plotting library
#import cartopy, cartopy.crs as ccrs # Plot maps
##import sys
#from datetime import timedelta, date, datetime # Manipulate dates
#-----------------------------------------------------------------------------------------------------------
def loadCPT(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 download_CMI(yyyymmddhhmn, band, path_dest):
os.makedirs(path_dest, exist_ok=True)
year = datetime.strptime(yyyymmddhhmn, '%Y%m%d%H%M').strftime('%Y')
day_of_year = datetime.strptime(yyyymmddhhmn, '%Y%m%d%H%M').strftime('%j')
hour = datetime.strptime(yyyymmddhhmn, '%Y%m%d%H%M').strftime('%H')
min = datetime.strptime(yyyymmddhhmn, '%Y%m%d%H%M').strftime('%M')
# AMAZON repository information
# https://noaa-goes16.s3.amazonaws.com/index.html
bucket_name = 'noaa-goes16'
product_name = 'ABI-L2-CMIPF'
# Initializes the S3 client
s3_client = boto3.client('s3', config=Config(signature_version=UNSIGNED))
#-----------------------------------------------------------------------------------------------------------
# File structure
prefix = f'{product_name}/{year}/{day_of_year}/{hour}/OR_{product_name}-M6C{int(band):02.0f}_G16_s{year}{day_of_year}{hour}{min}'
# Seach for the file on the server
s3_result = s3_client.list_objects_v2(Bucket=bucket_name, Prefix=prefix, Delimiter = "/")
#-----------------------------------------------------------------------------------------------------------
# Check if there are files available
if 'Contents' not in s3_result:
# There are no files
print(f'No files found for the date: {yyyymmddhhmn}, Band-{band}')
return -1
else:
# There are files
for obj in s3_result['Contents']:
key = obj['Key']
# Print the file name
file_name = key.split('/')[-1].split('.')[0]
# Download the file
if os.path.exists(f'{path_dest}/{file_name}.nc'):
print(f'File {path_dest}/{file_name}.nc exists')
else:
print(f'Downloading file {path_dest}/{file_name}.nc')
s3_client.download_file(bucket_name, key, f'{path_dest}/{file_name}.nc')
return f'{file_name}'
#-----------------------------------------------------------------------------------------------------------
def download_PROD(yyyymmddhhmn, product_name, path_dest):
os.makedirs(path_dest, exist_ok=True)
year = datetime.strptime(yyyymmddhhmn, '%Y%m%d%H%M').strftime('%Y')
day_of_year = datetime.strptime(yyyymmddhhmn, '%Y%m%d%H%M').strftime('%j')
hour = datetime.strptime(yyyymmddhhmn, '%Y%m%d%H%M').strftime('%H')
min = datetime.strptime(yyyymmddhhmn, '%Y%m%d%H%M').strftime('%M')
# AMAZON repository information
# https://noaa-goes16.s3.amazonaws.com/index.html
bucket_name = 'noaa-goes16'
# Initializes the S3 client
s3_client = boto3.client('s3', config=Config(signature_version=UNSIGNED))
#-----------------------------------------------------------------------------------------------------------
# File structure
prefix = f'{product_name}/{year}/{day_of_year}/{hour}/OR_{product_name}-M6_G16_s{year}{day_of_year}{hour}{min}'
# Seach for the file on the server
s3_result = s3_client.list_objects_v2(Bucket=bucket_name, Prefix=prefix, Delimiter = "/")
#-----------------------------------------------------------------------------------------------------------
# Check if there are files available
if 'Contents' not in s3_result:
# There are no files
print(f'No files found for the date: {yyyymmddhhmn}, Product-{product_name}')
return -1
else:
# There are files
for obj in s3_result['Contents']:
key = obj['Key']
# Print the file name
file_name = key.split('/')[-1].split('.')[0]
# Download the file
if os.path.exists(f'{path_dest}/{file_name}.nc'):
print(f'File {path_dest}/{file_name}.nc exists')
else:
print(f'Downloading file {path_dest}/{file_name}.nc')
s3_client.download_file(bucket_name, key, f'{path_dest}/{file_name}.nc')
return f'{file_name}'
#-----------------------------------------------------------------------------------------------------------
def download_GLM(yyyymmddhhmnss, path_dest):
os.makedirs(path_dest, exist_ok=True)
year = datetime.strptime(yyyymmddhhmnss, '%Y%m%d%H%M%S').strftime('%Y')
day_of_year = datetime.strptime(yyyymmddhhmnss, '%Y%m%d%H%M%S').strftime('%j')
hour = datetime.strptime(yyyymmddhhmnss, '%Y%m%d%H%M%S').strftime('%H')
min = datetime.strptime(yyyymmddhhmnss, '%Y%m%d%H%M%S').strftime('%M')
seg = datetime.strptime(yyyymmddhhmnss, '%Y%m%d%H%M%S').strftime('%S')
# AMAZON repository information
# https://noaa-goes16.s3.amazonaws.com/index.html
bucket_name = 'noaa-goes16'
# Initializes the S3 client
s3_client = boto3.client('s3', config=Config(signature_version=UNSIGNED))
#-----------------------------------------------------------------------------------------------------------
# File structure
product_name = "GLM-L2-LCFA"
prefix = f'{product_name}/{year}/{day_of_year}/{hour}/OR_{product_name}_G16_s{year}{day_of_year}{hour}{min}{seg}'
# Seach for the file on the server
s3_result = s3_client.list_objects_v2(Bucket=bucket_name, Prefix=prefix, Delimiter = "/")
#-----------------------------------------------------------------------------------------------------------
# Check if there are files available
if 'Contents' not in s3_result:
# There are no files
print(f'No files found for the date: {yyyymmddhhmnss}, Product-{product_name}')
return -1
else:
# There are files
for obj in s3_result['Contents']:
key = obj['Key']
# Print the file name
file_name = key.split('/')[-1].split('.')[0]
# Download the file
if os.path.exists(f'{path_dest}/{file_name}.nc'):
print(f'File {path_dest}/{file_name}.nc exists')
else:
print(f'Downloading file {path_dest}/{file_name}.nc')
s3_client.download_file(bucket_name, key, f'{path_dest}/{file_name}.nc')
return f'{file_name}'
#-----------------------------------------------------------------------------------------------------------
# Functions to convert lat / lon extent to array indices
def geo2grid(lat, lon, nc):
# Apply scale and offset
xscale, xoffset = nc.variables['x'].scale_factor, nc.variables['x'].add_offset
yscale, yoffset = nc.variables['y'].scale_factor, nc.variables['y'].add_offset
x, y = latlon2xy(lat, lon)
col = (x - xoffset)/xscale
lin = (y - yoffset)/yscale
return int(lin), int(col)
def latlon2xy(lat, lon):
# goes_imagery_projection:semi_major_axis
req = 6378137 # meters
# goes_imagery_projection:inverse_flattening
invf = 298.257222096
# goes_imagery_projection:semi_minor_axis
rpol = 6356752.31414 # meters
e = 0.0818191910435
# goes_imagery_projection:perspective_point_height + goes_imagery_projection:semi_major_axis
H = 42164160 # meters
# goes_imagery_projection: longitude_of_projection_origin
lambda0 = -1.308996939
# Convert to radians
latRad = lat * (math.pi/180)
lonRad = lon * (math.pi/180)
# (1) geocentric latitude
Phi_c = math.atan(((rpol * rpol)/(req * req)) * math.tan(latRad))
# (2) geocentric distance to the point on the ellipsoid
rc = rpol/(math.sqrt(1 - ((e * e) * (math.cos(Phi_c) * math.cos(Phi_c)))))
# (3) sx
sx = H - (rc * math.cos(Phi_c) * math.cos(lonRad - lambda0))
# (4) sy
sy = -rc * math.cos(Phi_c) * math.sin(lonRad - lambda0)
# (5)
sz = rc * math.sin(Phi_c)
# x,y
x = math.asin((-sy)/math.sqrt((sx*sx) + (sy*sy) + (sz*sz)))
y = math.atan(sz/sx)
return x, y
# Function to convert lat / lon extent to GOES-16 extents
def convertExtent2GOESProjection(extent):
# GOES-16 viewing point (satellite position) height above the earth
GOES16_HEIGHT = 35786023.0
# GOES-16 longitude position
GOES16_LONGITUDE = -75.0
a, b = latlon2xy(extent[1], extent[0])
c, d = latlon2xy(extent[3], extent[2])
return (a * GOES16_HEIGHT, c * GOES16_HEIGHT, b * GOES16_HEIGHT, d * GOES16_HEIGHT)
#-----------------------------------------------------------------------------------------------------------
# Function to reproject the data
def reproject(file_name, ncfile, array, extent, undef):
# Read the original file projection and configure the output projection
source_prj = osr.SpatialReference()
source_prj.ImportFromProj4(ncfile.GetProjectionRef())
target_prj = osr.SpatialReference()
target_prj.ImportFromProj4("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
# Reproject the data
GeoT = ncfile.GetGeoTransform()
driver = gdal.GetDriverByName('MEM')
raw = driver.Create('raw', array.shape[0], array.shape[1], 1, gdal.GDT_Float32)
raw.SetGeoTransform(GeoT)
raw.GetRasterBand(1).WriteArray(array)
# Define the parameters of the output file
kwargs = {'format': 'netCDF', \
'srcSRS': source_prj, \
'dstSRS': target_prj, \
'outputBounds': (extent[0], extent[3], extent[2], extent[1]), \
'outputBoundsSRS': target_prj, \
'outputType': gdal.GDT_Float32, \
'srcNodata': undef, \
'dstNodata': 'nan', \
'resampleAlg': gdal.GRA_NearestNeighbour}
# Write the reprojected file on disk
gdal.Warp(file_name, raw, **kwargs)