\n",
"\n",
"**Warning**: Much of this functionality will eventually be moved to obspy, see [this PR](https://github.com/obspy/obspy/pull/2210).\n",
"\n",
"
\n",
"\n",
"The following demonstrates obsplus' ability to serialize obspy catalog objects into json. All such conversions should be lossless.\n",
"\n",
"## Get a catalog"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"execution": {
"iopub.execute_input": "2025-06-10T20:42:48.436832Z",
"iopub.status.busy": "2025-06-10T20:42:48.436440Z",
"iopub.status.idle": "2025-06-10T20:42:50.320085Z",
"shell.execute_reply": "2025-06-10T20:42:50.319559Z"
}
},
"outputs": [],
"source": [
"import obsplus\n",
"\n",
"crandall = obsplus.load_dataset(\"crandall_test\")\n",
"\n",
"cat = crandall.event_client.get_events()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## json conversions"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"execution": {
"iopub.execute_input": "2025-06-10T20:42:50.322162Z",
"iopub.status.busy": "2025-06-10T20:42:50.321974Z",
"iopub.status.idle": "2025-06-10T20:42:50.726022Z",
"shell.execute_reply": "2025-06-10T20:42:50.725492Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/runner/micromamba/envs/test/lib/python3.12/site-packages/obspy/core/util/attribdict.py:119: UserWarning: Setting attribute \"seed_string\" which is not a default attribute (\"network_code\", \"station_code\", \"channel_code\", \"location_code\", \"resource_uri\").\n",
" warnings.warn(msg)\n"
]
}
],
"source": [
"import obsplus\n",
"\n",
"# convert to json str\n",
"json_str = obsplus.cat_to_json(cat)\n",
"\n",
"# print sample\n",
"\n",
"# convert back\n",
"cat2 = obsplus.json_to_cat(json_str)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"execution": {
"iopub.execute_input": "2025-06-10T20:42:50.727897Z",
"iopub.status.busy": "2025-06-10T20:42:50.727725Z",
"iopub.status.idle": "2025-06-10T20:42:51.048022Z",
"shell.execute_reply": "2025-06-10T20:42:51.047475Z"
}
},
"outputs": [],
"source": [
"# json serialization should be lossless after handling Quantity Errors\n",
"# this won't be needed once obspy 1.2.0 is released.\n",
"import obspy.core.event as ev\n",
"\n",
"from obsplus.utils import yield_obj_parent_attr\n",
"\n",
"\n",
"def _remove_empty_quantity_errors(catalog):\n",
" \"\"\"\n",
" Copy the catalog and set all empty QunatityErrors to None.\n",
" This is needed to check equality of catalogs that may have\n",
" None or empty QuantityErrors.\n",
"\n",
" Fixed in https://github.com/obspy/obspy/pull/2185\n",
" \"\"\"\n",
" cat = catalog.copy()\n",
" for obj, parent, attr in yield_obj_parent_attr(cat, cls=ev.QuantityError):\n",
" if not obj:\n",
" setattr(parent, attr, None)\n",
" return cat\n",
"\n",
"\n",
"cat1 = _remove_empty_quantity_errors(cat)\n",
"\n",
"cat2 = _remove_empty_quantity_errors(cat2)\n",
"\n",
"assert cat1 == cat2"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 4
}