\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": "2024-02-28T22:20:21.495930Z",
"iopub.status.busy": "2024-02-28T22:20:21.495753Z",
"iopub.status.idle": "2024-02-28T22:20:24.317352Z",
"shell.execute_reply": "2024-02-28T22:20:24.316739Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8 Event(s) in Catalog:\n",
"2007-08-06T08:48:40.010000Z | +39.464, -111.228 | 4.2 mb\n",
"2007-08-07T02:14:24.080000Z | +39.463, -111.223 | 1.17 ml\n",
"2007-08-07T03:44:18.470000Z | +39.462, -111.215 | 1.68 ml\n",
"2007-08-07T07:13:05.760000Z | +39.461, -111.224 | 2.55 ml\n",
"2007-08-07T02:05:04.490000Z | +39.465, -111.225 | 2.44 ml\n",
"2007-08-06T10:47:25.600000Z | +39.462, -111.232 | 1.92 ml\n",
"2007-08-07T21:42:51.130000Z | +39.463, -111.220 | 1.88 ml\n",
"2007-08-06T01:44:48.810000Z | +39.462, -111.238 | 2.32 ml\n"
]
}
],
"source": [
"import obspy\n",
"import obsplus\n",
"\n",
"crandall = obsplus.load_dataset('crandall_test')\n",
"\n",
"cat = crandall.event_client.get_events()\n",
"\n",
"print(cat)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## json conversions"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"execution": {
"iopub.execute_input": "2024-02-28T22:20:24.344211Z",
"iopub.status.busy": "2024-02-28T22:20:24.343838Z",
"iopub.status.idle": "2024-02-28T22:20:24.704544Z",
"shell.execute_reply": "2024-02-28T22:20:24.703858Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"resource_id\":{\"id\":\"smi:local/1023cbfe-e4dd-4b09-b5a5-9ce40f12725f\"},\"events\":[{\"resource_id\":{\"id\":\"smi:local/248839\"},\"event_type\":null,\"event_type_certainty\":null,\"creation_info\":{\"agency_id\":\"NIOSH\",\"agency_uri\":null,\"author\":\"DC\",\"author_uri\":null,\"creation_time\":\"2018-10-10T20:33:13.618111\",\"version\":null},\"preferred_origin_id\":{\"id\":\"smi:local/404329\"},\"preferred_magnitude_id\":null,\"prefe\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/share/miniconda3/envs/test/lib/python3.10/site-packages/obspy/core/util/attribdict.py:120: 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",
"print(json_str[0:400])\n",
"\n",
"# convert back\n",
"cat2 = obsplus.json_to_cat(json_str)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"execution": {
"iopub.execute_input": "2024-02-28T22:20:24.707425Z",
"iopub.status.busy": "2024-02-28T22:20:24.706970Z",
"iopub.status.idle": "2024-02-28T22:20:25.134041Z",
"shell.execute_reply": "2024-02-28T22:20:25.133386Z"
}
},
"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",
"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.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 4
}