"""Universal amplitude ratios of the Ising universality class -- numberdb.org/T476.

Run with SageMath (or a compatible passagemath environment):
    sage -pip install numberdb
    sage -python generate.py                 # compare every stored entry
    sage -python checks.py                   # local numerical checks
    sage -python generate.py --fill          # fill the existing draft only

For a private draft, set NUMBERDB_API_KEY. In the campaign wrapper the key
can instead arrive on stdin with NUMBERDB_KEY_FROM_STDIN=1. --fill writes
entries and attachments, never changes publication or review status.

This is a reproducible transcription, not a Monte Carlo simulation. The
values are the eight entries of the 'here' row of Table III (page 15) of
M. Hasenbusch, arXiv:1004.4983v3, DOI 10.1103/PhysRevB.82.174434.
Parenthesised uncertainties become explicit radii, without padding or
rounding the centres. digits=2 is only the minimum accepted precision;
the returned strings preserve every digit and the original uncertainty.
The source's estimates include its assessment of systematic errors and
are not rigorous enclosures or a joint confidence region.

The eight components are the selected universal bulk critical-amplitude
ratios. See the table's definitions for the thermal and field metric,
second-moment versus exponential correlation length, and Q versus R_xi.
"""

import os
import sys
import numberdb.sage as numberdb

# key: centre, radius, location in the primary source.
DATA = {
    'U0': ('0.536', '0.002', 'equation (55)'),
    'U2': ('4.713', '0.007', 'equation (31)'),
    'Uxi': ('1.939', '0.005', 'equation (33)'),
    'Qxi-minus': ('1.020', '0.005', 'equation (40)'),
    'Q-plus': ('0.01899', '0.00010', 'equation (64)'),
    'Q-minus': ('0.00487', '0.00002', 'equation (65)'),
    'u-star': ('14.08', '0.05', 'equation (38)'),
    'Qc': ('0.3293', '0.0002', 'equation (36)'),
}


class IsingAmplitudeRatios(numberdb.Generator):
    table = 'T476'
    parameters = ('ratio',)
    type = 'R'
    rigour = 'measured'
    digits = 2
    files = ('generate.py', 'checks.py', 'check.py', 'source-data.json',
             'source-manifest.json', 'numerical-checks.json', 'stored-checks.json',
             'audit.json', 'build-notes.md')

    def enumerate(self):
        for key in DATA:
            yield {'ratio': key}

    def value(self, params, digits):
        key = params['ratio']
        centre, radius, location = DATA[key]
        comment = 'CITE{Hasenbusch2010}, %s and Table III.' % location
        if key == 'Qxi-minus':
            comment += (' The uncertainty includes the estimated effect of using '
                        'a finite-distance effective correlation length to '
                        'approximate the exponential correlation length.')
        return {'number': centre + ' +/- ' + radius, 'comment': comment}


def client_from_environment():
    key = None
    if os.environ.get('NUMBERDB_KEY_FROM_STDIN') == '1':
        key = sys.stdin.read().strip()
    return numberdb.Client(api_key=key)


if __name__ == '__main__':
    client = client_from_environment()
    generator = IsingAmplitudeRatios()
    if '--fill' in sys.argv or os.environ.get('NUMBERDB_PUBLISH') == '1':
        print(generator.publish(
            client=client, assisted_by=os.environ.get('NUMBERDB_ASSISTED_BY', ''),
            message='Transcribe eight measured Ising amplitude ratios with source uncertainties'))
    report = generator.verify(sample=None, client=client)
    print(report)
    sys.exit(0 if report.ok else 1)
