Skip to content

Commit

Permalink
Added ordinal function for correct number endings (1st 2nd 3rd)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmemuir committed Jun 10, 2024
1 parent c8c3b0b commit 5c7980b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/coastsat/SDS_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ def retrieve_images(

# update the loading bar with the status
pbar.set_description_str(
desc=f"{inputs['sitename']}, {satname}: Loading bands for {i}th image ", refresh=True
desc=f"{inputs['sitename']}, {satname}: Loading bands for {SDS_tools.ordinal(i)} image ", refresh=True
)
# first delete dimensions key from dictionary
# otherwise the entire image is extracted (don't know why)
Expand All @@ -795,14 +795,14 @@ def retrieve_images(
# adjust polygon to match image coordinates so that there is no resampling
proj = image_ee.select("B1").projection()
pbar.set_description_str(
desc=f"{inputs['sitename']}, {satname}: adjusting polygon {i}th image ", refresh=True
desc=f"{inputs['sitename']}, {satname}: adjusting polygon {SDS_tools.ordinal(i)} image ", refresh=True
)
ee_region = adjust_polygon(
inputs["polygon"], proj, image_id=im_meta["id"], logger=logger
)
# download .tif from EE (one file with ms bands and one file with QA band)
pbar.set_description_str(
desc=f"{inputs['sitename']}, {satname}: Downloading tif for {i}th image ",
desc=f"{inputs['sitename']}, {satname}: Downloading tif for {SDS_tools.ordinal(i)} image ",
refresh=True,
)
fn_ms, fn_QA = download_tif(
Expand Down Expand Up @@ -847,7 +847,7 @@ def retrieve_images(
filepath_ms = os.path.join(fp_ms, im_fn["ms"])

pbar.set_description_str(
desc=f"{inputs['sitename']}, {satname}: Transforming {i}th image ", refresh=True
desc=f"{inputs['sitename']}, {satname}: Transforming {SDS_tools.ordinal(i)} image ", refresh=True
)
warp_image_to_target(
fn_in,
Expand Down Expand Up @@ -924,7 +924,7 @@ def retrieve_images(
proj_ms = image_ee.select("B1").projection()
proj_pan = image_ee.select("B8").projection()
pbar.set_description_str(
desc=f"{inputs['sitename']}, {satname}: adjusting polygon {i}th image ", refresh=True
desc=f"{inputs['sitename']}, {satname}: adjusting polygon {SDS_tools.ordinal(i)} image ", refresh=True
)
ee_region_ms = adjust_polygon(
inputs["polygon"],
Expand All @@ -941,7 +941,7 @@ def retrieve_images(

# download both ms and pan bands from EE
pbar.set_description_str(
desc=f"{inputs['sitename']}, {satname}: Downloading tif for {i}th image ",
desc=f"{inputs['sitename']}, {satname}: Downloading tif for {SDS_tools.ordinal(i)} image ",
refresh=True,
)
fn_ms, fn_QA = download_tif(
Expand Down Expand Up @@ -975,7 +975,7 @@ def retrieve_images(
)
# if multiple images taken at the same date add 'dupX' to the name (duplicate number X)
pbar.set_description_str(
desc=f"{inputs['sitename']}, {satname}: remove duplicates for {i}th image ",
desc=f"{inputs['sitename']}, {satname}: remove duplicates for {SDS_tools.ordinal(i)} image ",
refresh=True,
)
im_fn = handle_duplicate_image_names(
Expand All @@ -997,7 +997,7 @@ def retrieve_images(
fn_out = os.path.join(fp_ms, im_fn["ms"])
filepath_ms = os.path.join(fp_ms, im_fn["ms"])
pbar.set_description_str(
desc=f"{inputs['sitename']}, {satname}: Transforming {i}th image ", refresh=True
desc=f"{inputs['sitename']}, {satname}: Transforming {SDS_tools.ordinal(i)} image ", refresh=True
)
warp_image_to_target(
fn_in,
Expand Down Expand Up @@ -1073,7 +1073,7 @@ def retrieve_images(
proj_swir = image_ee.select("B11").projection()
proj_mask = image_ee.select("QA60").projection()
pbar.set_description_str(
desc=f"{inputs['sitename']}, {satname}: adjusting polygon {i}th image ", refresh=True
desc=f"{inputs['sitename']}, {satname}: adjusting polygon {SDS_tools.ordinal(i)} image ", refresh=True
)
ee_region_ms = adjust_polygon(
inputs["polygon"],
Expand All @@ -1095,7 +1095,7 @@ def retrieve_images(
)
# download the ms, swir and QA bands from EE
pbar.set_description_str(
desc=f"{inputs['sitename']}, {satname}: Downloading tif for {i}th image ",
desc=f"{inputs['sitename']}, {satname}: Downloading tif for {SDS_tools.ordinal(i)} image ",
refresh=True,
)
fn_ms = download_tif(
Expand Down Expand Up @@ -1140,7 +1140,7 @@ def retrieve_images(
)
# if multiple images taken at the same date add 'dupX' to the name (duplicate)
pbar.set_description_str(
desc=f"{inputs['sitename']}, {satname}: remove duplicates for {i}th image ",
desc=f"{inputs['sitename']}, {satname}: remove duplicates for {SDS_tools.ordinal(i)} image ",
refresh=True,
)
im_fn = handle_duplicate_image_names(
Expand All @@ -1161,7 +1161,7 @@ def retrieve_images(
fn_out = os.path.join(fp_swir, im_fn["swir"])
filepath_swir = os.path.join(fp_swir, im_fn["swir"])
pbar.set_description_str(
desc=f"{inputs['sitename']}, {satname}: Transforming {i}th image ", refresh=True
desc=f"{inputs['sitename']}, {satname}: Transforming {SDS_tools.ordinal(i)} image ", refresh=True
)
warp_image_to_target(
fn_in,
Expand Down
25 changes: 25 additions & 0 deletions src/coastsat/SDS_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1364,3 +1364,28 @@ def compare_timeseries(ts, gt, key, settings):
ax3.text(0, 0.98, str_stats, va="top", transform=ax3.transAxes)

return chain_sat, chain_sur, satnames, fig


def ordinal(n: int):
"""
Produces ordinal number suffix (1st 2nd 3rd) for readability when downloading imagery.
FM UofGlasgow 2024
Arguments:
-----------
n: int
number usually relating to an image in a list
Returns:
-----------
ordnum: string
original number of image but as a string with correct ordinal ending
"""
if 11 <= (n % 100) <= 13:
suffix = 'th'
else:
suffix = ['th','st','nd','rd','th'][min(n % 10, 4)]
ordnum = str(n) + suffix
return ordnum

0 comments on commit 5c7980b

Please sign in to comment.