diff --git a/src/lasp/tools/tools.py b/src/lasp/tools/tools.py index 103b4d2..f8a938b 100644 --- a/src/lasp/tools/tools.py +++ b/src/lasp/tools/tools.py @@ -34,11 +34,8 @@ __all__ = ['SmoothingType', 'smoothSpectralData', 'SmoothingWidth'] from enum import Enum, unique import bisect -import codecs import copy -import json import numpy as np -import os @unique @@ -206,29 +203,20 @@ def smoothSpectralData(freq, M, sw: SmoothingWidth, Psm[0] = P[0] # Reuse old value in case first data.. # ..point is skipped. Not plotted any way. - # Find smoothing matrix. Look it up, otherwise calculate and store. - # TODO: find a way to reduce file size, loading time, a good place to store - # TODO: also save the last table in memory - fname = 'smoothing_tables.json' # storage file + # Re-use smoothing matrix Q if available. Otherwise, calculate. + # Store in dict 'Qdict' nfft = int(2*(len(freq)-1)) - key = f"nfft{nfft}_Noct{Noct}" # name - if os.path.isfile(fname): - with open(fname) as f: - Qdict = json.load(f) - else: - Qdict = {'Help': 'This file contains smoothing tables'} - json.dump(Qdict, codecs.open(fname, 'w', encoding='utf-8'), - separators=(',', ':'), sort_keys=True, indent=4) + key = f"nfft{nfft}_Noct{Noct}" # matrix name + + if 'Qdict' not in globals(): # Guarantee Qdict exists + global Qdict + Qdict = {} if key in Qdict: - # Load = fast - Q = np.asarray(Qdict[key]) + Q = Qdict[key] else: - # Calculate new matrix; store Q = smoothCalcMatrix(freq, sw) - Qdict[key] = Q.tolist() # json cannot handle ndarray - json.dump(Qdict, codecs.open(fname, 'w', encoding='utf-8'), - separators=(',', ':'), sort_keys=True, indent=4) + Qdict[key] = Q # Apply smoothing Psm = np.matmul(Q, P)