<commit_before>#!/usr/bin/env python3

"""
Created on 1 Oct 2016

@author: <NAME> (<EMAIL>)

DESCRIPTION
The pt1000_calib utility is used to determine and save the voltage offset for each Pt1000 sensor.

The utility operates by measuring the temperature using a Sensirion SHT sensor, measuring the voltage output of the
Pt1000 sensor, and back-calculating the voltage offset.

Note that the scs_analysis/gases_sampler process must be restarted for changes to take effect.

SYNOPSIS
pt1000_calib.py [-s] [-v]

EXAMPLES
./pt1000_calib.py -s

DOCUMENT EXAMPLE
{"calibrated-on": "2017-07-19T13:56:48.289+00:00", "v20": 0.002891}

FILES
~/SCS/conf/pt1000_calib.json

SEE ALSO
scs_dev/gases_sampler
scs_mfr/dfe_conf
"""

import sys

from scs_core.data.json import JSONify

from scs_core.gas.pt1000_calib import Pt1000Calib

from scs_dfe.board.dfe_conf import DFEConf
from scs_dfe.climate.sht_conf import SHTConf

from scs_host.bus.i2c import I2C
from scs_host.sys.host import Host

from scs_mfr.cmd.cmd_pt1000_calib import CmdPt1000Calib


# --------------------------------------------------------------------------------------------------------------------

if __name__ == '__main__':

    try:
        I2C.open(Host.I2C_SENSORS)

        # ----------------------------------------------------------------------------------------------------------------
        # cmd...

        cmd = CmdPt1000Calib()

        if cmd.verbose:
            print("pt1000_calib: %s" % cmd, file=sys.stderr)
            sys.stderr.flush()


        # ------------------------------------------------------------------------------------------------------------
        # resources...

        # SHT...
        sht_conf = SHTConf.load(Host)
        sht = sht_conf.int_sht()

        # AFE...
        dfe_conf = DFEConf.load(Host)
        afe = dfe_conf.afe(Host)


        # ------------------------------------------------------------------------------------------------------------
        # run...

        if cmd.set:
            # SHT...
            sht_datum = sht.sample()

            if cmd.verbose:
                print(sht_datum, file=sys.stderr)

            # Pt1000 initial...
            pt1000_datum = afe.sample_pt1000()

            # Pt1000 correction...
            v20 = pt1000_datum.v20(sht_datum.temp)

            pt1000_calib = Pt1000Calib(None, v20)
            pt1000_calib.save(Host)

        # calibrated...
        pt1000_calib = Pt1000Calib.load(Host)

        print(JSONify.dumps(pt1000_calib))

        if cmd.verbose:
            afe = dfe_conf.afe(Host)
            pt1000_datum = afe.sample_pt1000()
<commit_msg>Improved error reporting in pt1000_calib.py<commit_after>#!/usr/bin/env python3

"""
Created on 1 Oct 2016

@author: <NAME> (<EMAIL>)

DESCRIPTION
The pt1000_calib utility is used to determine and save the voltage offset for each Pt1000 sensor.

The utility operates by measuring the temperature using a Sensirion SHT sensor, measuring the voltage output of the
Pt1000 sensor, and back-calculating the voltage offset.

For the utility to operate, the I2C address of the Pt1000 ADC must be set. This is done using the dfe_conf utility.

Note that the scs_analysis/gases_sampler process must be restarted for changes to take effect.

SYNOPSIS
pt1000_calib.py [-s] [-v]

EXAMPLES
./pt1000_calib.py -s

DOCUMENT EXAMPLE
{"calibrated-on": "2017-07-19T13:56:48.289+00:00", "v20": 0.002891}

FILES
~/SCS/conf/pt1000_calib.json

SEE ALSO
scs_dev/gases_sampler
scs_mfr/dfe_conf
"""

import sys

from scs_core.data.json import JSONify

from scs_core.gas.pt1000_calib import Pt1000Calib

from scs_dfe.board.dfe_conf import DFEConf
from scs_dfe.climate.sht_conf import SHTConf

from scs_host.bus.i2c import I2C
from scs_host.sys.host import Host

from scs_mfr.cmd.cmd_pt1000_calib import CmdPt1000Calib


# --------------------------------------------------------------------------------------------------------------------

if __name__ == '__main__':

    try:
        I2C.open(Host.I2C_SENSORS)

        # ----------------------------------------------------------------------------------------------------------------
        # cmd...

        cmd = CmdPt1000Calib()

        if cmd.verbose:
            print("pt1000_calib: %s" % cmd, file=sys.stderr)
            sys.stderr.flush()


        # ------------------------------------------------------------------------------------------------------------
        # resources...

        # SHT...
        sht_conf = SHTConf.load(Host)
        sht = sht_conf.int_sht()

        # AFE...
        dfe_conf = DFEConf.load(Host)

        # validate...
        if dfe_conf.pt1000_addr is None:
            print("pt1000_calib: a Pt1000 ADC has not been configured for this system.", file=sys.stderr)
            exit(1)

        afe = dfe_conf.afe(Host)


        # ------------------------------------------------------------------------------------------------------------
        # run...

        if cmd.set:
            # SHT...
            sht_datum = sht.sample()

            if cmd.verbose:
                print(sht_datum, file=sys.stderr)

            # Pt1000 initial...
            pt1000_datum = afe.sample_pt1000()

            # Pt1000 correction...
            v20 = pt1000_datum.v20(sht_datum.temp)

            pt1000_calib = Pt1000Calib(None, v20)
            pt1000_calib.save(Host)

        # calibrated...
        pt1000_calib = Pt1000Calib.load(Host)

        print(JSONify.dumps(pt1000_calib))

        if cmd.verbose:
            afe = dfe_conf.afe(Host)
            pt1000_datum = afe.sample_pt1000()

{'fraction_non_alphanumeric': 0.23934837092731828, 'fraction_numerical': 0.056211958467597564, 'mean_word_length': 1.4061154177433248, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6409555', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1644, 'n_words': 388}