-
Notifications
You must be signed in to change notification settings - Fork 0
/
STMData.py
99 lines (89 loc) · 3.25 KB
/
STMData.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
# -*- coding: utf-8 -*-
import numpy as np
class STMData:
UNITS = {'Z': 'm', 'I': 'A', 'V': 'V'}
UNITS_PREFIXES = {'': 1, 'm': 10 ** 3, 'μ': 10 ** 6, 'n': 10 ** 9, 'Å': 10 ** 10, 'p': 10 ** 12}
def __init__(self):
self.name = None
self.filename = None
self.parameters = {}
self.comment = {}
self.filetype = None
self.unit = None
self.xunit = None
def __repr__(self):
if self.filetype == 'Z':
return 'Topography image'
else:
return 'No data'
def auto_set_unit_xyz(self, array, unit='m'):
min = np.amin(array)
max = np.amax(array)
range = max - min
if range < 10 ** -9:
unit = 'p' + unit
return array * self.UNITS_PREFIXES['p'], unit
if 10 ** -9 <= range < 10 ** -8:
unit = 'Å'
return array * self.UNITS_PREFIXES['Å'], unit
if 10 ** -8 <= range < 10 ** -6:
unit = 'n' + unit
return array * self.UNITS_PREFIXES['n'], unit
if 10 ** -6 <= range < 10 ** -3:
unit = 'μ' + unit
return array * self.UNITS_PREFIXES['μ'], unit
if 10 ** -3 <= range < 10 ** -1:
unit = 'm' + unit
return array * self.UNITS_PREFIXES['m'], unit
return array, unit
def auto_set_unit(self, array, unit):
min = np.amin(array)
max = np.amax(array)
range = max - min
if unit == 'm':
array, unit = self.auto_set_unit_xyz(array, unit)
return array, unit
if range < 10 ** -9:
unit = 'p' + unit
return array * self.UNITS_PREFIXES['p'], unit
if 10 ** -9 <= range < 10 ** -6:
unit = 'n' + unit
return array * self.UNITS_PREFIXES['n'], unit
if 10 ** -6 <= range < 10 ** -3:
unit = 'μ' + unit
return array * self.UNITS_PREFIXES['μ'], unit
if 10 ** -3 <= range < 10 ** -1:
unit = 'm' + unit
return array * self.UNITS_PREFIXES['m'], unit
return array, unit
def update_unit(self, array, unit):
if unit[-1] == 'm' or unit[0] == 'Å':
if unit != 'm':
array = array*(1/self.UNITS_PREFIXES[unit[0]])
unit = 'm'
array, unit = self.auto_set_unit(array, unit)
return array, unit
else:
if len(unit) == 2:
array = array / self.UNITS_PREFIXES[unit[0]]
unit = unit[-1]
array, unit = self.auto_set_unit(array, unit)
return array, unit
def unit_to_si(self, array, unit):
if unit[-1] == 'm' or unit[0] == 'Å':
if unit != 'm':
array = array*(1/self.UNITS_PREFIXES[unit[0]])
unit = 'm'
return array, unit
else:
if len(unit) == 2:
array = array / self.UNITS_PREFIXES[unit[0]]
unit = unit[-1]
return array, unit
return array, unit
def set_unit(self, array, old_unit, new_unit):
array, _ = self.unit_to_si(array, old_unit)
if new_unit in self.UNITS:
return array
array = array*self.UNITS_PREFIXES[new_unit[0]]
return array