# Import Libraries
import pandas as pd
import datetime
import os
import matplotlib.pyplot as plt
%matplotlib inline
# 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)
os.listdir(data_folder)
# 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
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
mltdf.plot(kind='line', grid=True, legend=True)
mltdf.resample('Y').mean()
mltdf.resample('Y').mean().plot(kind='line', grid=True, legend=True)
mltdf.resample('Y').max()
mltdf.resample('Y').max().plot(kind='line', grid=True, legend=True)
mltdf.resample('Y').min()
mltdf.resample('Y').min().plot(kind='line', grid=True, legend=True)
# Composite
compdf = mltdf.resample('Y').mean()
compdf['min'] = mltdf.resample('Y').min()['value']
compdf['max'] = mltdf.resample('Y').max()['value']
compdf
compdf.plot(kind='line', grid=True, legend=True)
compdf.resample('D').interpolate(method='cubic').plot(kind='line', grid=True, legend=True)
# 2004
mlt2004df = mltdf[mltdf.index.to_series().dt.year == 2004]
mlt2004df.resample('D').interpolate(method='cubic').plot(kind='line', grid=True, legend=True)
# 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)