-
Notifications
You must be signed in to change notification settings - Fork 0
/
spa_import.py
83 lines (71 loc) · 2.66 KB
/
spa_import.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
# -*- coding: utf-8 -*-
import numpy as np
from veusz.plugins import (
ImportPlugin,
ImportDataset1D,
ImportFieldText,
ImportPluginException,
importpluginregistry,
)
class ImportSPA(ImportPlugin):
"""A plugin for reading Thermo Scientific OMNIC SPA file."""
name = 'OMNIC SPA import'
author = 'Takuro Hosomi'
description = 'Read a spectrum from Thermo Scientific OMNIC SPA file'
file_extensions = set(['.spa', '.SPA'])
def __init__(self):
ImportPlugin.__init__(self)
self.fields = [
ImportFieldText("x_name", descr="X-axis name", default="wavenumber"),
ImportFieldText("y_name", descr="Y-axis name", default=r"{auto}")
]
def doImport(self, params):
"""Actually import data"""
try:
self.spadata = SPAData(params.filename)
x_name = params.field_results["x_name"]
y_name = params.field_results["y_name"]
if y_name == r"{auto}":
y_name = self.spadata.title
return [
ImportDataset1D(x_name, self.spadata.wavenumbers),
ImportDataset1D(y_name, self.spadata.data),
]
except IOError as e:
raise e
except Exception as e:
raise ImportPluginException(str(e))
class SPAData():
def __init__(self, filepath):
self.datanum = 1
self.title = ""
self.max_wavenum = 0
self.min_wavenum = 0
self.wavenumbers = np.zeros(1)
self.data = np.zeros(1)
self.loadfromFile(filepath)
def loadfromFile(self, filepath):
with open(filepath, 'rb') as f:
# Get spectrum title
f.seek(30)
title = np.fromfile(f, np.uint8,255)
self.title = ''.join([chr(x) for x in title if x!=0])
# Get number of datapoints in wavenumber array
f.seek(564)
self.datanum = np.fromfile(f, np.int32,1)[0]
# Get wavenumber array
f.seek(576)
self.max_wavenum = np.fromfile(f, np.single, 1)[0]
self.min_wavenum = np.fromfile(f, np.single, 1)[0]
self.wavenumbers = np.linspace(self.max_wavenum, self.min_wavenum, self.datanum)
# Search and move start address of data
f.seek(288)
flag = 0
while flag != 3:
flag = np.fromfile(f, np.uint16, 1)
data_position=np.fromfile(f,np.uint16, 1)[0]
f.seek(data_position)
# Get spectrum data
self.data = np.fromfile(f, np.single, self.datanum)
# add the class to the registry.
importpluginregistry.append(ImportSPA)