-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
226 lines (129 loc) · 6.08 KB
/
helper.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
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 22 15:19:43 2022
@author: nprks
"""
import os
import datetime
import pandas as pd
import s3fs
import xarray as xr
import rioxarray
import netCDF4
import pytz
from osgeo import gdal
CREATE_GEOTIFF_FOR_NETCDF_VAR = [
'Rad',
'BCM',
'Area',
'CMI_C02',
'CMI_C07',
'CMI_C13',
'CMI_C14',
'CMI_C15',
]
def convertTZ(d, sourceTZ = "US/Pacific", destTZ = 'UTC'):
tzS = pytz.timezone(sourceTZ)
tzD = pytz.timezone(destTZ)
return tzS.normalize(tzS.localize(d)).astimezone(tzD)
def getDirs(dirName = "."):
return [name for name in os.listdir(dirName) if os.path.isdir(dirName + '/' + name)]
def getFiles(dirName = "."):
return [name for name in os.listdir(dirName) if os.path.isfile(dirName + '/' +name)]
def getTimeRange(t, bufferMinutes = 60):
if type(bufferMinutes) is not list:
bufferMinutes = [bufferMinutes, bufferMinutes]
t_delta_0 = datetime.timedelta(minutes=bufferMinutes[0])
t_delta_1 = datetime.timedelta(minutes=bufferMinutes[1])
return t - t_delta_0, t + t_delta_1
def getProductList(start_time, end_time, product = 'ABI-L1b-RadC', satellite = 16):
## Check if satellite number is valid
assert satellite in [16, 17, 18]
satellite = 'noaa-goes' + str(satellite)
DATES = pd.date_range(f"{start_time:%Y-%m-%d %H:00}", f"{end_time:%Y-%m-%d %H:00}", freq="1H")
# Use anonymous credentials to access public data from AWS
fs = s3fs.S3FileSystem(anon=True)
# List all files for each date
# ----------------------------
files = []
for DATE in DATES:
files += fs.ls(f"{satellite}/{product}/{DATE:%Y/%j/%H/}", refresh=True)
# Build a table of the files
# --------------------------
df = pd.DataFrame(files, columns=["file"])
df[["product_mode", "satellite", "start", "end", "creation"]] = (
df["file"].str.rsplit("_", expand=True, n=5).loc[:, 1:]
)
# Filter files by requested time range
# ------------------------------------
# Convert filename datetime string to datetime object
df["start"] = pd.to_datetime(df.start, format="s%Y%j%H%M%S%f", utc=True)
df["end"] = pd.to_datetime(df.end, format="e%Y%j%H%M%S%f", utc=True)
df["creation"] = pd.to_datetime(df.creation, format="c%Y%j%H%M%S%f.nc", utc=True)
df["latency"] = df["creation"] - df["end"]
## For L1 product, extract bands
if product[:-1] == 'ABI-L1b-Rad':
df["band"] = df['product_mode'].str[-2:].astype(int)
# Filter by files within the requested time range
df = df.loc[df.start >= start_time].loc[df.end <= end_time].reset_index(drop=True)
return df
def makeDir(path):
if not os.path.isdir(path):
os.makedirs(path, exist_ok=True)
def convert2GTiff(pathNetCDF, pathGTiff):
f = netCDF4.Dataset(pathNetCDF)
## Choose the first variable/subdataset in the netCDF file to convert to a GeoTIFF
## To select altnative variable stored in the file, adjust the list number, ie. [0] selects the 1st variable
# var = list(f.variables.keys())[0]
# print ("Selected variable: " + var)
for var in f.variables.keys():
if var not in CREATE_GEOTIFF_FOR_NETCDF_VAR:
continue
print ("Selected variable: " + var)
# ## Uncomment the line below to reveal the list of variables stored in the file
# print(list(f.variables.keys()))
#!TODO: Implement mechanism to save meta information to a xml, csv, or any other machine/human readable file.
# ## Open dataset and parse variable information
# ds = xr.open_dataset(pathNetCDF)
# var_name = ds[var].long_name
# units_name = ds[var].units
# variable = ds[var].data
netCDF_file = rioxarray.open_rasterio('netcdf:{0}:{1}'.format(pathNetCDF, var))
# Execute the conversion from netCDF to GeoTIFF
netCDF_file.rio.to_raster(os.path.dirname(pathGTiff) + '/' + var + '_' + os.path.basename(pathGTiff))
f = None
netCDF_file = None
def filterByStartTime(df, startTime):
return df[df['start'] == startTime]
def getFileName(AWS_FL_NAME, productName, tz = "US/Pacific"):
start_time = datetime.datetime.strptime(AWS_FL_NAME.split('_')[3], "s%Y%j%H%M%S%f")
band = ''
if productName == 'RadC':
band = '_B%s'%AWS_FL_NAME.split('_')[1][-2:]
start_time = convertTZ(start_time, sourceTZ = "UTC", destTZ = tz)
return start_time.strftime('%Y%m%d-%H%M') + band + '.tif'
def download(df, BASEDIR, startTime = None, makeGeoTiff = False, verbose = True):
product = df.iloc[0]['product_mode'].split('-')[2]
pathNetCDF = BASEDIR + 'NetCDF/%s/'%product
makeDir(pathNetCDF)
if startTime is not None:
assert any(startTime == df['start'])
df = filterByStartTime(df, startTime)
assert len(df) > 0
# Use anonymous credentials to access public data from AWS
fs = s3fs.S3FileSystem(anon=True)
for fl in df['file']:
fl_name = fl.split('/')[-1]
if verbose:
print("Downloading from AWS: ", fl)
fs.download(fl, pathNetCDF + '/' + fl_name)
fName = getFileName(AWS_FL_NAME = fl_name,
productName = product,
tz = "US/Pacific")
if makeGeoTiff:
pathGTiff = BASEDIR + 'GTif/%s/'%product
makeDir(pathGTiff)
if verbose:
print("Generating GeoTIFF: ", pathGTiff + '/' + fName)
convert2GTiff(pathNetCDF + '/' + fl_name,
pathGTiff + '/' + fName)