Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrates fetcher with byTileAop #85

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions neonwranglerpy/utilities/byTileAOP.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from neonwranglerpy.utilities.tools import get_api
from neonwranglerpy.utilities.defaults import NEON_API_BASE_URL
from neonwranglerpy.utilities.get_tile_urls import get_tile_urls

import neonwranglerpy.fetcher.fetcher as fetcher

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

for i in range(len(file_urls)):
split_path = file_urls[i]['url'].split('/')
dir_path = '/'.join(split_path[4:len(split_path) - 1])
save_dir_path = os.path.join(savepath, dir_path)
save_file_path = os.path.join(save_dir_path, file_urls[i]['name'])
if not os.path.exists(save_dir_path):
os.makedirs(save_dir_path)

try:
file_path, _ = urlretrieve(file_urls[i]['url'], save_file_path)
except HTTPError as e:
print("HTTPError :", e)
return None
files_to_stack_path = os.path.join(savepath, "filesToStack")
if not os.path.isdir(files_to_stack_path):
os.mkdir(files_to_stack_path)

if files_to_stack_path:
fetcher.run_threaded_batches(file_urls, 'aop', rate_limit=2, headers=None, savepath=files_to_stack_path)
return savepath
27 changes: 18 additions & 9 deletions neonwranglerpy/utilities/get_tile_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,29 @@ def get_tile_urls(
"""Get tile urls."""
file_urls = []
for i in range(len(month_url)):
temp = get_api(month_url[i]).json()
temp = get_api(month_url[i]).json()['data']

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

temp_ = temp['data']['files']
# temp_ = json.dumps(temp['data']['files'])
# df = pd.read_json(temp_)
# # get the files for easting and northing

# if easting is a signle value and northing is a single value
temp_ = temp['files']
dataSiteMonth = {
"data": {
"productCode": temp['productCode'],
"siteCode": temp['siteCode'],
"month": temp['month'],
"release": temp['release'],
"packages": temp['packages'],
"files": []
}
}

if isinstance(easting.astype(str), str) and isinstance(northing.astype(str), str):
file_urls = [x for x in temp_ if f'_{easting}_{northing}' in x['name']]
dataSiteMonth['data']['files'] = [x for x in temp_ if f'_{easting}_{northing}' in x['name']]
file_urls.append(dataSiteMonth)

elif isinstance(easting, np.ndarray) and isinstance(northing, np.ndarray):
for j in range(len(easting)):
urls = [
Expand All @@ -44,6 +51,8 @@ def get_tile_urls(

if not len(urls):
print(f"no tiles found for {easting[j]} and {northing[j]}")
file_urls.extend(urls)
dataSiteMonth['data']['files'].append(urls)
file_urls.append(dataSiteMonth)

print(f'{len(file_urls)} files found')
return file_urls