-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata_io.py
137 lines (84 loc) · 3.22 KB
/
data_io.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import numpy as np
import os
import datetime
"""
Read and Write data from various XRD data formats
"""
def read_ras_file(filename):
# Reads a data file from the .ras format
input_file = open(filename, 'r')
data_lines = input_file.readlines()
x_data = []
y_data = []
recording = False
for line in data_lines:
if "RAS_INT_END" in line:
recording = False
# RAS Data format is a 3 column space separated data file with 2theta in first column
# and intensity in second column
if recording:
data_elements = line.split()
x_data.append(float(data_elements[0]))
y_data.append(float(data_elements[1]))
if "RAS_INT_START" in line:
recording = True
x_data = np.array(x_data)
y_data = np.array(y_data)
return x_data, y_data
def read_mdi_file(filename):
input_file = open(filename, 'r')
data_lines = list(input_file.readlines())
metadata_line = data_lines[1].split()
start_angle = float(metadata_line[0])
end_angle = float(metadata_line[5])
data_points = int(metadata_line[6])
# Generate the 2-theta values
x_data = np.linspace(start_angle, end_angle, data_points)
y_data = []
# Load the intensity values
for line in data_lines[2:]:
for element in line.split():
y_data.append(float(element))
# Cast to numpy array
y_data = np.array(y_data)
return x_data, y_data
def write_mdi_file(filename, x_data, y_data, wavelength = 1.54056):
output_file = open(filename, 'w')
today = datetime.datetime.now()
date_string = today.strftime("%m-%d-%y @%H:%M")
header_string = date_string + " DIF CARBONXS EXPORT"
starting_2theta = x_data[0]
delta_2theta = x_data[1]-x_data[0]
scanrate = 1.0
# Dummy settings - Export a dummy diffraction
anode = "CU"
wavelength_str = "%6.5f"%wavelength
ending_2theta = x_data[-1]
total_data_points = len(x_data)
output_file.write(header_string+"\n")
output_file.write("%3.1f %3.1f %3.1f %s %s %4.1f %d \n"%(starting_2theta,
delta_2theta,
scanrate,
anode,
wavelength_str,
ending_2theta,
total_data_points
))
counter = 0
data_line = ""
# MDI Data format is eight columns of sequential data
for data_point in y_data:
data_line += "%8.1f "%data_point
counter += 1
if counter == 8:
output_file.write(data_line + '\n')
counter = 0
data_line = ""
# Write last output
output_file.write(data_line + '\n')
if __name__ == '__main__':
# generate synthetic data
x_data = np.linspace(20, 120, 2000)
y_data = 100*np.sin(x_data)+100
write_mdi_file(os.path.join('export_test', 'test_export.mdi'), x_data, y_data)
read_mdi_file(os.path.join('export_test', 'test_export.mdi'))