Skip to main content

Database for NECST.

Project description

NECSTDB

PyPI Python Test License

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

  1. Create new database instance

    >>> db_dir = "path/to/new_database_directory"
    >>> db = necstdb.opendb(db_dir, mode="w")  # "w" stands for "write"
    
  2. 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 b 1
    int16 h 2
    int32 i 4
    int64 q 8
    uint8 B 1
    uint16 H 2
    uint32 I 4
    uint64 Q 8
    float32 f 4
    float64 d 8
    string [length]s 1 * 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
    
  3. 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 append method every time you get new data.

    NOTE: Data to pass to append method 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.)

  4. Save files into the database

    >>> db.save_file("example.txt", "Content of the file.")
    

Database Reader

  1. Open the database instance

    >>> db = necstdb.opendb("path/to/database_directory")
    
  2. 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 astype keywords for read method 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: Keyword df is now supported.

  3. 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 use asbytes option explicitly.

    >>> db.read_file("example_binary.data", asbytes=True)
    b"Content of the file.", b""
    

    This method returns 2 str or bytes values. The second value is metadata for the file saved using db.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]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

necstdb-0.2.9.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

necstdb-0.2.9-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file necstdb-0.2.9.tar.gz.

File metadata

  • Download URL: necstdb-0.2.9.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.8.14 Linux/5.15.0-1021-azure

File hashes

Hashes for necstdb-0.2.9.tar.gz
Algorithm Hash digest
SHA256 488f644e2f0dd3ae0fdb9c179b19369f83f4be170af21aa33c363068169c3ee6
MD5 8cf4a01ddcacda8e194eb41b3f59f626
BLAKE2b-256 e6a41b2fdabde3490751e805d2c70cd7cfb92853c1e4581185d83e3355a37111

See more details on using hashes here.

File details

Details for the file necstdb-0.2.9-py3-none-any.whl.

File metadata

  • Download URL: necstdb-0.2.9-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.8.14 Linux/5.15.0-1021-azure

File hashes

Hashes for necstdb-0.2.9-py3-none-any.whl
Algorithm Hash digest
SHA256 03c2fb507d5eb1397e0f495ee00293632f8ea25dd294c1136631ee40b60b9f08
MD5 c9f23bf53b17f2e4310c7b9f41733306
BLAKE2b-256 b2f653913b541626e7e3b2f500188b5a11df49efdfdc003baa5be8fdc8e9ef11

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page