Created
July 12, 2022 01:09
-
-
Save nickjevershed/5b3249a68cec14a1e5babbb7af28acbb to your computer and use it in GitHub Desktop.
This file contains hidden or 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 pandas as pd | |
filenames = ['observatory-hill','parramatta','wollongong'] | |
# filenames = ['observatory-hill'] | |
# filename = 'lismore' | |
for filename in filenames: | |
df = pd.read_csv(f'{filename}.csv') | |
df['date'] = df.Year.map(str) + "-" + df.Month.map(str) + "-" + df.Day.map(str) | |
df['short_date'] = "1900" + "-" + df.Month.map(str) + "-" + df.Day.map(str) | |
df['date'] = pd.to_datetime(df['date'], format="%Y-%m-%d") | |
df['quarter'] = df['date'].dt.quarter | |
df['fy'] = df['date'].dt.to_period('Q-JUN').dt.qyear.apply(lambda x: str(x-1) + "-" + str(x)) | |
df['season'] = df['date'].dt.month%12 // 3 + 1 | |
df2 = df[df['short_date'] != "1900-2-29"].copy() | |
# df2 = df | |
df2['rainfall'] = df2['Rainfall amount (millimetres)']/df2['Period over which rainfall was measured (days)'] | |
df2['rainfall'] = df2['rainfall'].fillna(0) | |
df2['rainfall_cum'] = df2.groupby(['Year'])['rainfall'].apply(lambda x: x.cumsum()) | |
df2 = df2.rename(columns={"Year": "year"}) | |
df2['year'] = pd.to_numeric(df2['year']) | |
# def makeShortDate(row): | |
# if row['quarter'] == 1 or row['quarter'] == 2: | |
# return "1901" + "-" + row['short_date'] | |
# else: | |
# return "1900" + "-" + row['short_date'] | |
# | |
# | |
# df2['short_date'] = df2.apply(makeShortDate, axis=1) | |
df2['short_date'] = pd.to_datetime(df2['short_date'], format="%Y-%m-%d") | |
df2 = df2[df2['year'] >= 1900] | |
summer = df2[df2['season'] == 1] | |
summer_totals = summer[['fy','rainfall']].groupby(['fy']).sum() | |
short = df2[['rainfall_cum','date','short_date','year','fy']] | |
totals = short[short['year'] != 2022].groupby(['short_date']).median() | |
totals = totals.rename(columns={'rainfall_cum': "Median"}) | |
totals['Very dry'] = short[short['year'] != 2022].groupby(['short_date']).quantile(0.10)['rainfall_cum'] | |
totals['Very wet'] = short[short['year'] != 2022].groupby(['short_date']).quantile(0.90)['rainfall_cum'] | |
totals['Extremely dry'] = short[short['year'] != 2022].groupby(['short_date']).quantile(0.01)['rainfall_cum'] | |
totals['Extremely wet'] = short[short['year'] != 2022].groupby(['short_date']).quantile(0.99)['rainfall_cum'] | |
current_rainfall = df2[df2['year'] == 2022].copy().set_index('short_date') | |
totals['2022 to date'] = current_rainfall['rainfall_cum'] | |
# totals = totals[:"1900-06-30"] | |
totals = totals.reset_index() | |
totals = totals.rename(columns={"short_date":"date"}) | |
totals[['date','Very dry','Extremely dry', 'Very wet', 'Extremely wet','Median','2022 to date']].to_csv(f'../cumulative-chart/assets/{filename}-year.csv', index=False) | |
df3 = df2.copy() | |
df3 = df3.drop(['date'], axis=1) | |
df3 = df3.rename(columns={"short_date":"date"}) | |
df3[['year','date','rainfall']].to_csv(f'../charts/assets/{filename}-year.csv', index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment