def parse_ncnr_file(filename): """ Parse NCNR reduced data file returning *header* and *data*. *header* dictionary of fields such as 'data', 'title', 'instrument' *data* 2D array of data If 'columns' is present in header, it will be a list of the names of the columns. If 'instrument' is present in the header, the default instrument geometry will be specified. Slit geometry is set to the default from the instrument if it is not available in the reduced file. """ header, data = parse_file(filename) # Fill in instrument parameters, if not available from the file if 'instrument' in header and header['instrument'] in INSTRUMENTS: instrument = INSTRUMENTS[header['instrument']] header.setdefault('radiation',instrument.radiation) header.setdefault('wavelength',str(instrument.wavelength)) header.setdefault('dLoL',str(instrument.dLoL)) header.setdefault('d_s1',str(instrument.d_s1)) header.setdefault('d_s2',str(instrument.d_s2)) if 'columns' in header: header['columns'] = header['columns'].split() for key in ('wavelength','dLoL','d_s1','d_s2'): if key in header: header[key] = float(header[key]) return header, data
def parse_ncnr_file(filename): """ Parse NCNR reduced data file returning *header* and *data*. *header* dictionary of fields such as 'data', 'title', 'instrument' *data* 2D array of data If 'columns' is present in header, it will be a list of the names of the columns. If 'instrument' is present in the header, the default instrument geometry will be specified. Slit geometry is set to the default from the instrument if it is not available in the reduced file. """ header, data = parse_file(filename) # Fill in instrument parameters, if not available from the file if 'instrument' in header and header['instrument'] in INSTRUMENTS: instrument = INSTRUMENTS[header['instrument']] header.setdefault('radiation', instrument.radiation) header.setdefault('wavelength', str(instrument.wavelength)) header.setdefault('dLoL', str(instrument.dLoL)) header.setdefault('d_s1', str(instrument.d_s1)) header.setdefault('d_s2', str(instrument.d_s2)) if 'columns' in header: header['columns'] = header['columns'].split() for key in ('wavelength', 'dLoL', 'd_s1', 'd_s2'): if key in header: header[key] = float(header[key]) return header, data
def parse_sns_file(filename): """ Parse SNS reduced data, returning *header* and *data*. *header* dictionary of fields such as 'data', 'title', 'instrument' *data* 2D array of data """ raw_header, data = parse_file(filename) header = {} # guess instrument from file name original_file = raw_header.get("F", "unknown") if "REF_L" in original_file: instrument = "Liquids" elif "REF_M" in original_file: instrument = "Magnetic" else: instrument = "unknown" header["instrument"] = instrument header["filename"] = original_file header["radiation"] = "neutron" # Plug in default instrument values for slits if "instrument" in header and header["instrument"] in INSTRUMENTS: instrument = INSTRUMENTS[header["instrument"]] header["d_s1"] = instrument.d_s1 header["d_s2"] = instrument.d_s2 # Date-time field for the file header["date"] = raw_header.get("D", "") # Column names and units columnpat = re.compile(r"(?P<name>\w+)[(](?P<units>[^)]*)[)]") columns, units = zip(*columnpat.findall(raw_header.get("L", ""))) header["columns"] = columns header["units"] = units # extra information like title, angle, etc. commentpat = re.compile(r"(?P<name>.*)\s*:\s*(?P<value>.*)\s*\n") comments = dict(commentpat.findall(raw_header.get("C", ""))) header["title"] = comments.get("Title", "") header["description"] = comments.get("Notes", "") # parse values of the form "Long Name: (value, 'units')" in comments valuepat = re.compile(r"[(]\s*(?P<value>.*)\s*,\s*'(?P<units>.*)'\s*[)]") def parse_value(valstr): d = valuepat.match(valstr).groupdict() return float(d["value"]), d["units"] if "Detector Angle" in comments: header["angle"], _ = parse_value(comments["Detector Angle"]) return header, data
def parse_sns_file(filename): """ Parse SNS reduced data, returning *header* and *data*. *header* dictionary of fields such as 'data', 'title', 'instrument' *data* 2D array of data """ raw_header, data = parse_file(filename) header = {} # guess instrument from file name original_file = raw_header.get('F', 'unknown') if 'REF_L' in original_file: instrument = 'Liquids' elif 'REF_M' in original_file: instrument = 'Magnetic' else: instrument = 'unknown' header['instrument'] = instrument header['filename'] = original_file header['radiation'] = 'neutron' # Plug in default instrument values for slits if 'instrument' in header and header['instrument'] in INSTRUMENTS: instrument = INSTRUMENTS[header['instrument']] header['d_s1'] = instrument.d_s1 header['d_s2'] = instrument.d_s2 # Date-time field for the file header['date'] = raw_header.get('D', '') # Column names and units columnpat = re.compile(r'(?P<name>\w+)[(](?P<units>[^)]*)[)]') columns, units = zip(*columnpat.findall(raw_header.get('L', ''))) header['columns'] = columns header['units'] = units # extra information like title, angle, etc. commentpat = re.compile(r'(?P<name>.*)\s*:\s*(?P<value>.*)\s*\n') comments = dict(commentpat.findall(raw_header.get('C', ''))) header['title'] = comments.get('Title', '') header['description'] = comments.get('Notes', '') # parse values of the form "Long Name: (value, 'units')" in comments valuepat = re.compile(r"[(]\s*(?P<value>.*)\s*, \s*'(?P<units>.*)'\s*[)]") def parse_value(valstr): d = valuepat.match(valstr).groupdict() return float(d['value']), d['units'] if 'Detector Angle' in comments: header['angle'], _ = parse_value(comments['Detector Angle']) return header, data
def parse_sns_file(filename): """ Parse SNS reduced data, returning *header* and *data*. *header* dictionary of fields such as 'data', 'title', 'instrument' *data* 2D array of data """ raw_header, data = parse_file(filename) header = {} # guess instrument from file name original_file = raw_header.get('F','unknown') if 'REF_L' in original_file: instrument = 'Liquids' elif 'REF_M' in original_file: instrument = 'Magnetic' else: instrument = 'unknown' header['instrument'] = instrument header['filename'] = original_file header['radiation'] = 'neutron' # Plug in default instrument values for slits if 'instrument' in header and header['instrument'] in INSTRUMENTS: instrument = INSTRUMENTS[header['instrument']] header['d_s1'] = instrument.d_s1 header['d_s2'] = instrument.d_s2 # Date-time field for the file header['date'] = raw_header.get('D','') # Column names and units columnpat = re.compile(r'(?P<name>\w+)[(](?P<units>[^)]*)[)]') columns,units = zip(*columnpat.findall(raw_header.get('L',''))) header['columns'] = columns header['units'] = units # extra information like title, angle, etc. commentpat = re.compile(r'(?P<name>.*)\s*:\s*(?P<value>.*)\s*\n') comments = dict(commentpat.findall(raw_header.get('C',''))) header['title'] = comments.get('Title','') header['description'] = comments.get('Notes','') # parse values of the form "Long Name: (value, 'units')" in comments valuepat = re.compile(r"[(]\s*(?P<value>.*)\s*,\s*'(?P<units>.*)'\s*[)]") def parse_value(valstr): d = valuepat.match(valstr).groupdict() return float(d['value']),d['units'] if 'Detector Angle' in comments: header['angle'],_ = parse_value(comments['Detector Angle']) return header, data