Skip to content

Commit

Permalink
Add helpful error when attempting to download with missing system:tim…
Browse files Browse the repository at this point in the history
…e_start (#54)
  • Loading branch information
aazuspan committed Aug 27, 2022
1 parent 5fde7da commit 7c44741
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
12 changes: 12 additions & 0 deletions test/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import rasterio

import wxee
from wxee.exceptions import MissingPropertyError


@pytest.mark.ee
Expand Down Expand Up @@ -155,3 +156,14 @@ def test_to_tif():
assert src.descriptions == ("band_name",)

os.remove(file[0])


@pytest.mark.ee
def test_missing_start_time():
"""
Check that a helpful error is thrown when attempting to download an image with no
system:time_start property
"""
img = ee.Image.constant(0)
with pytest.raises(MissingPropertyError):
img.wx.to_xarray()
6 changes: 6 additions & 0 deletions wxee/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ class DownloadError(Exception):
"""Raised when a download fails."""

pass


class MissingPropertyError(Exception):
"""Raised when an Earth Engine object is missing a required property."""

pass
18 changes: 16 additions & 2 deletions wxee/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from wxee import constants
from wxee.accessors import wx_accessor
from wxee.exceptions import DownloadError
from wxee.exceptions import DownloadError, MissingPropertyError
from wxee.utils import (
_dataset_from_files,
_download_url,
Expand Down Expand Up @@ -255,7 +255,21 @@ def _get_download_id(self) -> ee.String:
the wx:dimension and wx:coordinate have not been set, they will be set to "time" and the formatted system:time_start, respectively.
"""
img = self._obj
date = _format_date(ee.Image(img).get("system:time_start"))

try:
date = _format_date(ee.Image(img).get("system:time_start")).getInfo()
except ee.EEException as e:
if "Parameter 'value' is required" in str(e):
raise MissingPropertyError(
f"Image is missing a `system:time_start` property which is required for "
"downloading.\n\nEarth Engine properties can be lost when modifying images, so make sure to manually "
"set or copy properties using the `.set` and `.copyProperties` methods after using methods like "
"`.multiply` or `.median`. If you don't need time information, you can set an arbitrary time with "
"`img = img.set('system:time_start', 0).`"
"\n\nSee https://github.com/aazuspan/wxee/issues/43 for more details."
)
else:
raise e

original_id = _replace_if_null(img.get("system:id"), "null")
# Replace any invalid file path characters with underscores.
Expand Down

0 comments on commit 7c44741

Please sign in to comment.