-
Notifications
You must be signed in to change notification settings - Fork 0
/
DownloadData.py
151 lines (122 loc) · 4.68 KB
/
DownloadData.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
"""
Simple script for downloading GOES data from AWS
"""
# ======================================================================================================================
import os
import pathlib
from enum import Enum
from typing import List, Union
import s3fs
# ======================================================================================================================
# Constants
AWS_BUCKETS = 's3://noaa-goes{SCID}/ABI-L1b-RadF/{year}/{dayOfYear:03}/{hour:02}/*-M6C{band:02}_*'
# ======================================================================================================================
class SCID(Enum):
"""
The goes spacecraft id
"""
SCID_16 = 16
SCID_17 = 17
# ======================================================================================================================
class Band(Enum):
"""
The goes detection band
"""
BLUE_1 = 1
RED_2 = 2
VEGGIE_3 = 3
CIRRUS_4 = 4
SNOW_ICE_5 = 5
CLOUD_PARTICLE_6 = 6
SWIR_7 = 7
UPPER_TROPOSHERE_8 = 8
MID_TROPOSHERE_9 = 9
LOW_TROPOSHERE_10 = 10
CLOUD_TOP_11 = 11
OZONE_12 = 12
CLEAN_LWIR_13 = 13
LWIR_14 = 14
DIRTY_LWIR_14 = 15
CO2_LWIR_16 = 16
# ======================================================================================================================
def download(outputDir: str,
scid: SCID,
band: Band,
year: int,
dayOfYear: Union[int, List[int]],
hour: Union[int, List[int]]) -> List[str]:
"""
Downloads the netcdf4 files from AWS
Args:
outputDir: the directory to save the downloaded files. If the directory does not exist it
will attempt to be created
scid: the spacecraft id
band: the detection band
year: the year
dayOfYear: the day of the year
hour: the hour of the day
Returns:
np.ndarray
"""
# TODO: add start and stop options
if not os.path.isdir(outputDir):
os.mkdir(outputDir)
if type(dayOfYear) is not list:
dayOfYear = [dayOfYear]
if type(hour) is not list:
hour = [hour]
bandDir = os.path.join(outputDir, f'{band.name}')
if not os.path.isdir(bandDir):
os.mkdir(bandDir)
fs = s3fs.S3FileSystem(anon=True)
downloadedFiles = list()
for theDay in dayOfYear:
if theDay < 0 or theDay > 365:
print(f'Input dayOfYear={theDay} is not valid. Valid range is [0, 365]')
continue
dayDir = os.path.join(bandDir, f'day{theDay:03}')
if not os.path.isdir(dayDir):
os.mkdir(dayDir)
for theHour in hour:
if theHour < 0 or theHour > 23:
print(f'input hour={theHour} is not valid. valid range is [0, 23]')
continue
hourDir = os.path.join(dayDir, f'hour{theHour:02}')
if not os.path.isdir(hourDir):
os.mkdir(hourDir)
files = fs.glob(AWS_BUCKETS.format(SCID=scid.value,
year=year,
dayOfYear=theDay,
hour=theHour,
band=band.value))
if len(files) == 0:
print(f'No data files found for '
f'scid={scid.name} '
f'year={year} '
f'dayOfYear={theDay} '
f'hour={theHour} '
f'band={band.name}')
continue
for file in files:
path = pathlib.Path(file)
newName = os.path.join(hourDir, path.name)
print(f'Downloading {file}...')
fs.get(file, newName)
downloadedFiles.append(newName)
return downloadedFiles
# ======================================================================================================================
if __name__ == '__main__':
CURRENT_DIR = pathlib.Path(__file__).parent.absolute()
OUTPUT_DIR = os.path.join(CURRENT_DIR, r'..\data')
SC_ID = SCID.SCID_16
BANDS = [Band.BLUE_1, Band.SWIR_7]
YEAR = 2019
DAY_OF_YEAR = list(range(200, 210))
HOUR = list(range(24))
for BAND in BANDS:
FILES_BLUE = download(outputDir=OUTPUT_DIR,
scid=SC_ID,
band=BAND,
year=YEAR,
dayOfYear=DAY_OF_YEAR,
hour=HOUR)