1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
"""
This file will generate a JSON blob usable by QCElemental for physical constants
"""
import datetime
import json
import requests
from yapf.yapflib.yapf_api import FormatCode
metadata_file = "../../raw_data/srd121_nist-codata-fundamental-physical-constants-2014-metadata.json"
with open(metadata_file, "r") as handle:
metadata = json.load(handle)
title = metadata["title"]
date_modified = metadata["modified"]
year = date_modified.split("-")[0]
doi = metadata["distribution"][-1]["accessURL"].strip("https://dx.doi.org/")
url = metadata["distribution"][0]["downloadURL"]
access_date = str(datetime.datetime.utcnow())
constants = requests.get(url).json()
output = '''
"""
This is a automatically generated file from the {0} NIST fundamental constants.
Title: {1}
Date: {2}
DOI: {3}
URL: {4}
Access Date: {5} UTC
File Authors: QCElemental Authors
"""
'''.format(
year, title, date_modified, doi, url, access_date
)
constants_json = {
"title": title,
"date": date_modified,
"doi": doi,
"url": url,
"access_data": access_date,
"constants": {},
}
for pc in constants["constant"]:
value = pc["Value"].strip()
uncertainty = pc["Uncertainty"]
if uncertainty == "(exact)":
value = value.replace("...", "")
constants_json["constants"][pc["Quantity "].lower()] = {
"quantity": pc["Quantity "],
"unit": pc["Unit"],
"value": value.replace(" ", ""),
"uncertainty": uncertainty,
}
output += "nist_{}_codata = {}".format(year, constants_json)
output = FormatCode(output)
fn = "nist_{}_codata.py".format(year)
with open(fn, "w") as handle:
handle.write(output[0])
|