def get_header(file,**kw): '''gets the header for the .p4 file, note that this Advanced the file position to the end of the header. Returns the size of the header, and the size of the header, if the header keyword is true. ''' if type(file) == str: #if called with a filename, recall with opened file. if test(kw, "gzip"): with gzip.open(file,'r') as f: return get_header(f,**kw); else: with open(file,'r') as f: return get_header(f,**kw); if test(kw, "size"): size = file.tell(); header = get_dict( file,'iiss', ['dump_type','dversion', 'title','revision']); if header['dump_type'] == 2 or header['dump_type'] == 3: #this is a fields file or a scalars file. d = get_dict(file,'fii',['timestamp','geometry','domains']); header.update(d); #reading quantities n = get_int(file); names=[get_str(file) for i in range(n)]; units=[get_str(file) for i in range(n)]; header['quantities'] = zip(names,units); elif header['dump_type'] == 6: #this is a particle movie file d = get_dict(file, 'iiii', ['geometry','sflagsx','sflagsy','sflagsz']); header.update(d); #reading params n = get_int(file); flags=[bool(get_int(file)) for i in range(n)]; units=[get_str(file) for i in range(n)]; labels=['q','x','y','z','ux','uy','uz','E']; if n == 8: pass; elif n == 7: labels = labels[:-1]; elif n == 11: labels+=['xi','yi','zi']; else: raise NotImplementedError('Not implemented for these number of parameters:{}.'.format(n)); header['params'] = [ (label,unit) for (label,unit,flag) in zip(labels,units,flags) if flag ]; elif header['dump_type'] == 10: #this is a particle extraction file: header['geometry'] = get_int(file); #reading quantities n = get_int(file); header['quantities'] = [get_str(file) for i in range(n)]; else: raise ValueError('Unknown dump_type: {}'.format(header['dump_type'])); if test(kw,'size'): return header, file.tell()-size; return header;
def read(fname,**kw): ''' Reads an lsp output file and returns a raw dump of data, sectioned into quantities either as an dictionary or a typed numpy array. Parameters: ----------- fname -- filename of thing to read Keyword Arguments: ------------------ vprint -- Verbose printer. Used in scripts override -- (type, start) => A tuple of a dump type and a place to start in the passed file, useful to attempting to read semicorrupted files. gzip -- Read as a gzip file. flds/sclr Specific Arguments: ----------------------------- var -- list of quantities to be read. For fields, this can consist of strings that include vector components, e.g., 'Ex'. If None (default), read all quantities. keep_edges -- If set to truthy, then don't remove the edges from domains before concatenation and don't reshape the flds data. sort -- If not None, sort using these indices, useful for avoiding resorting. If True and not an ndarray, just sort. first_sort -- If truthy, sort, and return the sort data for future flds that should have the same shape. ''' openf = gzip.open if test(kw, 'gzip') else open; with openf(fname,'rb') as file: if test(kw,'override'): dump, start = kw['override']; file.seek(start); header = {'dump_type': dump}; if not test(kw, 'var') and 2 <= header['dump_type'] <= 3 : raise ValueError("If you want to force to read as a scalar, you need to supply the quantities") else: header = get_header(file); vprint = kw['vprint'] if test(kw, 'vprint') else lambda s: None; if 2 <= header['dump_type'] <= 3 : if not test(kw, 'var'): var=[i[0] for i in header['quantities']]; else: var=kw['var']; keep_edges = test(kw, 'keep_edges'); first_sort = test(kw, 'first_sort'); if test(kw,'sort'): sort = kw['sort'] else: sort = None; readers = { 2: lambda: read_flds( file,header,var,vprint, keep_edges=keep_edges, first_sort=first_sort, sort=sort), 3: lambda: read_flds( file,header,var, vprint, keep_edges=keep_edges, first_sort=first_sort, sort=sort, vector=False), 6: lambda: read_movie(file, header), 10:lambda: read_pext(file,header) }; try: d = readers[header['dump_type']](); except KeyError: raise NotImplementedError("Other file types not implemented yet!"); return d;