AGDC-V2 API for WOfS (unification merged)

Last modified & tested OK on 2016-06-21

Fei Zhang

Outstanding Issues

Bug: list_cells: platforms can be 1 or 3 or more. But cannot be 2

qdict={'latitude': (-36.0, -35.0), 'platform': ['LANDSAT_5', 'LANDSAT_7', 'LANDSAT_8'], 'longitude': (149.01, 150.1), 'time': ('1990-01-01', '2016-03-31')}


In [1]:
##  Setting up environment: python modules etc

# * Optional! 
# * Skip this section if you use other means to ensure correct env setup, 
# * such as module load before starting jupyter notebook.

# make my current source code python modules available in this notebook
import sys
paths=sys.path
#paths.append('/g/data1/u46/fxz547/Githubz/agdc-v2')
paths.insert(0,'/g/data1/u46/fxz547/Githubz/agdc-v2')  #prepend a path
paths.append('/g/data1/u46/fxz547/Githubz/wofs')

In [2]:
from IPython.display import display
from pprint import pprint
from __future__ import print_function

from collections import defaultdict
import xarray as xr
import xarray.ufuncs

import datacube
from datacube.api import GridWorkflow
from datacube.storage import  masking


import numpy as np
import math

#from pyproj import Proj, transform
from osgeo import gdal, osr, ogr
from rasterio.warp import reproject, RESAMPLING
import rasterio

%matplotlib inline
import matplotlib.pyplot as plt
from pylab import rcParams
rcParams['figure.figsize'] = 10, 10 # increase plot size a bit...
rcParams['axes.formatter.useoffset'] = False  # disable scalar formatter / offset in axes labels

Python envionment and modules are ready to go

API with your specific configuration


In [3]:
dc = datacube.Datacube( app='wofs-dev')
#or to use a specific config file: dc = datacube.Datacube(config='/home/547/adh547/unification.datacube.conf', app='wofs-dev')

In [4]:
# where is  this function dc.grid_spec_for_product( product='ls5_nbar_albers')
# dc.grid_spec_for_product( product='ls5_nbar_albers')

In [ ]:


In [5]:
# use ? or ?? to check the function
#GridWorkflow?

In [4]:
#gw = GridWorkflow(dc, product='ls5_nbar_albers')
#OR
gw = GridWorkflow(dc.index , product='ls5_nbar_albers' )

In [7]:
#gw.list_cells?

List_cells ()

The is now no need to call list_cells for tile-based workflows, unless you just want to list the area covered:


In [5]:
# product_type= nbar | pqa
qdict={'latitude': (-36.0, -35.0), 'platform': ['LANDSAT_5', 'LANDSAT_7', 'LANDSAT_8'], 'longitude': (149.01, 150.1), 'time': ('1990-01-01', '2016-03-31')}

cells = gw.list_cells(product_type='nbar', product='ls5_nbar_albers', **qdict)
                      #longitude=(149.06,149.18), latitude=(-35.27, -35.33), time=('1996-01-01', '2016-03-20'))
len(cells)


Out[5]:
4

In [9]:
# cells is now is huge dict, like the following
# defaultdict(dict,
#             {(15, -41): {'geobox': <datacube.model.GeoBox at 0x7fc05f3e3390>,
#               'sources': <xarray.DataArray (time: 111)>
#               array([ (Dataset <id=6ce1955f-5ef9-4876-8bb8-69638f45efe8 type=ls5_nbar_albers location=/g/data/u46/users/gxr547/unicube/LS5_TM_NBAR/LS5_TM_NBAR_3577_15_-41_19900302231139000000.nc>,),
#                      (Dataset <id=94efe23b-f699-4a16-a244-ab0fc6b58461 type=ls5_nbar_albers location=/g/data/u46/users/gxr547/unicube/LS5_TM_NBAR/LS5_TM_NBAR_3577_15_-41_19900403231042000000.nc>,),
#                      (Dataset <id=e0f376e7-a7db-4cbb-b137-b9a2865c2b02 type=ls5_nbar_albers location=/g/data/u46/users/gxr547/unicube/LS5_TM_NBAR/LS5_TM_NBAR_3577_15_-41_19900708231050000000.nc>,),
#                      (Dataset <id=8eb3a1af-e906-419e-a22e-d523a65fa830 type=ls5_nbar_albers location=/g/data/u46/users/gxr547/unicube/LS5_TM_NBAR/LS5_TM_NBAR_3577_15_-41_19900724231046000000.nc>,),
#                      (Dataset <id=7c317cb9-b8de-48b8-92a5

In [6]:
# To get the cell indexes
cells.keys()


Out[6]:
[(16, -40), (15, -40), (15, -41), (16, -41)]

How to get satellite and sensor from the datacube?


In [7]:
prodlist=dc.list_products()  #pandas df

prodlist[prodlist.name=='ls5_nbar_albers'] # the row for your product


Out[7]:
name description platform product_type instrument format crs resolution tile_size spatial_dimensions
id
6 ls5_nbar_albers Landsat 5 Surface Reflectance NBAR 25 metre, 1... LANDSAT_5 nbar TM NetCDF EPSG:3577 [-25, 25] [100000.0, 100000.0] (y, x)

In [8]:
# get your values as numpy.ndarr
pppl=prodlist[prodlist.name=='ls5_nbar_albers'][['platform','instrument']].values

In [9]:
type(pppl)


Out[9]:
numpy.ndarray

In [10]:
pppl.shape


Out[10]:
(1, 2)

In [11]:
satname=pppl[0,0]
satsensor=pppl[0,1]
print(satname,satsensor)


LANDSAT_5 TM

List Tiles


In [12]:
nbar_tiles = gw.list_tiles(product_type='nbar', product='ls5_nbar_albers', platform=['LANDSAT_5', 'LANDSAT_7', 'LANDSAT_8'],
                           longitude=(149.06,149.18), latitude=(-35.27, -35.33), 
                           time=('1996-01-01', '2016-03-20'))

sorted(nbar_tiles.keys())


Out[12]:
[(15, -40, numpy.datetime64('2005-01-07T10:36:04.000000000+1100')),
 (15, -40, numpy.datetime64('2005-01-07T10:36:28.000000000+1100')),
 (15, -40, numpy.datetime64('2005-01-14T10:42:20.000000000+1100')),
 (15, -40, numpy.datetime64('2005-01-14T10:42:44.000000000+1100')),
 (15, -40, numpy.datetime64('2005-01-30T10:42:33.000000000+1100')),
 (15, -40, numpy.datetime64('2005-01-30T10:42:57.000000000+1100')),
 (15, -40, numpy.datetime64('2005-02-08T10:36:31.000000000+1100')),
 (15, -40, numpy.datetime64('2005-02-08T10:36:54.000000000+1100')),
 (15, -40, numpy.datetime64('2005-02-15T10:42:49.000000000+1100')),
 (15, -40, numpy.datetime64('2005-02-24T10:36:47.000000000+1100')),
 (15, -40, numpy.datetime64('2005-02-24T10:37:11.000000000+1100')),
 (15, -40, numpy.datetime64('2005-03-03T10:43:04.000000000+1100')),
 (15, -40, numpy.datetime64('2005-03-03T10:43:28.000000000+1100')),
 (15, -40, numpy.datetime64('2005-03-12T10:36:59.000000000+1100')),
 (15, -40, numpy.datetime64('2005-03-12T10:37:23.000000000+1100')),
 (15, -40, numpy.datetime64('2005-03-19T10:43:14.000000000+1100')),
 (15, -40, numpy.datetime64('2005-03-19T10:43:38.000000000+1100')),
 (15, -40, numpy.datetime64('2005-03-28T09:37:07.000000000+1000')),
 (15, -40, numpy.datetime64('2005-03-28T09:37:31.000000000+1000')),
 (15, -40, numpy.datetime64('2005-04-04T09:43:20.000000000+1000')),
 (15, -40, numpy.datetime64('2005-04-04T09:43:44.000000000+1000')),
 (15, -40, numpy.datetime64('2005-04-13T09:37:11.000000000+1000')),
 (15, -40, numpy.datetime64('2005-04-13T09:37:34.000000000+1000')),
 (15, -40, numpy.datetime64('2005-04-20T09:43:23.000000000+1000')),
 (15, -40, numpy.datetime64('2005-04-20T09:43:47.000000000+1000')),
 (15, -40, numpy.datetime64('2005-04-29T09:37:15.000000000+1000')),
 (15, -40, numpy.datetime64('2005-04-29T09:37:39.000000000+1000')),
 (15, -40, numpy.datetime64('2005-05-06T09:43:29.000000000+1000')),
 (15, -40, numpy.datetime64('2005-05-06T09:43:53.000000000+1000')),
 (15, -40, numpy.datetime64('2005-05-15T09:37:22.000000000+1000')),
 (15, -40, numpy.datetime64('2005-05-15T09:37:46.000000000+1000')),
 (15, -40, numpy.datetime64('2005-05-22T09:43:36.000000000+1000')),
 (15, -40, numpy.datetime64('2005-05-22T09:44:00.000000000+1000')),
 (15, -40, numpy.datetime64('2005-05-31T09:37:31.000000000+1000')),
 (15, -40, numpy.datetime64('2005-05-31T09:37:55.000000000+1000')),
 (15, -40, numpy.datetime64('2005-06-07T09:43:45.000000000+1000')),
 (15, -40, numpy.datetime64('2005-06-07T09:44:09.000000000+1000')),
 (15, -40, numpy.datetime64('2005-06-16T09:37:38.000000000+1000')),
 (15, -40, numpy.datetime64('2005-06-16T09:38:02.000000000+1000')),
 (15, -40, numpy.datetime64('2005-07-02T09:37:42.000000000+1000')),
 (15, -40, numpy.datetime64('2005-07-02T09:38:06.000000000+1000')),
 (15, -40, numpy.datetime64('2005-07-18T09:37:51.000000000+1000')),
 (15, -40, numpy.datetime64('2005-07-18T09:38:15.000000000+1000')),
 (15, -40, numpy.datetime64('2005-07-25T09:44:30.000000000+1000')),
 (15, -40, numpy.datetime64('2005-08-03T09:38:24.000000000+1000')),
 (15, -40, numpy.datetime64('2005-08-10T09:44:38.000000000+1000')),
 (15, -40, numpy.datetime64('2005-08-26T09:44:20.000000000+1000')),
 (15, -40, numpy.datetime64('2005-08-26T09:44:44.000000000+1000')),
 (15, -40, numpy.datetime64('2005-09-04T09:38:36.000000000+1000')),
 (15, -40, numpy.datetime64('2005-09-11T09:44:23.000000000+1000')),
 (15, -40, numpy.datetime64('2005-09-11T09:44:47.000000000+1000')),
 (15, -40, numpy.datetime64('2005-09-20T09:38:12.000000000+1000')),
 (15, -40, numpy.datetime64('2005-09-20T09:38:36.000000000+1000')),
 (15, -40, numpy.datetime64('2005-09-27T09:44:46.000000000+1000')),
 (15, -40, numpy.datetime64('2005-10-06T09:38:09.000000000+1000')),
 (15, -40, numpy.datetime64('2005-10-06T09:38:33.000000000+1000')),
 (15, -40, numpy.datetime64('2005-10-13T09:44:17.000000000+1000')),
 (15, -40, numpy.datetime64('2005-10-13T09:44:41.000000000+1000')),
 (15, -40, numpy.datetime64('2005-10-22T09:38:07.000000000+1000')),
 (15, -40, numpy.datetime64('2005-10-22T09:38:31.000000000+1000')),
 (15, -40, numpy.datetime64('2005-11-07T10:38:39.000000000+1100')),
 (15, -40, numpy.datetime64('2005-11-14T10:44:36.000000000+1100')),
 (15, -40, numpy.datetime64('2005-11-14T10:45:00.000000000+1100')),
 (15, -40, numpy.datetime64('2006-01-26T10:40:08.000000000+1100')),
 (15, -40, numpy.datetime64('2006-01-26T10:40:33.000000000+1100')),
 (15, -40, numpy.datetime64('2006-02-02T10:46:29.000000000+1100')),
 (15, -40, numpy.datetime64('2006-02-02T10:46:53.000000000+1100')),
 (15, -40, numpy.datetime64('2006-02-11T10:40:31.000000000+1100')),
 (15, -40, numpy.datetime64('2006-02-11T10:40:55.000000000+1100')),
 (15, -40, numpy.datetime64('2006-02-18T10:46:50.000000000+1100')),
 (15, -40, numpy.datetime64('2006-02-18T10:47:14.000000000+1100')),
 (15, -40, numpy.datetime64('2006-02-27T10:40:51.000000000+1100')),
 (15, -40, numpy.datetime64('2006-02-27T10:41:15.000000000+1100')),
 (15, -40, numpy.datetime64('2006-03-06T10:47:10.000000000+1100')),
 (15, -40, numpy.datetime64('2006-03-06T10:47:34.000000000+1100')),
 (15, -40, numpy.datetime64('2006-03-15T10:41:35.000000000+1100')),
 (15, -40, numpy.datetime64('2006-04-07T09:47:47.000000000+1000')),
 (15, -40, numpy.datetime64('2006-04-07T09:48:11.000000000+1000')),
 (15, -40, numpy.datetime64('2006-04-16T09:41:46.000000000+1000')),
 (15, -40, numpy.datetime64('2006-04-16T09:42:10.000000000+1000')),
 (15, -40, numpy.datetime64('2006-04-23T09:48:04.000000000+1000')),
 (15, -40, numpy.datetime64('2006-04-23T09:48:27.000000000+1000')),
 (15, -40, numpy.datetime64('2006-05-02T09:42:01.000000000+1000')),
 (15, -40, numpy.datetime64('2006-05-02T09:42:25.000000000+1000')),
 (15, -40, numpy.datetime64('2006-05-09T09:48:17.000000000+1000')),
 (15, -40, numpy.datetime64('2006-05-09T09:48:42.000000000+1000')),
 (15, -40, numpy.datetime64('2006-05-18T09:42:13.000000000+1000')),
 (15, -40, numpy.datetime64('2006-05-18T09:42:37.000000000+1000')),
 (15, -40, numpy.datetime64('2006-05-25T09:48:30.000000000+1000')),
 (15, -40, numpy.datetime64('2006-05-25T09:48:54.000000000+1000')),
 (15, -40, numpy.datetime64('2006-06-03T09:42:28.000000000+1000')),
 (15, -40, numpy.datetime64('2006-06-03T09:42:52.000000000+1000')),
 (15, -40, numpy.datetime64('2006-06-10T09:49:09.000000000+1000')),
 (15, -40, numpy.datetime64('2006-06-19T09:42:42.000000000+1000')),
 (15, -40, numpy.datetime64('2006-06-19T09:43:06.000000000+1000')),
 (15, -40, numpy.datetime64('2006-06-26T09:49:00.000000000+1000')),
 (15, -40, numpy.datetime64('2006-06-26T09:49:24.000000000+1000')),
 (15, -40, numpy.datetime64('2006-07-05T09:42:57.000000000+1000')),
 (15, -40, numpy.datetime64('2006-07-05T09:43:21.000000000+1000')),
 (15, -40, numpy.datetime64('2006-07-12T09:49:14.000000000+1000')),
 (15, -40, numpy.datetime64('2006-07-12T09:49:38.000000000+1000')),
 (15, -40, numpy.datetime64('2006-07-21T09:43:10.000000000+1000')),
 (15, -40, numpy.datetime64('2006-07-21T09:43:34.000000000+1000')),
 (15, -40, numpy.datetime64('2006-07-28T09:49:26.000000000+1000')),
 (15, -40, numpy.datetime64('2006-07-28T09:49:50.000000000+1000')),
 (15, -40, numpy.datetime64('2006-08-06T09:43:23.000000000+1000')),
 (15, -40, numpy.datetime64('2006-08-06T09:43:47.000000000+1000')),
 (15, -40, numpy.datetime64('2006-08-13T09:49:39.000000000+1000')),
 (15, -40, numpy.datetime64('2006-08-13T09:50:03.000000000+1000')),
 (15, -40, numpy.datetime64('2006-08-22T09:43:35.000000000+1000')),
 (15, -40, numpy.datetime64('2006-08-22T09:43:59.000000000+1000')),
 (15, -40, numpy.datetime64('2006-08-29T09:49:51.000000000+1000')),
 (15, -40, numpy.datetime64('2006-08-29T09:50:15.000000000+1000')),
 (15, -40, numpy.datetime64('2006-09-14T09:50:03.000000000+1000')),
 (15, -40, numpy.datetime64('2006-09-14T09:50:27.000000000+1000')),
 (15, -40, numpy.datetime64('2006-09-23T09:43:58.000000000+1000')),
 (15, -40, numpy.datetime64('2006-09-23T09:44:22.000000000+1000')),
 (15, -40, numpy.datetime64('2006-09-30T09:50:14.000000000+1000')),
 (15, -40, numpy.datetime64('2006-09-30T09:50:38.000000000+1000')),
 (15, -40, numpy.datetime64('2006-10-09T09:44:09.000000000+1000')),
 (15, -40, numpy.datetime64('2006-10-09T09:44:33.000000000+1000')),
 (15, -40, numpy.datetime64('2006-10-16T09:50:24.000000000+1000')),
 (15, -40, numpy.datetime64('2006-10-16T09:50:48.000000000+1000')),
 (15, -40, numpy.datetime64('2006-10-25T09:44:18.000000000+1000')),
 (15, -40, numpy.datetime64('2006-10-25T09:44:42.000000000+1000')),
 (15, -40, numpy.datetime64('2006-11-01T10:50:33.000000000+1100')),
 (15, -40, numpy.datetime64('2006-11-01T10:50:57.000000000+1100')),
 (15, -40, numpy.datetime64('2006-11-10T10:44:27.000000000+1100')),
 (15, -40, numpy.datetime64('2006-11-10T10:44:51.000000000+1100')),
 (15, -40, numpy.datetime64('2006-11-17T10:50:41.000000000+1100')),
 (15, -40, numpy.datetime64('2006-11-17T10:51:05.000000000+1100')),
 (15, -40, numpy.datetime64('2006-11-26T10:44:35.000000000+1100')),
 (15, -40, numpy.datetime64('2006-11-26T10:44:59.000000000+1100')),
 (15, -40, numpy.datetime64('2006-12-03T10:50:50.000000000+1100')),
 (15, -40, numpy.datetime64('2006-12-03T10:51:14.000000000+1100')),
 (15, -40, numpy.datetime64('2006-12-19T10:50:58.000000000+1100')),
 (15, -40, numpy.datetime64('2006-12-19T10:51:21.000000000+1100')),
 (15, -40, numpy.datetime64('2006-12-28T10:44:51.000000000+1100')),
 (15, -40, numpy.datetime64('2006-12-28T10:45:15.000000000+1100')),
 (15, -40, numpy.datetime64('2007-01-04T10:51:04.000000000+1100')),
 (15, -40, numpy.datetime64('2007-01-04T10:51:28.000000000+1100')),
 (15, -40, numpy.datetime64('2007-01-13T10:44:57.000000000+1100')),
 (15, -40, numpy.datetime64('2007-01-13T10:45:21.000000000+1100')),
 (15, -40, numpy.datetime64('2007-01-20T10:51:10.000000000+1100')),
 (15, -40, numpy.datetime64('2007-01-20T10:51:34.000000000+1100')),
 (15, -40, numpy.datetime64('2007-01-29T10:45:01.000000000+1100')),
 (15, -40, numpy.datetime64('2007-01-29T10:45:25.000000000+1100')),
 (15, -40, numpy.datetime64('2007-02-05T10:51:13.000000000+1100')),
 (15, -40, numpy.datetime64('2007-02-05T10:51:37.000000000+1100')),
 (15, -40, numpy.datetime64('2007-02-14T10:45:03.000000000+1100')),
 (15, -40, numpy.datetime64('2007-02-14T10:45:27.000000000+1100')),
 (15, -40, numpy.datetime64('2007-02-21T10:51:13.000000000+1100')),
 (15, -40, numpy.datetime64('2007-02-21T10:51:37.000000000+1100')),
 (15, -40, numpy.datetime64('2007-03-02T10:45:02.000000000+1100')),
 (15, -40, numpy.datetime64('2007-03-02T10:45:26.000000000+1100')),
 (15, -40, numpy.datetime64('2007-03-09T10:51:12.000000000+1100')),
 (15, -40, numpy.datetime64('2007-03-09T10:51:36.000000000+1100')),
 (15, -40, numpy.datetime64('2007-03-18T10:45:24.000000000+1100')),
 (15, -40, numpy.datetime64('2007-03-25T09:51:09.000000000+1000')),
 (15, -40, numpy.datetime64('2007-03-25T09:51:33.000000000+1000')),
 (15, -40, numpy.datetime64('2007-04-03T09:44:56.000000000+1000')),
 (15, -40, numpy.datetime64('2007-04-03T09:45:20.000000000+1000')),
 (15, -40, numpy.datetime64('2007-04-10T09:51:05.000000000+1000')),
 (15, -40, numpy.datetime64('2007-04-10T09:51:29.000000000+1000')),
 (15, -40, numpy.datetime64('2007-04-19T09:44:51.000000000+1000')),
 (15, -40, numpy.datetime64('2007-04-26T09:50:58.000000000+1000')),
 (15, -40, numpy.datetime64('2007-04-26T09:51:22.000000000+1000')),
 (15, -40, numpy.datetime64('2007-05-05T09:44:43.000000000+1000')),
 (15, -40, numpy.datetime64('2007-05-05T09:45:07.000000000+1000')),
 (15, -40, numpy.datetime64('2007-05-12T09:50:49.000000000+1000')),
 (15, -40, numpy.datetime64('2007-05-12T09:51:13.000000000+1000')),
 (15, -40, numpy.datetime64('2007-05-21T09:44:31.000000000+1000')),
 (15, -40, numpy.datetime64('2007-05-21T09:44:56.000000000+1000')),
 (15, -40, numpy.datetime64('2007-06-13T09:50:20.000000000+1000')),
 (15, -40, numpy.datetime64('2007-06-13T09:50:44.000000000+1000')),
 (15, -40, numpy.datetime64('2007-06-22T09:43:59.000000000+1000')),
 (15, -40, numpy.datetime64('2007-06-22T09:44:23.000000000+1000')),
 (15, -40, numpy.datetime64('2007-06-29T09:50:01.000000000+1000')),
 (15, -40, numpy.datetime64('2007-06-29T09:50:25.000000000+1000')),
 (15, -40, numpy.datetime64('2007-07-08T09:43:47.000000000+1000')),
 (15, -40, numpy.datetime64('2007-07-15T09:49:55.000000000+1000')),
 (15, -40, numpy.datetime64('2007-07-15T09:50:19.000000000+1000')),
 (15, -40, numpy.datetime64('2007-07-24T09:43:39.000000000+1000')),
 (15, -40, numpy.datetime64('2007-07-24T09:44:03.000000000+1000')),
 (15, -40, numpy.datetime64('2007-07-31T09:49:47.000000000+1000')),
 (15, -40, numpy.datetime64('2007-07-31T09:50:11.000000000+1000')),
 (15, -40, numpy.datetime64('2007-08-09T09:43:31.000000000+1000')),
 (15, -40, numpy.datetime64('2007-08-09T09:43:55.000000000+1000')),
 (15, -40, numpy.datetime64('2007-08-16T09:50:01.000000000+1000')),
 (15, -40, numpy.datetime64('2007-08-25T09:43:20.000000000+1000')),
 (15, -40, numpy.datetime64('2007-08-25T09:43:44.000000000+1000')),
 (15, -40, numpy.datetime64('2007-09-01T09:49:27.000000000+1000')),
 (15, -40, numpy.datetime64('2007-09-01T09:49:51.000000000+1000')),
 (15, -40, numpy.datetime64('2007-09-10T09:43:13.000000000+1000')),
 (15, -40, numpy.datetime64('2007-09-10T09:43:37.000000000+1000')),
 (15, -40, numpy.datetime64('2007-09-26T09:43:04.000000000+1000')),
 (15, -40, numpy.datetime64('2007-09-26T09:43:28.000000000+1000')),
 (15, -40, numpy.datetime64('2007-10-03T09:49:10.000000000+1000')),
 (15, -40, numpy.datetime64('2007-10-03T09:49:34.000000000+1000')),
 (15, -40, numpy.datetime64('2008-01-16T10:41:11.000000000+1100')),
 (15, -40, numpy.datetime64('2008-01-16T10:41:35.000000000+1100')),
 (15, -40, numpy.datetime64('2008-01-23T10:47:14.000000000+1100')),
 (15, -40, numpy.datetime64('2008-01-23T10:47:38.000000000+1100')),
 (15, -40, numpy.datetime64('2008-02-08T10:46:58.000000000+1100')),
 (15, -40, numpy.datetime64('2008-02-17T10:41:01.000000000+1100')),
 (15, -40, numpy.datetime64('2008-02-24T10:46:40.000000000+1100')),
 (15, -40, numpy.datetime64('2008-02-24T10:47:04.000000000+1100')),
 (15, -40, numpy.datetime64('2008-03-04T10:40:19.000000000+1100')),
 (15, -40, numpy.datetime64('2008-03-04T10:40:43.000000000+1100')),
 (15, -40, numpy.datetime64('2008-03-11T10:46:22.000000000+1100')),
 (15, -40, numpy.datetime64('2008-03-11T10:46:46.000000000+1100')),
 (15, -40, numpy.datetime64('2008-03-20T10:40:00.000000000+1100')),
 (15, -40, numpy.datetime64('2008-09-19T09:41:11.000000000+1000')),
 (15, -40, numpy.datetime64('2008-09-28T09:34:43.000000000+1000')),
 (15, -40, numpy.datetime64('2008-09-28T09:35:07.000000000+1000')),
 (15, -40, numpy.datetime64('2008-10-05T10:40:41.000000000+1100')),
 (15, -40, numpy.datetime64('2008-10-05T10:41:05.000000000+1100')),
 (15, -40, numpy.datetime64('2008-10-21T10:40:09.000000000+1100')),
 (15, -40, numpy.datetime64('2008-10-21T10:40:33.000000000+1100')),
 (15, -40, numpy.datetime64('2008-10-30T10:33:39.000000000+1100')),
 (15, -40, numpy.datetime64('2008-10-30T10:34:03.000000000+1100')),
 (15, -40, numpy.datetime64('2008-11-15T10:33:20.000000000+1100')),
 (15, -40, numpy.datetime64('2008-11-15T10:33:44.000000000+1100')),
 (15, -40, numpy.datetime64('2008-12-08T10:40:16.000000000+1100')),
 (15, -40, numpy.datetime64('2008-12-08T10:40:40.000000000+1100')),
 (15, -40, numpy.datetime64('2008-12-17T10:34:21.000000000+1100')),
 (15, -40, numpy.datetime64('2008-12-24T10:40:44.000000000+1100')),
 (15, -40, numpy.datetime64('2008-12-24T10:41:08.000000000+1100')),
 (15, -40, numpy.datetime64('2009-01-02T10:34:47.000000000+1100')),
 (15, -40, numpy.datetime64('2009-01-02T10:35:11.000000000+1100')),
 (15, -40, numpy.datetime64('2009-01-09T10:41:09.000000000+1100')),
 (15, -40, numpy.datetime64('2009-01-09T10:41:33.000000000+1100')),
 (15, -40, numpy.datetime64('2009-01-18T10:35:12.000000000+1100')),
 (15, -40, numpy.datetime64('2009-01-18T10:35:36.000000000+1100')),
 (15, -40, numpy.datetime64('2009-01-25T10:41:34.000000000+1100')),
 (15, -40, numpy.datetime64('2009-01-25T10:41:58.000000000+1100')),
 (15, -40, numpy.datetime64('2009-02-03T10:35:38.000000000+1100')),
 (15, -40, numpy.datetime64('2009-02-03T10:36:02.000000000+1100')),
 (15, -40, numpy.datetime64('2009-02-10T10:41:59.000000000+1100')),
 (15, -40, numpy.datetime64('2009-02-10T10:42:23.000000000+1100')),
 (15, -40, numpy.datetime64('2009-02-19T10:36:02.000000000+1100')),
 (15, -40, numpy.datetime64('2009-02-19T10:36:26.000000000+1100')),
 (15, -40, numpy.datetime64('2009-02-26T10:42:23.000000000+1100')),
 (15, -40, numpy.datetime64('2009-02-26T10:42:47.000000000+1100')),
 (15, -40, numpy.datetime64('2009-03-07T10:36:25.000000000+1100')),
 (15, -40, numpy.datetime64('2009-03-07T10:36:49.000000000+1100')),
 (15, -40, numpy.datetime64('2009-03-14T10:42:46.000000000+1100')),
 (15, -40, numpy.datetime64('2009-03-14T10:43:10.000000000+1100')),
 (15, -40, numpy.datetime64('2009-03-30T10:43:07.000000000+1100')),
 (15, -40, numpy.datetime64('2009-03-30T10:43:31.000000000+1100')),
 (15, -40, numpy.datetime64('2009-04-08T09:37:07.000000000+1000')),
 (15, -40, numpy.datetime64('2009-04-08T09:37:31.000000000+1000')),
 (15, -40, numpy.datetime64('2009-04-15T09:43:26.000000000+1000')),
 (15, -40, numpy.datetime64('2009-04-15T09:43:51.000000000+1000')),
 (15, -40, numpy.datetime64('2009-04-24T09:37:25.000000000+1000')),
 (15, -40, numpy.datetime64('2009-05-01T09:43:44.000000000+1000')),
 (15, -40, numpy.datetime64('2009-05-01T09:44:08.000000000+1000')),
 (15, -40, numpy.datetime64('2009-05-10T09:37:44.000000000+1000')),
 (15, -40, numpy.datetime64('2009-05-10T09:38:07.000000000+1000')),
 (15, -40, numpy.datetime64('2009-05-17T09:44:02.000000000+1000')),
 (15, -40, numpy.datetime64('2009-05-17T09:44:26.000000000+1000')),
 (15, -40, numpy.datetime64('2009-06-27T09:38:36.000000000+1000')),
 (15, -40, numpy.datetime64('2009-07-13T09:38:53.000000000+1000')),
 (15, -40, numpy.datetime64('2009-07-29T09:39:08.000000000+1000')),
 (15, -40, numpy.datetime64('2009-08-05T09:45:25.000000000+1000')),
 (15, -40, numpy.datetime64('2009-08-30T09:39:36.000000000+1000')),
 (15, -40, numpy.datetime64('2009-08-30T09:40:00.000000000+1000')),
 (15, -40, numpy.datetime64('2009-09-06T09:45:53.000000000+1000')),
 (15, -40, numpy.datetime64('2009-09-06T09:46:17.000000000+1000')),
 (15, -40, numpy.datetime64('2009-09-15T09:39:49.000000000+1000')),
 (15, -40, numpy.datetime64('2009-09-15T09:40:13.000000000+1000')),
 (15, -40, numpy.datetime64('2009-09-22T09:46:29.000000000+1000')),
 (15, -40, numpy.datetime64('2009-10-01T09:40:01.000000000+1000')),
 (15, -40, numpy.datetime64('2009-10-01T09:40:25.000000000+1000')),
 (15, -40, numpy.datetime64('2009-10-08T10:46:16.000000000+1100')),
 (15, -40, numpy.datetime64('2009-10-08T10:46:40.000000000+1100')),
 (15, -40, numpy.datetime64('2009-10-17T10:40:11.000000000+1100')),
 (15, -40, numpy.datetime64('2009-10-17T10:40:35.000000000+1100')),
 (15, -40, numpy.datetime64('2009-10-24T10:46:26.000000000+1100')),
 (15, -40, numpy.datetime64('2009-10-24T10:46:50.000000000+1100')),
 (15, -40, numpy.datetime64('2009-11-02T10:40:20.000000000+1100')),
 (15, -40, numpy.datetime64('2009-11-02T10:40:44.000000000+1100')),
 (15, -40, numpy.datetime64('2009-11-09T10:46:34.000000000+1100')),
 (15, -40, numpy.datetime64('2009-11-09T10:46:58.000000000+1100')),
 (15, -40, numpy.datetime64('2009-12-11T10:46:50.000000000+1100')),
 (15, -40, numpy.datetime64('2009-12-11T10:47:14.000000000+1100')),
 (15, -40, numpy.datetime64('2010-01-12T10:47:02.000000000+1100')),
 (15, -40, numpy.datetime64('2010-01-12T10:47:26.000000000+1100')),
 (15, -40, numpy.datetime64('2010-01-21T10:40:53.000000000+1100')),
 (15, -40, numpy.datetime64('2010-01-21T10:41:17.000000000+1100')),
 (15, -40, numpy.datetime64('2010-01-28T10:47:06.000000000+1100')),
 (15, -40, numpy.datetime64('2010-01-28T10:47:30.000000000+1100')),
 (15, -40, numpy.datetime64('2010-02-06T10:41:21.000000000+1100')),
 (15, -40, numpy.datetime64('2010-02-22T10:41:00.000000000+1100')),
 (15, -40, numpy.datetime64('2010-02-22T10:41:24.000000000+1100')),
 (15, -40, numpy.datetime64('2010-03-26T10:41:01.000000000+1100')),
 (15, -40, numpy.datetime64('2010-03-26T10:41:25.000000000+1100')),
 (15, -40, numpy.datetime64('2010-04-02T10:47:10.000000000+1100')),
 (15, -40, numpy.datetime64('2010-04-02T10:47:34.000000000+1100')),
 (15, -40, numpy.datetime64('2010-04-11T09:40:58.000000000+1000')),
 (15, -40, numpy.datetime64('2010-04-11T09:41:22.000000000+1000')),
 (15, -40, numpy.datetime64('2010-04-27T09:41:18.000000000+1000')),
 (15, -40, numpy.datetime64('2010-05-04T09:47:04.000000000+1000')),
 (15, -40, numpy.datetime64('2010-05-04T09:47:28.000000000+1000')),
 (15, -40, numpy.datetime64('2010-09-25T09:46:23.000000000+1000')),
 (15, -40, numpy.datetime64('2010-09-25T09:46:47.000000000+1000')),
 (15, -40, numpy.datetime64('2010-10-04T10:40:32.000000000+1100')),
 (15, -40, numpy.datetime64('2010-10-11T10:46:15.000000000+1100')),
 (15, -40, numpy.datetime64('2010-10-11T10:46:39.000000000+1100')),
 (15, -40, numpy.datetime64('2010-10-20T10:39:59.000000000+1100')),
 (15, -40, numpy.datetime64('2010-10-20T10:40:23.000000000+1100')),
 (15, -40, numpy.datetime64('2010-11-12T10:46:02.000000000+1100')),
 (15, -40, numpy.datetime64('2010-11-12T10:46:26.000000000+1100')),
 (15, -40, numpy.datetime64('2010-11-21T10:39:52.000000000+1100')),
 (15, -40, numpy.datetime64('2010-11-21T10:40:16.000000000+1100')),
 (15, -40, numpy.datetime64('2010-12-14T10:46:05.000000000+1100')),
 (15, -40, numpy.datetime64('2010-12-14T10:46:29.000000000+1100')),
 (15, -40, numpy.datetime64('2010-12-23T10:39:57.000000000+1100')),
 (15, -40, numpy.datetime64('2010-12-23T10:40:21.000000000+1100')),
 (15, -40, numpy.datetime64('2010-12-30T10:46:09.000000000+1100')),
 (15, -40, numpy.datetime64('2010-12-30T10:46:33.000000000+1100')),
 (15, -40, numpy.datetime64('2011-01-08T10:40:00.000000000+1100')),
 (15, -40, numpy.datetime64('2011-01-08T10:40:24.000000000+1100')),
 (15, -40, numpy.datetime64('2011-01-15T10:46:35.000000000+1100')),
 (15, -40, numpy.datetime64('2011-01-31T10:46:12.000000000+1100')),
 (15, -40, numpy.datetime64('2011-01-31T10:46:36.000000000+1100')),
 (15, -40, numpy.datetime64('2011-02-25T10:40:02.000000000+1100')),
 (15, -40, numpy.datetime64('2011-02-25T10:40:27.000000000+1100')),
 (15, -40, numpy.datetime64('2011-03-04T10:46:12.000000000+1100')),
 (15, -40, numpy.datetime64('2011-03-04T10:46:36.000000000+1100')),
 (15, -40, numpy.datetime64('2011-03-13T10:39:59.000000000+1100')),
 (15, -40, numpy.datetime64('2011-03-13T10:40:23.000000000+1100')),
 (15, -40, numpy.datetime64('2011-03-20T10:46:06.000000000+1100')),
 (15, -40, numpy.datetime64('2011-03-20T10:46:30.000000000+1100')),
 (15, -40, numpy.datetime64('2011-03-29T10:39:53.000000000+1100')),
 (15, -40, numpy.datetime64('2011-03-29T10:40:17.000000000+1100')),
 (15, -40, numpy.datetime64('2011-04-05T09:46:04.000000000+1000')),
 (15, -40, numpy.datetime64('2011-04-05T09:46:28.000000000+1000')),
 (15, -40, numpy.datetime64('2011-04-14T09:39:49.000000000+1000')),
 (15, -40, numpy.datetime64('2011-08-27T09:44:59.000000000+1000')),
 (15, -40, numpy.datetime64('2011-08-27T09:45:23.000000000+1000')),
 (15, -40, numpy.datetime64('2011-09-12T09:44:48.000000000+1000')),
 (15, -40, numpy.datetime64('2011-09-12T09:45:12.000000000+1000')),
 (15, -40, numpy.datetime64('2011-09-21T09:38:29.000000000+1000')),
 (15, -40, numpy.datetime64('2011-09-21T09:38:53.000000000+1000')),
 (15, -40, numpy.datetime64('2011-10-14T10:44:17.000000000+1100')),
 (15, -40, numpy.datetime64('2011-10-14T10:44:41.000000000+1100')),
 (15, -40, numpy.datetime64('2011-11-08T10:37:44.000000000+1100')),
 (15, -40, numpy.datetime64('2011-11-08T10:38:08.000000000+1100')),
 (15, -40, numpy.datetime64('2011-11-15T10:43:42.000000000+1100')),
 (15, -40, numpy.datetime64('2011-11-15T10:44:06.000000000+1100'))]

In [13]:
# This nbar_tiles includes scenes and tiles, a very very long list
# nbar_tiles has 18 cells as keys
len(nbar_tiles)


Out[13]:
351

When using the search terms, there is not currently a way to filter out non-tile based data, such as scenes, which go far beyond the requested area. For now we must use product='ls5_nbar_albers'.


In [14]:
#bug if just 2 platforms in the list?
#qdict={'latitude': (-36.0, -35.0), 'longitude': (149.01, 150.1),'platform':['LANDSAT_5', 'LANDSAT_7'], 'time': ('1990-01-01', '2016-03-31')}
qdict={'latitude': (-36.0, -35.0), 'longitude': (149.01, 150.1),'platform':['LANDSAT_5', 'LANDSAT_7','LANDSAT_8'], 'time': ('1990-01-01', '2016-03-31')}

print (qdict)
nbar_tiles = gw.list_tiles(product='ls5_nbar_albers',**qdict)
                          # ,longitude=(149.06, 149.18), latitude=(-35.27, -35.33),  time=('1996-01-01', '2016-03-20'))
sorted_keylist = sorted(nbar_tiles.keys())

print (len(sorted_keylist))

sorted_keylist[:5]


{'latitude': (-36.0, -35.0), 'platform': ['LANDSAT_5', 'LANDSAT_7', 'LANDSAT_8'], 'longitude': (149.01, 150.1), 'time': ('1990-01-01', '2016-03-31')}
1086
Out[14]:
[(15, -41, numpy.datetime64('2005-01-07T10:36:28.000000000+1100')),
 (15, -41, numpy.datetime64('2005-01-14T10:42:44.000000000+1100')),
 (15, -41, numpy.datetime64('2005-01-30T10:42:57.000000000+1100')),
 (15, -41, numpy.datetime64('2005-02-01T10:30:37.000000000+1100')),
 (15, -41, numpy.datetime64('2005-02-08T10:36:54.000000000+1100'))]

In [19]:
#nbar_tiles  # only the nbar_albers tiles

In [ ]:


In [ ]:


In [15]:
# Pixel Quality Tiles
pq_tiles = gw.list_tiles(product='ls5_pq_albers',**qdict)
                         # ,longitude=(149.06, 149.18), latitude=(-35.27, -35.33), time=('1996-01-01', '2016-03-20'))


len(sorted(pq_tiles.keys()))


Out[15]:
86

In [16]:
# after unification:
import logging 
def get_nbarpqa_tiles_by_cell( acell, qdict):
    """
    return a list of tiles
    :param acell: a cell index tuple (15, -40)
    :return:
    """
    # gw.list_tiles((15,-40), product='ls5_nbar_albers')

    nbar_tiles = gw.list_tiles(acell, product='ls5_nbar_albers',
                                    **qdict)  # , platform='LANDSAT_8')  # ,time=('2000', '2007'))
    pq_tiles = gw.list_tiles(acell, product='ls5_pq_albers',
                                  **qdict)  # , platform='LANDSAT_8')  # , time=('2000', '2007'))

    if (len(pq_tiles) == len(nbar_tiles)):
        print("The cells have %s nbar and %s pq tiles", len(nbar_tiles), len(pq_tiles))
    else:
        logging.warn("Mismatched NBAR-PQA tiles: The cells have %s nbar and %s pq tiles", len(nbar_tiles),
                     len(pq_tiles))

    # Cell, Time -> Product -> TileDef
    tile_def = defaultdict(dict)

    for index, tile in nbar_tiles.items():
        tile_def[index[:2], index[2]]['nbar'] = tile

    for index, tile in pq_tiles.items():
        tile_def[index[:2], index[2]]['pqa'] = tile


    for index, products in tile_def.items():
        if len(products) < 2:
            logging.warn('un-paired nbar-pqa product for cell %s', str(index))
            logging.warn("remove this un-paired tile from the dict")
            tile_def.pop(index)
        else:
            logging.debug('%s,%s', index, len(products))

    return tile_def

Get the key for the first tile:


In [19]:
#Get the key for the tile:
acell=(15,-40)
#qdict = {'platform': ['LANDSAT_5'], 'time': ('1990-01-01', '1990-03-31')}
qdict = {'platform': ['LANDSAT_5'], 'time': ('2011-01-01', '2011-03-31')}

tile_def=get_nbarpqa_tiles_by_cell( (15,-40), qdict )
#keys = list(tile_def)


The cells have %s nbar and %s pq tiles 15 15

In [20]:
tile_keys=tile_def.keys()

print (len(tile_keys))

for key in tile_keys[:10]:
    print (key)
    cell = key[0]
    dt_stamp=key[1]
    
    print (dt_stamp)
    
    #ISO 
    isostr=str(dt_stamp)[:19].replace(':','-')      
    print (isostr)


15
((15, -40), numpy.datetime64('2011-03-04T10:46:12.000000000+1100'))
2011-03-04T10:46:12.000000000+1100
2011-03-04T10-46-12
((15, -40), numpy.datetime64('2011-03-20T10:46:06.000000000+1100'))
2011-03-20T10:46:06.000000000+1100
2011-03-20T10-46-06
((15, -40), numpy.datetime64('2011-03-04T10:46:36.000000000+1100'))
2011-03-04T10:46:36.000000000+1100
2011-03-04T10-46-36
((15, -40), numpy.datetime64('2011-01-08T10:40:24.000000000+1100'))
2011-01-08T10:40:24.000000000+1100
2011-01-08T10-40-24
((15, -40), numpy.datetime64('2011-01-31T10:46:12.000000000+1100'))
2011-01-31T10:46:12.000000000+1100
2011-01-31T10-46-12
((15, -40), numpy.datetime64('2011-01-31T10:46:36.000000000+1100'))
2011-01-31T10:46:36.000000000+1100
2011-01-31T10-46-36
((15, -40), numpy.datetime64('2011-03-29T10:39:53.000000000+1100'))
2011-03-29T10:39:53.000000000+1100
2011-03-29T10-39-53
((15, -40), numpy.datetime64('2011-03-13T10:40:23.000000000+1100'))
2011-03-13T10:40:23.000000000+1100
2011-03-13T10-40-23
((15, -40), numpy.datetime64('2011-02-25T10:40:27.000000000+1100'))
2011-02-25T10:40:27.000000000+1100
2011-02-25T10-40-27
((15, -40), numpy.datetime64('2011-03-29T10:40:17.000000000+1100'))
2011-03-29T10:40:17.000000000+1100
2011-03-29T10-40-17

In [21]:
key= tile_keys[0]
tile_def[key]


Out[21]:
{'nbar': {'geobox': GeoBox(4000, 4000, Affine(25.0, 0.0, 1500000.0,
         0.0, -25.0, -3900000.0), EPSG:3577),
  'sources': <xarray.DataArray (time: 1)>
  array([ (Dataset <id=8fbe60dd-2e41-4c9a-a5c9-30ba15fe5415 type=ls5_nbar_albers location=/g/data/rs0/datacube/002/LS5_TM_NBAR/15_-40/LS5_TM_NBAR_3577_15_-40_20110303234612000000.nc>,)], dtype=object)
  Coordinates:
    * time     (time) datetime64[ns] 2011-03-03T23:46:12},
 'pqa': {'geobox': GeoBox(4000, 4000, Affine(25.0, 0.0, 1500000.0,
         0.0, -25.0, -3900000.0), EPSG:3577),
  'sources': <xarray.DataArray (time: 1)>
  array([ (Dataset <id=b6a6a189-9c4a-48d0-a646-62c9957d5da0 type=ls5_pq_albers location=/g/data/rs0/datacube/002/LS5_TM_PQ/15_-40/LS5_TM_PQ_3577_15_-40_20110303234612000000.nc>,)], dtype=object)
  Coordinates:
    * time     (time) datetime64[ns] 2011-03-03T23:46:12}}

In [22]:
key


Out[22]:
((15, -40), numpy.datetime64('2011-03-04T10:46:12.000000000+1100'))

Load the nbar data for a given tile (as keyed by cell and time)


In [23]:
time_slice=1

cell=(15,-40)

key= tile_keys[time_slice]
print(key)

tile = tile_def[key]['nbar']

print (cell)
print (tile)

nbar_data = gw.load(tile) 

nbar_data


((15, -40), numpy.datetime64('2011-03-20T10:46:06.000000000+1100'))
(15, -40)
{'geobox': GeoBox(4000, 4000, Affine(25.0, 0.0, 1500000.0,
       0.0, -25.0, -3900000.0), EPSG:3577), 'sources': <xarray.DataArray (time: 1)>
array([ (Dataset <id=f54f0d45-5c36-4092-a8ce-0046893965c3 type=ls5_nbar_albers location=/g/data/rs0/datacube/002/LS5_TM_NBAR/15_-40/LS5_TM_NBAR_3577_15_-40_20110319234606000000.nc>,)], dtype=object)
Coordinates:
  * time     (time) datetime64[ns] 2011-03-19T23:46:06}
Out[23]:
<xarray.Dataset>
Dimensions:  (time: 1, x: 4000, y: 4000)
Coordinates:
  * time     (time) datetime64[ns] 2011-03-19T23:46:06
  * y        (y) float64 -3.9e+06 -3.9e+06 -3.9e+06 -3.9e+06 -3.9e+06 ...
  * x        (x) float64 1.5e+06 1.5e+06 1.5e+06 1.5e+06 1.5e+06 1.5e+06 ...
Data variables:
    blue     (time, y, x) int16 286 333 310 333 333 333 379 402 379 310 310 ...
    green    (time, y, x) int16 534 534 581 675 722 675 675 722 722 675 628 ...
    red      (time, y, x) int16 428 505 505 543 581 581 619 581 581 505 505 ...
    nir      (time, y, x) int16 3129 3081 3034 3509 3842 3699 3177 3557 3414 ...
    swir1    (time, y, x) int16 1994 1860 1961 2261 2595 2629 2495 2529 2395 ...
    swir2    (time, y, x) int16 944 896 944 1088 1184 1184 1232 1184 1088 ...
Attributes:
    crs: EPSG:3577

In [24]:
# It looks that nbar_data.blue is 3D array (time,y,x); But actually you cannot do nbar_data.blue[1, :,:]??
type(nbar_data.blue)
nbar_data.blue


Out[24]:
<xarray.DataArray 'blue' (time: 1, y: 4000, x: 4000)>
array([[[ 286,  333,  310, ..., -999, -999, -999],
        [ 402,  425,  356, ..., -999, -999, -999],
        [ 448,  471,  494, ..., -999, -999, -999],
        ..., 
        [-999, -999, -999, ..., -999, -999, -999],
        [-999, -999, -999, ..., -999, -999, -999],
        [-999, -999, -999, ..., -999, -999, -999]]], dtype=int16)
Coordinates:
  * time     (time) datetime64[ns] 2011-03-19T23:46:06
  * y        (y) float64 -3.9e+06 -3.9e+06 -3.9e+06 -3.9e+06 -3.9e+06 ...
  * x        (x) float64 1.5e+06 1.5e+06 1.5e+06 1.5e+06 1.5e+06 1.5e+06 ...
Attributes:
    units: 1
    crs: EPSG:3577
    nodata: -999
    spectral_definition: {u'wavelength': [410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, ...

In [25]:
y_size=4000
x_size=4000

#del raw_image
raw_image = np.zeros((6, y_size, x_size), dtype='int16') #'float32')

raw_image[0,:,:] = nbar_data.blue[:,:]
raw_image[1,:,:] = nbar_data.green[:,:]
raw_image[2,:,:] = nbar_data.red[:,:]
raw_image[3,:,:] = nbar_data.nir[:,:]
raw_image[4,:,:] = nbar_data.swir1[:,:]
raw_image[5,:,:] = nbar_data.swir2[:,:]

In [26]:
raw_image.shape


Out[26]:
(6, 4000, 4000)

In [27]:
type(raw_image)


Out[27]:
numpy.ndarray

In [28]:
from wofs.waters.detree.classifier import WaterClassifier

classifier = WaterClassifier()

# TODO: water classification using the input data tiles

water_classified_img = classifier.classify(raw_image)

In [29]:
# display all or part of the classified image
plt.imshow(water_classified_img )  #show Lake BurleyGriffin  clfimg[2200:2600,1500:2500]) if cell=(15,-40)
plt.colorbar(orientation='vertical', shrink=0.8, label='water=128 or no water=0');



In [33]:
# The first step produce a very rough water tile as shown above. It even has water in no data region.
# This is why it must be filtered by no_data, cloud, etc. below

In [ ]:

NBAR data

We can use the nodata value, or use the contiguous bit in the PQ data to mask out nulls.

Here is nodata:


In [30]:
no_data = (nbar_data.green == nbar_data.green.nodata)
no_data.plot()


Out[30]:
<matplotlib.collections.QuadMesh at 0x7fdcd577b650>

In [31]:
type(no_data)


Out[31]:
xarray.core.dataarray.DataArray

In [32]:
# a mask 1+2D array
no_data


Out[32]:
<xarray.DataArray 'green' (time: 1, y: 4000, x: 4000)>
array([[[False, False, False, ...,  True,  True,  True],
        [False, False, False, ...,  True,  True,  True],
        [False, False, False, ...,  True,  True,  True],
        ..., 
        [ True,  True,  True, ...,  True,  True,  True],
        [ True,  True,  True, ...,  True,  True,  True],
        [ True,  True,  True, ...,  True,  True,  True]]], dtype=bool)
Coordinates:
  * time     (time) datetime64[ns] 2011-03-19T23:46:06
  * y        (y) float64 -3.9e+06 -3.9e+06 -3.9e+06 -3.9e+06 -3.9e+06 ...
  * x        (x) float64 1.5e+06 1.5e+06 1.5e+06 1.5e+06 1.5e+06 1.5e+06 ...

In [33]:
nbar_data.green.where(~no_data).plot()


Out[33]:
<matplotlib.collections.QuadMesh at 0x7fdcd5443a10>

In [34]:
nbar_data.green.where(~ no_data[0]).plot()


Out[34]:
<matplotlib.collections.QuadMesh at 0x7fdcd5662090>

In [35]:
# Get the nodata values for each band (in case they aren't the same)
no_data_values = nbar_data.apply(lambda data_array: data_array.nodata).to_array(dim='band')

# Turn the Dataset into a DataArray, so we can check all bands
stack = nbar_data.to_array(dim='band')

# Find all values that are set to no data, from any band
no_data_mask = (stack == no_data_values).any(dim='band')

no_data_mask.plot()


Out[35]:
<matplotlib.collections.QuadMesh at 0x7fdcd566c450>

In [40]:
no_data_mask


Out[40]:
<xarray.DataArray (time: 1, y: 4000, x: 4000)>
array([[[ True,  True,  True, ...,  True,  True,  True],
        [ True,  True,  True, ...,  True,  True,  True],
        [ True,  True,  True, ...,  True,  True,  True],
        ..., 
        [False, False, False, ..., False, False, False],
        [False, False, False, ..., False, False, False],
        [False, False, False, ..., False, False, False]]], dtype=bool)
Coordinates:
  * y        (y) float64 -3.9e+06 -3.9e+06 -3.9e+06 -3.9e+06 -3.9e+06 ...
  * x        (x) float64 1.5e+06 1.5e+06 1.5e+06 1.5e+06 1.5e+06 1.5e+06 ...
  * time     (time) datetime64[ns] 1990-03-02T23:11:39

In [41]:
no_data_mask.squeeze()


Out[41]:
<xarray.DataArray (y: 4000, x: 4000)>
array([[ True,  True,  True, ...,  True,  True,  True],
       [ True,  True,  True, ...,  True,  True,  True],
       [ True,  True,  True, ...,  True,  True,  True],
       ..., 
       [False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False]], dtype=bool)
Coordinates:
  * y        (y) float64 -3.9e+06 -3.9e+06 -3.9e+06 -3.9e+06 -3.9e+06 ...
  * x        (x) float64 1.5e+06 1.5e+06 1.5e+06 1.5e+06 1.5e+06 1.5e+06 ...
    time     datetime64[ns] 1990-03-02T23:11:39

Load the PQ data:


In [36]:
# cell = key[0]
tile = tile_def[key]['pqa']
pq_data = gw.load( tile)

We can look at the bits available to us:


In [37]:
masking.get_flags_def(pq_data)


Out[37]:
{u'blue_saturated': {u'bits': 0,
  u'description': u'Blue band is saturated',
  u'values': {u'0': True, u'1': False}},
 u'cloud_acca': {u'bits': 10,
  u'description': u'Cloud Shadow (ACCA)',
  u'values': {u'0': u'cloud', u'1': u'no_cloud'}},
 u'cloud_fmask': {u'bits': 11,
  u'description': u'Cloud (Fmask)',
  u'values': {u'0': u'cloud', u'1': u'no_cloud'}},
 u'cloud_shadow_acca': {u'bits': 12,
  u'description': u'Cloud Shadow (ACCA)',
  u'values': {u'0': u'cloud_shadow', u'1': u'no_cloud_shadow'}},
 u'cloud_shadow_fmask': {u'bits': 13,
  u'description': u'Cloud Shadow (Fmask)',
  u'values': {u'0': u'cloud_shadow', u'1': u'no_cloud_shadow'}},
 u'contiguous': {u'bits': 8,
  u'description': u'All bands for this pixel contain non-null values',
  u'values': {u'0': False, u'1': True}},
 u'ga_good_pixel': {u'bits': [13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
  u'description': u'Best Quality Pixel',
  u'values': {u'16383': True}},
 u'green_saturated': {u'bits': 1,
  u'description': u'Green band is saturated',
  u'values': {u'0': True, u'1': False}},
 u'land_sea': {u'bits': 9,
  u'description': u'Land or Sea',
  u'values': {u'0': u'sea', u'1': u'land'}},
 u'nir_saturated': {u'bits': 3,
  u'description': u'NIR band is saturated',
  u'values': {u'0': True, u'1': False}},
 u'red_saturated': {u'bits': 2,
  u'description': u'Red band is saturated',
  u'values': {u'0': True, u'1': False}},
 u'swir1_saturated': {u'bits': 4,
  u'description': u'SWIR1 band is saturated',
  u'values': {u'0': True, u'1': False}},
 u'swir2_saturated': {u'bits': 7,
  u'description': u'SWIR2 band is saturated',
  u'values': {u'0': True, u'1': False}},
 u'tir_saturated': {u'bits': 5,
  u'description': u'Thermal Infrared band is saturated',
  u'values': {u'0': True, u'1': False}}}

In [38]:
# better display using pandas
import pandas
pandas.DataFrame.from_dict(masking.get_flags_def(pq_data), orient='index')


Out[38]:
values bits description
blue_saturated {u'1': False, u'0': True} 0 Blue band is saturated
cloud_acca {u'1': u'no_cloud', u'0': u'cloud'} 10 Cloud Shadow (ACCA)
cloud_fmask {u'1': u'no_cloud', u'0': u'cloud'} 11 Cloud (Fmask)
cloud_shadow_acca {u'1': u'no_cloud_shadow', u'0': u'cloud_shadow'} 12 Cloud Shadow (ACCA)
cloud_shadow_fmask {u'1': u'no_cloud_shadow', u'0': u'cloud_shadow'} 13 Cloud Shadow (Fmask)
contiguous {u'1': True, u'0': False} 8 All bands for this pixel contain non-null values
ga_good_pixel {u'16383': True} [13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0] Best Quality Pixel
green_saturated {u'1': False, u'0': True} 1 Green band is saturated
land_sea {u'1': u'land', u'0': u'sea'} 9 Land or Sea
nir_saturated {u'1': False, u'0': True} 3 NIR band is saturated
red_saturated {u'1': False, u'0': True} 2 Red band is saturated
swir1_saturated {u'1': False, u'0': True} 4 SWIR1 band is saturated
swir2_saturated {u'1': False, u'0': True} 7 SWIR2 band is saturated
tir_saturated {u'1': False, u'0': True} 5 Thermal Infrared band is saturated

We can make a mask from a combination of bits. Here we will use the cloud masks and the contiguous bit.


In [39]:
contiguous = masking.make_mask(pq_data, contiguous=1).pixelquality
contiguous.plot()


Out[39]:
<matplotlib.collections.QuadMesh at 0x7fdc276f9e90>

In [40]:
no_contiguous = masking.make_mask(pq_data, contiguous=0).pixelquality
no_contiguous.plot()


Out[40]:
<matplotlib.collections.QuadMesh at 0x7fdc33150a10>

In [41]:
# are the same?
np.sum (contiguous != no_contiguous)


Out[41]:
<xarray.DataArray 'pixelquality' ()>
array(16000000)

In [42]:
good_data = masking.make_mask(pq_data, cloud_acca='no_cloud', cloud_fmask='no_cloud', contiguous=True).pixelquality
good_data.plot()


Out[42]:
<matplotlib.collections.QuadMesh at 0x7fdcd52223d0>

In [43]:
good_data  # is a mask image True/false


Out[43]:
<xarray.DataArray 'pixelquality' (time: 1, y: 4000, x: 4000)>
array([[[ True,  True,  True, ..., False, False, False],
        [ True,  True,  True, ..., False, False, False],
        [ True,  True,  True, ..., False, False, False],
        ..., 
        [False, False, False, ..., False, False, False],
        [False, False, False, ..., False, False, False],
        [False, False, False, ..., False, False, False]]], dtype=bool)
Coordinates:
  * y        (y) float64 -3.9e+06 -3.9e+06 -3.9e+06 -3.9e+06 -3.9e+06 ...
  * x        (x) float64 1.5e+06 1.5e+06 1.5e+06 1.5e+06 1.5e+06 1.5e+06 ...
  * time     (time) datetime64[ns] 2011-03-19T23:46:06

In [44]:
cloud_free_nbar = nbar_data.where(good_data)
cloud_free_nbar.red.plot()


Out[44]:
<matplotlib.collections.QuadMesh at 0x7fdcd5784890>

In [45]:
cloud_free_nbar.red


Out[45]:
<xarray.DataArray 'red' (time: 1, y: 4000, x: 4000)>
array([[[ 428.,  505.,  505., ...,   nan,   nan,   nan],
        [ 657.,  695.,  657., ...,   nan,   nan,   nan],
        [ 809.,  847.,  847., ...,   nan,   nan,   nan],
        ..., 
        [  nan,   nan,   nan, ...,   nan,   nan,   nan],
        [  nan,   nan,   nan, ...,   nan,   nan,   nan],
        [  nan,   nan,   nan, ...,   nan,   nan,   nan]]])
Coordinates:
  * time     (time) datetime64[ns] 2011-03-19T23:46:06
  * y        (y) float64 -3.9e+06 -3.9e+06 -3.9e+06 -3.9e+06 -3.9e+06 ...
  * x        (x) float64 1.5e+06 1.5e+06 1.5e+06 1.5e+06 1.5e+06 1.5e+06 ...

In [ ]:


In [ ]:

How to mask out the 100% GOOD pixels from NBAR data?


In [46]:
pq_data


Out[46]:
<xarray.Dataset>
Dimensions:       (time: 1, x: 4000, y: 4000)
Coordinates:
  * time          (time) datetime64[ns] 2011-03-19T23:46:06
  * y             (y) float64 -3.9e+06 -3.9e+06 -3.9e+06 -3.9e+06 -3.9e+06 ...
  * x             (x) float64 1.5e+06 1.5e+06 1.5e+06 1.5e+06 1.5e+06 ...
Data variables:
    pixelquality  (time, y, x) int16 16383 16383 16383 16383 16383 16383 ...
Attributes:
    crs: EPSG:3577

In [47]:
perfect_flag = int(masking.get_flags_def(pq_data)['ga_good_pixel']['values'].keys()[0])   # should be 16383
# perfect_flag = 16383

print ('perfect_flag', perfect_flag)

perfect_pixel_mask=(pq_data.pixelquality == perfect_flag)
perfect_pixel_mask.plot()


perfect_flag 16383
Out[47]:
<matplotlib.collections.QuadMesh at 0x7fdcd54e6e90>

In [48]:
perfect_nbar_data = nbar_data.where(perfect_pixel_mask)

In [49]:
y_size=4000
x_size=4000

#del raw_image
raw_image = np.zeros((6, y_size, x_size), dtype='int16') #'float32')

raw_image[0,:,:] = perfect_nbar_data.blue[:,:]
raw_image[1,:,:] = perfect_nbar_data.green[:,:]
raw_image[2,:,:] = perfect_nbar_data.red[:,:]
raw_image[3,:,:] = perfect_nbar_data.nir[:,:]
raw_image[4,:,:] = perfect_nbar_data.swir1[:,:]
raw_image[5,:,:] = perfect_nbar_data.swir2[:,:]


water_img = classifier.classify(raw_image)

In [50]:
# display all or part of the classified image
#plt.imshow(water_img)
plt.imshow(water_img[2200:2600,1500:2500])  #show Lake BurleyGriffin  clfimg[2200:2600,1500:2500]) if cell=(15,-40)
plt.colorbar(orientation='vertical', shrink=0.8, label='water=128 or no water=0');



In [51]:
water_img


Out[51]:
array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ..., 
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint8)

In [52]:
pq_data


Out[52]:
<xarray.Dataset>
Dimensions:       (time: 1, x: 4000, y: 4000)
Coordinates:
  * time          (time) datetime64[ns] 2011-03-19T23:46:06
  * y             (y) float64 -3.9e+06 -3.9e+06 -3.9e+06 -3.9e+06 -3.9e+06 ...
  * x             (x) float64 1.5e+06 1.5e+06 1.5e+06 1.5e+06 1.5e+06 ...
Data variables:
    pixelquality  (time, y, x) int16 16383 16383 16383 16383 16383 16383 ...
Attributes:
    crs: EPSG:3577

In [53]:
perfect_pixel_mask = masking.make_mask(pq_data, ga_good_pixel=True).pixelquality

In [54]:
perfect_pixel_mask = masking.make_mask(pq_data.pixelquality, ga_good_pixel=True)

In [ ]:
perfect_pixel_mask.plot()


Out[ ]:
<matplotlib.collections.QuadMesh at 0x7fdcc421ded0>

How to get DSM tiles


In [62]:
acellindex=(15,-40)
dsm_cells = gw.list_tiles(acellindex, product='dsm1sv10')
print (dsm_cells)


defaultdict(<type 'dict'>, {(15, -40, numpy.datetime64('2000-02-12T04:43:00.000000000+1100')): {'geobox': GeoBox(4000, 4000, Affine(25.0, 0.0, 1500000.0,
       0.0, -25.0, -3900000.0), EPSG:3577), 'sources': <xarray.DataArray (time: 1)>
array([ (Dataset <id=a066a2ab-42f7-4e72-bc6d-a47a558b8172 type=dsm1sv10 location=/g/data/v10/projects/ingest_test_data/milestone1/dsm1sv1_0_Clean/agdc-metadata.yaml>,)], dtype=object)
Coordinates:
  * time     (time) datetime64[ns] 2000-02-11T17:43:00}})

In [63]:
# ----------------------------------------------------------------
def get_dsm_data(acellindex, qdict={}):
    """
    get dsm data for sia, terrainshadow, highslope filters
    :param acellindex:
    :param qdict:
    :return:
    """
    query = qdict.copy()  #avoid modify the original input qdict in-situ
    query.pop('time', None)
    query.pop('platform', None)
    dsm_cells = gw.list_cells(acellindex, product='dsm1sv10', **query)

    def load_dsm(cell):
        data = gw.load(cell)
        return data.squeeze('time').drop('time')

    return {index: load_dsm(cell) for index, cell in dsm_cells.items()}

In [64]:
dsmtile= get_dsm_data(acellindex, qdict={})

In [65]:
print(dsmtile)


{(15, -40): <xarray.Dataset>
Dimensions:    (x: 4000, y: 4000)
Coordinates:
  * y          (y) float64 -3.9e+06 -3.9e+06 -3.9e+06 -3.9e+06 -3.9e+06 ...
  * x          (x) float64 1.5e+06 1.5e+06 1.5e+06 1.5e+06 1.5e+06 1.5e+06 ...
Data variables:
    elevation  (y, x) float32 582.532 588.824 587.487 584.112 579.31 574.467 ...
Attributes:
    crs: EPSG:3577}

In [66]:
dsmtile[(15,-40)]['elevation'].plot()


Out[66]:
<matplotlib.collections.QuadMesh at 0x7f59bc8d31d0>

In [ ]: