Skip to content

Commit 65a4df8

Browse files
authored
Merge pull request #145 from descarteslabs/feat/derived-bands
Endpoints for derived bands
2 parents f5c08a8 + 6ccdbc6 commit 65a4df8

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

descarteslabs/services/metadata.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ def sources(self):
5858
r = self.session.get('/sources')
5959
return r.json()
6060

61-
def bands(self, limit=None, products=None, offset=None, wavelength=None, resolution=None, tags=None):
62-
"""Seach for imagery data bands that you have access to.
61+
def bands(self, products=None, limit=None, offset=None, wavelength=None, resolution=None, tags=None):
62+
"""Search for imagery data bands that you have access to.
6363
64+
:param list(str) products: A list of product(s) to return bands for.
6465
:param int limit: Number of results to return.
6566
:param int offset: Index to start at when returning results.
6667
:param float wavelenth: A wavelength in nm e.g 700 that the band sensor must measure.
@@ -81,6 +82,26 @@ def bands(self, limit=None, products=None, offset=None, wavelength=None, resolut
8182
r = self.session.post('/bands/search', json=kwargs)
8283
return r.json()
8384

85+
def derived_bands(self, bands=None, limit=None, offset=None):
86+
"""Search for predefined derived bands that you have access to.
87+
88+
:param list(str) bands: A list of source bands that must be part of
89+
the derived band i.e ["nir"]
90+
:param int limit: Number of results to return.
91+
:param int offset: Index to start at when returning results.
92+
"""
93+
params = ['bands', 'limit', 'offset']
94+
95+
args = locals()
96+
kwargs = {
97+
param: args[param]
98+
for param in params
99+
if args[param] is not None
100+
}
101+
102+
r = self.session.post('/bands/derived/search', json=kwargs)
103+
return r.json()
104+
84105
def products(self, bands=None, limit=None, offset=None):
85106
"""Search products that are available on the platform.
86107
@@ -118,6 +139,17 @@ def available_products(self):
118139

119140
return r.json()
120141

142+
def translate(self, const_id):
143+
"""Translate a deprecated constellation identifier
144+
into a new-style product identifier.
145+
146+
:param string const_id: The constellation identifier to translate.
147+
"""
148+
149+
r = self.session.get('/products/translate/{}'.format(const_id))
150+
151+
return r.json()
152+
121153
def summary(self, products=None, const_id=None, sat_id=None, date='acquired', part=None,
122154
place=None, geom=None, start_time=None, end_time=None, cloud_fraction=None,
123155
cloud_fraction_0=None, fill_fraction=None, pixels=None, params=None,
@@ -513,3 +545,12 @@ def get_band(self, band_id):
513545
"""
514546
r = self.session.get('/bands/%s' % band_id)
515547
return r.json()
548+
549+
def get_derived_band(self, derived_band_id):
550+
"""Get information about a single product.
551+
552+
:param str derived_band_id: Derived band identifier.
553+
554+
"""
555+
r = self.session.get('/bands/derived/%s' % derived_band_id)
556+
return r.json()

descarteslabs/tests/test_metadata.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ def test_cloud_fraction(self):
6161
for feature in r['features']:
6262
self.assertEqual(feature['properties']['cloud_fraction'], 0.0)
6363

64+
def translate_to_product(self):
65+
r = self.instance.translate('l8')
66+
self.assertEqual(r['product'], 'landsat:LC08:PRE:TOAR')
67+
6468
def test_search_by_product(self):
6569
r = self.instance.search(start_time='2016-07-06', end_time='2016-07-07', products=['landsat:LC08:PRE:TOAR'])
6670
self.assertGreater(len(r['features']), 0)
@@ -178,11 +182,6 @@ def test_search_products(self):
178182
r = self.instance.products(limit=1)
179183
self.assertEqual(len(r), 1)
180184

181-
def test_search_bands(self):
182-
r = self.instance.bands()
183-
# no bands are publicly available.
184-
self.assertEqual(len(r), 0)
185-
186185
def test_products_get(self):
187186
product_id = 'landsat:LC08:PRE:TOAR'
188187
r = self.instance.get_product(product_id)
@@ -191,10 +190,23 @@ def test_products_get(self):
191190
def test_bands_get(self):
192191
band_id = 'landsat:LC08:PRE:TOAR:red'
193192
try:
194-
self.instance.get_product(band_id)
193+
band = self.instance.get_band(band_id)
194+
self.assertEqual(band_id, band['id'])
195195
except NotFoundError:
196196
pass
197197

198+
def test_derived_bands_get(self):
199+
band_id = 'derived:ndvi'
200+
try:
201+
d_band = self.instance.get_derived_band(band_id)
202+
self.assertIn('bands', d_band)
203+
except NotFoundError:
204+
pass
205+
206+
def test_derived_bands_search(self):
207+
bands = ['red', 'nir']
208+
bands = self.instance.derived_bands(bands=bands)
209+
198210

199211
if __name__ == '__main__':
200212
unittest.main()

0 commit comments

Comments
 (0)