In [30]:
# Import Libraries
import pandas as pd
import datetime
import os

import matplotlib.pyplot as plt
%matplotlib inline
In [31]:
# Import Folders
data_folder = os.path.abspath("data")
output_folder = os.path.abspath("output")
#check if outfolder exists if not create it
if not os.path.exists(output_folder):
    os.makedirs(output_folder)
In [32]:
os.listdir(data_folder)
Out[32]:
['data.xlsx']
In [35]:
# Read Sheet
dataSheet = 'data.xlsx'
dataSheetPath = os.path.join(data_folder, dataSheet)
xl = pd.ExcelFile(dataSheetPath)
sheet_names = xl.sheet_names
print("Found", len(sheet_names), "sheets")
for sheet in sheet_names:
    df = xl.parse(sheet)
df
Found 1 sheets
Out[35]:
Unnamed: 0 145 153 161 169 177 185 193 201 209 ... 289 297 305 313 321 329 337 345 353 361
0 2004 0.383 0.407 0.339 0.434 0.431 0.326 0.408 0.480 0.511 ... 0.797 0.786 0.732 0.562 0.694 0.630 0.587 0.535 0.501 0.502
1 2005 0.431 0.391 0.367 0.356 0.357 0.309 0.298 0.075 0.303 ... 0.626 0.302 0.648 0.736 0.662 0.604 0.473 0.442 0.476 0.500
2 2006 0.348 0.462 0.475 0.467 0.304 0.472 0.379 0.104 0.050 ... 0.716 0.665 0.487 0.711 0.615 0.426 0.459 0.478 0.521 0.602
3 2007 0.344 0.247 0.355 0.170 0.368 0.379 0.265 0.226 0.187 ... 0.565 0.731 0.650 0.655 0.492 0.403 0.405 0.484 0.600 0.664
4 2008 0.330 0.346 0.150 0.219 0.285 0.307 0.303 0.326 0.130 ... 0.590 0.762 0.699 0.675 0.521 0.359 0.415 0.391 0.409 0.472
5 2009 0.375 0.339 0.318 0.312 0.230 0.201 0.177 0.285 0.283 ... 0.761 0.765 0.760 0.481 0.512 0.562 0.481 0.406 0.480 0.535
6 2010 0.370 0.405 0.258 0.386 0.469 0.413 0.291 0.199 0.246 ... 0.547 0.610 0.589 0.530 0.594 0.400 0.239 0.482 0.494 0.508
7 2011 0.328 0.366 0.194 0.322 0.185 0.392 0.226 0.361 0.196 ... 0.802 0.775 0.728 0.637 0.544 0.446 0.394 0.452 0.513 0.502
8 2012 0.315 0.294 0.297 0.347 0.426 0.441 0.482 0.150 0.419 ... 0.814 0.562 0.531 0.734 0.699 0.399 0.517 0.435 0.441 0.469
9 2013 0.242 0.337 0.419 0.419 0.384 0.075 0.062 0.296 0.270 ... 0.718 0.673 0.810 0.704 0.735 0.594 0.526 0.445 0.420 0.425
10 2014 0.388 0.378 0.330 0.292 0.326 0.114 0.216 0.164 0.253 ... 0.666 0.678 0.740 0.785 0.656 0.689 0.599 0.373 0.431 0.391
11 2015 0.339 0.253 0.399 0.399 0.452 0.476 0.402 0.475 0.469 ... 0.677 0.815 0.741 0.754 0.568 0.536 0.536 0.477 0.466 0.540
12 2016 0.412 0.017 0.533 0.607 0.203 0.533 0.489 0.503 0.236 ... 0.746 0.622 0.812 0.715 0.685 0.617 0.507 0.437 0.491 0.478
13 2017 0.322 0.300 0.251 0.263 0.316 0.472 0.298 0.451 0.423 ... 0.806 0.775 0.796 0.732 0.614 0.557 0.465 0.425 0.416 0.476
14 2018 0.187 0.333 0.340 0.377 0.309 0.349 0.278 0.418 0.386 ... 0.806 0.774 0.698 0.739 0.460 0.592 0.483 0.359 0.354 0.470

15 rows × 29 columns

In [41]:
mltdf = pd.melt(df, id_vars='Unnamed: 0')
dateparser = lambda x: datetime.datetime.strptime(x, '%Y-%j')
mltdf.columns = ['year', 'julianDay', 'value']
mltdf['date'] = mltdf['year'].astype(str) + "-" + mltdf['julianDay'].astype(str)
mltdf['date'] = mltdf['date'].apply(dateparser)
mltdf.index = mltdf['date']
mltdf = mltdf[['value']]
mltdf
Out[41]:
value
date
2004-05-24 0.383
2005-05-25 0.431
2006-05-25 0.348
2007-05-25 0.344
2008-05-24 0.330
2009-05-25 0.375
2010-05-25 0.370
2011-05-25 0.328
2012-05-24 0.315
2013-05-25 0.242
2014-05-25 0.388
2015-05-25 0.339
2016-05-24 0.412
2017-05-25 0.322
2018-05-25 0.187
2004-06-01 0.407
2005-06-02 0.391
2006-06-02 0.462
2007-06-02 0.247
2008-06-01 0.346
2009-06-02 0.339
2010-06-02 0.405
2011-06-02 0.366
2012-06-01 0.294
2013-06-02 0.337
2014-06-02 0.378
2015-06-02 0.253
2016-06-01 0.017
2017-06-02 0.300
2018-06-02 0.333
... ...
2004-12-18 0.501
2005-12-19 0.476
2006-12-19 0.521
2007-12-19 0.600
2008-12-18 0.409
2009-12-19 0.480
2010-12-19 0.494
2011-12-19 0.513
2012-12-18 0.441
2013-12-19 0.420
2014-12-19 0.431
2015-12-19 0.466
2016-12-18 0.491
2017-12-19 0.416
2018-12-19 0.354
2004-12-26 0.502
2005-12-27 0.500
2006-12-27 0.602
2007-12-27 0.664
2008-12-26 0.472
2009-12-27 0.535
2010-12-27 0.508
2011-12-27 0.502
2012-12-26 0.469
2013-12-27 0.425
2014-12-27 0.391
2015-12-27 0.540
2016-12-26 0.478
2017-12-27 0.476
2018-12-27 0.470

420 rows × 1 columns

In [64]:
mltdf.plot(kind='line', grid=True, legend=True)
Out[64]:
<matplotlib.axes._subplots.AxesSubplot at 0x1b0203fc4e0>
In [71]:
mltdf.resample('Y').mean()
Out[71]:
value
date
2004-12-31 0.528143
2005-12-31 0.473250
2006-12-31 0.493536
2007-12-31 0.461179
2008-12-31 0.442857
2009-12-31 0.468429
2010-12-31 0.472071
2011-12-31 0.486821
2012-12-31 0.497929
2013-12-31 0.469179
2014-12-31 0.461321
2015-12-31 0.540464
2016-12-31 0.483643
2017-12-31 0.507536
2018-12-31 0.461107
In [72]:
mltdf.resample('Y').mean().plot(kind='line', grid=True, legend=True)
Out[72]:
<matplotlib.axes._subplots.AxesSubplot at 0x1b0205f6dd8>
In [67]:
mltdf.resample('Y').max()
Out[67]:
value
date
2004-12-31 0.797
2005-12-31 0.769
2006-12-31 0.798
2007-12-31 0.774
2008-12-31 0.794
2009-12-31 0.769
2010-12-31 0.748
2011-12-31 0.802
2012-12-31 0.814
2013-12-31 0.842
2014-12-31 0.785
2015-12-31 0.815
2016-12-31 0.812
2017-12-31 0.814
2018-12-31 0.806
In [68]:
mltdf.resample('Y').max().plot(kind='line', grid=True, legend=True)
Out[68]:
<matplotlib.axes._subplots.AxesSubplot at 0x1b0204f9710>
In [69]:
mltdf.resample('Y').min()
Out[69]:
value
date
2004-12-31 0.326
2005-12-31 0.075
2006-12-31 0.050
2007-12-31 0.151
2008-12-31 0.130
2009-12-31 0.177
2010-12-31 0.199
2011-12-31 0.185
2012-12-31 0.150
2013-12-31 0.062
2014-12-31 0.114
2015-12-31 0.230
2016-12-31 0.017
2017-12-31 0.251
2018-12-31 0.154
In [70]:
mltdf.resample('Y').min().plot(kind='line', grid=True, legend=True)
Out[70]:
<matplotlib.axes._subplots.AxesSubplot at 0x1b020570400>
In [73]:
# Composite
compdf = mltdf.resample('Y').mean()
compdf['min'] = mltdf.resample('Y').min()['value']
compdf['max'] = mltdf.resample('Y').max()['value']
compdf
Out[73]:
value min max
date
2004-12-31 0.528143 0.326 0.797
2005-12-31 0.473250 0.075 0.769
2006-12-31 0.493536 0.050 0.798
2007-12-31 0.461179 0.151 0.774
2008-12-31 0.442857 0.130 0.794
2009-12-31 0.468429 0.177 0.769
2010-12-31 0.472071 0.199 0.748
2011-12-31 0.486821 0.185 0.802
2012-12-31 0.497929 0.150 0.814
2013-12-31 0.469179 0.062 0.842
2014-12-31 0.461321 0.114 0.785
2015-12-31 0.540464 0.230 0.815
2016-12-31 0.483643 0.017 0.812
2017-12-31 0.507536 0.251 0.814
2018-12-31 0.461107 0.154 0.806
In [76]:
compdf.plot(kind='line', grid=True, legend=True)
Out[76]:
<matplotlib.axes._subplots.AxesSubplot at 0x1b021715e10>
In [83]:
compdf.resample('D').interpolate(method='cubic').plot(kind='line', grid=True, legend=True)
Out[83]:
<matplotlib.axes._subplots.AxesSubplot at 0x1b02211e0f0>
In [ ]:
 
In [ ]:
 
In [90]:
# 2004
mlt2004df = mltdf[mltdf.index.to_series().dt.year == 2004]
mlt2004df.resample('D').interpolate(method='cubic').plot(kind='line', grid=True, legend=True)
Out[90]:
<matplotlib.axes._subplots.AxesSubplot at 0x1b02237b4e0>
In [121]:
# Export to images
years = mltdf.index.to_series().dt.year.unique()
for year in years:
    _df = mltdf[mltdf.index.to_series().dt.year == year]
    _df.columns = ['NDVI']
    #title = "Year: %s - Min: %s, Max: %s" % (year, _df['NDVI'].min(), _df['NDVI'].max())
    _df = _df.resample('D').interpolate(method='cubic')
    minval = round(_df['NDVI'].min(), 2)
    maxval = round(_df['NDVI'].max(), 2)
    title = "Year: %s - Min: %s, Max: %s" % (year, minval, maxval)
    _df.plot(kind='line', grid=True, legend=True, title= title)
    plotfile = os.path.join(output_folder, str(year))
    plt.savefig(plotfile)
In [115]:
 
Out[115]:
'Year: 2004 - Min: 0.326, Max: 0.797'
In [ ]: