In [1]:
# Analytics Expression Language Example

import xarray as xr
from datetime import datetime
from datacube.api import API
from datacube.ndexpr import NDexpr

In [2]:
# Instantiating API and NDexpr

g = API()
nd = NDexpr()


/home/rc/miniconda/lib/python2.7/site-packages/datacube-1.0.3+70.gc24cf61.dirty-py2.7.egg/datacube/api/_api.py:64: UserWarning: No application name set during API initialization
  warnings.warn('No application name set during API initialization')

In [3]:
# construct data request parameters for band_30 and band_40
data_request_descriptor = {
    'platform': 'LANDSAT_5',
    'product': 'nbar',
    'variables': ('red', 'nir'),
    'dimensions': {
        'x': {
            'range': (149.07, 149.18)
        },
        'y': {
            'range': (-35.32, -35.28)
        },
        'time': {
            'range': (datetime(1990, 1, 1), datetime(1990, 12, 31))
        }
    }
}

# Retrieving data from API
d1 = g.get_data(data_request_descriptor)

# construct data request parameters for PQ
pq_request_descriptor = {
    'platform': 'LANDSAT_5',
    'product': 'pqa',
    'variables': ('pixelquality'),
    'dimensions': {
        'x': {
            'range': (149.07, 149.18)
        },
        'y': {
            'range': (-35.32, -35.28)
        },
        'time': {
            'range': (datetime(1990, 1, 1), datetime(1990, 12, 31))
        }
    }
}

# Retrieving data from API
d2 = g.get_data(pq_request_descriptor)

In [4]:
# The following 3 lines shouldn't be done like this
# Currently done like this for the sake of the example.

b30 = d1['arrays']['red']
b40 = d1['arrays']['nir']
pq = d2['arrays']['pixelquality']

In [5]:
# NDexpr demo begins here

# perform ndvi as expressed in this language.
ndvi = nd.evaluate('((b40 - b30) / (b40 + b30))')

# perform mask on ndvi as expressed in this language.
masked_ndvi = nd.evaluate('ndvi{(pq == 32767) | (pq == 16383) | (pq == 2457)}')

In [6]:
print(ndvi)


<xarray.DataArray (time: 12, y: 231, x: 420)>
dask.array<elemwis..., shape=(12, 231, 420), dtype=float64, chunksize=(1, 231, 420)>
Coordinates:
  * time     (time) datetime64[ns] 1990-03-02T23:11:16 1990-05-05T23:10:28 ...
  * y        (y) float64 -3.957e+06 -3.957e+06 -3.957e+06 -3.957e+06 ...
  * x        (x) float64 1.545e+06 1.545e+06 1.545e+06 1.545e+06 1.545e+06 ...

In [7]:
print(masked_ndvi)


<xarray.DataArray (time: 12, y: 231, x: 420)>
dask.array<elemwis..., shape=(12, 231, 420), dtype=float64, chunksize=(1, 231, 420)>
Coordinates:
  * time     (time) datetime64[ns] 1990-03-02T23:11:16 1990-05-05T23:10:28 ...
  * y        (y) float64 -3.957e+06 -3.957e+06 -3.957e+06 -3.957e+06 ...
  * x        (x) float64 1.545e+06 1.545e+06 1.545e+06 1.545e+06 1.545e+06 ...

In [8]:
# currently dimensions are integer indices, later will be labels when
# Data Access API Interface has been finalised.

reduction_on_dim0 = nd.evaluate('median(masked_ndvi, 0)')
print(reduction_on_dim0)


<xarray.DataArray (y: 231, x: 420)>
array([[ 0.73212527,  0.73539463,  0.72473605, ...,  0.59518451,
         0.60830706,  0.58306872],
       [ 0.7266308 ,  0.73954829,  0.76156652, ...,  0.61830349,
         0.60506634,  0.60726634],
       [ 0.73212527,  0.7534574 ,  0.75202868, ...,  0.62756548,
         0.63782567,  0.62214765],
       ..., 
       [ 0.52543601,  0.5277012 ,  0.45971818, ...,  0.32512965,
         0.33821287,  0.41319136],
       [ 0.53044136,  0.54308146,  0.5111048 , ...,  0.42529602,
         0.50864267,  0.46456217],
       [ 0.55190161,  0.54160832,  0.54160832, ...,  0.54872513,
         0.55118373,  0.48095632]])
Coordinates:
  * y        (y) float64 -3.957e+06 -3.957e+06 -3.957e+06 -3.957e+06 ...
  * x        (x) float64 1.545e+06 1.545e+06 1.545e+06 1.545e+06 1.545e+06 ...

In [9]:
reduction_on_dim01 = nd.evaluate('median(masked_ndvi, 0, 1)')
print(reduction_on_dim01)


<xarray.DataArray (x: 420)>
array([ 0.64716312,  0.63130141,  0.61868247,  0.61391542,  0.60340276,
        0.59467849,  0.60356416,  0.61881766,  0.62484235,  0.62029204,
        0.62696296,  0.61071061,  0.60028249,  0.58451505,  0.58392989,
        0.59976798,  0.59895753,  0.58481675,  0.5597446 ,  0.56663276,
        0.56349213,  0.5696635 ,  0.57903731,  0.60062894,  0.59716351,
        0.60839161,  0.61572052,  0.61572945,  0.60070671,  0.62942797,
        0.63959854,  0.64738598,  0.6447136 ,  0.63424125,  0.6388767 ,
        0.6495583 ,  0.63754395,  0.62567288,  0.60625   ,  0.59943708,
        0.60211268,  0.5792613 ,  0.56115021,  0.55319901,  0.55046564,
        0.56461676,  0.58592891,  0.59024691,  0.60006317,  0.60408163,
        0.59771542,  0.59215686,  0.58400533,  0.57438273,  0.56755374,
        0.56815044,  0.57192363,  0.58173665,  0.5995    ,  0.59690031,
        0.5877974 ,  0.5800232 ,  0.57953568,  0.58183242,  0.57293002,
        0.56092699,  0.5534309 ,  0.55106541,  0.54068027,  0.54573765,
        0.56598392,  0.55711289,  0.55015353,  0.55052265,  0.55237187,
        0.55528195,  0.54484415,  0.532831  ,  0.52224545,  0.50275739,
        0.50289752,  0.49083769,  0.50091743,  0.54097497,  0.56345886,
        0.57426836,  0.57289528,  0.55979705,  0.5446898 ,  0.54381271,
        0.54380841,  0.537577  ,  0.54056902,  0.54695218,  0.56050339,
        0.5786034 ,  0.56942699,  0.57164404,  0.57236992,  0.58119483,
        0.58683927,  0.58144031,  0.56495222,  0.56258657,  0.5545761 ,
        0.54836032,  0.54567157,  0.54101419,  0.52129826,  0.5209286 ,
        0.52170062,  0.51807943,  0.51524208,  0.51232352,  0.51231306,
        0.52056239,  0.52139274,  0.5216196 ,  0.52206545,  0.53163686,
        0.52824579,  0.51158301,  0.50954774,  0.5198765 ,  0.52043597,
        0.53378649,  0.53266996,  0.53168942,  0.55102041,  0.54919236,
        0.53250672,  0.52972359,  0.53322658,  0.52563689,  0.51656426,
        0.50839965,  0.50149155,  0.49312377,  0.48547718,  0.47518276,
        0.48665049,  0.49213829,  0.49802251,  0.5153185 ,  0.52056239,
        0.51597503,  0.51197987,  0.5146147 ,  0.51743028,  0.52623173,
        0.53163686,  0.53127247,  0.52508361,  0.52914022,  0.52873563,
        0.53138075,  0.52984798,  0.52469396,  0.53299492,  0.53427967,
        0.53134153,  0.53045608,  0.53124083,  0.53198199,  0.53142451,
        0.52868716,  0.52751596,  0.52503682,  0.54022754,  0.53963752,
        0.53228457,  0.52390589,  0.51895999,  0.52982456,  0.52531663,
        0.52499933,  0.53063498,  0.53583199,  0.5519893 ,  0.53715542,
        0.51966122,  0.5212156 ,  0.51642285,  0.51660517,  0.51793655,
        0.51826484,  0.5416936 ,  0.53145689,  0.51415168,  0.52459467,
        0.50840955,  0.50395778,  0.51839179,  0.53116102,  0.5298826 ,
        0.50453985,  0.49242612,  0.47327502,  0.4667115 ,  0.47486772,
        0.47997497,  0.48341745,  0.48172222,  0.47122078,  0.45884225,
        0.44631386,  0.42475172,  0.42110176,  0.43501611,  0.42806418,
        0.44185788,  0.434238  ,  0.4536206 ,  0.46677698,  0.45930925,
        0.4550723 ,  0.45623632,  0.4345791 ,  0.43142067,  0.42302853,
        0.42801121,  0.41962422,  0.44075054,  0.44690759,  0.41405082,
        0.4182135 ,  0.43485136,  0.45052131,  0.44150184,  0.43855435,
        0.44452537,  0.45646357,  0.44329229,  0.42287695,  0.41632852,
        0.44169847,  0.45090294,  0.4591594 ,  0.44799376,  0.42669969,
        0.43229463,  0.41385768,  0.39332948,  0.38927419,  0.35374431,
        0.32616131,  0.32741407,  0.30829651,  0.29519871,  0.29695493,
        0.32944083,  0.33668045,  0.35165487,  0.38625239,  0.38733706,
        0.38017899,  0.39831105,  0.40587219,  0.40977981,  0.41466128,
        0.40884279,  0.3881726 ,  0.39249504,  0.37025316,  0.35394827,
        0.33747064,  0.30133998,  0.29514172,  0.32187209,  0.35094851,
        0.37355177,  0.37759336,  0.39525521,  0.36336279,  0.33257666,
        0.32554434,  0.37358715,  0.38643285,  0.40918298,  0.44444444,
        0.46147736,  0.47062484,  0.46      ,  0.472568  ,  0.47778315,
        0.46334716,  0.44700738,  0.4476726 ,  0.46424266,  0.46426051,
        0.47151899,  0.47515745,  0.47844154,  0.48316598,  0.49474212,
        0.49309011,  0.50174564,  0.5009901 ,  0.51078176,  0.52142513,
        0.5119852 ,  0.50694087,  0.50606594,  0.51318444,  0.50469748,
        0.50779221,  0.51626831,  0.51701418,  0.50976157,  0.52216749,
        0.52572442,  0.53087349,  0.54120417,  0.54075724,  0.54122237,
        0.54449838,  0.54688618,  0.54801642,  0.55504056,  0.56627485,
        0.57676903,  0.5744898 ,  0.56649534,  0.57063859,  0.57351027,
        0.57749465,  0.5860367 ,  0.5852639 ,  0.57955205,  0.58061002,
        0.58912769,  0.59786176,  0.58555217,  0.58601196,  0.58494005,
        0.5839948 ,  0.58312236,  0.57715431,  0.56185176,  0.55892678,
        0.57253046,  0.56723404,  0.55678836,  0.54387806,  0.53451811,
        0.54870941,  0.54870941,  0.53721156,  0.52724662,  0.5195056 ,
        0.53600661,  0.54413695,  0.55123452,  0.53219448,  0.53796321,
        0.54551161,  0.54220711,  0.53588357,  0.53952647,  0.53164557,
        0.514198  ,  0.51922476,  0.54727656,  0.56772152,  0.56002566,
        0.55941499,  0.56093512,  0.57221352,  0.58122144,  0.60186047,
        0.60988838,  0.61175711,  0.61261888,  0.61154983,  0.60847018,
        0.60926993,  0.60947965,  0.60455587,  0.60764873,  0.61259588,
        0.60863398,  0.60553633,  0.59170903,  0.58205901,  0.56641604,
        0.56629393,  0.56648659,  0.56470588,  0.56173469,  0.55895647,
        0.57115437,  0.58333333,  0.58121822,  0.57669307,  0.57529177,
        0.57563641,  0.57962472,  0.58165865,  0.58155455,  0.57796327,
        0.5893443 ,  0.59803729,  0.60252366,  0.59866716,  0.59035409,
        0.59673469,  0.60523069,  0.60903285,  0.61407307,  0.61531449,
        0.61168917,  0.61096178,  0.61711281,  0.62464722,  0.61994949,
        0.61159227,  0.60675226,  0.60559346,  0.61357913,  0.61174596])
Coordinates:
  * x        (x) float64 1.545e+06 1.545e+06 1.545e+06 1.545e+06 1.545e+06 ...

In [10]:
reduction_on_dim012 = nd.evaluate('median(masked_ndvi, 0, 1, 2)')
print(reduction_on_dim012)


<xarray.DataArray ()>
array(0.5337864854058376)

In [ ]: