#!/usr/bin/env python # vim: ft=python ts=8 sts=4 sw=4 expandtab autoindent smartindent """ Find the trigger time for Differenctial Scanning Calorimetry data in nx.hdf files. """ import sys import os import h5py import argparse import time from collections import defaultdict class SkipFile(BaseException): """This exception should be raised to skip processing a file""" pass PARSER = argparse.ArgumentParser( description = """Report the time offset for the start of the DSC profile relative to the start time of the histogram data. This program can process multiple hdf files by specifying the path to the first file and the number of files to process. You can also speficy a list of 'file_path numfile' pairs.""", usage='dsc file_path numfile {file_path numfile}' ) PARSER.add_argument('files', nargs='+', help = 'List of "file_path numfile" pairs') ARGS = PARSER.parse_args() FAILS = defaultdict(list) for startfile, num in zip(ARGS.files[0::2], ARGS.files[1::2]): numfiles = int(num) hfdir = os.path.dirname(startfile) hfbase = os.path.basename(startfile) idx = hfbase.find('.') startFID = hfbase[:idx] ext = hfbase[idx:] idnum = int(startFID[3:]) inst_abname = hfbase[:3] hfval = {} print for i in range(numfiles): try: currid = idnum + i fileID = inst_abname + '%07d' % currid if (hfdir == ''): nxfile = fileID + ext else: nxfile = hfdir + '/' + fileID + ext try: hf = h5py.File(nxfile, 'r') except: FAILS['badfile'].append(nxfile) continue if (hfbase.startswith('QKK')): rootpath = fileID + '/' else: rootpath = 'entry1/' for dpath in ['time_stamp', 'instrument/detector/start_time', 'sample/dsc_val']: dscpath = rootpath + dpath if dscpath in hf: hfval[dpath] = hf[dscpath][:] else: FAILS['badpath:{0}'.format(dpath)].append(nxfile) hf.close() raise SkipFile print fileID datiter = {} dat_hasnext = 0 time_tuple = time.strptime(hf[rootpath + 'start_time'][0], '%Y-%m-%d %H:%M:%S') hfval['time_stamp'] += time.mktime(time_tuple) hfval['time_stamp'] -= hfval['instrument/detector/start_time'] dsc0 = hfval['sample/dsc_val'][0] no_transition = True for i in range(1, len(hfval['sample/dsc_val'][1:])): dsc1 = hfval['sample/dsc_val'][i] if abs(dsc1 - dsc0) > 1.9: no_transition = False msg = 'dsc_val transition from {dsc0} to {dsc1} volts at {time} seconds from detector start time, array index = {index}' print msg.format(dsc0=dsc0, dsc1=dsc1, time=hfval['time_stamp'][i], index=i) dsc0 = dsc1 if no_transition: print 'dsc_val no transition' print hf.close() except SkipFile: continue for k in FAILS.keys(): if (len(FAILS[k])): print >> sys.stderr, 'Skipped following files. Reason = ', k print >> sys.stderr, FAILS[k]