# PPSD spectrogram time restrictions

**URL:** https://discourse.obspy.org/t/ppsd-spectrogram-time-restrictions/1401
**Category:** Usage Questions
**Created:** [April 15, 2022, 10:05am UTC](https://discourse.obspy.org/t/ppsd-spectrogram-time-restrictions/1401 "2022-04-15T10:05:48Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Korhan](https://discourse.obspy.org/user_avatar/discourse.obspy.org/korhan/32/604_2.png) [@Korhan](https://discourse.obspy.org/u/Korhan)
#### Post date: [April 15, 2022, 10:05am UTC](https://discourse.obspy.org/t/ppsd-spectrogram-time-restrictions/1401/1 "2022-04-15T10:05:48Z")

</div>

Dear all,

I have a big .npz file including previous PPSD calculations. I want to restrict the days to a specific time range and plot the spectrogram with PPSD.plot\_spectrogram().

For instance:

```
from obspy.signal import PPSD
from obspy.core import UTCDateTime

start = UTCDateTime("2022-04-01")
end = UTCDateTime("2022-04-05")

ppsd = PPSD.load_npz("data.npz")
ppsd.calculate_histogram(starttime=start, endtime=end)
ppsd.plot_spectrogram()

```

This does not plot the spectrogram within the set time range. There is no temporal restrictions option within the plot\_spectrogram() like the plot\_temporal().

Could you please help me to find a solution?

Thank you,

Korhan

---

<div class="post-metadata">

### Author: ![megies](https://discourse.obspy.org/user_avatar/discourse.obspy.org/megies/32/9_2.png) [@megies](https://discourse.obspy.org/u/megies)
#### Post date: [April 19, 2022, 8:32am UTC](https://discourse.obspy.org/t/ppsd-spectrogram-time-restrictions/1401/2 "2022-04-19T08:32:56Z")

</div>

I can think about the following options..

### 1. short term / quick fix

After loading in your npz, you can modify the data yourself, i.e. get rid of unwanted portions. You would want to modify `ppsd._binned_psds` and `ppsd._times_processed`. Both are just plain lists, so you need to make sure to modify them in the exact same way, or your data timestamping will be wrong when plotted.  
From a quick look at the code, you should be able to use `ppsd._stack_selection()` for this purpose to create a yes/no mask.

```python
from obspy import UTCDateTime
from obspy.signal.tests.test_spectral_estimation import _get_ppsd
ppsd = _get_ppsd()
starttime = UTCDateTime("2011-03-31T00:10:00")
endtime = UTCDateTime("2011-03-31T01:20:00")
mask = ppsd._stack_selection(starttime=starttime, endtime=endtime)
ppsd._times_processed = [t for i, t in enumerate(ppsd._times_processed) if mask[i]]
ppsd._binned_psds = [t for i, t in enumerate(ppsd._binned_psds) if mask[i]]
ppsd.plot_spectrogram()

```

If you want to avoid loading the npz over and over for different stacks, you could make backups of those two variables before cropping them.

Just be aware that private methods (starting with an underscore) are not guaranteed to stay as is and might get changed without notice.

### 2. mid term

You could subclass `PPSD` and add the above programmatically into `plot_spectrogram`, just overwriting that one routine, similar to how it is done in `plot_temporal`.

```python
from obspy import PPSD

class MyPPSD(PPSD):
    def plot_spectrogram(...., **temporal_restrictions):
        ...
        if temporal_restrictions:
            ...
        else:
            ...
        ...

```

### 3. long term

You could add this change to obspy code and make a pull request 😉

---

<div class="post-metadata">

### Author: ![Korhan](https://discourse.obspy.org/user_avatar/discourse.obspy.org/korhan/32/604_2.png) [@Korhan](https://discourse.obspy.org/u/Korhan)
#### Post date: [April 20, 2022, 8:10am UTC](https://discourse.obspy.org/t/ppsd-spectrogram-time-restrictions/1401/3 "2022-04-20T08:10:38Z")

</div>

Just tried the quick fix and it works like a charm. Thank you @megies for the solution and further ideas. 😄
