Last active
October 18, 2017 14:57
-
-
Save rdmtinez/2ae1635697bff41aa1abb4e35fe9fc2a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import pandas as pd | |
import numpy as np | |
from matplotlib import pyplot as plt, gridspec | |
from matplotlib.backends.backend_pdf import PdfPages | |
from collections import OrderedDict | |
from datetime import datetime, timedelta | |
from IPython.core.interactiveshell import InteractiveShell | |
InteractiveShell.ast_node_interactivity = 'all' | |
wkdir = 'C:/Users/MAR8RNG/BOSCH-PROJECTS/1-Humidity/python/condensed_CSVs/' | |
bse_prm = 'basic/' | |
os.chdir(wkdir+bse_prm) | |
csv_list = os.listdir(wkdir+bse_prm) | |
###there are issues when resampling data as the name of the months are removed, consider | |
###that part to be part of the work after the CSV has been made instead 'edit RawDataCond | |
for csv in csv_list: | |
imei=csv[:-4] | |
imei | |
with open(csv) as f: | |
df=pd.read_csv(f, parse_dates=['DateTime'], index_col='DateTime') | |
#duplicate df['dVWC] column | |
df['Evap']=df['dVWC'] | |
#resampling requires index to be of type 'datetime'--done at import | |
#Hourly based resampling with 'Evap' column summed and the rest mean-ed | |
df=df.resample('H').agg({'Voltage':'mean', 'IMEI':'mean', | |
'DigiTemp':'mean','HM1':'mean', | |
'T1':'mean','VWC':'mean','dVWC':'mean', | |
'Evap':'sum'}) | |
#Unsure why this step has to be taken: reset index, re-establish to_datetime | |
#in order to insert the 'string' columns removed by the resampling | |
df.reset_index(inplace=True) | |
df['DateTime'] = pd.to_datetime(df['DateTime']) | |
#These are to simplify indexing | |
df.insert(1, column="Month", value = df['DateTime'].dt.strftime('%b')) | |
df.insert(2, column="Week", value = df['DateTime'].dt.week) | |
df.insert(3, column="Day", value = df['DateTime'].dt.strftime('%a-%d')) | |
#For iterating over months and obtaining weeks | |
mon_list = df['Month'].unique() | |
df.set_index(['Month'], drop=False, inplace=True) | |
mon_week = OrderedDict() | |
for m in mon_list: | |
if m not in mon_week: | |
mon_week[m] = df.loc[m]['Week'].unique() | |
#Month-Week (e.g. Dec-52) Selection | |
df.set_index(['Month', 'Week'], inplace=True) | |
df.sort_index(inplace=True) | |
##Dataframe | |
'df.head()' | |
df.head() | |
#dailydf | |
#resample at hourly interval so that that 'Evap' is the hourly difference | |
#at each observation | |
#take the daily averages of evaporation | |
#and make a monthly plot for this average | |
#df.plot[x='Datetime', y='Evap'] | |
#Save figs as PDFs | |
with PdfPages(wkdir+'figures/'+imei+'.pdf') as pdf: | |
#Montly-Weekly plots | |
for m in mon_list: | |
#create figure | |
fig = plt.figure() | |
#number of weekly plots in fig | |
nweeks = len(mon_week[m]) | |
#create grid in for figure | |
#gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1]) | |
grid = gridspec.GridSpec(3, 3, height_ratios=[5,3,3]) | |
#plot monthly and position in grid | |
month=plt.subplot(grid[0,:]) | |
df.loc[m].groupby('Day', sort=False).VWC.mean().plot(figsize=(16,9),ax=month,rot=45,legend=True,style='r') | |
df.loc[m].groupby('Day', sort=False).VWC.max().plot(figsize=(16,9),ax=month,rot=45,legend=True, style='ro') | |
df.loc[m].groupby('Day', sort=False).Evap.mean().plot(figsize=(16,9),ax=month,rot=45,legend=True,style='b') | |
df.loc[m].groupby('Day', sort=False).Evap.max().plot(figsize=(16,9),ax=month,rot=45,legend=True, style='bo') | |
#df.loc[m].groupby('Day', sort=False).Evap.min().plot(figsize=(16,9),ax=month, rot=45, legend=True) | |
#df.loc[m].groupby('Day', sort=False).Evap.max().plot(figsize=(16,9),ax=month, rot=45, legend=True) | |
month.legend(['VWC-mean', 'VWC-max', 'Evap-mean', 'Evap-max']) | |
n=5 | |
i=1 | |
for w in mon_week[m]: | |
#plot weekly and position in grid | |
week=plt.subplot(grid[i,5%n]) | |
df.loc[m,w].groupby('Day', sort=False).Evap.mean().plot(ax=week, | |
rot=45, grid=True) | |
#df.hist() | |
#sets up week grid positions | |
n-=1 | |
if n==2: | |
i=2 | |
n=5 | |
####don't forget to add labels#### | |
####graph the moving average, median, i.e smoothing#### | |
####graph all using the same y-tick window#### | |
plt.suptitle(imei, y=1) | |
grid.tight_layout(fig) | |
pdf.savefig(fig) | |
plt.close('all') | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment