Source code for irfpy.vnpd.vnpddata

""" Data access for VEX/NPD

*Preparation*

The preparation is 

Setup ``.irfpyrc`` file.

::

    [vnpd]
    ncdfbase = /path/to/ncdf

Simple usage is as follows.
    
>>> import datetime
>>> from irfpy.vnpd import vnpddata
>>> dc = vnpddata.DataCenterRawMode(1, _get_basedir())
>>> obstime, data = dc.nearest(datetime.datetime(2009, 1, 4, 21, 27, 30))
>>> print(obstime)
2009-01-04 21:27:30.242400
>>> print(data)
<NpdRawPacket: 2009-01-04T21:27:30 cnts=103 147 204 79 1 400>

The obtained data is with the :class:`NpdRawPacket` class.

You can use ``get_array`` or ``iter`` for getting multiple data.

>>> obstime, data = dc.get_array(datetime.datetime(2009, 1, 4, 21, 27),
...                              datetime.datetime(2009, 1, 4, 21, 28))
>>> from pprint import pprint
>>> pprint(obstime)    # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
[datetime.datetime(2009, 1, 4, 21, 27, 0, 249690),
 datetime.datetime(2009, 1, 4, 21, 27, 0, 577370),
...
 datetime.datetime(2009, 1, 4, 21, 27, 59, 126480),
 datetime.datetime(2009, 1, 4, 21, 27, 59, 454160)]
>>> pprint(data)   # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
[<NpdRawPacket: 2009-01-04T21:27:00 cnts=102 111 186 74 1 343>,
 <NpdRawPacket: 2009-01-04T21:27:00 cnts=117 130 188 85 2 376>,
...
 <NpdRawPacket: 2009-01-04T21:27:59 cnts=95 127 157 77 1 337>,
 <NpdRawPacket: 2009-01-04T21:27:59 cnts=92 124 180 75 2 358>]

>>> for obstime, data in dc.iter(datetime.datetime(2009, 1, 4, 21, 27),
...                              datetime.datetime(2009, 1, 4, 21, 28)):
...     print(obstime, data)    # doctest: +ELLIPSIS
2009-01-04 21:27:00.249690 <NpdRawPacket: 2009-01-04T21:27:00 cnts=102 111 186 74 1 343>
2009-01-04 21:27:00.577370 <NpdRawPacket: 2009-01-04T21:27:00 cnts=117 130 188 85 2 376>
...
2009-01-04 21:27:59.126480 <NpdRawPacket: 2009-01-04T21:27:59 cnts=95 127 157 77 1 337>
2009-01-04 21:27:59.454160 <NpdRawPacket: 2009-01-04T21:27:59 cnts=92 124 180 75 2 358>

The :class:`DataCenterRawMode` is an implementation of irfpy data center.
See also the `data center tutorial <https://irfpy.irf.se/projects/util/tutorial/tutorial_datacenter.html>`_
and `data center API <https://irfpy.irf.se/projects/util/api/api_irfpy.util.datacenter.html>`_
for more details.
"""
import os as _os
import datetime as _datetime
import logging as _logging
import gzip as _gzip
import tempfile as _tempfile

from netCDF4 import MFDataset as _MFDataset

import numpy as _np

from irfpy.util import datacenter as _datacenter
from irfpy.util import irfpyrc as _irfpyrc
from irfpy.util import exception as _ex

from irfpy.npdcommon.npdpacket import NpdRawPacket
from irfpy.npdcommon.npd_ncdf import NetcdfFileRaw
from irfpy.npdcommon.npd_ncdf import load_npd_rawmode

_logger = _logging.getLogger(__name__)

def _get_basedir():
    """ Return the basedir for VEX.

    :return: The path of the data. Specified by ``.irfpyrc``.
    """
    rc = _irfpyrc.Rc()
    ncdfbase = rc.get('vnpd', 'ncdfbase')

    return ncdfbase

def _find_ncdf_files(npdnr, mode):
    """ Return the list of the NPD data.

    :param npdnr: 1 or 2
    :param mode: 'raw', 'tof' or 'bin'
    :return: A list of file name, sorted by time.

    >>> raw1list = _find_ncdf_files(1, 'raw')
    >>> print(len(raw1list))
    1932

    >>> raw2list = _find_ncdf_files(1, 'raw')
    >>> print(len(raw2list))
    1932
    """
    ncdfbase = _get_basedir()

    if not _os.path.isdir(ncdfbase):
        raise _ex.IrfpyException('Directory {} (irfpy setting file(s)) not found.'.format(ncdfbase))

    filelist = []

    for _p, _d, _f in _os.walk(ncdfbase):
        for _ff in _f:
            if _ff.lower().startswith('npd{:1d}{:3s}'.format(npdnr, mode.lower())):

                # File size check. 0-size file could be there but should not be in the list.
                filename = _os.path.join(_p, _ff)
                if _os.path.getsize(filename) == 0:
                    continue

                filelist.append(filename)

    filelist.sort()

    return filelist


def _approxiate_starttime(filename):
    """ Obtain the time of the first data approximately from the filename.

    :param filename: File name
    :return: The time of the start of the data

    >>> print(_approxiate_starttime('./vex2tof20131009110054.nc.gz'))
    2013-10-09 11:00:54
    """
    fn = _os.path.basename(filename)

    yr = int(fn[7:11])
    mo = int(fn[11:13])
    dy = int(fn[13:15])
    hr = int(fn[15:17])
    mn = int(fn[17:19])
    se = int(fn[19:21])

    return _datetime.datetime(yr, mo, dy, hr, mn, se)



[docs]class DataCenterRawMode(_datacenter.BaseDataCenter): """ Datacenter for RAW mode file. """ def __init__(self, npd_number, basedir=None): self.basedir = basedir if basedir is None: basedir = _get_basedir() self.npd_number = npd_number _datacenter.BaseDataCenter.__init__(self)
[docs] def search_files(self): flist = _find_ncdf_files(self.npd_number, 'raw') return flist
[docs] def approximate_starttime(self, filename): return _approxiate_starttime(filename)
# def exact_starttime(self, filename): # return _datacenter.BaseDataCenter.exact_starttime(self, filename) # As a first step. Let's see if it works.
[docs] def read_file(self, filename): tlist, dlist = load_npd_rawmode(filename) return tlist, dlist