Skip to content

Instantly share code, notes, and snippets.

@jkingslake
Last active June 2, 2021 17:54
Show Gist options
  • Save jkingslake/0946ae96f065f8def236867f6895b5c9 to your computer and use it in GitHub Desktop.
Save jkingslake/0946ae96f065f8def236867f6895b5c9 to your computer and use it in GitHub Desktop.
perform the unstacking of the paleo PISM ensemble while keeping the attributes of the parameters
def unstack_paleo_pism_xarrays(PPds):
# save the values of id in a data variable before "unstacking", because unstacking will delete id.
PPds = PPds.assign(index=PPds["id"])
# also save the attributes of the ensemble parameters
A_par_esia = snapshots.par_esia.attrs
A_par_ppq = snapshots.par_ppq.attrs
A_par_prec = snapshots.par_prec.attrs
A_par_visc = snapshots.par_visc.attrs
# make MultiIndex object (this assumes that these do not change over time and that time is the first dimension)
arrays = [PPds.par_esia[0].values, PPds.par_ppq[0].values, PPds.par_prec[0].values, PPds.par_visc[0].values]
MI = pd.MultiIndex.from_arrays(arrays, names=('par_esia', 'par_ppq','par_prec','par_visc'))
# replace the id coordinate with this MultiIndex Object
PPds.coords['id'] = MI
# unstack
out = PPds.unstack(dim='id')
out.par_esia.attrs = A_par_esia
out.par_ppq.attrs = A_par_ppq
out.par_prec.attrs = A_par_prec
out.par_visc.attrs = A_par_visc
return out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment