Skip to content

Commit

Permalink
fix: add a format parameter to addDate method (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
12rambau committed Mar 23, 2024
2 parents 054b045 + 619a9c5 commit 26ccfe8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
20 changes: 16 additions & 4 deletions geetools/Image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ def __init__(self, obj: ee.Image):
self._obj = obj

# -- band manipulation -----------------------------------------------------
def addDate(self) -> ee.Image:
def addDate(self, format: ee_str = "") -> ee.Image:
"""Add a band with the date of the image in the provided format.
The date is stored as a Timestamp in millisecond in a band "date".
If no format is provided, the date is stored as a Timestamp in millisecond in a band "date". If format band is provided, the date is store in a int8 band with the date in the provided format. This format needs to be a string that can be converted to a number.
If not an error will be thrown.
Args:
format: A date pattern, as described at http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html
Returns:
The image with the date band added.
Expand All @@ -51,8 +55,16 @@ def addDate(self) -> ee.Image:
value = date.reduceRegion(ee.Reducer.first(), buffer, 10).get("date")
ee.Date(value).format('YYYY-MM-dd').getInfo()
"""
date = self._obj.date().millis()
return self._obj.addBands(ee.Image.constant(date).rename("date"))
# parse the inputs
isMillis = ee.String(format).equals(ee.String(""))
format = ee.String(format) if format else ee.String("YYYYMMdd")

# extract the date from the object and create a image band from it
date = self._obj.date()
date = ee.Algorithms.If(isMillis, date.millis(), ee.Number.parse(date.format(format)))
dateBand = ee.Image.constant(ee.Number(date)).rename("date")

return self._obj.addBands(dateBand)

def addSuffix(self, suffix: ee_str, bands: ee_list = []) -> ee.Image:
"""Add a suffix to the image selected band.
Expand Down
5 changes: 5 additions & 0 deletions tests/test_Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ def test_add_date(self, s2_sr_vatican_2020, vatican_buffer, num_regression):
values = image.reduceRegion(ee.Reducer.first(), vatican_buffer, 10)
num_regression.check({k: np.nan if v is None else v for k, v in values.getInfo().items()})

def test_add_date_format(self, s2_sr_vatican_2020, vatican_buffer, num_regression):
image = s2_sr_vatican_2020.geetools.addDate("yyyyMMdd")
values = image.reduceRegion(ee.Reducer.first(), vatican_buffer, 10)
num_regression.check({k: np.nan if v is None else v for k, v in values.getInfo().items()})

def test_deprecated_make_date_band(self, s2_sr_vatican_2020, vatican_buffer, num_regression):
with pytest.deprecated_call():
image = geetools.tools.date.makeDateBand(s2_sr_vatican_2020)
Expand Down
2 changes: 2 additions & 0 deletions tests/test_Image/test_add_date_format.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
,AOT,B1,B11,B12,B2,B3,B4,B5,B6,B7,B8,B8A,B9,MSK_CLDPRB,MSK_SNWPRB,QA10,QA20,QA60,SCL,TCI_B,TCI_G,TCI_R,WVP,date
0,72,214,1175,958,188,366,285,910,1232,1328,1216,1431,2104,2,0,0,0,0,5,21,38,29,588,20200101
10 changes: 10 additions & 0 deletions tests/test_Image/test_get_stac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ links:
- href: https://developers.google.com/earth-engine/datasets/catalog/COPERNICUS_S2_SR_HARMONIZED#terms-of-use
rel: license
type: text/html
- href: https://storage.googleapis.com/earthengine-stac/catalog/COPERNICUS/COPERNICUS_S2_SR_HARMONIZED.json
rel: latest-version
title: COPERNICUS/S2_SR_HARMONIZED
type: application/json
- href: https://storage.googleapis.com/earthengine-stac/catalog/COPERNICUS/COPERNICUS_S2_SR.json
rel: predecessor-version
title: COPERNICUS/S2_SR
type: application/json
providers:
- name: European Union/ESA/Copernicus
roles:
Expand All @@ -121,6 +129,7 @@ providers:
url: https://developers.google.com/earth-engine/datasets/catalog/COPERNICUS_S2_SR_HARMONIZED
stac_extensions:
- https://stac-extensions.github.io/eo/v1.0.0/schema.json
- https://stac-extensions.github.io/version/v1.0.0/schema.json
stac_version: 1.0.0
summaries:
MSK_CLDPRB:
Expand Down Expand Up @@ -615,3 +624,4 @@ summaries:
- Sentinel-2B
title: 'Harmonized Sentinel-2 MSI: MultiSpectral Instrument, Level-2A'
type: Collection
version: harmonized

0 comments on commit 26ccfe8

Please sign in to comment.