#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Fri Sep 15 09:51:11 EDT 2023 if __name__ == '__main__': import sys,os from misc.timer import Timer tt = Timer(f'[{os.getcwd()}] start ' + ' '.join(sys.argv)) import sys, os.path, os, glob, datetime import xarray as xr, numpy as np, pandas as pd, matplotlib.pyplot as plt #more imports wython = '/tigress/wenchang/wython' if wython not in sys.path: sys.path.append(wython); print('added to python path:', wython) from misc import get_kws_from_argv ##old way #from tc_monitor_AM2p5_amipLMR2019SST0850ic_tigercpu_intelmpi_18_540PE import ds_counts_yearly as ds_am2p5 #from tc_monitor_HIRAM_amipLMR2019SST0850ic_tigercpu_intelmpi_18_540PE import ds_counts_yearly as ds_hiram from misc.seasons import sel_season # if __name__ == '__main__': tt.check('end import') # #start from here basin = 'NA' #sediment HU #ifile = '/tigress/wenchang/analysis/LMHU/data/Lizzie/sedimentHU_v20220711ens_noCaySal_wy_max_lp40_count.nc' ifile = '/tigress/wenchang/analysis/LMHU/data/Lizzie/sedimentHU_v20220711ens_noCaySal_wy_median_lp40_count.nc' da = xr.open_dataset(ifile)['sedimentHUcount'].sel(member='sediment_estimate')#.sel(year=slice(850, year_end)) da_lizzie = da da = xr.open_dataset(ifile)['sedimentHUcount'].sel(member='lower_estimate')#.sel(year=slice(850, year_end)) da_lower = da da = xr.open_dataset(ifile)['sedimentHUcount'].sel(member='upper_estimate')#.sel(year=slice(850, year_end)) da_upper = da #sst HU ifile = '/tigress/wenchang/analysis/LMHU/data/LMR/LMR2019_ssttc_rMDRtransformed18702000_HU_lp40_CI95.nc' da = xr.open_dataset(ifile)['HU'].sel(quantile=0.5)#.sel(year=slice(850, year_end)) da_ssthu = da #HiRAM HU ifile = '/projects/w/wenchang/analysis/TC/HIRAM/amipLMR2019SST0850ic_tigercpu_intelmpi_18_540PE/netcdf/tc_counts.TS30.ens01.0850-1999.nc' ds_hiram_hu = xr.open_dataset(ifile).rename(en='ens') #HiRAM TC ifile = '/projects/w/wenchang/analysis/TC/HIRAM/amipLMR2019SST0850ic_tigercpu_intelmpi_18_540PE/netcdf/tc_counts.TS17.ens01.0850-1999.nc' ds_hiram_tc = xr.open_dataset(ifile).rename(en='ens') #HiRAM seeds ifile = '/projects/w/wenchang/analysis/TC/HIRAM/amipLMR2019SST0850ic_tigercpu_intelmpi_18_540PE/netcdf/tc_counts.seeds.ens01.0850-1999.minohour12.nc' ds_hiram_sd = xr.open_dataset(ifile).rename(en='ens') ifile_seeds = ifile #save data for later use #HiRAM transition probability ifile = 'prob_HIRAM_amipLMR2019SST0850ic_tigercpu_intelmpi_18_540PE_ens01_0850-1999_NAmean.nc' da = xr.open_dataarray(ifile)#.groupby('time.year').mean('time') da_prob = da ** 2 #adjust for HU from TC ifile_prob = ifile #save data for later use #HiRAM nseeds x prob time_prob = da_prob.time da_nseedsXprob = ds_hiram_sd[basin].assign_coords(time=time_prob) * da_prob #HiRAM transition probability from PI ifile = 'probPI_HIRAM_amipLMR2019SST0850ic_tigercpu_intelmpi_18_540PE_ens01_0850-1999_NAmean.nc' da = xr.open_dataarray(ifile)#.groupby('time.year').mean('time') da_probPI = da**2 #HiRAM transition probability from Vshear ifile = 'probVshear_HIRAM_amipLMR2019SST0850ic_tigercpu_intelmpi_18_540PE_ens01_0850-1999_NAmean.nc' da = xr.open_dataarray(ifile)#.groupby('time.year').mean('time') da_probVshear = da**2 #HiRAM transition probability from chi ifile = 'probchi_HIRAM_amipLMR2019SST0850ic_tigercpu_intelmpi_18_540PE_ens01_0850-1999_NAmean.nc' da = xr.open_dataarray(ifile)#.groupby('time.year').mean('time') da_probchi = da**2 #percent decline from 1351-1450 to 1451-1550 if True: dc = {} #timeslice_ref = slice('1900', '1999') #yearslice_ref = slice(1900, 1999) timeslice_ref = slice('1351', '1550') yearslice_ref = slice(1351, 1550) norm = lambda da: da/da.sel(year=yearslice_ref).mean('year')*100 - 100 pct_diff = lambda da: da.sel(year=slice(1451, 1550)).mean('year') - da.sel(year=slice(1351,1450)).mean('year') label = 'sediment' da = da_lizzie da = da.pipe(norm).pipe(pct_diff) dc[label] = da label = 'SST' da = da_ssthu da = da.pipe(norm).pipe(pct_diff) dc[label] = da label = 'HiRAM' da = ds_hiram_hu[basin].groupby('time.year').sum('time') da = da.pipe(norm).pipe(pct_diff) dc[label] = da """ label = 'seeds and prob' da = da_nseedsXprob.groupby('time.year').sum('time') da = da.pipe(norm).pipe(pct_diff) dc[label] = da """ label = 'seeds' da = ds_hiram_sd[basin].groupby('time.month') * da_prob.sel(time=timeslice_ref).groupby('time.month').mean('time') da = da.groupby('time.year').mean('time') da = da.pipe(norm).pipe(pct_diff) dc[label] = da label = 'prob' da = ds_hiram_sd[basin].sel(time=timeslice_ref).groupby('time.month').mean('time') * da_prob.groupby('time.month') da = da.groupby('time.year').mean('time') da = da.pipe(norm).pipe(pct_diff) dc[label] = da label = 'prob of shear' da = ds_hiram_sd[basin].sel(time=timeslice_ref).groupby('time.month').mean('time') * da_probVshear.groupby('time.month') da = da.groupby('time.year').mean('time') da = da.pipe(norm).pipe(pct_diff) dc[label] = da label = 'prob of PI' da = ds_hiram_sd[basin].sel(time=timeslice_ref).groupby('time.month').mean('time') * da_probPI.groupby('time.month') da = da.groupby('time.year').mean('time') da = da.pipe(norm).pipe(pct_diff) dc[label] = da label = 'prob of $\chi$' da = ds_hiram_sd[basin].sel(time=timeslice_ref).groupby('time.month').mean('time') * da_probchi.groupby('time.month') da = da.groupby('time.year').mean('time') da = da.pipe(norm).pipe(pct_diff) dc[label] = da ds = xr.Dataset(dc) if __name__ == '__main__': from wyconfig import * #my plot settings import xfilter iframe = get_kws_from_argv('iframe', None) if iframe is not None: iframe = int(iframe) fig, ax = plt.subplots() df = ds.mean('ens').to_pandas() df.plot.bar(rot=30, ax=ax) #if iframe is not None: # df.where(np.arange(df.size)