# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# [![colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/davemlz/eemont/blob/master/docs/tutorials/013-Time-Series-By-Region-Pandas.ipynb)
# [![Open in SageMaker Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/davemlz/eemont/blob/master/docs/tutorials/013-Time-Series-By-Region-Pandas.ipynb)
# [![Open in Planetary Computer](https://img.shields.io/badge/Open-Planetary%20Computer-black?style=flat&logo=microsoft)](https://pccompute.westeurope.cloudapp.azure.com/compute/hub/user-redirect/git-pull?repo=https://github.com/davemlz/eemont&urlpath=lab/tree/eemont/docs/tutorials/013-Time-Series-By-Region-Pandas.ipynb&branch=master)

# + [markdown] id="jZEthLln92Ep"
# # Time Series By Region (with Conversion to Pandas and Visualization with Seaborn)

# + [markdown] id="dNa470OZ8Oec"
# _Tutorial created by **<NAME>**_: [GitHub](https://github.com/davemlz) | [Twitter](https://twitter.com/dmlmont)
#
# - GitHub Repo: [https://github.com/davemlz/eemont](https://github.com/davemlz/eemont)
# - PyPI link: [https://pypi.org/project/eemont/](https://pypi.org/project/eemont/)
# - Conda-forge: [https://anaconda.org/conda-forge/eemont](https://anaconda.org/conda-forge/eemont)
# - Documentation: [https://eemont.readthedocs.io/](https://eemont.readthedocs.io/)
# - More tutorials: [https://github.com/davemlz/eemont/tree/master/docs/tutorials](https://github.com/davemlz/eemont/tree/master/docs/tutorials)

# + [markdown] id="CD7h0hbi92Er"
# ## Let's start!

# + [markdown] id="E0rc6Cya92Es"
# If required, please uncomment:

# + id="NYzyvKtk92Es"
# #!pip install eemont
# #!pip install geemap

# + [markdown] id="x3Rm3qt_92Et"
# Import the required packges.

# + id="H0C9S_Hh92Et"
import ee, eemont, geemap
import pandas as pd
import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt

# + [markdown] id="k1sdX2p592Eu"
# Authenticate and Initialize Earth Engine and geemap.

# + id="7QDXqVwy8Oef"
Map = geemap.Map()
# -

# Let's use a buffered point for the time series!

point = ee.Geometry.Point([11.178576,51.122064]).buffer(100)

# Let's pre-process and process our image collection:

L8 = (ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
      .filterBounds(point)
      .maskClouds()
      .scaleAndOffset()
      .spectralIndices(['GNDVI','EVI']))

# ## Time Series By Region

# Let's get the L8 time series for our buffer. Checklist:
#
# - Image Collection: The Landsat 8 collection.
# - Bands to use for the time series: GNDVI and EVI.
# - Geometry: Our buffered point.
# - Statistics to compute: Minimum, Mean, and Maximum.
# - Scale: 30 m.

ts = L8.getTimeSeriesByRegion(geometry = point,
                              bands = ['EVI','GNDVI'],
                              reducer = [ee.Reducer.min(),ee.Reducer.mean(),ee.Reducer.max()],
                              scale = 30)

# ## Conversion to Pandas

# The time series is retrieved as a feature collection. To convert it to a pandas dataframe we'll use `geemap` (This may take a little bit):

tsPandas = geemap.ee_to_pandas(ts)

# Let's check our pandas data frame:

tsPandas

# What can we see here?
#
# - The values for each band (GNDVI and EVI) are in separated columns.
# - There are some -9999 values in the GNDVI and EVI columns. These values represent the NA values (e.g. Clouds or shadows). The -9999 can be changed by modifying the `naValue` parameter in the `getTimeSeriesByRegion` method (e.g. `naValue = -10000`).
# - Multiple reducers can be used. In the output dataframe they are specified by a single column named `reducer`: min, mean, max.
# - The date is a string that needs to be converted to a date data type.
#
# Given this, let's curate our data frame!

# First, let's get rid of the -9999 value:

tsPandas[tsPandas == -9999] = np.nan

# And now, let's convert the date to a date data type:

tsPandas['date'] = pd.to_datetime(tsPandas['date'],infer_datetime_format = True)

# We can also gather the GNDVI and EVI columns into a single column to make the data frame more 'tidy-er' (This is optional):

tsPandas = pd.melt(tsPandas,
                   id_vars = ['reducer','date'],
                   value_vars = ['GNDVI','EVI'],
                   var_name = 'Index',
                   value_name = 'Value')

# Let's check our curated data frame:

tsPandas

# ## Visualization

# Now, let's visualize our time series using `seaborn`:

plt.figure(figsize = (20,10))
sns.lineplot(data = tsPandas,
             x = 'date',
             y = 'Value',
             hue = 'Index',
             style = 'reducer')

StarcoderdataJupyterScriptsDedupFiltered