Source code for irfpy.npicommon.backend

''' Backend of NPI data set.
'''
import logging

from irfpy.util.julday import JdSeries

[docs]class NpiFileCommon: ''' NPI data file for the base of both MEX and VEX. This is a backend of the NPI database. The class is an abstract class, therefore, it should be impolemented according to MEX and VEX. :param filename: File name to open. :param compress: Force decompression. ``None`` or "plain" or "gzip" or "bzip2". If ``None``'s case, one assumed the compression from the suffix. :param baseuriname: If specified, ``self.filename`` is replaced by the baseuriname instead of the real file name. This may be used if the open file is a temporal file (decompressed or network file). ''' def __init__(self, filename, compress=None, baseuriname=None, loglevel=None): if baseuriname is None: self.filename = filename else: self.filename = baseuriname if loglevel is None: loglevel = logging.WARN self.logger = logging.getLogger("%s:%s" % (self.__class__.__name__, self.filename)) # self.logger.setLevel(loglevel) self.npidata = JdSeries() fp = self.openfile(filename, compress=compress) self.loadfile(fp) fp.close()
[docs] def openfile(self, filename, compress=None): cmp = None # compress switch. if compress is not None: if compress in ("gz", "gzip"): cmp = "gzip" elif compress in ('bz2', 'bzip2', 'bz', 'bzip'): cmp = "bzip2" elif compress in ('plain'): cmp = "plain" if cmp is None: if filename.endswith('.gz'): cmp = "gzip" elif filename.endswith('.bz2'): cmp = "bzip2" else: cmp = "plain" if cmp == "gzip": import gzip self.logger.debug('Using gzip') f = gzip.open(filename) if cmp == "bzip2": import bz2 self.logger.debug('Using bzip2') f = bz2.BZ2File(filename, 'r') else: self.logger.debug('Using plain') f = open(filename, 'r') return f
[docs] def loadfile(self, fp): ''' ABSTRACT: Read the data. Abstract method. Load the file File should be loaded from the file pointer fp, and saved it into the ``JdSeries`` instance named ``self.npidata``. ''' raise NotImplementedError( "The method loadfile in class %s should be overrided in the child class." % self.__class__.__name__)
[docs] def getDataset(self): ''' Return the reference to the dataset, i.e. ``self.npidata``. ''' return self.npidata