Commit 2bd2d0fee9f05ed3fa5732849bcc73fdd7faa934
1 parent
fc102800db
Exists in
master
change keypressed behaviour
Showing 1 changed file with 11 additions and 15 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=='': | 107 | 107 | if keypressed.isdigit(): | |
| # Power values | 108 | |||
| sensors_values = instrument.value() | 109 | |||
| sensors_values = sensors_values.replace('E', 'e') | 110 | |||
| string = "%f\t%s" % (dx , sensors_values) | 111 | |||
| data_file.write(string) # Write in a file | 112 | |||
| print(string) | 113 | |||
| 114 | ||||
| xmes.append(dx) | 115 | |||
| Pmes.append(float(sensors_values)) | 116 | |||
| 117 | ||||
| dx = dx + 0.05 | 118 | |||
| 119 | ||||
| elif keypressed.isdigit(): | 120 | |||
| print('Closing %s' %filename) | 121 | 108 | print('Closing %s' %filename) | |
| data_file.close() | 122 | 109 | data_file.close() | |
| t0 = time.time() | 123 | 110 | t0 = time.time() | |
| filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(t0)) + '-' + keypressed + '.dat' | 124 | 111 | filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(t0)) + '-' + keypressed + '.dat' | |
| print('Opening %s' %filename) | 125 | 112 | print('Opening %s' %filename) | |
| data_file = open(filename, 'wr', 0) | 126 | 113 | data_file = open(filename, 'wr', 0) | |
| dx = 0. | 127 | 114 | dx = 0. | |
| 128 | 115 | |||
| xmes = [] | 129 | 116 | xmes = [] | |
| Pmes = [] | 130 | 117 | Pmes = [] | |
| line1, = ax1.plot(xmes, Pmes, 'o') | 131 | 118 | line1, = ax1.plot(xmes, Pmes, 'o') | |
| 132 | 119 | |||
| else: | 133 | 120 | else: | |
| raise KeyboardInterrupt | 134 | 121 | # Power values | |
| 122 | sensors_values = instrument.value() | |||
| 123 | sensors_values = sensors_values.replace('E', 'e') | |||
| 124 | string = "%f\t%s" % (dx , sensors_values) | |||
| 125 | data_file.write(string) # Write in a file | |||
| 126 | print(string) | |||
| 135 | 127 | |||
| 128 | xmes.append(dx) | |||
| 129 | Pmes.append(float(sensors_values)) | |||
| 130 | ||||
| 131 | dx = dx + 0.05 | |||
| 136 | 132 | |||
| line1.set_data(xmes, Pmes) | 137 | 133 | line1.set_data(xmes, Pmes) |