Skip to content

Commit b7f3155

Browse files
committed
Integrates fetcher with byTileAop
Signed-off-by: nagesh bansal <[email protected]>
1 parent 369f430 commit b7f3155

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

neonwranglerpy/utilities/byTileAOP.py

+6-14
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from neonwranglerpy.utilities.tools import get_api
1212
from neonwranglerpy.utilities.defaults import NEON_API_BASE_URL
1313
from neonwranglerpy.utilities.get_tile_urls import get_tile_urls
14-
14+
import neonwranglerpy.fetcher.fetcher as fetcher
1515

1616
def load_shared_flights():
1717
"""Return the dataframe about the table types of Data Products."""
@@ -134,18 +134,10 @@ def by_tile_aop(dpID, site, year, easting, northing, buffer=0, savepath=None):
134134
if not os.path.isdir(savepath):
135135
os.makedirs(savepath)
136136

137-
for i in range(len(file_urls)):
138-
split_path = file_urls[i]['url'].split('/')
139-
dir_path = '/'.join(split_path[4:len(split_path) - 1])
140-
save_dir_path = os.path.join(savepath, dir_path)
141-
save_file_path = os.path.join(save_dir_path, file_urls[i]['name'])
142-
if not os.path.exists(save_dir_path):
143-
os.makedirs(save_dir_path)
144-
145-
try:
146-
file_path, _ = urlretrieve(file_urls[i]['url'], save_file_path)
147-
except HTTPError as e:
148-
print("HTTPError :", e)
149-
return None
137+
files_to_stack_path = os.path.join(savepath, "filesToStack")
138+
if not os.path.isdir(files_to_stack_path):
139+
os.mkdir(files_to_stack_path)
150140

141+
if files_to_stack_path:
142+
fetcher.run_threaded_batches(file_urls, 'aop', rate_limit=2, headers=None, savepath=files_to_stack_path)
151143
return savepath

neonwranglerpy/utilities/get_tile_urls.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,29 @@ def get_tile_urls(
1212
"""Get tile urls."""
1313
file_urls = []
1414
for i in range(len(month_url)):
15-
temp = get_api(month_url[i]).json()
15+
temp = get_api(month_url[i]).json()['data']
1616

1717
if not len(temp):
1818
print(f"No files found for site {temp['data']['siteCode']} and "
1919
f"year {temp['data']['month']}.")
2020
continue
2121

22-
temp_ = temp['data']['files']
23-
# temp_ = json.dumps(temp['data']['files'])
24-
# df = pd.read_json(temp_)
25-
# # get the files for easting and northing
26-
27-
# if easting is a signle value and northing is a single value
22+
temp_ = temp['files']
23+
dataSiteMonth = {
24+
"data": {
25+
"productCode": temp['productCode'],
26+
"siteCode": temp['siteCode'],
27+
"month": temp['month'],
28+
"release": temp['release'],
29+
"packages": temp['packages'],
30+
"files": []
31+
}
32+
}
2833

2934
if isinstance(easting.astype(str), str) and isinstance(northing.astype(str), str):
30-
file_urls = [x for x in temp_ if f'_{easting}_{northing}' in x['name']]
35+
dataSiteMonth['data']['files'] = [x for x in temp_ if f'_{easting}_{northing}' in x['name']]
36+
file_urls.append(dataSiteMonth)
37+
3138
elif isinstance(easting, np.ndarray) and isinstance(northing, np.ndarray):
3239
for j in range(len(easting)):
3340
urls = [
@@ -44,6 +51,8 @@ def get_tile_urls(
4451

4552
if not len(urls):
4653
print(f"no tiles found for {easting[j]} and {northing[j]}")
47-
file_urls.extend(urls)
54+
dataSiteMonth['data']['files'].append(urls)
55+
file_urls.append(dataSiteMonth)
56+
4857
print(f'{len(file_urls)} files found')
4958
return file_urls

0 commit comments

Comments
 (0)