Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveMHill committed Oct 27, 2020
0 parents commit 8fe4398
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2020 Steven Hill

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
24 changes: 24 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
phenocube
=========

phenocube is an python package providing a wide range of tools for working with the phenocube environment

Usage
-----

Installation
------------

Requirements
^^^^^^^^^^^^

Compatibility
-------------

Licence
-------

Authors
-------

`phenocube` was written by `Steven Hill <[email protected]>`_.
5 changes: 5 additions & 0 deletions phenocube/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""phenocube - phenocube is an python package providing a wide range of tools for working with the phenocube environment"""

__version__ = '0.1.0'
__author__ = 'Steven Hill <[email protected]>'
__all__ = []
70 changes: 70 additions & 0 deletions phenocube/spectralindices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
def spectralindices(dataset, indices = ['NDVI'], norm = True, drop = False):

"""Calculate a suite of spectral indices
Description
----------
Takes an xarray dataset containing spectral bands, calculates one or more
spectral indices and adds the resulting array as a new variable in the original dataset.
Indices:
- EVI: Enhanced Vegetation Index (Huete 2002)
- LAI: Leaf Area Index (Boegh 2002)
- MNDWI: Modified Normalised Difference Water Index (Xu 1996)
- MSAVI: Modified Soil Adjusted Vegetation Index (Qi et al. 1994)
- NBR: Normalised Burn Ratio (Lopez Garcia 1991)
- NDVI: Normalised Difference Vegetation Index (Rouse 1973)
- NDWI: Normalised Difference Water Index (McFeeters 1996)
- SAVI: Soil Adjusted Vegetation Index (Huete 1988)
Parameters
----------
dataset : xarray.Dataset
A two-dimensional or multi-dimensional array containing the spectral bands required to calculate the index.
indices : str or list of strs
A string or a list of strings giving the names of the indices to calculate
norm : bool, optional
If norm = True values are scaled to 0.0-1.0 by dividing by 10000.0
drop : bool, optional
If drop = True only the index will be returned and the other bands will be dropped
Returns
-------
dataset : xarray Dataset
The original xarray Dataset containing the calculated spectral indices as a DataArray.
"""

index_dict = {
'NDVI': lambda dataset: (dataset.nir - dataset.red) / (dataset.nir + dataset.red),
'EVI': lambda dataset: ((2.5 * (dataset.nir - dataset.red)) / (dataset.nir + 6 * dataset.red - 7.5 * dataset.blue + 1)),
'LAI': lambda dataset: (3.618 * ((2.5 * (dataset.nir - dataset.red)) / (dataset.nir + 6 * dataset.red -7.5 * dataset.blue + 1)) - 0.118),
'SAVI': lambda dataset: ((1.5 * (dataset.nir - dataset.red)) / (dataset.nir + dataset.red + 0.5)),
'MSAVI': lambda dataset: ((2 * dataset.nir + 1 -((2 * dataset.nir + 1)**2 - 8 * (dataset.nir - dataset.red))**0.5) / 2),
'NBR': lambda dataset: (dataset.nir - dataset.swir2) / (dataset.nir + dataset.swir2),
'NDWI': lambda dataset: (dataset.green - dataset.nir) / (dataset.green + dataset.nir),
'MNDWI': lambda dataset: (dataset.green - dataset.swir1) / (dataset.green + dataset.swir1)

}

if drop:
drop_bands = list(dataset.data_vars)

for index in indices:
if index not in index_dict:
raise ValueError(f"{index} is not a valid index")
func = index_dict.get(str(index))
try:
if norm:
index_new = func(dataset / 10000.0)
else:
index_new = func(dataset / 1.0)
except AttributeError:
raise ValueError('Missing bands: The data set does not seem to contain all bands needed to calculate this index')

dataset[index] = index_new

if drop:
dataset = dataset.drop(drop_bands)

return(dataset)
44 changes: 44 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import io
import os
import re

from setuptools import find_packages
from setuptools import setup


def read(filename):
filename = os.path.join(os.path.dirname(__file__), filename)
text_type = type(u"")
with io.open(filename, mode="r", encoding='utf-8') as fd:
return re.sub(text_type(r':[a-z]+:`~?(.*?)`'), text_type(r'``\1``'), fd.read())


setup(
name="phenocube",
version="0.1.0",
url="https://github.com/phenocube/phenocube-py",
license='MIT',

author="Steven Hill",
author_email="[email protected]",

description="phenocube is an python package providing a wide range of tools for working with the phenocube environment",
long_description=read("README.rst"),

packages=find_packages(exclude=('tests',)),

install_requires=[],

classifiers=[
'Development Status :: 2 - Pre-Alpha',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
],
)
4 changes: 4 additions & 0 deletions tests/test_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Sample Test passing with nose and pytest

def test_pass():
assert True, "dummy sample test"
6 changes: 6 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[tox]
envlist = py27,py34,py35,py36,py37

[testenv]
commands = py.test phenocube
deps = pytest

0 comments on commit 8fe4398

Please sign in to comment.