Skip to content

Instantly share code, notes, and snippets.

@markusritschel
Created May 14, 2020 10:57
Show Gist options
  • Save markusritschel/c932421b25b4940720bcee488d78152c to your computer and use it in GitHub Desktop.
Save markusritschel/c932421b25b4940720bcee488d78152c to your computer and use it in GitHub Desktop.
A simple way to find all the breaks in the time stamp, i.e., changes in consecutive times longer than some fixed threshold
import xarray as xr
ds = xr.open_dataset('data.nc')
# find temporal breaks that are larger than a given threshold
thrsh = 300 # threshold in seconds
interval_ends = ds.time.diff('time').dt.seconds > thrsh # gives a boolean array with True at the end of each interval longer than the threshold
# plot the intervals
import matplotlib.pyplot as plt
plt.figure(figsize=(20,6))
ds.z.plot()
ds.where(interval_ends).z.plot(marker='<')
ds.where(interval_ends.shift(time=-1)).z.plot(marker='>') # .shift(time=-1) shifts the array along the axis/dimension 'time', hence, takes the index one step before, i.e. at the start of the interval
# label phases
## first, set edge markers for start and end of each break phase
ds['edges'] = interval_ends + interval_ends.shift(time=-1)
### optionally: you might need to convert this into numeric values
ds['edges'] = ds.edges.astype(float)
## now label each phase with a unique number
ds['phases'] = ds.edges.cumsum()
# group by phases
ds.groupby('phases'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment