Commit ccf79815aaf5faf8c0139e271c426218c4e6c90a

Authored by mer0m
Committed by GitHub
1 parent ba03d6f146
Exists in master

Add files via upload

Showing 1 changed file with 210 additions and 0 deletions Side-by-side Diff

  1 +#!/usr/bin/env python
  2 +
  3 +# -*- coding: utf-8 -*-
  4 +
  5 +import argparse, time, os, instruments, inspect
  6 +
  7 +#==============================================================================
  8 +
  9 +# Path
  10 +PATH = os.getcwd()
  11 +# Sampling time acquisition
  12 +SAMPLING_TIME = 1
  13 +# File duration
  14 +FILE_DURATION = 3600*24
  15 +# Default instrument
  16 +INSTRUMENT = None
  17 +# Default instrument adress
  18 +ADRESS = None
  19 +#Default val type
  20 +VAL_TYPE = None
  21 +
  22 +#==============================================================================
  23 +
  24 +def parse():
  25 + """
  26 + Specific parsing procedure for transfering data from any abstract instrument.
  27 + :returns: populated namespace (parser)
  28 + """
  29 +
  30 + parser = argparse.ArgumentParser(description = 'Acquire data from INSTRUMENT',
  31 + epilog = 'Example: \'./datalogger.py -i myInstrument -st 10\' logs myInstrument every 10 seconds to output file YYYYMMDD-HHMMSS-INSTRUMENT.dat')
  32 +
  33 + parser.add_argument('-l',
  34 + action='store_true',
  35 + dest='list',
  36 + default=False,
  37 + help='List all available instruments')
  38 +
  39 + parser.add_argument('-I',
  40 + action='store',
  41 + dest='instLog',
  42 + default=INSTRUMENT,
  43 + help='Instrument to log (default '+str(INSTRUMENT)+')')
  44 +
  45 + parser.add_argument('-ip',
  46 + action='store',
  47 + dest='adress',
  48 + default=ADRESS,
  49 + help='Adress of instrument (IP, USB...) (default '+str(ADRESS)+')')
  50 +
  51 + parser.add_argument('-v',
  52 + action='store',
  53 + dest='vtype',
  54 + default=VAL_TYPE,
  55 + help='Value type to measure (default '+str(VAL_TYPE)+')')
  56 +
  57 + parser.add_argument('-st',
  58 + action='store',
  59 + dest='samplingtime',
  60 + default=SAMPLING_TIME,
  61 + help='Sampling time acquistion (default '+str(SAMPLING_TIME)+' second)')
  62 +
  63 + parser.add_argument('-fd',
  64 + action='store',
  65 + dest='fileduration',
  66 + default=FILE_DURATION,
  67 + help='File duration (infinite : \'-fd -1\') (default '+str(FILE_DURATION)+' second)')
  68 +
  69 + parser.add_argument('-p',
  70 + action='store',
  71 + dest='path',
  72 + default=PATH,
  73 + help='Absolute path (default '+PATH+')')
  74 +
  75 + args = parser.parse_args()
  76 + return args
  77 +
  78 +#==============================================================================
  79 +
  80 +def acq_routine(instrument, path, samplingtime, fileduration):
  81 + instrument.connect()
  82 + t0 = time.time()
  83 + filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(t0)) + '-' + instrument.model() + '.dat'
  84 + print('Opening %s' %filename)
  85 + try:
  86 + year = time.strftime("%Y", time.gmtime(t0))
  87 + month = time.strftime("%Y-%m", time.gmtime(t0))
  88 + os.chdir(path + '/' + year + '/' + month)
  89 + except:
  90 + try:
  91 + os.chdir(path + '/' + year)
  92 + os.mkdir(month)
  93 + os.chdir(path + '/' + year + '/' + month)
  94 + except:
  95 + os.chdir(path)
  96 + os.mkdir(year)
  97 + os.chdir(path + '/' + year)
  98 + os.mkdir(month)
  99 + os.chdir(path + '/' + year + '/' + month)
  100 +
  101 + data_file = open(filename, 'wr', 0)
  102 +
  103 + # Infinite loop
  104 + while True:
  105 + # tic
  106 + tic = time.time()
  107 +
  108 + if time.time() - t0 >= fileduration:
  109 + t0 = time.time()
  110 + print('Closing %s\n' %filename)
  111 + data_file.close()
  112 +
  113 + try:
  114 + year = time.strftime("%Y", time.gmtime(t0))
  115 + month = time.strftime("%Y-%m", time.gmtime(t0))
  116 + os.chdir(path + '/' + year + '/' + month)
  117 + except:
  118 + try:
  119 + os.chdir(path + '/' + year)
  120 + os.mkdir(month)
  121 + os.chdir(path + '/' + year + '/' + month)
  122 + except:
  123 + os.chdir(path)
  124 + os.mkdir(year)
  125 + os.chdir(path + '/' + year)
  126 + os.mkdir(month)
  127 + os.chdir(path + '/' + year + '/' + month)
  128 +
  129 + filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(t0)) + '-' + instrument.model() + '.dat'
  130 + print('Opening %s\n' %filename)
  131 + data_file = open(filename, 'wr', 0)
  132 +
  133 + try:
  134 + try:
  135 + #epoch time
  136 + epoch = time.time()
  137 + #MJD time
  138 + mjd = epoch / 86400.0 + 40587
  139 + # Meas values
  140 + meas = instrument.getValue()
  141 + meas = meas.replace(",", "\t")
  142 + meas = meas.replace(";", "\t")
  143 + meas = meas.replace("+", "")
  144 +
  145 + string = "%f\t%f\t%s" % (epoch, mjd, meas)
  146 + data_file.write(string) # Write in a file
  147 + print(string)
  148 +
  149 + # Sleep until sampletime
  150 + time.sleep(samplingtime - (time.time() - tic))
  151 +
  152 + except Exception as ex:
  153 + print 'Exception during controler data reading: ' + str(ex)
  154 +
  155 + except KeyboardInterrupt:
  156 + print '\n --> Disconnected'
  157 + instrument.disconnect()
  158 + data_file.close()
  159 +
  160 + # To stop the loop in a clean way
  161 + break
  162 +
  163 +#==============================================================================
  164 +
  165 +def main():
  166 + """
  167 + Main script
  168 + """
  169 + # Parse command line
  170 + args = parse()
  171 + # path
  172 + path = args.path
  173 + # Sampling time
  174 + samplingtime=float(args.samplingtime)
  175 + # File duration
  176 + fileduration=int(args.fileduration)
  177 + # Instrument to log
  178 + instLog = args.instLog
  179 + # instrument adress
  180 + adress = args.adress
  181 + # val type
  182 + vtype = args.vtype
  183 +
  184 + try:
  185 + if args.list:
  186 + print('\nInstruments:')
  187 + for name, obj in inspect.getmembers(instruments):
  188 + if inspect.ismodule(obj) and name.startswith('__') == False and name.startswith('abstract') == False:
  189 + print('\n' + name)
  190 + exec('print("\t" + instruments.%s.ALL_VAL_TYPE)'%name)
  191 +
  192 + else:
  193 + if instLog == None:
  194 + raise Exception('No instrument selected !')
  195 +
  196 + if adress == None and vtype == None:
  197 + exec('myInstrument = instruments.%s.%s()'%(instLog, instLog))
  198 + elif adress == None and vtype != None:
  199 + exec('myInstrument = instruments.%s.%s(vtype="%s")'%(instLog, instLog, vtype))
  200 + elif adress != None and vtype != None:
  201 + exec('myInstrument = instruments.%s.%s(adress="%s", vtype="%s")'%(instLog, instLog, adress, vtype))
  202 + acq_routine(myInstrument, path, samplingtime, fileduration)
  203 +
  204 + except Exception as ex:
  205 + print 'Oops: '+str(ex)
  206 +
  207 +#==============================================================================
  208 +
  209 +if __name__ == "__main__":
  210 + main()