# Monthly Ice Rates of Change

Compute the Change in ice concentration month over month.

Walt: "I would do it by taking extent on last day of month minus the last day of the previous month. Then divide by # of days in month to get daily rate. I used the 5-day values."

``````

In [ ]:

import calendar

import numpy as np
import pandas as pd
from pandas import ExcelWriter

import seaice_timeseries.api as si

``````
``````

In [ ]:

km2_2_mi2 = 0.386102
CLIMATOLOGY_YEARS = [1981, 2010]
climatology_string = '1981-2010'

``````
``````

In [ ]:

month_dict = {}
for i in range(1,13):
month_dict[i] = calendar.month_name[i]

``````
``````

In [ ]:

def rolling_mean(df, column, num_days=5, min_valid=2):
return pd.rolling_mean(df[column], window=num_days, min_periods=min_valid)

``````
``````

In [ ]:

si.c.DATA_BASE_DIR = '/Users/savoie/tmp/wget-test/'

``````
``````

In [ ]:

dir(si)

``````
``````

In [ ]:

df_in = si.all_daily('north', si.c.DATA_BASE_DIR)

``````
``````

In [ ]:

``````
``````

In [ ]:

df = df_in.copy()
df['day'] = df.index.day
df['month'] = df.index.month
df['year'] = df.index.year

``````
``````

In [ ]:

print df.loc['1987-12-01':'1988-02-01']

``````

Set missing data. By looking that the number of interpolated days is equal to the last() day value we find missing data.

``````

In [ ]:

a = df.groupby([df.index.year, df.index.month])
r = a['interpolated_extent'].count() == a['day'].last()

``````
``````

In [ ]:

r[r == False]

``````

Do the diff and compute delta ice.

``````

In [ ]:

a = df.groupby([df.index.year, df.index.month]).last()
a['ice change Mkm^2 per month'] = a['5 Day'].diff(periods=1)

``````

``````

In [ ]:

a.loc[r == False, ['5 Day', 'ice change Mkm^2 per month']] = None

``````

now compute the other derived vars

``````

In [ ]:

a['ice change km^2 per day'] = (a['ice change Mkm^2 per month'] / a['day']) * 1000000
a['ice change mi^2 per month'] = a['ice change Mkm^2 per month'] * km2_2_mi2 * 1000000
a['ice change mi^2 per day'] = a['ice change km^2 per day'] * km2_2_mi2

``````
``````

In [ ]:

a = a.unstack(1)
a = a.rename(columns=month_dict)

``````

Compute Average data.

``````

In [ ]:

df = si.monthly_rates_of_change('north', si.c.DATA_BASE_DIR)
df = df.unstack(1)
df = df.rename(columns=month_dict)

``````
``````

In [ ]:

``````
``````

In [ ]:

clim = si.climatology_average_rates_of_change('north', si.c.DATA_BASE_DIR)

``````
``````

In [ ]:

sheets = [u'ice change Mkm^2 per month', u'ice change km^2 per day', u'ice change mi^2 per month', u'ice change mi^2 per day']
sheet = sheets[1]

``````
``````

In [ ]:

clim[sheet].name ='wtf'

``````
``````

In [ ]:

clim[sheet].to_frame().rename(index=month_dict).T

``````
``````

In [ ]:

bottom_offset = 3
writer = ExcelWriter('../output/Sea_Ice_Rates_of_Change.xls', engine='xlsxwriter')

sheets = [u'ice change Mkm^2 per month', u'ice change km^2 per day', u'ice change mi^2 per month', u'ice change mi^2 per day']

for sheet in sheets:
a[sheet].to_excel(writer, "{0}".format(sheet), float_format="%.3f", startrow=1, startcol=0)
writer.sheets[sheet].write_string(0,0, "{} from 5 Day Averaged Daily Values".format( sheet ))
clim[sheet].name = climatology_string
clim[sheet].to_frame().T.to_excel(writer, "{0}".format(sheet),
startrow=a[sheet].index.size + bottom_offset, float_format="%.3f")

workbook = writer.book
# add colors blue with blue
'font_color': '#06009C'})

# add colors red with red
'font_color': '#9C0006'})

for sheet in sheets:
worksheet = writer.sheets[sheet]
worksheet.conditional_format('B3:ZZ369', {'type':     'cell',
'criteria': '>',
'value':    0,
'format':   format1})

worksheet.conditional_format('B3:ZZ369', {'type':     'cell',
'criteria': '<',
'value':    0,
'format':   format2})

writer.save()

``````
``````

In [ ]:

sheets = [u'ice change Mkm^2 per month', u'ice change km^2 per day', u'ice change mi^2 per month', u'ice change mi^2 per day']

``````
``````

In [ ]:

``````