You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sdap.apache.org by nc...@apache.org on 2018/03/24 03:36:55 UTC

[incubator-sdap-nexus] branch master updated: SDAP-47 Update NEXUS CLI to support datainbounds algorithm (#12)

This is an automated email from the ASF dual-hosted git repository.

nchung pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-sdap-nexus.git


The following commit(s) were added to refs/heads/master by this push:
     new 263e118  SDAP-47 Update NEXUS CLI to support datainbounds algorithm (#12)
263e118 is described below

commit 263e1184687d812dd365e0e3bba5c391cc5e0629
Author: Nga Quach <ng...@gmail.com>
AuthorDate: Fri Mar 23 20:36:54 2018 -0700

    SDAP-47 Update NEXUS CLI to support datainbounds algorithm (#12)
    
    * SDAP-47 Update NEXUS CLI to support datainbounds algorithm
    
    * SDAP-47 Changed method name to subset, allow only bounding_box or metadata_filter, and return list of Point.
    
    * SDAP-47 Changed method name to subset.
    
    * SDAP-47 Updated description of parameter.
---
 client/nexuscli/__init__.py           |  1 +
 client/nexuscli/nexuscli.py           | 63 +++++++++++++++++++++++++++++++++++
 client/nexuscli/test/nexuscli_test.py | 10 ++++++
 3 files changed, 74 insertions(+)

diff --git a/client/nexuscli/__init__.py b/client/nexuscli/__init__.py
index d6deec9..250b474 100644
--- a/client/nexuscli/__init__.py
+++ b/client/nexuscli/__init__.py
@@ -18,3 +18,4 @@ from nexuscli.nexuscli import set_target
 from nexuscli.nexuscli import time_series
 from nexuscli.nexuscli import dataset_list
 from nexuscli.nexuscli import daily_difference_average
+from nexuscli.nexuscli import subset
diff --git a/client/nexuscli/nexuscli.py b/client/nexuscli/nexuscli.py
index 9bf51e6..b542dba 100644
--- a/client/nexuscli/nexuscli.py
+++ b/client/nexuscli/nexuscli.py
@@ -43,6 +43,15 @@ __pdoc__['TimeSeries.count'] = "`numpy` array containing counts"
 __pdoc__['TimeSeries.minimum'] = "`numpy` array containing minimums"
 __pdoc__['TimeSeries.maximum'] = "`numpy` array containing maximums"
 
+Point = namedtuple('Point', ('time', 'latitude', 'longitude', 'variable'))
+Point.__doc__ = '''\
+An object containing Point attributes.
+'''
+__pdoc__['Point.time'] = "time value as `datetime` object"
+__pdoc__['Point.latitude'] = "latitude value"
+__pdoc__['Point.longitude'] = "longitude value"
+__pdoc__['Point.variable'] = "dictionary of variable values"
+
 ISO_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
 
 target = 'http://localhost:8083'
@@ -207,3 +216,57 @@ def time_series(datasets, bounding_box, start_datetime, end_datetime, spark=Fals
             )
 
     return time_series_result
+
+
+def subset(dataset, bounding_box, start_datetime, end_datetime, parameter, metadata_filter):
+    """
+    Fetches point values for a given dataset and geographical area or metadata criteria and time range.
+
+    __dataset__ Name of the dataset as a String  
+    __bounding_box__ Bounding box for area of interest as a `shapely.geometry.polygon.Polygon`  
+    __start_datetime__ Start time as a `datetime.datetime`  
+    __end_datetime__ End time as a `datetime.datetime`  
+    __parameter__ The parameter of interest. One of 'sst', 'sss', 'wind' or None  
+    __metadata_filter__ List of key:value String metadata criteria  
+
+    __return__ List of `nexuscli.nexuscli.Point` namedtuples
+    """
+    url = "{}/datainbounds?".format(target)
+
+    params = {
+        'ds': dataset,
+        'startTime': start_datetime.strftime(ISO_FORMAT),
+        'endTime': end_datetime.strftime(ISO_FORMAT),
+        'parameter': parameter,
+    }
+    if bounding_box:
+        params['b'] = ','.join(str(b) for b in bounding_box.bounds)
+    else:
+        if metadata_filter and len(metadata_filter) > 0:
+            params['metadataFilter'] = metadata_filter
+
+    response = session.get(url, params=params)
+    response.raise_for_status()
+    response = response.json()
+
+    data = np.array(response['data']).flatten()
+
+    assert len(data) > 0, "No data found in {} between {} and {} for Datasets {}.".format(bounding_box.wkt if bounding_box is not None else metadata_filter,
+                                                                                          start_datetime.strftime(
+                                                                                              ISO_FORMAT),
+                                                                                          end_datetime.strftime(
+                                                                                              ISO_FORMAT),
+                                                                                          dataset)
+
+    subset_result = []
+    for d in data:
+        subset_result.append(
+            Point(
+                time=datetime.utcfromtimestamp(d['time']).replace(tzinfo=UTC),
+                longitude=d['longitude'],
+                latitude=d['latitude'],
+                variable=d['data'][0]
+            )
+        )
+
+    return subset_result
diff --git a/client/nexuscli/test/nexuscli_test.py b/client/nexuscli/test/nexuscli_test.py
index d202a3f..61a7e46 100644
--- a/client/nexuscli/test/nexuscli_test.py
+++ b/client/nexuscli/test/nexuscli_test.py
@@ -39,3 +39,13 @@ class TestCli(unittest.TestCase):
                                                datetime(2013, 1, 1), datetime(2014, 12, 31))
 
         self.assertEqual(1, len(ts))
+
+    def test_data_in_bounds_with_metadata_filter(self):
+        subset = nexuscli.subset("MUR-JPL-L4-GLOB-v4.1", None, datetime(2018, 1, 1), datetime(2018, 1, 2),
+                                         None, ["id:60758e00-5721-3a6e-bf57-78448bb0aeeb"])
+        print(subset)
+
+    def test_data_in_bounds_with_bounding_box(self):
+        subset = nexuscli.subset("MUR-JPL-L4-GLOB-v4.1", box(-150, 45, -149, 46), datetime(2018, 1, 1),
+                                         datetime(2018, 1, 1), None, None)
+        print(subset)

-- 
To stop receiving notification emails like this one, please contact
nchung@apache.org.