def __init__(self,args): # Set logical unit number to pass between subroutines lubfr = 11 luprt = 51 # Physical file to be opened f = args print '1' # Open file for READING using FORTRAN open call py_bufrlib.fortopen(lubfr,f,'R') # Open file for WRITING using FORTRAN open call py_bufrlib.fortopen(luprt,'dumpbufr.out','W') print '2' # Connect file to BUFRLIB library via openbf py_bufrlib.openbf(11,'IN',11) print '3' # Set date length py_bufrlib.datelen(10) print '4' # Attempt to read a subset from the BUFR file ctr = 0 while True: subset,jdate,iret = py_bufrlib.readns(11) print 'SUBSET: ', subset, ', DATE: ', jdate, ', RET: ', iret, ' :: Count: ', ctr py_bufrlib.ufbdmp(lubfr,luprt) if iret == -1: break ctr=ctr+1
def __init__(self, args): # Set logical unit number to pass between subroutines lubfr = 11 luprt = 51 # Physical file to be opened f = args # Open file for READING using FORTRAN open call py_bufrlib.fortopen(lubfr, f, 'R') # Open file for WRITING using FORTRAN open call py_bufrlib.fortopen(luprt, 'dumpbufr.out', 'W') # Connect file to BUFRLIB library via openbf py_bufrlib.openbf(lubfr, 'IN', lubfr) py_bufrlib.datelen(10) # Attempt to read a subset from the BUFR file iret = 0 while iret != -1: subset, jdate, iret = py_bufrlib.readns(lubfr) print ' MESSAGE TYPE ' + subset + '\n\n' lun, il, im = py_bufrlib.status(lubfr) lunidx = lun - 1 ptag = stride_tricks.as_strided(py_bufrlib.tables.tag,\ strides=(py_bufrlib.tables.tag.shape[1],1)) ptyp = stride_tricks.as_strided(py_bufrlib.tables.typ,\ strides=(py_bufrlib.tables.typ.shape[1],1)) ########################################### # OK, in order to get character arrays FROM the BUFRLIB common block data # we need to reorganize the arrays. Typically the N-dimensional character # array returns from FORTRAN as # # ([dimenson length,]dimension length, character length) # # This, combined with FORTRAN ordering creates a messy way to get # characters back into the expected string order # See http://cens.ioc.ee/pipermail/f2py-users/2012-September/002320.html # for an example. # # Fortunately, after stumbling around with Numpy strides and the help of this # site: http://scipy-lectures.github.com/advanced/advanced_numpy/index.html#indexing-scheme-strides # there seems to be a standardized way to reorder the array such that # the resulting array is indexed as is expected when examining the FORTRAN code. # # So, for a "simple" character array like TYP(MAXJL) found in the BUFRLIB common # block /TABLES/ where TYP is CHARACTER*3, it returns from BUFRLIB to python # in the dimension (MAXJL,3). (Refer back to the first link for the example). # # We can rerder the array by using the following convention (essentially feeding # the shape of the N+1 D array in reverse order so that the first stride element # is the length of the character length): # # a = stride_tricks.as_strided(py_bufrlib."COMMONBLOCK"."VARIABLE", # strides=(py_bufrlib."COMMONBLOCK"."VARIABLE".shape[N],\ # ... # strides=(py_bufrlib."COMMONBLOCK"."VARIABLE".shape[1],1)) # # We then access 'a' as we would a N-dimensional array, returning a # numpy array with the characters in order. (use the .tostring() method to # "string-ize" it if desired.) # # This has worked on a 1D character array (TAG, TYP in the above code) and a # 2D character array (TABB, below). I have not tried this on anything greater # than a 2D character array, though the logic is extensible, I assume. # ########################################### for nv in range(0, py_bufrlib.usrint.nval[lunidx]): node = py_bufrlib.usrint.inv[nv, lunidx] - 1 nemo = ptag[node].tostring() ityp = py_bufrlib.tables.itp[node] type = ptyp[node].tostring() numb = '' desc = '' unit = '' rval = '' # print '\tNODE: ', node # print '\tNEMO: ' + nemo # print '\tITYP: ', ityp # print '\tTYPE: ', type if ityp >= 1 and ityp <= 3: idn, tab, nemtab_iret = py_bufrlib.nemtab(lun, nemo) # print '\t\tIDN: ', idn # print '\t\tTAB: >>' + tab + '<<' # print '\t\tN: ', nemtab_iret tabb = stride_tricks.as_strided(py_bufrlib.tababd.tabb,\ strides=(py_bufrlib.tababd.tabb.shape[2],\ py_bufrlib.tababd.tabb.shape[1],1)) numb = tabb[nemtab_iret - 1, lunidx][0:6].tostring() desc = tabb[nemtab_iret - 1, lunidx][15:69].tostring().rstrip() unit = tabb[nemtab_iret - 1, lunidx][70:93].tostring().rstrip() rval = py_bufrlib.usrint.val[nv, lunidx] # # print '\t\tNUMB: ', numb # print '\t\tDESC: ', desc # print '\t\tUNIT: ', unit # print '\t\tRVAL: >>>', rval, '<<<' if ityp == 0 or ityp == 1: continue #print 'SKIPPING DUE TO TYPE: ', type elif ityp == 2: if py_bufrlib.ibfms(float(rval)) != 0: rval = 'MISSING' print '{0:6s} {1:10s} {2:>20s} {3:24s} {4:48s}'.format(\ numb,nemo,rval,unit,desc) else: # if isinstance(rval, basestring): print '{0:6s} {1:10s} {2:>20s} {3:24s} {4:48s}'.format(\ numb,nemo,str(rval),unit,desc) # else: # print '{0:6s} {1:10s} {2:20.2f} {3:24s} {4:48s}'.format(\ # numb,nemo,rval,unit,desc) else: continue
def __init__(self,args): # Set logical unit number to pass between subroutines lubfr = 11 luprt = 51 # Physical file to be opened f = args print '1' # Open file for READING using FORTRAN open call py_bufrlib.fortopen(lubfr,f,'R') # Open file for WRITING using FORTRAN open call #py_bufrlib.fortopen(luprt,'dumpbufr.out','W') print '2' # Connect file to BUFRLIB library via openbf py_bufrlib.openbf(11,'IN',11) print '3' # Set date length py_bufrlib.datelen(10) print '4' # Attempt to read a subset from the BUFR file ctr = 0 tableas = py_bufrlib.mytableas(11) tableds = py_bufrlib.mytableds(11) tablebs = py_bufrlib.mytablebs(11) #jtab,tab = py_bufrlib.getabdb(11) tabs = py_bufrlib.mygetabdb(11) for i in range(len(tabs)): myline = tabs[i].tostring().replace('\0','') if len(myline) > 0: print myline.strip() tab = py_bufrlib.mytabledict(11) tbldict = {} for i in range(len(tab)): myline = tab[i].tostring().replace('\0','') if len(myline) > 0: #print myline.strip() k,v = myline.strip().split('::') tbldict[k.strip()] = v print tbldict print tabs.tolist() print tabs[:].tostring() for s in tabs[:]: print s.tostring() print '------' vals = [tabs[x].tostring().replace('\0','').strip() for x in range(len(tabs)) if len(tabs[x].tostring().replace('\0','')) > 0] ta = [tableas[x].tostring().replace('\0','').strip() for x in range(len(tableas)) if len(tableas[x].tostring().replace('\0','')) > 0] td = [tableds[x].tostring().replace('\0','').strip() for x in range(len(tableds)) if len(tableds[x].tostring().replace('\0','')) > 0] tb = [tablebs[x].tostring().replace('\0','').strip() for x in range(len(tablebs)) if len(tablebs[x].tostring().replace('\0','')) > 0] print '=== Ds =====', td print '=== Bs =====', tb print '=== As =====', ta self.ta_dict = self.makedict(ta) self.td_dict = self.makedict(td) self.tb_dict = self.makedict(tb) # print '=== td_dict =====', self.td_dict # for x in self.ta_dict['NC000002']: # if x in self.td_dict: # print x + ' Children: ' , self.td_dict[x] # elif x in self.tb_dict: # print x + ' Bs: ' , self.tb_dict[x] # else: # print 'Error: ' + x + ' not found in Table D or B' print '+++++++++++++++++++++' for a in sorted(self.ta_dict): print a + ':' for tln in self.ta_dict[a]: bs = self.getbs(tln,[]) print '\t' + tln + ': ', bs
def __init__(self, args): # Set logical unit number to pass between subroutines lubfr = 11 luprt = 51 # Physical file to be opened f = args # Open file for READING using FORTRAN open call py_bufrlib.fortopen(lubfr, f, "R") # Open file for WRITING using FORTRAN open call py_bufrlib.fortopen(luprt, "dumpbufr.out", "W") # Connect file to BUFRLIB library via openbf py_bufrlib.openbf(lubfr, "IN", lubfr) py_bufrlib.datelen(10) # Attempt to read a subset from the BUFR file iret = 0 while iret != -1: subset, jdate, iret = py_bufrlib.readns(lubfr) print " MESSAGE TYPE " + subset + "\n\n" lun, il, im = py_bufrlib.status(lubfr) lunidx = lun - 1 ptag = stride_tricks.as_strided(py_bufrlib.tables.tag, strides=(py_bufrlib.tables.tag.shape[1], 1)) ptyp = stride_tricks.as_strided(py_bufrlib.tables.typ, strides=(py_bufrlib.tables.typ.shape[1], 1)) ########################################### # OK, in order to get character arrays FROM the BUFRLIB common block data # we need to reorganize the arrays. Typically the N-dimensional character # array returns from FORTRAN as # # ([dimenson length,]dimension length, character length) # # This, combined with FORTRAN ordering creates a messy way to get # characters back into the expected string order # See http://cens.ioc.ee/pipermail/f2py-users/2012-September/002320.html # for an example. # # Fortunately, after stumbling around with Numpy strides and the help of this # site: http://scipy-lectures.github.com/advanced/advanced_numpy/index.html#indexing-scheme-strides # there seems to be a standardized way to reorder the array such that # the resulting array is indexed as is expected when examining the FORTRAN code. # # So, for a "simple" character array like TYP(MAXJL) found in the BUFRLIB common # block /TABLES/ where TYP is CHARACTER*3, it returns from BUFRLIB to python # in the dimension (MAXJL,3). (Refer back to the first link for the example). # # We can rerder the array by using the following convention (essentially feeding # the shape of the N+1 D array in reverse order so that the first stride element # is the length of the character length): # # a = stride_tricks.as_strided(py_bufrlib."COMMONBLOCK"."VARIABLE", # strides=(py_bufrlib."COMMONBLOCK"."VARIABLE".shape[N],\ # ... # strides=(py_bufrlib."COMMONBLOCK"."VARIABLE".shape[1],1)) # # We then access 'a' as we would a N-dimensional array, returning a # numpy array with the characters in order. (use the .tostring() method to # "string-ize" it if desired.) # # This has worked on a 1D character array (TAG, TYP in the above code) and a # 2D character array (TABB, below). I have not tried this on anything greater # than a 2D character array, though the logic is extensible, I assume. # ########################################### for nv in range(0, py_bufrlib.usrint.nval[lunidx]): node = py_bufrlib.usrint.inv[nv, lunidx] - 1 nemo = ptag[node].tostring() ityp = py_bufrlib.tables.itp[node] type = ptyp[node].tostring() numb = "" desc = "" unit = "" rval = "" # print '\tNODE: ', node # print '\tNEMO: ' + nemo # print '\tITYP: ', ityp # print '\tTYPE: ', type if ityp >= 1 and ityp <= 3: idn, tab, nemtab_iret = py_bufrlib.nemtab(lun, nemo) # print '\t\tIDN: ', idn # print '\t\tTAB: >>' + tab + '<<' # print '\t\tN: ', nemtab_iret tabb = stride_tricks.as_strided( py_bufrlib.tababd.tabb, strides=(py_bufrlib.tababd.tabb.shape[2], py_bufrlib.tababd.tabb.shape[1], 1), ) numb = tabb[nemtab_iret - 1, lunidx][0:6].tostring() desc = tabb[nemtab_iret - 1, lunidx][15:69].tostring().rstrip() unit = tabb[nemtab_iret - 1, lunidx][70:93].tostring().rstrip() rval = py_bufrlib.usrint.val[nv, lunidx] # # print '\t\tNUMB: ', numb # print '\t\tDESC: ', desc # print '\t\tUNIT: ', unit # print '\t\tRVAL: >>>', rval, '<<<' if ityp == 0 or ityp == 1: continue # print 'SKIPPING DUE TO TYPE: ', type elif ityp == 2: if py_bufrlib.ibfms(float(rval)) != 0: rval = "MISSING" print "{0:6s} {1:10s} {2:>20s} {3:24s} {4:48s}".format(numb, nemo, rval, unit, desc) else: # if isinstance(rval, basestring): print "{0:6s} {1:10s} {2:>20s} {3:24s} {4:48s}".format( numb, nemo, str(rval), unit, desc ) # else: # print '{0:6s} {1:10s} {2:20.2f} {3:24s} {4:48s}'.format(\ # numb,nemo,rval,unit,desc) else: continue
def __init__(self, args): # Set logical unit number to pass between subroutines lubfr = 11 luprt = 51 # Physical file to be opened f = args print '1' # Open file for READING using FORTRAN open call py_bufrlib.fortopen(lubfr, f, 'R') # Open file for WRITING using FORTRAN open call #py_bufrlib.fortopen(luprt,'dumpbufr.out','W') print '2' # Connect file to BUFRLIB library via openbf py_bufrlib.openbf(11, 'IN', 11) print '3' # Set date length py_bufrlib.datelen(10) print '4' # Attempt to read a subset from the BUFR file ctr = 0 tableas = py_bufrlib.mytableas(11) tableds = py_bufrlib.mytableds(11) tablebs = py_bufrlib.mytablebs(11) #jtab,tab = py_bufrlib.getabdb(11) tabs = py_bufrlib.mygetabdb(11) for i in range(len(tabs)): myline = tabs[i].tostring().replace('\0', '') if len(myline) > 0: print myline.strip() tab = py_bufrlib.mytabledict(11) tbldict = {} for i in range(len(tab)): myline = tab[i].tostring().replace('\0', '') if len(myline) > 0: #print myline.strip() k, v = myline.strip().split('::') tbldict[k.strip()] = v print tbldict print tabs.tolist() print tabs[:].tostring() for s in tabs[:]: print s.tostring() print '------' vals = [ tabs[x].tostring().replace('\0', '').strip() for x in range(len(tabs)) if len(tabs[x].tostring().replace('\0', '')) > 0 ] ta = [ tableas[x].tostring().replace('\0', '').strip() for x in range(len(tableas)) if len(tableas[x].tostring().replace('\0', '')) > 0 ] td = [ tableds[x].tostring().replace('\0', '').strip() for x in range(len(tableds)) if len(tableds[x].tostring().replace('\0', '')) > 0 ] tb = [ tablebs[x].tostring().replace('\0', '').strip() for x in range(len(tablebs)) if len(tablebs[x].tostring().replace('\0', '')) > 0 ] print '=== Ds =====', td print '=== Bs =====', tb print '=== As =====', ta self.ta_dict = self.makedict(ta) self.td_dict = self.makedict(td) self.tb_dict = self.makedict(tb) # print '=== td_dict =====', self.td_dict # for x in self.ta_dict['NC000002']: # if x in self.td_dict: # print x + ' Children: ' , self.td_dict[x] # elif x in self.tb_dict: # print x + ' Bs: ' , self.tb_dict[x] # else: # print 'Error: ' + x + ' not found in Table D or B' print '+++++++++++++++++++++' for a in sorted(self.ta_dict): print a + ':' for tln in self.ta_dict[a]: bs = self.getbs(tln, []) print '\t' + tln + ': ', bs