Skip to content

Commit

Permalink
fixed report not showing downloaded files
Browse files Browse the repository at this point in the history
  • Loading branch information
2320sharon committed Nov 13, 2023
1 parent a2cbbe3 commit 3d4c654
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/coastsat/SDS_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from typing import List, Dict, Union, Tuple
import time
from functools import wraps
import traceback

# earth engine module
import ee
Expand Down Expand Up @@ -55,11 +56,11 @@ def setup_logger(folder, base_filename="download_report"):

# Create a custom logger
logger = logging.getLogger("satellite_download_logger")
logger.setLevel(logging.ERROR) # Log errors and above
logger.setLevel(logging.INFO) # Log errors and above

# Create handlers
file_handler = logging.FileHandler(log_filepath)
file_handler.setLevel(logging.ERROR)
file_handler.setLevel(logging.INFO)

# Create formatters and add it to handlers
log_format = logging.Formatter(
Expand Down Expand Up @@ -128,7 +129,7 @@ def wrapper(*args, **kwargs):
except ee.ee_exception.EEException as e:
try:
interpret_ee_exception(e)
except RequestSizeExceededError as request_limit_exceeded:
except RequestSizeExceededError:
# Handle request size exceeded error specifically
raise RequestSizeExceededError(
f"{image_id} download failed to download due request size exceeding maximum limit likely due to file corruption."
Expand All @@ -144,7 +145,7 @@ def wrapper(*args, **kwargs):


@retry
def remove_dimensions_from_bands(image_ee):
def remove_dimensions_from_bands(image_ee, **kwargs):
# first delete dimensions key from dictionary
# otherwise the entire image is extracted (don't know why)
try:
Expand All @@ -153,8 +154,10 @@ def remove_dimensions_from_bands(image_ee):
del im_bands[j]["dimensions"]
return im_bands
except Exception as e:
print(f"An error occurred: {e}")
raise
print(
f"An error occurred when attempting to edit bands for image {kwargs.get('image_id','')}: {e}"
)
raise e


def get_image_quality(satname: str, im_meta: dict) -> str:
Expand Down Expand Up @@ -401,9 +404,6 @@ def retrieve_images(
suffix = ".tif"
count = 1
num_satellites = len(im_dict_T1.keys())
# total allowed errors throught download process
MAX_ALLOWED_ERRORS = 10
error_counter = 0
for satname in tqdm(
im_dict_T1.keys(), desc=f"Downloading Imagery for {num_satellites} satellites"
):
Expand All @@ -413,8 +413,6 @@ def retrieve_images(
# initialise variables and loop through images
bands_id = bands_dict[satname]
all_names = [] # list for detecting duplicates
# reset the error counter for each satellite
error_counter = 0
# loop through each image
for i in tqdm(
range(len(im_dict_T1[satname])),
Expand Down Expand Up @@ -462,7 +460,10 @@ def retrieve_images(

# first delete dimensions key from dictionary
# otherwise the entire image is extracted (don't know why)
im_bands = remove_dimensions_from_bands(image_ee)
im_bands = remove_dimensions_from_bands(
image_ee,
image_id=im_meta["id"],
)

# =============================================================================================#
# Landsat 5 download
Expand Down Expand Up @@ -539,8 +540,8 @@ def retrieve_images(
)

# delete original downloads
for _ in [fn_ms, fn_QA]:
os.remove(_)
for original_file in [fn_ms, fn_QA]:
os.remove(original_file)
if save_jpg:
tif_paths = SDS_tools.get_filepath(inputs, satname)
SDS_preprocess.save_single_jpg(
Expand Down Expand Up @@ -675,6 +676,7 @@ def retrieve_images(
collection=inputs["landsat_collection"],
apply_cloud_mask=apply_cloud_mask,
)

# =============================================================================================#
# Sentinel-2 download
# =============================================================================================#
Expand Down Expand Up @@ -793,7 +795,7 @@ def retrieve_images(
)
except Exception as error:
logger.error(
f"The download for satellite {satname} {im_meta.get('id','unknown')} failed due to \n {error}"
f"The download for satellite {satname} {im_meta.get('id','unknown')} failed due to \n {error} \n Traceback {traceback.format_exc()}"
)
print(
f"The download for satellite {satname} {im_meta.get('id','unknown')} failed due to \n {error}"
Expand Down Expand Up @@ -834,6 +836,7 @@ def retrieve_images(
f"Successfully downloaded image id {im_meta.get('id','unknown')} as {im_fn.get('ms')}"
)
except Exception as e:
print(traceback.format_exc())
logger.error(
f"Could not save metasdata for {im_meta.get('id','unknown')} that failed.\n{e}"
)
Expand Down

0 comments on commit 3d4c654

Please sign in to comment.