Commit c437a22764bd943cfa536769e67c04b5e713f1e8
1 parent
2bd2d0fee9
Exists in
master
replace spaces by tabs
Showing 1 changed file with 9 additions and 9 deletions Inline Diff
pm100d_waist.py
#!/usr/bin/env python | 1 | 1 | #!/usr/bin/env python | |
2 | 2 | |||
import argparse, time, os | 3 | 3 | import argparse, time, os | |
import matplotlib.pyplot as plt | 4 | 4 | import matplotlib.pyplot as plt | |
5 | 5 | |||
#============================================================================== | 6 | 6 | #============================================================================== | |
7 | 7 | |||
# Path | 8 | 8 | # Path | |
PATH = os.getcwd() | 9 | 9 | PATH = os.getcwd() | |
# File footer | 10 | 10 | # File footer | |
OFILENAME = '130' | 11 | 11 | OFILENAME = '130' | |
12 | 12 | |||
#============================================================================== | 13 | 13 | #============================================================================== | |
14 | 14 | |||
def parse(): | 15 | 15 | def parse(): | |
""" | 16 | 16 | """ | |
Specific parsing procedure for transfering data from Thorlabs PM100D. | 17 | 17 | Specific parsing procedure for transfering data from Thorlabs PM100D. | |
:returns: populated namespace (parser) | 18 | 18 | :returns: populated namespace (parser) | |
""" | 19 | 19 | """ | |
parser = argparse.ArgumentParser(description = 'Acquire data from Thorlabs PM100D', | 20 | 20 | parser = argparse.ArgumentParser(description = 'Acquire data from Thorlabs PM100D', | |
epilog = 'Example: \'./pm100d.py -st 10 -o toto\' logs PM100D every 10 seconds to output file YYYYMMDD-HHMMSS-toto.dat') | 21 | 21 | epilog = 'Example: \'./pm100d.py -st 10 -o toto\' logs PM100D every 10 seconds to output file YYYYMMDD-HHMMSS-toto.dat') | |
22 | 22 | |||
parser.add_argument('-p', | 23 | 23 | parser.add_argument('-p', | |
action='store', | 24 | 24 | action='store', | |
dest='path', | 25 | 25 | dest='path', | |
default=PATH, | 26 | 26 | default=PATH, | |
help='Absolute path (default '+PATH+')') | 27 | 27 | help='Absolute path (default '+PATH+')') | |
28 | 28 | |||
parser.add_argument('-o', | 29 | 29 | parser.add_argument('-o', | |
action='store', | 30 | 30 | action='store', | |
dest='ofile', | 31 | 31 | dest='ofile', | |
default=OFILENAME, | 32 | 32 | default=OFILENAME, | |
help='Output data filename (default '+OFILENAME+')') | 33 | 33 | help='Output data filename (default '+OFILENAME+')') | |
34 | 34 | |||
args = parser.parse_args() | 35 | 35 | args = parser.parse_args() | |
return args | 36 | 36 | return args | |
37 | 37 | |||
#============================================================================== | 38 | 38 | #============================================================================== | |
39 | 39 | |||
class usbtmc: | 40 | 40 | class usbtmc: | |
def __init__(self, device): | 41 | 41 | def __init__(self, device): | |
self.device = device | 42 | 42 | self.device = device | |
self.FILE = os.open(device, os.O_RDWR) | 43 | 43 | self.FILE = os.open(device, os.O_RDWR) | |
44 | 44 | |||
def write(self, command): | 45 | 45 | def write(self, command): | |
os.write(self.FILE, command); | 46 | 46 | os.write(self.FILE, command); | |
47 | 47 | |||
def read(self, length = 4000): | 48 | 48 | def read(self, length = 4000): | |
return os.read(self.FILE, length) | 49 | 49 | return os.read(self.FILE, length) | |
50 | 50 | |||
def getName(self): | 51 | 51 | def getName(self): | |
self.write("*IDN?") | 52 | 52 | self.write("*IDN?") | |
return self.read(300) | 53 | 53 | return self.read(300) | |
54 | 54 | |||
def sendReset(self): | 55 | 55 | def sendReset(self): | |
self.write("*RST") | 56 | 56 | self.write("*RST") | |
57 | 57 | |||
#============================================================================== | 58 | 58 | #============================================================================== | |
59 | 59 | |||
class instrument: | 60 | 60 | class instrument: | |
"""Class to control a SCPI compatible instrument""" | 61 | 61 | """Class to control a SCPI compatible instrument""" | |
def __init__(self, device): | 62 | 62 | def __init__(self, device): | |
print 'Connecting to device %s...' %device | 63 | 63 | print 'Connecting to device %s...' %device | |
self.meas = usbtmc(device) | 64 | 64 | self.meas = usbtmc(device) | |
self.name = self.meas.getName() | 65 | 65 | self.name = self.meas.getName() | |
print self.name | 66 | 66 | print self.name | |
print ' --> Ok' | 67 | 67 | print ' --> Ok' | |
68 | 68 | |||
def write(self, command): | 69 | 69 | def write(self, command): | |
"""Send an arbitrary command directly to the scope""" | 70 | 70 | """Send an arbitrary command directly to the scope""" | |
self.meas.write(command) | 71 | 71 | self.meas.write(command) | |
72 | 72 | |||
def read(self, command): | 73 | 73 | def read(self, command): | |
"""Read an arbitrary amount of data directly from the scope""" | 74 | 74 | """Read an arbitrary amount of data directly from the scope""" | |
return self.meas.read(command) | 75 | 75 | return self.meas.read(command) | |
76 | 76 | |||
def reset(self): | 77 | 77 | def reset(self): | |
"""Reset the instrument""" | 78 | 78 | """Reset the instrument""" | |
self.meas.sendReset() | 79 | 79 | self.meas.sendReset() | |
80 | 80 | |||
def value(self): | 81 | 81 | def value(self): | |
self.write('MEAS?') | 82 | 82 | self.write('MEAS?') | |
return self.read(300) | 83 | 83 | return self.read(300) | |
84 | 84 | |||
#============================================================================== | 85 | 85 | #============================================================================== | |
86 | 86 | |||
def acqu_PM100D(instrument, path, ofile): | 87 | 87 | def acqu_PM100D(instrument, path, ofile): | |
88 | 88 | |||
t0 = time.time() | 89 | 89 | t0 = time.time() | |
filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(t0)) + '-' + ofile + '.dat' | 90 | 90 | filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(t0)) + '-' + ofile + '.dat' | |
print('Opening %s' %filename) | 91 | 91 | print('Opening %s' %filename) | |
data_file = open(filename, 'wr', 0) | 92 | 92 | data_file = open(filename, 'wr', 0) | |
dx = 0. | 93 | 93 | dx = 0. | |
94 | 94 | |||
xmes = [] | 95 | 95 | xmes = [] | |
Pmes = [] | 96 | 96 | Pmes = [] | |
plt.ion() | 97 | 97 | plt.ion() | |
fig = plt.figure() | 98 | 98 | fig = plt.figure() | |
ax1 = fig.add_subplot(111) | 99 | 99 | ax1 = fig.add_subplot(111) | |
line1, = ax1.plot(xmes, Pmes, 'o') | 100 | 100 | line1, = ax1.plot(xmes, Pmes, 'o') | |
101 | 101 | |||
# Infinite loop | 102 | 102 | # Infinite loop | |
while True: | 103 | 103 | while True: | |
try: | 104 | 104 | try: | |
keypressed = raw_input("Press Enter to continue...\n") | 105 | 105 | keypressed = raw_input("Press Enter to continue...\n") | |
106 | 106 | |||
if keypressed.isdigit(): | 107 | 107 | if keypressed.isdigit(): | |
print('Closing %s' %filename) | 108 | 108 | print('Closing %s' %filename) | |
data_file.close() | 109 | 109 | data_file.close() | |
t0 = time.time() | 110 | 110 | t0 = time.time() | |
filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(t0)) + '-' + keypressed + '.dat' | 111 | 111 | filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(t0)) + '-' + keypressed + '.dat' | |
print('Opening %s' %filename) | 112 | 112 | print('Opening %s' %filename) | |
data_file = open(filename, 'wr', 0) | 113 | 113 | data_file = open(filename, 'wr', 0) | |
dx = 0. | 114 | 114 | dx = 0. | |
115 | 115 | |||
xmes = [] | 116 | 116 | xmes = [] | |
Pmes = [] | 117 | 117 | Pmes = [] | |
line1, = ax1.plot(xmes, Pmes, 'o') | 118 | 118 | line1, = ax1.plot(xmes, Pmes, 'o') | |
119 | 119 | |||
else: | 120 | 120 | else: | |
# Power values | 121 | 121 | # Power values | |
sensors_values = instrument.value() | 122 | 122 | sensors_values = instrument.value() | |
sensors_values = sensors_values.replace('E', 'e') | 123 | 123 | sensors_values = sensors_values.replace('E', 'e') | |
string = "%f\t%s" % (dx , sensors_values) | 124 | 124 | string = "%f\t%s" % (dx , sensors_values) | |
data_file.write(string) # Write in a file | 125 | 125 | data_file.write(string) # Write in a file | |
print(string) | 126 | 126 | print(string) | |
127 | 127 | |||
xmes.append(dx) | 128 | 128 | xmes.append(dx) | |
Pmes.append(float(sensors_values)) | 129 | 129 | Pmes.append(float(sensors_values)) | |
130 | 130 | |||
dx = dx + 0.05 | 131 | 131 | dx = dx + 0.05 | |
132 | 132 | |||
line1.set_data(xmes, Pmes) | 133 | 133 | line1.set_data(xmes, Pmes) | |
ax1.relim() | 134 | 134 | ax1.relim() | |
ax1.autoscale_view() | 135 | 135 | ax1.autoscale_view() | |
fig.canvas.draw() | 136 | 136 | fig.canvas.draw() | |
137 | 137 | |||
except Exception as ex: | 138 | 138 | except Exception as ex: | |
print 'Exception during controler data reading: ' + str(ex) | 139 | 139 | print 'Exception during controler data reading: ' + str(ex) | |
140 | 140 | |||
except KeyboardInterrupt: | 141 | 141 | except KeyboardInterrupt: | |
print '\n --> Disconnected' | 142 | 142 | print '\n --> Disconnected' |