def get_table_data( arg, ncols=1, colkeys=None, make_copy=True, fix_type=True, blockname=None, hdrkeys=None): """ get_table_data( filename , ncols=1 [, colkeys=None [, make_copy=True [, fix_type=True [, blockname=None [, hdrkeys=None ]]]]]) get_table_data( TABLECrate , ncols=1 [, colkeys=None [, make_copy=True [, fix_type=True [, blockname=None [, hdrkeys=None ] ]]]]) """ filename = '' close_dataset = False if type(arg) == str: arg = get_filename_from_dmsyntax(arg) tbl = open_crate(arg) if not isinstance(tbl, pycrates.TABLECrate): #######??????????????????????????????????######## dtn close_crate_dataset( tbl.get_dataset() ) #######??????????????????????????????????######## dtn raise IOErr('badfile', arg, 'TABLECrate obj') filename = tbl.get_filename() close_dataset = True # Make a copy of the data, since we don't know that pycrates will # do something sensible wrt reference counting elif isinstance(arg, pycrates.TABLECrate): tbl = arg filename = arg.get_filename() make_copy=False else: raise IOErr('badfile', arg, 'TABLECrate obj') # Crates "caches" open files by their filename in memory. If you try # to open a file multiple times (with DM syntax) it corrupts the Crate # in memory. This is a work-around to open the CrateDataset without # DM syntax and iterate through the crates looking for the block # name that matches. if blockname is not None: crate = _get_crate_by_blockname(tbl.get_dataset(), blockname) tbl = crate or tbl cnames = list(pycrates.get_col_names(tbl, vectors=False, rawonly=True)) if colkeys is not None: colkeys = [str(name).strip() for name in list(colkeys)] elif (type(arg) == str and (not os.path.isfile(arg)) and '[' in arg and ']' in arg): colkeys = cnames # Try Channel, Counts or X,Y before defaulting to first two table cols elif 'CHANNEL' in cnames and 'COUNTS' in cnames: colkeys = ['CHANNEL','COUNTS'] elif 'X' in cnames and 'Y' in cnames: colkeys = ['X','Y'] else: colkeys = cnames[:ncols] cols = [] for name in colkeys: for col in _require_tbl_col(tbl, name, cnames, make_copy, fix_type): cols.append(col) hdr={} if hdrkeys is not None: for key in hdrkeys: hdr[key] = _require_hdr_key(tbl, key) if close_dataset: close_crate_dataset(tbl.get_dataset()) return colkeys, cols, filename, hdr
def get_table_data(arg, ncols=1, colkeys=None, make_copy=True, fix_type=True, blockname=None, hdrkeys=None): """ get_table_data( filename , ncols=1 [, colkeys=None [, make_copy=True [, fix_type=True [, blockname=None [, hdrkeys=None ]]]]]) get_table_data( TABLECrate , ncols=1 [, colkeys=None [, make_copy=True [, fix_type=True [, blockname=None [, hdrkeys=None ] ]]]]) """ filename = '' close_dataset = False if type(arg) == str: arg = get_filename_from_dmsyntax(arg) tbl = open_crate(arg) if not isinstance(tbl, pycrates.TABLECrate): # ??????????????????????????????????######## dtn close_crate_dataset(tbl.get_dataset()) # ??????????????????????????????????######## dtn raise IOErr('badfile', arg, 'TABLECrate obj') filename = tbl.get_filename() close_dataset = True # Make a copy of the data, since we don't know that pycrates will # do something sensible wrt reference counting elif isinstance(arg, pycrates.TABLECrate): tbl = arg filename = arg.get_filename() make_copy = False else: raise IOErr('badfile', arg, 'TABLECrate obj') # Crates "caches" open files by their filename in memory. If you try # to open a file multiple times (with DM syntax) it corrupts the Crate # in memory. This is a work-around to open the CrateDataset without # DM syntax and iterate through the crates looking for the block # name that matches. if blockname is not None: crate = _get_crate_by_blockname(tbl.get_dataset(), blockname) tbl = crate or tbl cnames = list(pycrates.get_col_names(tbl, vectors=False, rawonly=True)) if colkeys is not None: colkeys = [str(name).strip() for name in list(colkeys)] elif (type(arg) == str and (not os.path.isfile(arg)) and '[' in arg and ']' in arg): colkeys = cnames # Try Channel, Counts or X,Y before defaulting to first two table cols elif 'CHANNEL' in cnames and 'COUNTS' in cnames: colkeys = ['CHANNEL', 'COUNTS'] elif 'X' in cnames and 'Y' in cnames: colkeys = ['X', 'Y'] else: colkeys = cnames[:ncols] cols = [] for name in colkeys: for col in _require_tbl_col(tbl, name, cnames, make_copy, fix_type): cols.append(col) hdr = {} if hdrkeys is not None: for key in hdrkeys: hdr[key] = _require_hdr_key(tbl, key) if close_dataset: close_crate_dataset(tbl.get_dataset()) return colkeys, cols, filename, hdr
def get_table_data( arg, ncols=1, colkeys=None, make_copy=True, fix_type=True, blockname=None, hdrkeys=None): """ get_table_data( filename , ncols=1 [, colkeys=None [, make_copy=True [, fix_type=True [, blockname=None [, hdrkeys=None ]]]]]) get_table_data( TABLECrate , ncols=1 [, colkeys=None [, make_copy=True [, fix_type=True [, blockname=None [, hdrkeys=None ] ]]]]) """ filename = '' if type(arg) == str and pycrates.Crate(arg).is_image()==0: filename = arg isbinary = True colnames = True dmsyn = '' if '[' in filename and ']' in filename: parts = filename.split('[') filename = parts.pop(0) if parts: dmsyn = parts.pop(0).lower() if not is_binary_file(filename): isbinary = False fd = open(filename, 'r') try: last=None line = fd.readline().strip() while len(line) > 0 and line[0] in '#%': last = line line = fd.readline().strip() if (last is not None and (len(last.split(' ')) != len(line.split(' ')) )): colnames = False finally: fd.close() if blockname is not None: arg += "[%s]" % str(blockname).upper() if (not isbinary) and (not colnames) and (not 'cols' in dmsyn): arg += "[opt colnames=none]" tbl = _open_crate(pycrates.TABLECrate, [arg]) filename = tbl.get_filename() # Make a copy of the data, since we don't know that pycrates will # do something sensible wrt reference counting elif isinstance(arg, pycrates.TABLECrate): tbl = arg filename = arg.get_filename() make_copy=False else: raise IOErr('badfile', arg, 'TABLECrate obj') cnames = list(pycrates.get_col_names(tbl, vectors=False, rawonly=True)) if colkeys is not None: colkeys = [str(name).strip() for name in list(colkeys)] elif (type(arg) == str and (not os.path.isfile(arg)) and '[' in arg and ']' in arg): colkeys = cnames # Try Channel, Counts or X,Y before defaulting to first two table cols elif 'CHANNEL' in cnames and 'COUNTS' in cnames: colkeys = ['CHANNEL','COUNTS'] elif 'X' in cnames and 'Y' in cnames: colkeys = ['X','Y'] else: colkeys = cnames[:ncols] cols = [] for name in colkeys: for col in _require_tbl_col(tbl, name, cnames, make_copy, fix_type): cols.append(col) hdr={} if hdrkeys is not None: for key in hdrkeys: hdr[key] = _require_hdr_key(tbl, key) return colkeys, cols, filename, hdr