forked from diegormsouza/geo-sat-python-mar-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Script_10.py
102 lines (88 loc) · 4.28 KB
/
Script_10.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
# Training: Python and GOES-R Imagery: Script 10 - Downloading data from AWS (function)
#-----------------------------------------------------------------------------------------------------------
# Required modules
from netCDF4 import Dataset # Read / Write NetCDF4 files
import matplotlib.pyplot as plt # Plotting library
from datetime import datetime # Basic Dates and time types
import cartopy, cartopy.crs as ccrs # Plot maps
import os # Miscellaneous operating system interfaces
import boto3 # Amazon Web Services (AWS) SDK for Python
from botocore import UNSIGNED # boto3 config
from botocore.config import Config # boto3 config
#-----------------------------------------------------------------------------------------------------------
# Function to download files
def download_file(s3_client, prefix):
# 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("No files found for the date: ",year,day_of_year)
quit()
else:
# There are files
for obj in s3_result['Contents']:
# Print the file name
key = obj['Key']
print(key)
file_name = key.split('/')[-1].split('.')[0]
# Download the file
if not os.path.exists(f'{input}/{file_name}.nc'):
s3_client.download_file(bucket_name, key, f'{input}/{file_name}.nc')
return file_name
#-----------------------------------------------------------------------------------------------------------
# Input and output directories
input = "Samples"; os.makedirs(input, exist_ok=True)
output = "Output"; os.makedirs(output, exist_ok=True)
# AMAZON repository information
# https://noaa-goes16.s3.amazonaws.com/index.html
bucket_name = 'noaa-goes16'
product_name = 'ABI-L2-CMIPF'
year = 2021
day_of_year = 37
hour = 18
min = 00
band = 13
# Initializes the S3 client
s3_client = boto3.client('s3', config=Config(signature_version=UNSIGNED))
#-----------------------------------------------------------------------------------------------------------
# File structure
prefix = f'{product_name}/{year}/{day_of_year:03.0f}/{hour:02.0f}/OR_{product_name}-M6C{band:02.0f}_G16_s{year}{day_of_year:03.0f}{hour:02.0f}{min:02.0f}'
# Download the file
file_name = download_file(s3_client, prefix)
#-----------------------------------------------------------------------------------------------------------
# Open the GOES-R image
file = Dataset(f'{input}/{file_name}.nc')
# Get the pixel values
data = file.variables['CMI'][:]
#-----------------------------------------------------------------------------------------------------------
# Choose the plot size (width x height, in inches)
plt.figure(figsize=(10,10))
# Use the Geostationary projection in cartopy
ax = plt.axes(projection=ccrs.Geostationary(central_longitude=-75.0, satellite_height=35786023.0))
img_extent = (-5434894.67527,5434894.67527,-5434894.67527,5434894.67527)
# Add coastlines, borders and gridlines
ax.coastlines(resolution='10m', color='white', linewidth=0.8)
ax.add_feature(cartopy.feature.BORDERS, edgecolor='white', linewidth=0.5)
ax.gridlines(color='white', alpha=0.5, linestyle='--', linewidth=0.5)
# Define the color scale based on the channel
if band <= 6:
colormap = "gray" # Black to white for visible channels
prodname = "Reflectance (%)"
else:
colormap = "gray_r" # White to black for IR channels
prodname = "Brightness Temperatures (°C)"
# Plot the image
img = ax.imshow(data, origin='upper', extent=img_extent, cmap=colormap)
# Add a colorbar
plt.colorbar(img, label=prodname, extend='both', orientation='horizontal', pad=0.05, fraction=0.05)
# Extract the date
date = (datetime.strptime(file.time_coverage_start, '%Y-%m-%dT%H:%M:%S.%fZ'))
# Add a title
plt.title('GOES-16 Band-' + str(band) + ' ' + date.strftime('%Y-%m-%d %H:%M') + ' UTC', fontweight='bold', fontsize=10, loc='left')
plt.title('Full Disk', fontsize=10, loc='right')
#-----------------------------------------------------------------------------------------------------------
# Save the image
plt.savefig(f'{output}/{file_name}.png')
# Show the image
plt.show()