Example #1
0
    def dump_subset(self,filename,append=False,verbose=False):
        """
        dump a textual representation of the decoded
        data in the currently loaded subset to a file.

        If `append=True`, append to an existing file
        (otherwise over-write file).

        If `verbose=True`, more complete but harder to read info is written.

        `ncepbufr.open.load_subset` must be called before
        trying to print the decoded subset using `ncepbufr.open.dump_subset`.
        """
        lunout = random.choice(_funits)
        if not append:
            iret = _bufrlib.fortran_open(filename,lunout,'formatted','rewind')
        else:
            iret = _bufrlib.fortran_open(filename,lunout,'formatted','append')
        if iret != 0:
            msg='error opening %s' % filename
        if not verbose:
            _bufrlib.ufdump(self.lunit,lunout)
        else:
            _bufrlib.ufbdmp(self.lunit,lunout)
        iret = _bufrlib.fortran_close(lunout)
        if iret == 0:
            bisect.insort_left(_funits,lunout)
        else:
            raise IOError('error closing %s' % filename)
Example #2
0
    def dump_subset(self, filename, append=False, verbose=False):
        """
        dump a textual representation of the decoded
        data in the currently loaded subset to a file.

        If `append=True`, append to an existing file
        (otherwise over-write file).

        If `verbose=True`, more complete but harder to read info is written.

        `ncepbufr.open.load_subset` must be called before
        trying to print the decoded subset using `ncepbufr.open.dump_subset`.
        """
        lunout = random.choice(_funits)
        if not append:
            iret = _bufrlib.fortran_open(filename, lunout, 'formatted',
                                         'rewind')
        else:
            iret = _bufrlib.fortran_open(filename, lunout, 'formatted',
                                         'append')
        if iret != 0:
            msg = 'error opening %s' % filename
        if not verbose:
            _bufrlib.ufdump(self.lunit, lunout)
        else:
            _bufrlib.ufbdmp(self.lunit, lunout)
        iret = _bufrlib.fortran_close(lunout)
        if iret == 0:
            bisect.insort_left(_funits, lunout)
        else:
            raise IOError('error closing %s' % filename)
Example #3
0
 def dump_table(self,filename):
     """
     dump embedded bufr table to a file
     """
     lundx = random.choice(_funits)
     iret = _bufrlib.fortran_open(filename,lundx,'formatted','rewind')
     if iret != 0:
         msg='error opening %s' % filename
     _bufrlib.dxdump(self.lunit,lundx)
     iret = _bufrlib.fortran_close(lundx)
     if iret == 0:
         bisect.insort_left(_funits,lundx)
     else:
         raise IOError('error closing %s' % filename)
Example #4
0
 def dump_table(self, filename):
     """
     dump embedded bufr table to a file
     """
     lundx = random.choice(_funits)
     iret = _bufrlib.fortran_open(filename, lundx, 'formatted', 'rewind')
     if iret != 0:
         msg = 'error opening %s' % filename
     _bufrlib.dxdump(self.lunit, lundx)
     iret = _bufrlib.fortran_close(lundx)
     if iret == 0:
         bisect.insort_left(_funits, lundx)
     else:
         raise IOError('error closing %s' % filename)
Example #5
0
    def __init__(self,filename,mode='r',table=None,datelen=10):
        """
        bufr object constructor

        `filename`: bufr file name.

        `mode`: `'r'` for read, `'w'` for write, `'a'` for append (default
        `'r'`).

        `datelen`:  number of digits for date specification (default 10, gives
        `YYYYMMDDHH`).
        """
        # randomly choose available fortran unit number
        self.lunit = random.choice(_funits)
        '''bufr file opened with this fortran unit number'''
        _funits.remove(self.lunit)
        if not _funits:
            raise IOError("too many files open")
        if mode == 'r':
            self._ioflag = 'IN'
        elif mode == 'w':
            if table is None:
                msg="must specify file containing bufr table when mode='w'"
                raise ValueError(msg)
            self._ioflag = 'OUT'
        elif mode == 'a':
            self._ioflag = 'APN'
        else:
            raise ValueError("mode must be 'r', 'w' or 'a'")
        if mode == 'r' or mode == 'a':
            # table embedded in bufr file
            iret = _bufrlib.fortran_open(filename,self.lunit,"unformatted","rewind")
            if iret != 0:
                msg='error opening %s' % filename
                raise IOError(msg)
            _bufrlib.openbf(self.lunit,self._ioflag,self.lunit)
            self.lundx = None
            self.table = None
        elif mode == 'w':
            self.lundx = random.choice(_funits)
            self.table = table
            iret = _bufrlib.fortran_open(table,self.lundx,"formatted","rewind")
            if iret != 0:
                msg='error opening %s' % table
            iret = _bufrlib.fortran_open(filename,self.lunit,"unformatted","rewind")
            if iret != 0:
                msg='error opening %s' % filename
            _bufrlib.openbf(self.lunit,self._ioflag,self.lundx)
        # set date length (default 10 means YYYYMMDDHH)
        self.set_datelength()
        # initialized message number counter
        self.msg_counter = 0
        '''current bufr message number'''
        self.msg_type = None
        '''current bufr message type'''
        self.msg_date = None
        '''reference date for bufr message'''
        self.receipt_time = None
        '''tank recipt time for bufr message (`YYYYMMDDHHMM`), -1 if missing'''
        self.subsets = None
        '''number of subsets in the bufr message'''
        # missing value in decoded data.
        # (if equal to self.missing_value, data is masked)
        self.missing_value = _bufrlib.getbmiss()
        '''bufr missing value'''
Example #6
0
    def __init__(self, filename, mode='r', table=None, datelen=10):
        """
        bufr object constructor

        `filename`: bufr file name.

        `mode`: `'r'` for read, `'w'` for write, `'a'` for append (default
        `'r'`).

        `datelen`:  number of digits for date specification (default 10, gives
        `YYYYMMDDHH`).
        """
        # randomly choose available fortran unit number
        self.lunit = random.choice(_funits)
        '''bufr file opened with this fortran unit number'''
        _funits.remove(self.lunit)
        if not _funits:
            raise IOError("too many files open")
        if mode == 'r':
            self._ioflag = 'IN'
        elif mode == 'w':
            if table is None:
                msg = "must specify file containing bufr table when mode='w'"
                raise ValueError(msg)
            self._ioflag = 'OUT'
        elif mode == 'a':
            self._ioflag = 'APN'
        else:
            raise ValueError("mode must be 'r', 'w' or 'a'")
        if mode == 'r' or mode == 'a':
            # table embedded in bufr file
            iret = _bufrlib.fortran_open(filename, self.lunit, "unformatted",
                                         "rewind")
            if iret != 0:
                msg = 'error opening %s' % filename
                raise IOError(msg)
            _bufrlib.openbf(self.lunit, self._ioflag, self.lunit)
            self.lundx = None
            self.table = None
        elif mode == 'w':
            self.lundx = random.choice(_funits)
            self.table = table
            iret = _bufrlib.fortran_open(table, self.lundx, "formatted",
                                         "rewind")
            if iret != 0:
                msg = 'error opening %s' % table
            iret = _bufrlib.fortran_open(filename, self.lunit, "unformatted",
                                         "rewind")
            if iret != 0:
                msg = 'error opening %s' % filename
            _bufrlib.openbf(self.lunit, self._ioflag, self.lundx)
        # set date length (default 10 means YYYYMMDDHH)
        self.set_datelength()
        # initialized message number counter
        self.msg_counter = 0
        '''current bufr message number'''
        self.msg_type = None
        '''current bufr message type'''
        self.msg_date = None
        '''reference date for bufr message'''
        self.receipt_time = None
        '''tank recipt time for bufr message (`YYYYMMDDHHMM`), -1 if missing'''
        self.subsets = None
        '''number of subsets in the bufr message'''
        # missing value in decoded data.
        # (if equal to self.missing_value, data is masked)
        self.missing_value = _bufrlib.getbmiss()
        '''bufr missing value'''
Example #7
0
    def __init__(self,filename,mode='r',table=None):
        """
        bufr object constructor

        `filename`: bufr file name.

        `mode`: `'r'` for read, `'w'` for write, `'a'` for append (default
        `'r'`).

        `table`:  bufr table filename or ncepbufr.open instance.
        Must be specified for `mode='w'`, optional for `mode='r'`.
        If table is an existing ncepbufr.open instance, the table
        will be shared. If not, it is assumed to be the filename of a bufr table.
        For `mode='r'`, bufr table embedded in file will be used if not specified.
        """
        # randomly choose available fortran unit number
        self.lunit = random.choice(_funits)
        self.filename = filename
        '''bufr file opened with this fortran unit number'''
        _funits.remove(self.lunit)
        if not _funits:
            raise IOError("too many files open")
        if mode == 'r':
            self._ioflag = 'IN'
        elif mode == 'w':
            if table is None:
                msg="must specify file containing bufr table when mode='w'"
                raise ValueError(msg)
            self._ioflag = 'OUT'
        elif mode == 'a':
            self._ioflag = 'APN'
        else:
            raise ValueError("mode must be 'r', 'w' or 'a'")
        if mode == 'r' or mode == 'a':
            if not os.path.isfile(filename):
                msg='%s does not exist' % filename
                raise IOError(msg)
            iret = _bufrlib.fortran_open(filename,self.lunit,"unformatted","rewind")
            if iret != 0:
                msg='error opening %s' % filename
                raise IOError(msg)
            if table is None:
                # table embedded in bufr file
                _bufrlib.openbf(self.lunit,self._ioflag,self.lunit)
                self.lundx = self.lunit # table unit number same as bufr unit number
            else:
                try:
                    # share a bufr table with another instance
                    self.lundx = table.lunit
                except AttributeError:
                    # external table file specified
                    self.lundx = random.choice(_funits)
                    iret = _bufrlib.fortran_open(table,self.lundx,"formatted","rewind")
                    if iret != 0:
                        msg='error opening %s' % filename
                        raise IOError(msg)
                    _funits.remove(self.lundx)
                _bufrlib.openbf(self.lunit,self._ioflag,self.lundx)
        elif mode == 'w':
            try:
                # share a bufr table with another instance
                self.lundx = table.lunit
            except AttributeError:
                # read bufr table from a file.
                self.lundx = random.choice(_funits)
                iret = _bufrlib.fortran_open(table,self.lundx,"formatted","rewind")
                if iret != 0:
                    msg='error opening %s' % table
                    raise IOError(msg)
                _funits.remove(self.lundx)
            iret = _bufrlib.fortran_open(filename,self.lunit,"unformatted","rewind")
            if iret != 0:
                msg='error opening %s' % filename
                raise IOError(msg)
            _bufrlib.openbf(self.lunit,self._ioflag,self.lundx)
        # initialized message number counter
        self.msg_counter = 0
        '''current bufr message number'''
        self.msg_type = None
        '''current bufr message type'''
        self.msg_date = None
        '''reference date for bufr message'''
        self.receipt_time = None
        '''tank receipt time for bufr message (`YYYYMMDDHHMM`), -1 if missing'''
        self.subsets = None
        '''number of subsets in the bufr message'''
        # missing value in decoded data.
        # (if equal to self.missing_value, data is masked)
        self.missing_value = get_missing_value()
        '''bufr missing value'''