def _open (self, file_id): from rpnpy.librmn.base import fnom from rpnpy.librmn.fstd98 import fstouv from rpnpy.librmn.const import FST_RO from fstd2nc.extra import librmn opened_file_id = getattr(self,'_opened_file_id',-1) # Check if this file already opened. if opened_file_id == file_id: return self._opened_funit # Close any open files before continuing. self._close() filename = self._files[file_id] # Open the file. self._opened_file_id = file_id self._opened_funit = fnom(filename,FST_RO) fstouv(self._opened_funit,FST_RO) self._opened_librmn_index = librmn.file_index(self._opened_funit) return self._opened_funit
def _get_fileinfo(self): """ Reads some basic general information from the burp file without having to fully open the file. Returns ------- nrep number of reports in the file rep_max length of longest report in the file """ assert 'r' in self.mode, "BurpFile must be in read mode to use this function." ier = _brp.mrfopt(_rbc.BURPOP_MSGLVL, _rbc.BURPOP_MSG_FATAL) unit = _rb.fnom(self.fname, _rc.FST_RO) nrep = _brp.mrfnbr(unit) rep_max = _brp.mrfmxl(unit) ier = _rb.fclos(unit) return nrep, rep_max
def _get_fileinfo(self): """ Reads some basic general information from the burp file without having to fully open the file. Returns: (nrep, rep_max), tuple where: nrep : number of reports in the file rep_max : length of longest report in the file """ assert 'r' in self.mode, "BurpFile must be in read mode to use this function." ier = _brp.mrfopt(_rbc.BURPOP_MSGLVL, _rbc.BURPOP_MSG_FATAL) unit = _rb.fnom(self.fname, _rc.FST_RO) nrep = _brp.mrfnbr(unit) rep_max = _brp.mrfmxl(unit) ier = _rb.fclos(unit) return nrep, rep_max
def burp_open(filename, filemode=_rbc.BURP_MODE_READ): """ Open the specified burp file Shortcut for fnom+mrfopn iunit = burp_open(filename) iunit = burp_open(filename, FST_RO) Args: paths : path/name of the file to open if paths is a list, open+link all files if path is a dir, open+link all fst files in dir filemode : a string with the desired filemode (see librmn doc) or one of these constants: BURP_MODE_READ, BURP_MODE_CREATE, BURP_MODE_APPEND Returns: int, file unit number associated with provided path None in ReadOnly mode if no burp file was found in path Raises: TypeError on wrong input arg types ValueError on invalid input arg value BurpError on any other error Examples: >>> import os, os.path >>> import rpnpy.librmn.all as rmn >>> ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() >>> filename = os.path.join(ATM_MODEL_DFILES,'bcmk_burp','2007021900.brp') >>> funit1 = rmn.burp_open(filename) >>> funit2 = rmn.burp_open('newfile.brp', rmn.BURP_MODE_CREATE) >>> #... >>> rmn.burp_close(funit1) >>> rmn.burp_close(funit2) >>> os.unlink('newfile.fst') # Remove test file See Also: mrfopn mrfcls burp_close rpnpy.librmn.base.fnom rpnpy.librmn.burp_const BurpError """ if not isinstance(filename, str): raise TypeError("burp_open: Expecting arg of type str, Got {0}"\ .format(type(filename))) if filename.strip() == '': raise ValueError("burp_open: must provide a valid filename") if filemode == _rbc.BURP_MODE_CREATE: fstmode = _rc.FST_RW elif filemode == _rbc.BURP_MODE_APPEND: fstmode = _rc.FST_RW_OLD elif filemode == _rbc.BURP_MODE_READ: fstmode = _rc.FST_RO else: raise ValueError('filemode should be one of BURP_MODE_READ, BURP_MODE_CREATE, BURP_MODE_APPEND') if filemode != _rbc.BURP_MODE_CREATE: if not isBURP(filename): raise BurpError('Not a burp file: {0}'.format(filename)) funit = _rb.fnom(filename, fstmode) if not funit: raise BurpError('Problem associating a unit with the file: {0}' .format(filename)) nrep = mrfopn(funit, filemode) return funit