postprocessing-seismo-lib 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,28 @@
1
+ Metadata-Version: 2.1
2
+ Name: postprocessing_seismo_lib
3
+ Version: 0.1.0
4
+ Summary: A library for building and parsing Seismology API message bodies.
5
+ Home-page: https://scsngit.gps.caltech.edu/services/associator
6
+ Author: Ryan Tam
7
+ Author-email: rwtam@caltech.edu
8
+ License: UNKNOWN
9
+ Description: # postprocessing_seismo_lib
10
+
11
+ `postprocessing_seismo_lib` is a lightweight Python library for building and parsing structured API messages, especially for use with nested JSON structures used in event-based data systems.
12
+
13
+ ## Features
14
+
15
+ - Build a full message with metadata and body using `build_message`
16
+ - Extract the `body` section from a structured JSON file using `extract_body_from_file`
17
+
18
+ ## Installation
19
+
20
+ You can install the library locally for development:
21
+
22
+ ```bash
23
+ pip install -e .
24
+ Platform: UNKNOWN
25
+ Classifier: Programming Language :: Python :: 3
26
+ Classifier: Operating System :: OS Independent
27
+ Requires-Python: >=3.6
28
+ Description-Content-Type: text/markdown
@@ -0,0 +1,15 @@
1
+ # postprocessing_seismo_lib
2
+
3
+ `postprocessing_seismo_lib` is a lightweight Python library for building and parsing structured API messages, especially for use with nested JSON structures used in event-based data systems.
4
+
5
+ ## Features
6
+
7
+ - Build a full message with metadata and body using `build_message`
8
+ - Extract the `body` section from a structured JSON file using `extract_body_from_file`
9
+
10
+ ## Installation
11
+
12
+ You can install the library locally for development:
13
+
14
+ ```bash
15
+ pip install -e .
@@ -0,0 +1 @@
1
+ from .utils import convert_file_to_json, build_message, extract_body_from_file
@@ -0,0 +1,116 @@
1
+ import json
2
+ import pandas as pd
3
+ import xmltodict
4
+ import traceback
5
+
6
+ def convert_file_to_json(input_file: str,
7
+ output_file: str,
8
+ id: str,
9
+ event_file: str = None,
10
+ pick_file: str = None,
11
+ error_log_file: str = None):
12
+ """
13
+ Auto-detect format (csv, quakeml, arcout) and convert to standard JSON response.
14
+
15
+ Parameters:
16
+ input_file (str): Main input file (for quakeml or arcout).
17
+ output_file (str): Output JSON filename.
18
+ id (str): Event ID to use in the JSON.
19
+ event_file (str): (Optional) For CSV: Path to events CSV file.
20
+ pick_file (str): (Optional) For CSV: Path to picks CSV file.
21
+ error_log_file (str): Optional path to write traceback if an error occurs.
22
+ """
23
+ try:
24
+ detected_format = None
25
+ body = None
26
+
27
+ # Case 1: CSV — both event_file and pick_file must be provided
28
+ if event_file and pick_file:
29
+ df_events = pd.read_csv(event_file)
30
+ df_picks = pd.read_csv(pick_file)
31
+
32
+ events_list = df_events.to_dict(orient="records")
33
+ picks_list = df_picks.to_dict(orient="records")
34
+
35
+ for pick in picks_list:
36
+ for key in ["Amplitude", "Filter", "Quality", "Site", "Source"]:
37
+ if key in pick and not isinstance(pick[key], str):
38
+ pick[key] = json.dumps(pick[key])
39
+
40
+ body = [events_list, picks_list]
41
+ detected_format = "csv"
42
+
43
+ # Case 2: Try XML parsing
44
+ elif input_file:
45
+ try:
46
+ with open(input_file, "r") as f:
47
+ xml_str = f.read()
48
+ body_dict = xmltodict.parse(xml_str)
49
+
50
+ extracted_id = (
51
+ body_dict.get('q:quakeml', {})
52
+ .get('eventParameters', {})
53
+ .get('event', {})
54
+ .get('@ns0:eventid', 'unknown_id')
55
+ )
56
+ id = id or extracted_id
57
+ body = body_dict
58
+ detected_format = "quakeml"
59
+
60
+ except Exception:
61
+ # Fallback to arcout if XML parsing fails
62
+ with open(input_file, "r") as f:
63
+ lines = f.readlines()
64
+ body = [lines]
65
+ detected_format = "arcout"
66
+
67
+ else:
68
+ raise ValueError("No input_file or CSV files provided.")
69
+
70
+ # Build final response
71
+ response = {
72
+ "status": 200,
73
+ "headers": {
74
+ "Content-Type": "application/json"
75
+ },
76
+ "body_meta": {
77
+ "id": id,
78
+ "format": detected_format
79
+ },
80
+ "body": body
81
+ }
82
+
83
+ with open(output_file, "w") as f:
84
+ json.dump(response, f, indent=2)
85
+
86
+ print(f"[✓] Format: {detected_format} — Saved output to {output_file}")
87
+
88
+ except Exception as e:
89
+ error_msg = traceback.format_exc()
90
+ print(f"[✗] Error encountered: {e}")
91
+ if error_log_file:
92
+ with open(error_log_file, "w") as f:
93
+ f.write(error_msg)
94
+ print(f"[!] Error logged to {error_log_file}")
95
+ else:
96
+ print("[!] No error_log_file specified; error not saved.")
97
+
98
+
99
+ def build_message(body, id_str, format_str):
100
+ """
101
+ Constructs the full JSON message given body content, ID, and format.
102
+ """
103
+ return {
104
+ "status": 200,
105
+ "headers": {"Content-Type": "application/json"},
106
+ "body_meta": {"id": id_str, "format": format_str},
107
+ "body": body
108
+ }
109
+
110
+ def extract_body_from_file(filepath):
111
+ """
112
+ Loads a JSON file and returns only the 'body' field.
113
+ """
114
+ with open(filepath, 'r', encoding='utf-8') as f:
115
+ data = json.load(f)
116
+ return data.get("body", None)
@@ -0,0 +1,28 @@
1
+ Metadata-Version: 2.1
2
+ Name: postprocessing-seismo-lib
3
+ Version: 0.1.0
4
+ Summary: A library for building and parsing Seismology API message bodies.
5
+ Home-page: https://scsngit.gps.caltech.edu/services/associator
6
+ Author: Ryan Tam
7
+ Author-email: rwtam@caltech.edu
8
+ License: UNKNOWN
9
+ Description: # postprocessing_seismo_lib
10
+
11
+ `postprocessing_seismo_lib` is a lightweight Python library for building and parsing structured API messages, especially for use with nested JSON structures used in event-based data systems.
12
+
13
+ ## Features
14
+
15
+ - Build a full message with metadata and body using `build_message`
16
+ - Extract the `body` section from a structured JSON file using `extract_body_from_file`
17
+
18
+ ## Installation
19
+
20
+ You can install the library locally for development:
21
+
22
+ ```bash
23
+ pip install -e .
24
+ Platform: UNKNOWN
25
+ Classifier: Programming Language :: Python :: 3
26
+ Classifier: Operating System :: OS Independent
27
+ Requires-Python: >=3.6
28
+ Description-Content-Type: text/markdown
@@ -0,0 +1,8 @@
1
+ README.md
2
+ setup.py
3
+ postprocessing_seismo_lib/__init__.py
4
+ postprocessing_seismo_lib/utils.py
5
+ postprocessing_seismo_lib.egg-info/PKG-INFO
6
+ postprocessing_seismo_lib.egg-info/SOURCES.txt
7
+ postprocessing_seismo_lib.egg-info/dependency_links.txt
8
+ postprocessing_seismo_lib.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ postprocessing_seismo_lib
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,19 @@
1
+ from setuptools import setup, find_packages
2
+
3
+ setup(
4
+ name='postprocessing_seismo_lib',
5
+ version='0.1.0',
6
+ packages=find_packages(),
7
+ install_requires=[],
8
+ author='Ryan Tam',
9
+ author_email='rwtam@caltech.edu',
10
+ description='A library for building and parsing Seismology API message bodies.',
11
+ long_description=open('README.md').read(),
12
+ long_description_content_type='text/markdown',
13
+ url='https://scsngit.gps.caltech.edu/services/associator', # Optional
14
+ classifiers=[
15
+ 'Programming Language :: Python :: 3',
16
+ 'Operating System :: OS Independent',
17
+ ],
18
+ python_requires='>=3.6',
19
+ )