def burp_close(iunit): """ Close the burp file associated with provided file unit number. Shortcut for fclos+mrfcls Args: iunit : unit number associated to the file obtained with fnom or burp_open Returns: None Raises: TypeError on wrong input arg types FSTDError 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) >>> #... >>> rmn.burp_close(funit1) See Also: mrfopn mrfcls burp_open rpnpy.librmn.base.fnom rpnpy.librmn.base.fclos rpnpy.librmn.burp_const BurpError """ mrfcls(iunit) _rb.fclos(iunit)
def _close (self): from rpnpy.librmn.base import fclos from rpnpy.librmn.fstd98 import fstfrm opened_funit = getattr(self,'_opened_funit',-1) if opened_funit >= 0: fstfrm(opened_funit) fclos(opened_funit) self._opened_file_id = -1 self._opened_funit = -1 self._opened_librmn_index = -1
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 write_burpfile(self): """ Writes BurpFile instance to a BURP file. """ assert 'w' in self.mode, "BurpFile must be in write mode to use this function." print "Writing BURP file to \'%s\'" % self.fname handle = 0 nsup = 0 nxaux = 0 warn = True # convert lat, lon back into integers ilon = _np.round(100*self.lon).astype(int) ilat = _np.round(100*self.lat+9000).astype(int) ilon[ilon<0] = ilon[ilon<0]+36000 idate = self.year*10000 + self.month*100 + self.day itime = self.hour*100 + self.minute # buffer for report data nbuf = self._calc_nbuf() buf = _np.empty((nbuf, ), dtype=_np.int32) buf[0] = nbuf # open BURP file _brp.mrfopt(_rbc.BURPOP_MSGLVL, _rbc.BURPOP_MSG_FATAL) unit = _brp.burp_open(self.fname, _rbc.BURP_MODE_CREATE) # loop over reports for irep in xrange(self.nrep): # write report header _brp.mrbini(unit, buf, itime[irep], self.flgs[irep], self.stnids[irep], self.codtyp[irep], ilat[irep], ilon[irep], self.dx[irep], self.dy[irep], self.alt[irep], self.delay[irep], idate[irep], self.rs[irep], self.runn[irep], self.sup[irep], nsup, self.xaux[irep], nxaux) for iblk in xrange(self.nblk[irep]): nele = self.nelements[irep][iblk] nlev = self.nlev[irep][iblk] nt = self.nt[irep][iblk] # convert BUFR codes to CMC codes lstele = _np.empty((nele, ), dtype=_np.int32) _brp.mrbcol(self.elements[irep][iblk], lstele, nele) # convert real values to integer table values rval = _np.ravel(self.rval[irep][iblk], order='F') tblval = _np.round(rval).astype(_np.int32) if self.datyp[irep][iblk] < 5: _brp.mrbcvt(lstele, tblval, rval, nele, nlev, nt, _rbc.MRBCVT_ENCODE) tbl_out = tblval elif self.datyp[irep][iblk] < 7: tbl_out = rval else: if warn: _warnings.warn("Unrecognized data type value of %i. Unconverted table values will be written." % self.datyp[irep][iblk]) warn = False tbl_out = tblval # add block to report _brp.mrbadd(buf, _ct.pointer(_ct.c_int(iblk+1)), nele, nlev, nt, self.bfam[irep][iblk], self.bdesc[irep][iblk], self.btyp[irep][iblk], self.nbit[irep][iblk], _ct.pointer(_ct.c_int(self.bit0[irep][iblk])), self.datyp[irep][iblk], lstele, tbl_out) # write report _brp.mrfput(unit, handle, buf) # close BURP file _brp.mrfcls(unit) _rb.fclos(unit) return
def write_burpfile(self): """ Writes BurpFile instance to a BURP file. Returns: None Raises: BurpError """ assert 'w' in self.mode, "BurpFile must be in write mode to use this function." print("Writing BURP file to \'{}\'".format(self.fname)) handle = 0 nsup = 0 nxaux = 0 warn = True # convert lat, lon back into integers ilon = _np.round(100 * self.lon).astype(int) ilat = _np.round(100 * self.lat + 9000).astype(int) ilon[ilon < 0] = ilon[ilon < 0] + 36000 idate = self.year * 10000 + self.month * 100 + self.day itime = self.hour * 100 + self.minute # buffer for report data nbuf = self._calc_nbuf() buf = _np.empty((nbuf, ), dtype=_np.int32) buf[0] = nbuf # open BURP file _brp.mrfopt(_rbc.BURPOP_MSGLVL, _rbc.BURPOP_MSG_FATAL) unit = _brp.burp_open(self.fname, _rbc.BURP_MODE_CREATE) # loop over reports for irep in range(self.nrep): # write report header _brp.mrbini(unit, buf, itime[irep], self.flgs[irep], self.stnids[irep], self.codtyp[irep], ilat[irep], ilon[irep], self.dx[irep], self.dy[irep], self.alt[irep], self.delay[irep], idate[irep], self.rs[irep], self.runn[irep], self.sup[irep], nsup, self.xaux[irep], nxaux) for iblk in range(self.nblk[irep]): nele = self.nelements[irep][iblk] nlev = self.nlev[irep][iblk] nt = self.nt[irep][iblk] # convert BUFR codes to CMC codes cmcids = _brp.mrbcol(self.elements[irep][iblk]) # convert real values to integer table values if self.datyp[irep][iblk] < 5: tblval = _brp.mrbcvt_encode(cmcids, self.rval[irep][iblk]) tblval = _np.ravel(tblval, order='F') else: rval = _np.ravel(self.rval[irep][iblk], order='F') tblval = _np.round(rval).astype(_np.int32) if self.datyp[irep][iblk] > 6 and warn: _warnings.warn( "Unrecognized data type value of %i. Unconverted table values will be written." % self.datyp[irep][iblk]) warn = False # add block to report _brp.mrbadd(buf, _ct.pointer(_ct.c_int(iblk + 1)), nele, nlev, nt, self.bfam[irep][iblk], self.bdesc[irep][iblk], self.btyp[irep][iblk], self.nbit[irep][iblk], _ct.pointer(_ct.c_int(self.bit0[irep][iblk])), self.datyp[irep][iblk], cmcids, tblval) # write report _brp.mrfput(unit, handle, buf) # close BURP file _brp.mrfcls(unit) _rb.fclos(unit) return