#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Sun Feb 14 14:51:58 EST 2021 if __name__ == '__main__': from misc.timer import Timer tt = Timer(f'start {__file__}') import sys, os.path, os, glob, datetime import xarray as xr, numpy as np, pandas as pd import matplotlib.pyplot as plt #more imports import xfilter lowpass = lambda x: x.filter.lowpass(1/40, dim='year', padtype='even') # if __name__ == '__main__': tt.check('end import') # #start from here #read from csv file ifile = os.path.join(os.path.dirname(__file__), 'hurricanes_adjusted_1851-2019.csv') df = pd.read_csv(ifile, index_col='YEAR') print('[loaded]:', ifile) #convert to dataset ds = xr.Dataset(df) newnames = {name: '_'.join(name.split()) for name in list(ds.data_vars)} newnames['YEAR'] = 'year' ds = ds.rename(newnames) ds['Upper_Adjusted_Hurricanes'] = ds['Median_Adjusted_Hurricanes'] + ds['uncertainty'] ds['Lower_Adjusted_Hurricanes'] = ds['Median_Adjusted_Hurricanes'] - ds['uncertainty'] ds['AHlp'] = ds['Median_Adjusted_Hurricanes'].pipe(lowpass).assign_attrs(long_name='40-year-Lowpass Median Adjusted Hurricanes') ds['upperAHlp'] = ds['Upper_Adjusted_Hurricanes'].pipe(lowpass) ds['lowerAHlp'] = ds['Lower_Adjusted_Hurricanes'].pipe(lowpass) ds['AMHlp'] = ds['Median_Adjusted_Major_Hurricanes'].pipe(lowpass).assign_attrs(long_name='40-year-Lowpass Median Adjusted Major Hurricanes') ds['Upper_Adjusted_Major_Hurricanes'] = ds['Median_Adjusted_Major_Hurricanes'] + ds['uncertainty'] ds['Lower_Adjusted_Major_Hurricanes'] = ds['Median_Adjusted_Major_Hurricanes'] - ds['uncertainty'] if __name__ == '__main__': from wyconfig import * #my plot settings fig, ax = plt.subplots() ds['Median_Adjusted_Hurricanes'].plot(label='Median_Adjusted_Hurricanes', ax=ax) ax.fill_between(ds.year, ds['lowerAHlp'].pipe(lowpass), ds['upperAHlp'].pipe(lowpass), alpha=0.4, color='C1' ) ds['AHlp'].pipe(lowpass).plot(label='40-year lowpass', ax=ax) ds['AMHlp'].pipe(lowpass).plot(label='40-year lowpass major hurricanes', ax=ax) ax.legend(frameon=False, ncol=2) ax.set_ylabel('#') figname = __file__.replace('.py', f'_{tt.today()}.png') if len(sys.argv) > 1 and sys.argv[1] == 'savefig': plt.savefig(figname) print('[saved]:', figname) tt.check(f'**Done**') plt.show()