{ "cells": [ { "cell_type": "markdown", "id": "e20fc857", "metadata": {}, "source": [ "# Working With Time Series\n", "\n", "In this example notebook, we'll introduce the `TimeSeries` class, which is the basis of most of the functionality in `wxee`. Any `ImageCollection` can be turned into a `TimeSeries`, allowing you to access additional time series-specific methods and properties." ] }, { "cell_type": "markdown", "id": "bb4ec543", "metadata": {}, "source": [ "## Setup" ] }, { "cell_type": "code", "execution_count": null, "id": "e3967cd3", "metadata": {}, "outputs": [], "source": [ "!pip install wxee" ] }, { "cell_type": "code", "execution_count": 1, "id": "74e107b7", "metadata": { "ExecuteTime": { "end_time": "2021-11-21T06:03:23.978651Z", "start_time": "2021-11-21T06:03:23.502531Z" } }, "outputs": [], "source": [ "import ee\n", "import wxee" ] }, { "cell_type": "code", "execution_count": 2, "id": "ed6c82c7", "metadata": { "ExecuteTime": { "end_time": "2021-11-21T06:03:27.757386Z", "start_time": "2021-11-21T06:03:25.729961Z" } }, "outputs": [], "source": [ "ee.Authenticate()\n", "wxee.Initialize()" ] }, { "cell_type": "markdown", "id": "f1dc9555", "metadata": {}, "source": [ "## Creating a Time Series\n", "\n", "There are two ways to create a `TimeSeries`." ] }, { "cell_type": "markdown", "id": "4b36156a", "metadata": {}, "source": [ "### From an ID\n", "\n", "You can create a `TimeSeries` the same way you create an `ImageCollection`, by using an ID from the Earth Engine catalog. Let's create a `TimeSeries` from the Sentinel-2 dataset." ] }, { "cell_type": "code", "execution_count": 3, "id": "77c7f8ff", "metadata": { "ExecuteTime": { "end_time": "2021-11-21T06:03:28.697209Z", "start_time": "2021-11-21T06:03:28.695074Z" } }, "outputs": [], "source": [ "ts = wxee.TimeSeries(\"COPERNICUS/S2_SR\")" ] }, { "cell_type": "markdown", "id": "fcf287cf", "metadata": {}, "source": [ "### From an Existing Collection\n", "\n", "You can also turn an existing `ImageCollection` into a `TimeSeries`, which may be helpful if you decide you want time series functionality after doing other processing. Just use the `wx` accessor and the `to_time_series` method." ] }, { "cell_type": "code", "execution_count": 4, "id": "e1e57451", "metadata": { "ExecuteTime": { "end_time": "2021-11-21T06:03:29.891431Z", "start_time": "2021-11-21T06:03:29.889398Z" } }, "outputs": [], "source": [ "col = ee.ImageCollection(\"COPERNICUS/S2_SR\")\n", "ts = col.wx.to_time_series()" ] }, { "cell_type": "markdown", "id": "9275fefb", "metadata": {}, "source": [ "## Earth Engine Methods\n", "\n", "A `TimeSeries` has all the methods of an Earth Engine `ImageCollection`. For example, we can use `filterMetadata`, `filterBounds`, and `size` to see how many cloudless (or at least mostly cloudless) images there are over a given location (Svalbard, Norway)." ] }, { "cell_type": "code", "execution_count": 5, "id": "031e8adc", "metadata": { "ExecuteTime": { "end_time": "2021-11-21T06:03:31.045515Z", "start_time": "2021-11-21T06:03:31.043047Z" } }, "outputs": [], "source": [ "svalbard = ee.Geometry.Point([15.5861, 78.1969])" ] }, { "cell_type": "code", "execution_count": 6, "id": "79bbd408", "metadata": { "ExecuteTime": { "end_time": "2021-11-21T06:03:32.372611Z", "start_time": "2021-11-21T06:03:31.954378Z" } }, "outputs": [ { "data": { "text/plain": [ "80" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cloudless = ts.filterBounds(svalbard).filterMetadata(\"CLOUDY_PIXEL_PERCENTAGE\", \"less_than\", 5)\n", "cloudless.size().getInfo()" ] }, { "cell_type": "markdown", "id": "b8795ab6", "metadata": {}, "source": [ "## wxee Methods\n", "\n", "A time series also has additional methods added by `wxee` to help describe and process time series data. Let's start by looking at the `describe`, `dataframe`, and `timeline` methods that provide info on how many images are in the time series and when they were acquired." ] }, { "cell_type": "markdown", "id": "2af95088", "metadata": {}, "source": [ "### `describe`" ] }, { "cell_type": "markdown", "id": "68905d0f", "metadata": {}, "source": [ "First, `describe` provides a brief summary of the time series including the number of images, the `system:start_time` of the first and last chronological image, and the mean interval between images." ] }, { "cell_type": "code", "execution_count": 7, "id": "bbd65f3b", "metadata": { "ExecuteTime": { "end_time": "2021-11-21T06:03:37.114647Z", "start_time": "2021-11-21T06:03:34.266089Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1mCOPERNICUS/S2_SR\u001b[0m\n", "\tImages: 80\n", "\tStart date: 2018-03-27 12:27:55 UTC\n", "\tEnd date: 2021-09-03 11:59:18 UTC\n", "\tMean interval: 15.90 days\n" ] } ], "source": [ "cloudless.describe()" ] }, { "cell_type": "markdown", "id": "adb10543", "metadata": {}, "source": [ "As you can see, there have been 80 mostly cloudless images captured over this area--on average, about one every 16 days." ] }, { "cell_type": "markdown", "id": "0ab529c2", "metadata": {}, "source": [ "### `dataframe`" ] }, { "cell_type": "markdown", "id": "bc475eaf", "metadata": {}, "source": [ "We can summarize the ID and start and end dates of those 80 images as a `pandas.DataFrame` using `dataframe`." ] }, { "cell_type": "code", "execution_count": 8, "id": "06b7cdf6", "metadata": { "ExecuteTime": { "end_time": "2021-11-21T06:03:45.511506Z", "start_time": "2021-11-21T06:03:37.119701Z" } }, "outputs": [ { "data": { "text/html": [ "
| \n", " | id | \n", "time_start | \n", "time_end | \n", "
|---|---|---|---|
| 0 | \n", "COPERNICUS/S2_SR/20180327T122729_20180327T1227... | \n", "2018-03-27 12:27:55 | \n", "2018-03-27 12:27:55 | \n", "
| 1 | \n", "COPERNICUS/S2_SR/20180405T120651_20180405T1206... | \n", "2018-04-05 12:06:45 | \n", "2018-04-05 12:06:45 | \n", "
| 2 | \n", "COPERNICUS/S2_SR/20180405T125659_20180405T1257... | \n", "2018-04-05 12:57:02 | \n", "2018-04-05 12:57:02 | \n", "
| 3 | \n", "COPERNICUS/S2_SR/20180406T122649_20180406T1226... | \n", "2018-04-06 12:26:49 | \n", "2018-04-06 12:26:49 | \n", "
| 4 | \n", "COPERNICUS/S2_SR/20180407T115639_20180407T1156... | \n", "2018-04-07 11:56:39 | \n", "2018-04-07 11:56:39 | \n", "