NECSTDB
Database for NECST.
TL;DR
NECST, an abbreviation of NEw Control System for Telescope, is a flexible control system for radio telescopes. Its efficient data storage is provided here.
The database contains tables, which keep individual topic of time series data with some metadata attached to them, e.g. spectral data from one spectrometer board (array of data + timestamp), weather data (temperature + humidity + wind speed + ... + timestamp), etc.
Features
This package provides:
- database writer with quite flexible data format support
- database reader which supports various output format
Installation
pip install necstdb
Usage
Database Writer
-
Create new database instance
>>> db_dir = "path/to/new_database_directory" >>> db = necstdb.opendb(db_dir, mode="w") # "w" stands for "write"
-
Create table with data information
>>> data1_info = { ... "data": [ ... {"key": "recorded_time", "format": "d", "size": 8}, ... {"key": "n_scan_lines", "format": "i", "size": 4}, ... {"key": "obsmode", "format": "3s", "size": 3}, ... {"key": "timestamp", "format": "d", "size": 8}, ... ], ... "memo": "generated by db_logger_operation", ... "necstdb_version": necstdb.__version__, ... } >>> db.create_table("data1", data1_info)
The keys "key" and "format" in "data" list are required. The format characters and the sizes are listed below. For more information, see the struct module documentation and/or the ROS message wiki.
- Changed in v0.2.5: "size" in "data" list is no longer required, but is optional.
The "memo" and "necstdb_version" keys are not necessary. You can also add any other keys to the data information dict.
ROS format Format character Size [byte] bool ?1 int8 b1 int16 h2 int32 i4 int64 q8 uint8 B1 uint16 H2 uint32 I4 uint64 Q8 float32 f4 float64 d8 string [length]s1 * length NOTE: Array data are also supported. To write them, use repeat count syntax (e.g.
3d) or format character sequence syntax (e.g.ddd) for the "format", and the sum of the elements' sizes for the "size".{"key": "data_array", "format": "3d", "size": 3 * 8} # repeat count syntax {"key": "data_array", "format": "ddd", "size": 8 + 8 + 8} # format character sequence syntax
-
Write data into table
>>> table1 = db.open_table("data1", mode="ab") # "ab" stands for "append binary" >>> data1 = [1.6294488758e9, 3, b"SKY", 1.6294488757e9] # string data are not allowed, use bytes instead >>> table1.append(*data1)
Call the
appendmethod every time you get new data.NOTE: Data to pass to
appendmethod should be flattened. The nested structure will be reconstructed on reading.data = [1, 2, [3, 4, 5], 6, 7] data = necstdb.utils.flatten_data(data) table.append(*data)
NOTE: Multi-dimensional data are not supported.
NOTE: Shape of array or length of string in every data must be the same as the one in data information. (Mismatch of string length won't raise an error, but stored data can be broken or incomplete.)
-
Save files into the database
>>> db.save_file("example.txt", "Content of the file.")
Database Reader
-
Open the database instance
>>> db = necstdb.opendb("path/to/database_directory")
-
Read a desired topic
>>> data1 = db.open_table("data1").read(astype="array") >>> data1 array([[1.6294488758e9, 3, b'SKY', 1.6294488757e9], ...], dtype=[('recorded_time', '<f8'), ('n_scan_lines', '<i4'), ('obsmode', '|S3'), ('timestamp', '<f8')])
The supported
astypekeywords forreadmethod are:Output type Keywords Notes tuple "tuple" *1 dict "dict" pandas.DataFrame "pandas", "dataframe", "data_frame", "df" *2 numpy.ndarray "array", "structuredarray", "structured_array", "sa" bytes "buffer", "raw" *1: Array data are not supported, but will be flattened.
*2: Changed in v0.2.5: Keyworddfis now supported. -
Read files saved in the database
>>> db.read_file("example.txt") "Content of the file.", ""
This method assumes the file can be read as
str. This attempt will fail for binary files, so useasbytesoption explicitly.>>> db.read_file("example_binary.data", asbytes=True) b"Content of the file.", b""
This method returns 2
strorbytesvalues. The second value is metadata for the file saved usingdb.save_file(filename, data, metadata)method.
Misc
List all the tables contained in the database
>>> db = necstdb.opendb("path/to/database_directory")
>>> db.list_tables()
['data1', 'spectral_data', 'weather_data', ...]
Archive the database
>>> db = necstdb.opendb("path/to/database_directory")
>>> db.checkout(saveto="path/to/archive.tar.gz", compression="gz")
Get informations of all tables in the database
>>> db = necstdb.opendb("path/to/database_directory")
>>> db.get_info()
"""
file size [byte] #records record size [byte] format
table name
data1 2448 102 24 di3sd
spectral_data 41948160 320 131088 d32768fd
weather_data 6328 113 56 ddddddd
"""
Read particular columns and/or rows of the database
>>> db = necstdb.opendb("path/to/database_directory")
>>> data = db.open_table("data1").read(num=5, start=3, cols=["timestamp", "obsmode"], astype="tuple") # order of cols won't be preserved
((b'SKY', 1.6294488788e9) # 4th element (caution 0-based indexing)
(b'SKY', 1.6294488798e9)
(b'SKY', 1.6294488808e9)
(b'SKY', 1.6294488818e9)
(b'HOT', 1.6294488828e9))
Flatten nested array
>>> data = [1, 2, 3, [4, 5], 6]
>>> flattened = necstdb.utils.flatten_data(data)
[1, 2, 3, 4, 5, 6]
Release files for necstdb 0.2.10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| necstdb-0.2.10.tar.gz | 12.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| necstdb-0.2.10-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 24.1 kB
Release files / necstdb-0.2.10.tar.gz
| Download URL | necstdb-0.2.10.tar.gz |
|---|---|
| Size | 12.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8a254d3e5ec719dbcf92b71a31860e5767d78c827148e26935bff8d216261485
|
|
BLAKE2b-256 checksum How to use checksums |
f759ba2342b3f5c09c332876144526424d068fd5c0143badc848b404c90e151c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.4.1 CPython/3.12.13 Linux/6.17.0-1015-azure
|
Release files / necstdb-0.2.10-py3-none-any.whl
| Download URL | necstdb-0.2.10-py3-none-any.whl |
|---|---|
| Size | 12.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
17d021198e6c6648f928f838497537fe4d6509e2affe9962fd35008c8cbfde7a
|
|
BLAKE2b-256 checksum How to use checksums |
ff187dbd287146812280bb11df1840104f042bfc37543eeb1c2240d3f73458ba
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.4.1 CPython/3.12.13 Linux/6.17.0-1015-azure
|