Source code for irfpy.mima.irfmom
""" IRF version (official version) of moment file reader.
Prepare the data file (contact to Futaana).
Setup ``.irfpyrc``.
::
[mima]
moment_scan = /path/to/data/file
> from irfpy.mima import irfmom
> import datetime
> t0 = datetime.datetime(2010, 1, 1)
> t1 = datetime.datetime(2010, 1, 2)
> ns, vs, kTs, fs = irfmom.get_moment_data(t0, t1)
> tlist, nlist = ns.get_data()
> print(tlist[0])
2010-01-01 15:52:20
> print(nlist[0])
2.05347
.. todo::
Implement higher-level user interface.
.. todo::
Test is disabled so far. This is because of the data retrieval system related issue.
(Quick & Dirty implementation here!)
"""
import dateutil.parser
from irfpy.util.irfpyrc import Rc as _Rc
from irfpy.util.timeseries import ScalarSeries as _ScalarSeries
[docs]class MomentFile:
""" Represent a moment file, i.e. MomentsScan.ascii
It is for backend, which does not concern to users.
> mf = MomentFile()
"""
def __init__(self):
rc = _Rc(raises=True)
self.filename = rc.get('mima', 'moment_scan')
self.version, self.data = self._load_file(self.filename)
@classmethod
def _load_file(cls, filename):
""" Loading the file, store as four time series array.
"""
version = 'unknown'
tlist = []
nlist = []
vlist = []
kTlist = []
flist = []
with open(filename) as fp:
for line in fp:
### This takes >10 s. Probably good to optimize. TODO: Optimization
if line.find('Version') != -1:
version = line.strip().split()[-1]
if line.startswith('%'):
continue
tokens = line.split()
if len(tokens) != 5:
continue
t = dateutil.parser.parse(tokens[0]).replace(tzinfo=None)
n = float(tokens[1])
v = float(tokens[2])
kT = float(tokens[3])
f = int(tokens[4])
tlist.append(t)
nlist.append(n)
vlist.append(v)
kTlist.append(kT)
flist.append(f)
data = (_ScalarSeries(tlist, nlist),
_ScalarSeries(tlist, vlist),
_ScalarSeries(tlist, kTlist),
_ScalarSeries(tlist, flist))
return version, data
[docs] def get_data(self, t0, t1):
""" Get the data between t0 and t1.
"""
return tuple([self.data[i].clipped(t0, t1) for i in range(4)])
def __str__(self):
s = "<irfpy.mima.irfmom.MomentFile: filename={} version={} ndata={}>".format(self.filename, self.version, len(self.data[0]))
return s
_default_momfile = None
[docs]def get_moment_data(t0, t1):
""" Return the moment data.
:returns: The moment data, as a tuple of four :class:`irfpy.util.timeseries.ScalarSeries` objects
(``density``, ``speed``, ``temperature``, ``flag``).
"""
global _default_momfile
if _default_momfile is None:
_default_momfile = MomentFile()
return _default_momfile.get_data(t0, t1)
[docs]def get_moment_array(t0, t1):
""" Return the moment data as a numpy array.
:returns: The moment data as a numpy array
The data is organized as (N, 5) shaped array where N is the number of data.
The five column is, time (in floating point time format of matplotlib),
density, speed, temperature, and flag.
"""
raise NotImplementedError('TBI')