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

ENH: Allow dict of TIF files #25

Closed
szwiep opened this issue Nov 23, 2023 · 1 comment · Fixed by #130
Closed

ENH: Allow dict of TIF files #25

szwiep opened this issue Nov 23, 2023 · 1 comment · Fixed by #130
Assignees

Comments

@szwiep
Copy link
Contributor

szwiep commented Nov 23, 2023

Issue

When passing images to spectral_recovery.io.read_and_stack_tifs, each image must be named after the year the image was captured/derived, e.g "2022.tif", "2023.tif". This is true whether the user passes a list or a directory to the path_to_tifs= parameter. If a user's image names do not match the convention they are forced to rename all of their images before using the tool.

Context

This convention was used because there needed to be a way to definitively map an image to it's year/time coordinate in the DataArray structure that read_and_stack_tifs produces. See the involved code below:

image_dict = {}
if isinstance(path_to_tifs, str):
    # check if path is a directory
    if Path(path_to_tifs).is_dir():
        path_to_tifs = list(Path(path_to_tifs).glob("*.tif"))
for file in path_to_tifs:
    with rioxarray.open_rasterio(Path(file), chunks="auto") as data:
        image_dict[Path(file).stem] = data

time_keys = []
for filename in image_dict.keys():
    if _str_is_year(filename):
        time_keys.append(pd.to_datetime(filename))
    else:
        raise ValueError(
            f"TIF filenames must be in format 'YYYY' but received: '{filename}'"
        ) from None

stacked_data = _stack_bands(image_dict.values(), time_keys, dim_name="time")

The current workflow of mapping files to time, generally is:

  1. Find all TIF files in a directory or read all files passed in the list
  2. Parse years from filenames
  3. For each image, convert year filenames to datetime objects and assign as image's time coordinate

Proposed Solution

Some more thought/discussion is needed on how/if this naming convention should be loosened.

@szwiep
Copy link
Contributor Author

szwiep commented Apr 18, 2024

In the current version (0.4.1), I think the easiest way to finally address this is to allow users to to pass a dictionary mapping years to filenames. This should be in addition to being able to pass a path to a dir so to maintain backwards compatibility.

@szwiep szwiep changed the title Allow less restrictive file naming ENH: Allow dict of TIF files Apr 18, 2024
@szwiep szwiep linked a pull request May 24, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants