Source code for irfpy.npdcommon.npd_ncdf

""" Netcdf files of NPD
"""
import os as _os
import gzip as _gzip
import tempfile as _tempfile
import logging as _logging
import datetime as _datetime

from netCDF4 import MFDataset as _MFDataset

import irfpy.util.exception as _ex

from irfpy.npdcommon.npdpacket import NpdRawPacket

_logger = _logging.getLogger(__name__)

[docs]class NetcdfFile: def __init__(self, filename): """ Read the ncdf file, load everything into memory. :param filename: The file name The data is kept to attributes as follows - :attr:`dimkey` where the names of dimensions are stored - :attr:`dim`, a dictionary, where the dimension is stored - :attr:`varkey` where the variable names are stored - :attr:`var` where the variables (data) are stored """ if filename.lower().endswith('.gz'): gzipped = True else: gzipped = False if gzipped: with _gzip.open(filename) as fp: s = fp.read() else: with open(filename, 'rb') as fp: s = fp.read() with _tempfile.NamedTemporaryFile() as ncf: ncfname = ncf.name ncf.write(s) _logger.debug('Tmpfile: {} (exist? {})'.format(ncfname, _os.path.exists(ncfname))) with _MFDataset(ncfname) as f: self.dimkey = list(f.dimensions.keys()) self.varkey = list(f.variables.keys()) self.dim = {} for d in self.dimkey: self.dim[d] = len(f.dimensions[d]) self.var = {} for v in self.varkey: self.var[v] = f.variables[v][:] _logger.debug('Tmpfile: {} (exist? {})'.format(ncfname, _os.path.exists(ncfname)))
[docs] def parse(self): raise NotImplementedError('An extended class should implement this method')
[docs]class NetcdfFileRaw(NetcdfFile): """ Represent a raw mode data file. """ def __init__(self, filename): try: NetcdfFile.__init__(self, filename) except (OSError, EOFError, ValueError) as e: # NetCDF Unknwon file format, Compressed file ended before... _logger.warning('Failed to load the file {}.'.format(filename)) _logger.warning('\tException: {}'.format(str(e))) raise _ex.IrfpyException('The file {} could not be read by some reason.'.format(filename))
[docs] def parse(self): """ Parsing the netcdf file :return: A tuple, ``(tlist, dlist)``. ``dlist`` is a list of :class:`NpdRawPacket` objects. """ # Time from irfpy.util import utc as _utc tlist = [_utc.convert(t, _datetime.datetime) for t in self.var['Time']] dtlist = self.var['DeltaT'] # (N, ) cntslist = self.var['Cnts'] # (N, 6) caliblist = self.var['Calib'] # (N, 4) regslist = self.var['Regs'] # (N, 3) intenslist = self.var['Intens'] # (N, 512) shaped dlist = [NpdRawPacket(tlist[i], dtlist[i], cntslist[i], intenslist[i], caliblist[i], regslist[i]) for i in range(len(tlist))] return (tlist, dlist)
[docs]def load_npd_rawmode(filename): """ Loading function of NPD raw mode data. :param filename: The netcdf data file. Gzipped. :return: A tuple, (tlist, dlist). dlist element format, TBD. """ try: rawFile = NetcdfFileRaw(filename) except _ex.IrfpyException as e: _logger.warning('NCDF file could not be read. ERRMSG: {}\n'.format(str(e))) return (), () tlist, dlist = rawFile.parse() return tlist, dlist