Merge branch 'develop' into documentation

This commit is contained in:
fjosw 2021-12-10 10:38:36 +00:00
commit 6ce314bcaf
2 changed files with 19 additions and 10 deletions

View file

@ -279,22 +279,24 @@ def import_json_string(json_string, verbose=True, full_output=False):
ret[-1].tag = taglist[i] ret[-1].tag = taglist[i]
return np.reshape(ret, layout) return np.reshape(ret, layout)
prog = json_string.get('program', '') json_dict = json.loads(json_string)
version = json_string.get('version', '')
who = json_string.get('who', '') prog = json_dict.get('program', '')
date = json_string.get('date', '') version = json_dict.get('version', '')
host = json_string.get('host', '') who = json_dict.get('who', '')
date = json_dict.get('date', '')
host = json_dict.get('host', '')
if prog and verbose: if prog and verbose:
print('Data has been written using %s.' % (prog)) print('Data has been written using %s.' % (prog))
if version and verbose: if version and verbose:
print('Format version %s' % (version)) print('Format version %s' % (version))
if np.any([who, date, host] and verbose): if np.any([who, date, host] and verbose):
print('Written by %s on %s on host %s' % (who, date, host)) print('Written by %s on %s on host %s' % (who, date, host))
description = json_string.get('description', '') description = json_dict.get('description', '')
if description and verbose: if description and verbose:
print() print()
print('Description: ', description) print('Description: ', description)
obsdata = json_string['obsdata'] obsdata = json_dict['obsdata']
ol = [] ol = []
for io in obsdata: for io in obsdata:
if io['type'] == 'Obs': if io['type'] == 'Obs':
@ -346,11 +348,11 @@ def load_json(fname, verbose=True, gz=True, full_output=False):
if not fname.endswith('.gz'): if not fname.endswith('.gz'):
fname += '.gz' fname += '.gz'
with gzip.open(fname, 'r') as fin: with gzip.open(fname, 'r') as fin:
d = json.loads(fin.read().decode('utf-8')) d = fin.read().decode('utf-8')
else: else:
if fname.endswith('.gz'): if fname.endswith('.gz'):
warnings.warn("Trying to read from %s without unzipping!" % fname, UserWarning) warnings.warn("Trying to read from %s without unzipping!" % fname, UserWarning)
with open(fname, 'r', encoding='utf-8') as fin: with open(fname, 'r', encoding='utf-8') as fin:
d = json.loads(fin.read()) d = fin.read()
return import_json_string(d, verbose, full_output) return import_json_string(d, verbose, full_output)

View file

@ -1,4 +1,4 @@
import pyerrors.obs as pe import pyerrors as pe
import pyerrors.input.json as jsonio import pyerrors.input.json as jsonio
import numpy as np import numpy as np
import os import os
@ -66,3 +66,10 @@ def test_jsonio():
assert np.all(o == r) assert np.all(o == r)
assert(description == rl['description']) assert(description == rl['description'])
def test_json_string_reconstruction():
my_obs = pe.Obs([np.random.rand(100)], ['name'])
json_string = pe.input.json.create_json_string(my_obs)
reconstructed_obs = pe.input.json.import_json_string(json_string)
assert my_obs == reconstructed_obs