Blame view
datalogger.py
6.97 KB
ccf79815a Add files via upload |
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
#!/usr/bin/env python # -*- coding: utf-8 -*- import argparse, time, os, instruments, inspect #============================================================================== # Path PATH = os.getcwd() # Sampling time acquisition SAMPLING_TIME = 1 # File duration FILE_DURATION = 3600*24 # Default instrument INSTRUMENT = None # Default instrument adress ADRESS = None #Default val type VAL_TYPE = None #============================================================================== def parse(): """ Specific parsing procedure for transfering data from any abstract instrument. :returns: populated namespace (parser) """ parser = argparse.ArgumentParser(description = 'Acquire data from INSTRUMENT', epilog = 'Example: \'./datalogger.py -i myInstrument -st 10\' logs myInstrument every 10 seconds to output file YYYYMMDD-HHMMSS-INSTRUMENT.dat') parser.add_argument('-l', action='store_true', dest='list', default=False, help='List all available instruments') parser.add_argument('-I', action='store', dest='instLog', default=INSTRUMENT, help='Instrument to log (default '+str(INSTRUMENT)+')') parser.add_argument('-ip', action='store', dest='adress', default=ADRESS, help='Adress of instrument (IP, USB...) (default '+str(ADRESS)+')') parser.add_argument('-v', action='store', dest='vtype', default=VAL_TYPE, help='Value type to measure (default '+str(VAL_TYPE)+')') parser.add_argument('-st', action='store', dest='samplingtime', default=SAMPLING_TIME, help='Sampling time acquistion (default '+str(SAMPLING_TIME)+' second)') parser.add_argument('-fd', action='store', dest='fileduration', default=FILE_DURATION, help='File duration (infinite : \'-fd -1\') (default '+str(FILE_DURATION)+' second)') parser.add_argument('-p', action='store', dest='path', default=PATH, help='Absolute path (default '+PATH+')') args = parser.parse_args() return args #============================================================================== def acq_routine(instrument, path, samplingtime, fileduration): instrument.connect() t0 = time.time() filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(t0)) + '-' + instrument.model() + '.dat' print('Opening %s' %filename) try: year = time.strftime("%Y", time.gmtime(t0)) month = time.strftime("%Y-%m", time.gmtime(t0)) os.chdir(path + '/' + year + '/' + month) except: try: os.chdir(path + '/' + year) os.mkdir(month) os.chdir(path + '/' + year + '/' + month) except: os.chdir(path) os.mkdir(year) os.chdir(path + '/' + year) os.mkdir(month) os.chdir(path + '/' + year + '/' + month) data_file = open(filename, 'wr', 0) # Infinite loop while True: # tic tic = time.time() if time.time() - t0 >= fileduration: t0 = time.time() print('Closing %s ' %filename) data_file.close() try: year = time.strftime("%Y", time.gmtime(t0)) month = time.strftime("%Y-%m", time.gmtime(t0)) os.chdir(path + '/' + year + '/' + month) except: try: os.chdir(path + '/' + year) os.mkdir(month) os.chdir(path + '/' + year + '/' + month) except: os.chdir(path) os.mkdir(year) os.chdir(path + '/' + year) os.mkdir(month) os.chdir(path + '/' + year + '/' + month) filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(t0)) + '-' + instrument.model() + '.dat' print('Opening %s ' %filename) data_file = open(filename, 'wr', 0) try: try: #epoch time epoch = time.time() #MJD time mjd = epoch / 86400.0 + 40587 # Meas values meas = instrument.getValue() meas = meas.replace(",", "\t") meas = meas.replace(";", "\t") meas = meas.replace("+", "") string = "%f\t%f\t%s" % (epoch, mjd, meas) data_file.write(string) # Write in a file print(string) # Sleep until sampletime time.sleep(samplingtime - (time.time() - tic)) except Exception as ex: print 'Exception during controler data reading: ' + str(ex) except KeyboardInterrupt: print ' --> Disconnected' instrument.disconnect() data_file.close() # To stop the loop in a clean way break #============================================================================== def main(): """ Main script """ # Parse command line args = parse() # path path = args.path # Sampling time samplingtime=float(args.samplingtime) # File duration fileduration=int(args.fileduration) # Instrument to log instLog = args.instLog # instrument adress adress = args.adress # val type vtype = args.vtype try: if args.list: print(' Instruments:') for name, obj in inspect.getmembers(instruments): if inspect.ismodule(obj) and name.startswith('__') == False and name.startswith('abstract') == False: print(' ' + name) exec('print("\t" + instruments.%s.ALL_VAL_TYPE)'%name) else: if instLog == None: raise Exception('No instrument selected !') if adress == None and vtype == None: exec('myInstrument = instruments.%s.%s()'%(instLog, instLog)) elif adress == None and vtype != None: exec('myInstrument = instruments.%s.%s(vtype="%s")'%(instLog, instLog, vtype)) elif adress != None and vtype != None: exec('myInstrument = instruments.%s.%s(adress="%s", vtype="%s")'%(instLog, instLog, adress, vtype)) acq_routine(myInstrument, path, samplingtime, fileduration) except Exception as ex: print 'Oops: '+str(ex) #============================================================================== if __name__ == "__main__": main() |