# ---
# 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
# ---

# <h1><font size=12>
# Weather Derivates </h1>
# <h1> Precipitation Bogota Exploration - El Dorado Airport<br></h1>
#
# Developed by [<NAME>](mailto:<EMAIL>) <br>  
# 28 Nov 2018 
#

# Import modules to read and visualize.
import pandas as pd
import numpy as np
import pickle
# %pylab inline

# ## Data And Study area Section

# +
# Configure path to read txts.

path = '../../datasets/ideamBogota/'

from io import StringIO

# """Determine whether a year is a leap year."""
def isleapyear(year):
    if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
        return True
    return False

# Read only one year.

def loadYear(year):
    year=str(year)
    
    filedata =  open(path+ year +'.txt', 'r') 
    
    # Create a dataframe from the year's txt.
    
    columnNames=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
    precipitationYear =pd.read_csv(StringIO('\n'.join(' '.join(l.split()) for l in filedata)),sep=' ',header=None, names=columnNames,skiprows=lambda x: x in list(range(0,3)) , skipfooter=4  )
    
    # Sort data to solve problem of 28 days of Feb.
    
    for i in range(28,30):
        for j in reversed(range(1,12)):
            precipitationYear.iloc[i,j]= precipitationYear.iloc[i,j-1]
        
        
        # Fix leap years. 
        
        if isleapyear(int(year)) and i == 28:
            count = 1
        else:
            precipitationYear.iloc[i,1]= np.nan 
            
    
    # Fix problem related to months with 31 days.
    
    precipitationYear.iloc[30,11] = precipitationYear.iloc[30,6]
    precipitationYear.iloc[30,9]  = precipitationYear.iloc[30,5]
    precipitationYear.iloc[30,7] = precipitationYear.iloc[30,4]
    precipitationYear.iloc[30,6] = precipitationYear.iloc[30,3]
    precipitationYear.iloc[30,4] = precipitationYear.iloc[30,2]
    precipitationYear.iloc[30,2] = precipitationYear.iloc[30,1]
    
    for i in [1,3,5,8,10]:
        precipitationYear.iloc[30,i] = np.nan
    
    return precipitationYear

# Convert one year data frame to timeseries.

def convertOneYearToSeries(dataFrameYear,nYear):
    dataFrameYearT = dataFrameYear.T
    dates = pd.date_range(str(nYear)+'-01-01', end = str(nYear)+'-12-31' , freq='D')
    
    dataFrameYearAllTime = dataFrameYearT.stack()
    
    dataFrameYearAllTime.index = dates
    
    return dataFrameYearAllTime

# Concatenate all time series between a years range.

def concatYearsPrecipitation(startYear,endYear):
    
    precipitationAllTime = loadYear(startYear)
    precipitationAllTime = convertOneYearToSeries(precipitationAllTime,startYear)
    
    for i in range(startYear,endYear+1):
        tempPrecipitation=loadYear(i)
        tempPrecipitation= convertOneYearToSeries(tempPrecipitation,i)
        
        precipitationAllTime = pd.concat([precipitationAllTime,tempPrecipitation]) 
    
    return precipitationAllTime



# +
# Plot precipitation over a set of years.

startYear = 2005
endYear = 2015

precipitationAllTime = concatYearsPrecipitation(startYear,endYear)
meanAllTime = precipitationAllTime.mean()
ax = precipitationAllTime.plot(figsize=(20,10),grid=True,color='steelblue')
ax.axhline(y=meanAllTime, xmin=-1, xmax=1, color='r', linestyle='--', lw=2)
ax.set_xlabel('Year')
ax.set_ylabel('Precipitation Amount (mm)')


# +

columnNames=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
vv = pd.DataFrame(columns=columnNames)

for year in range(1972,2015+1):
    vv.loc[year] = [sum(loadYear(year)[x]) for x in loadYear(year).columns ]
# -

vv.head()

# +
filehandler = open("../../datasets/historicalMonths.pk","wb")
pickle.dump(vv,filehandler)
filehandler.close()

'''
file = open("Fruits.obj",'rb')
object_file = pickle.load(file)
file.close()
'''
# -

plt.figure(figsize=(12,8))
vv.boxplot()
plt.ylabel('Precipitation Amount (mm)')
plt.xlabel('Month')

vv

# +
# Plot precipitation over a set of years.

startYear = 2005
endYear = 2015

vdates = pd.date_range(str(startYear)+'-01-01', end = str(endYear)+'-12-31' , freq='M')

vvT = vv
dataVV = vvT.stack()

dataVV.index = vdates

precipitationAllTime = concatYearsPrecipitation(startYear,endYear)
meanAllTime = precipitationAllTime.mean()
ax = precipitationAllTime.plot(figsize=(20,10),grid=True,color='darkblue',label=' Daily Rainfall')
#ax.axhline(y=meanAllTime, xmin=-1, xmax=1, color='r', linestyle='--', lw=2)
ax.set_xlabel('Year')
ax.set_ylabel('Precipitation Amount (mm)')

ax2 =  dataVV.plot(kind='area',figsize=(20,10),grid=True,color='grey', label='Monthly Index')
plt.legend(loc=9,fontsize='x-large')
# -

precipitationAllTime.describe()



StarcoderdataJupyterScriptsDedupFiltered