events and pickle library

Hi everyone,

Something weird is happening when using the pickle library.

I use a python code to get the event list:
# get the events
catalog=fdsn.get_events(starttime=starttime_check,
                         endtime=endtime_check,
                         minmagnitude=cfg.mag_thres,
                         magnitudetype="MLh",
                         includeallorigins=False,
                         includeallmagnitudes=False,
                         includearrivals=True,
                         orderby="time-asc",
                         limit=cfg.event_limit)

# and save them into a file for later use
with open('catalog','w') as f: # tried with wb also
     pickle.dump(catalog,f)

From another script:
with open('catalog','r') as f:
     catalog=pickle.load(f)
     print catalog

When I run both scripts sequentially, I have no problem. Even if I run, later, the second one script repetitively.

However, if, for instance, I reboot my computer and run only the second script, I get the error (at the print statement):

AttributeError: 'UTCDateTime' object has no attribute '_UTCDateTime__ns'

Any ideas? What could be the cause of "destroying" the data?

Thanks,
Nikos

Hi Nikos,

are you using the same ObsPy + Python version every time? Pickling is
really fragile when the internal structure of objects is changed. We do
make an effort to support cross-version pickling (of course this can
only work in a forward compatible way - i.e. pickled objects from an
older ObsPy version should be readable on newer ObsPy versions) and if
we missed something here please open an issue on github with a small but
complete reproducible example.

Some general advice: Best try to avoid pickling data if you can, mainly
for compatibility reasons. Your specific example would work by just doing

cat.write("catalog.xml", format="quakeml")

Which you can then read in subsequent scripts. This works as long as the
chosen data format can represent everything in the objects. The
`client.get_events(...)` method alternatively also supports a `filename`
argument which can be used to download the catalog directly to a file.

Cheers,

Lion