-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rama Vasudevan
committed
Jun 21, 2024
1 parent
b17a435
commit 23fb930
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Neutron reflectivity captured at SNS comes in this format. | ||
Created on Fri Jun 21 2024 | ||
@author: Rama Vasudevan | ||
""" | ||
|
||
import numpy as np | ||
import re | ||
from sidpy import Dataset, Dimension, Reader | ||
|
||
class NeutronReflectivity(Reader): | ||
|
||
def read(self): | ||
""" | ||
Reads a single neutron reflectivity curve, acquired at SNS/ORNL. | ||
Returns | ||
------- | ||
data_set: sidpy.Dataset object | ||
wraps all the raw data and metadata from the input file into a Dataset object | ||
""" | ||
self.file_path = self._input_file_path | ||
metadata, headings, data = self._read_data() | ||
|
||
# create the sidpy dataset | ||
data_set = Dataset.from_array(data[:,1], name='Neutron Reflectivity') | ||
|
||
data_set.data_type = 'spectrum' | ||
data_set.units = headings[1][1] | ||
data_set.quantity = headings[1][0] | ||
|
||
# set dimensions | ||
data_set.set_dimension(0, Dimension(data[:,0], name=headings[0][0], | ||
units = headings[0][1], | ||
quantity=headings[0][0], | ||
dimension_type='spectral')) | ||
|
||
metadata['column_headings'] = headings | ||
metadata['raw_data'] = data | ||
|
||
data_set.metadata = metadata | ||
|
||
|
||
def _read_data(self): | ||
|
||
with open(self.file_path, 'r') as f: | ||
header = [] | ||
for ind,line in enumerate(f): | ||
if '#' in line: | ||
header.append(line[2:]) | ||
|
||
column_headings = header[-1] | ||
columns = re.split(r'\s{2,}', column_headings.strip()) | ||
|
||
columns_with_units = [] | ||
|
||
for column in columns: | ||
match = re.match(r'([^\[]+)(\s*\[.*\])?', column) | ||
name = match.group(1).strip() | ||
unit = match.group(2).strip() if match.group(2) else None | ||
columns_with_units.append((name, unit)) | ||
|
||
data = np.loadtxt(self.file_path) | ||
metadata = header | ||
|
||
return metadata, columns_with_units, data | ||
|
||
|