def main(): usage = """usage: %prog [options] programname Analyze pulse program file <programname> to make suggestions for automated set-up""" parser = OptionParser(usage) parser.add_option( "-a", "--add_files", action="store_true", dest="addfiles", default=False, help="look for <programname> in add_files path [default: %default]") parser.add_option("-v", "--verboselevel", type="int", default=0, dest="verboselevel", help="set verbose level [default: %default]") (options, args) = parser.parse_args() ut.cd_curd() # print options if len(args) == 0: parser.error("incorrect number of arguments") return ppname = args[0] pp.pp_file = os.path.abspath(ppname) pp.verbose_level = options.verboselevel if options.addfiles: (pp.pp_file, adir) = ut.find_file_dir(ppname, 'PP', addfiles=True) if not pp.pp_file: raise Exception('%s not found in add_files path %s' % (ppname, pp.addfiles_path)) ut.putcomment('Analyzing pulse program file: ' + pp.pp_file, 1, ornament=False) if pp.verbose_level > 1: ut.putcomment('pp.ppGlobals', 1) ut.show_vars(pp) parfilename = 'acqus' analyzepp(pp.pp_file, parfilename)
def printJcamp(j_dc, u_dc): ut.putcomment('Jcamp variables', 0) for key in sorted(j_dc): print('%s:' % key), j_dc[key] ut.putcomment('User variables', 0) arrays = u_dc['$ARRAY'] for key in sorted(u_dc): if key == '$ARRAY': continue if key in arrays.keys(): print('%s (%d:%d):' % (key, arrays[key][0], arrays[key][1])), for (ii, val) in zip(range(arrays[key][0], arrays[key][1] + 1), u_dc[key]): print('%s(%d)' % (val, ii)), print else: print('%s:' % key), u_dc[key]
def main(): setopts, args = getopt.gnu_getopt(sys.argv[1:], 'ahv:') ut.cd_curd() if len(args) == 0: printhelp(sys.argv[0]) return pp.verbose_level = 0 printarraylistflag = False for opt in setopts: if opt[0] == '-h': printhelp(sys.argv[0]) return elif opt[0] == '-a': printarraylistflag = True elif opt[0] == '-v': pp.verbose_level = int(opt[1]) parname = args[0] ut.putcomment('Reading Jcamp file: ' + parname, 1, ornament=False) if pp.verbose_level > 1: ut.putcomment('pp.ppGlobals', 1) ut.show_vars(pp) (j_dc, u_dc) = parseJcamptxt(ut.read_file(parname)) if printarraylistflag: printarraylist(u_dc) return if len(args) == 1: printJcamp(j_dc, u_dc) return if len(args) > 1: parlist = reduce(lambda x, y: x + ' ' + y, args[1:]).split(',') for par in parlist: printpar(par, u_dc) return
def analyzepp(progfilename, parfilename): """Analyze pulse program <progfilename> using acqu file <parfilename>. Outputs the acquisition parameters and their value. """ (pythontext, nonpythontext) = ut.split_python_text(ut.read_file(progfilename)) nonpythontext = ut.strip_comment(nonpythontext, ';') (j_dc, u_dc) = sj.parseJcamptxt(ut.read_file(parfilename)) arrays = u_dc['$ARRAY'] usedpars = [] usedpararrays = {} words = sorted(set(re.split('\W+', nonpythontext))) ut.putcomment('parsing <%s>' % os.path.basename(progfilename), 1) for w in words: m = pp.search_commands_re.match(w) if m: if m.group('comarray'): c = m.group('comarray').upper() ind = int(m.group('ind')) p = pp.coms2pars_dict[c][0] usedpars += [p] ut.putcomment('%s: %s %d' % (w, pp.coms2pars_dict[c], ind), 1, ornament=False) if not p in usedpararrays.keys(): usedpararrays[p] = [ind] else: usedpararrays[p] += [ind] elif m.group('com'): c = m.group('com').upper() p = pp.coms2pars_dict[c][0] ut.putcomment('%s: %s' % (w, pp.coms2pars_dict[c]), 1, ornament=False) usedpars += [p] ut.putcomment('end parsing', 1) usedpars = sorted(set(usedpars)) for par in usedpars: conpars = pp.coms2pars_dict[pp.pars2coms_dict[par][0]] if par in pp.par_array_names: for ind in sorted(usedpararrays[par]): for p in conpars: print('%s %d: %s' % (p, ind, u_dc[p][ind])) else: for p in conpars: print('%s: %s' % (p, u_dc[p]))
def main(): setopts, args = getopt.gnu_getopt(sys.argv[1:], 'ahr:v:') if len(args) == 0: printhelp(sys.argv[0]) return ppname = args[0] expname = ppname.split('.')[0] pp.addfiles_path = pp.addfiles_path.replace('water_nh', expname) pp.pp_file = os.path.join(pp.addfiles_path, 'pp/user/%s' % ppname) pplogname = ppname + '.log' pp.pp_log = os.path.join(pp.addfiles_path, 'pp/user/%s' % pplogname) pp.verbose_level = 0 pp.run_flag = 'DRY' for opt in setopts: if opt[0] == '-h': printhelp(sys.argv[0]) return elif opt[0] == '-a': (pp.pp_file, adir) = ut.find_file_dir(ppname, 'PP', addfiles=True) if not pp.pp_file: raise Exception('%s not found in add_files path %s' % (ppname, pp.addfiles_path)) elif opt[0] == '-r': pp.run_flag = opt[1] elif opt[0] == '-v': pp.verbose_level = int(opt[1]) ut.putcomment('Installing pulse program file: ' + pp.pp_file, 1, ornament=False) ut.putcomment('Logfile: %s' % os.path.abspath(pp.pp_log), 1, ornament=False) ut.putcomment('run_flag: %s' % pp.run_flag, 1, ornament=False) if pp.verbose_level > 1: ut.putcomment('pp.ppGlobals', 1) ut.show_vars(pp) (pythontext, nonpythontext) = ut.split_python_text(ut.read_file(pp.pp_file)) if not pythontext: raise Exception('%s contains no python text' % pp.pp_file) pp.pp_log_fd = open(pp.pp_log, 'w') now = datetime.datetime.now() pp.pp_log_fd.write('Date and Time: %s \n' % now) exec pythontext pp.pp_log_fd.close()
def copy_parfile(ptype, pname, addfilecreatepath): """Copy individual parameter file <pname> of type <ptype> to <addfilecretepath>. <pname> is sought according to the Bruker search path. The file is copied into a subdirectory of <addfilecretepath> according to the relative directory in the Bruker lists directory. In case the file comes from outside of the lists directory (non-standard Bruker path), the file is put into the first subdirectory entry of lists according to the standard search path. """ (filefullpath, defaultdir) = ut.find_file_dir(pname, ptype) if not filefullpath: ut.putcomment('<%s> not found in %s' % (pname, ptype), 0, ornament=False) return ut.putcomment('<%s> found in %s as <%s>' % (pname, ptype, filefullpath), 1, ornament=False) fs = filefullpath.split(pp.lists_path + os.sep + 'lists' + os.sep) if len(fs) == 2: d = os.path.dirname(fs[1]) else: ut.putcomment('<%s> found in %s with unnormal path <%s>' % (pname, ptype, filefullpath), 1, ornament=False) (dum, d) = pp.parfile_dirs_default_dict[ptype][0].split('lists' + os.sep) dd = os.path.join(addfilecreatepath, d) if not os.path.isdir(dd): os.makedirs(dd) shutil.copy2(filefullpath, os.path.join(dd, pname))
def main(): setopts,args = getopt.gnu_getopt(sys.argv[1:], 'ad:hv:') ut.cd_curd() pp.verbose_level = 0 addfilesdirname = 'add_files' for opt in setopts: if opt[0] == '-h': printhelp(sys.argv[0]) return elif opt[0] == '-a': cds=subprocess.Popen(["curd", "-a"], stdout=subprocess.PIPE).communicate()[0].strip() os.chdir(cds) elif opt[0] == '-d': addfilesdirname = opt[1] print addfilesdirname elif opt[0] == '-v': pp.verbose_level = 1 pp.verbose_level = int(opt[1]) if len(args) == 0: parname = 'acqus' else: parname = args[0] ut.putcomment('Reading Jcamp file: ' + parname, 1, ornament=True) if pp.verbose_level > 1: ut.putcomment('pp.ppGlobals', 1) ut.show_vars(pp) (j_dc, u_dc) = sj.parseJcamptxt(ut.read_file(parname)) make_add_files(u_dc, addfilesdirname)
def PP_PUTPAR(name, value): if pp.run_flag not in pp.run_flags: raise Exception('unknown run_flag: ' + pp.run_flag) brukername = name = name.upper() ut.putcomment('PP_PUTPAR input: %s %s' % (name, value), 2, ornament=False) m = pp.par_array_names_re.match(name) if m: ut.putcomment('detected array ' + m.group('arn') + ' ' + m.group('ind'), 3, ornament=False) an = m.group('arn') ind = m.group('ind') m1 = re.match('(PL|SP)DB', an) if m1: an = m1.group(1) + 'W' value = pow(10.0, -value / 10.) m1 = re.match('(D|IN|P|INP)', an) if m1 and (type(value) == str): m2 = pp.time_units_re.match(value) if m2: val = m2.group('val') un = m2.group('unit') value = float(val) * pp.time_units[un] if re.match('(P|INP)', an): value = value * 1e6 brukername = an + ' ' + ind pp.pp_log_fd.write('%s: %s\n' % (brukername, str(value))) ut.putcomment('PP_PUTPAR: %s %s' % (brukername, str(value)), ornament=False) if pp.run_flag == 'DRY': # print top.Cmd.putPar(brukername, str(value)) return None elif pp.run_flag in ['FORCE', 'NORMAL', 'INTERACTIVE']: return TC.PUTPAR(brukername, str(value)) return None
def write_bruker_search_path(ftype, destfile, sourcefile=None, sourcetext=None): """Will copy a file from sourcefile (out of the add_files directory) or text to destfile in first directory of Bruker search path for ftype = cpd, f1, gp, ... with checks for overwrite, identity, etc. """ if pp.run_flag not in pp.run_flags: raise Exception('unknown run_flag: ' + pp.run_flag) destfile = destfile + pp.name_tag ut.putcomment('write_bruker_search_path: start', 2) ut.putcomment('ftype: %s, destfile: %s' % (ftype, destfile), 2, ornament=False) if sourcetext and sourcefile: raise Exception('both sourcefile and sourcetext defined') if sourcetext: source = sourcetext sourcestring = 'sourcetext' ut.putcomment('input is from sourcetext', 2, ornament=False) ut.putcomment(source, 2, ornament=False) else: ut.putcomment('input is from sourcefile: ' + pp.addfiles_path + '/' + sourcefile, 2, ornament=False) sourcestring = 'sourcefile <%s>' % sourcefile f = open(os.path.join(pp.addfiles_path, sourcefile)) source = f.read() f.close() ut.putcomment(source, 3, ornament=False) (destfilefullpath, destdir) = ut.find_file_dir(destfile, ftype) if destfilefullpath: ut.putcomment('destination file exists: ' + destfilefullpath, 2, ornament=False) if not ut.cmp_text_file(source, destfilefullpath): outstring = ('PP_FILE NO_ACTION: %s equals destfile <%s>' % (sourcestring, destfilefullpath)) ut.putcomment(outstring, 1, ornament=False) pp.pp_log_fd.write('%s\n' % outstring) else: outstring = ('PP_FILE CONFLICT: %s is not equal to destfile <%s>' % (sourcestring, destfilefullpath)) ut.putcomment(outstring, 0, ornament=False) pp.pp_log_fd.write('%s\n' % outstring) if pp.run_flag == 'DRY': outstring = ('PP_FILE OVERWRITE: %s will overwrite destfile <%s>' % (sourcestring, destfilefullpath)) pp.pp_log_fd.write('%s\n' % outstring) elif pp.run_flag == 'NORMAL': raise Exception('%s\nPP_FILE NO_OVERWRITE: run_flag is %s\n' % (outstring, pp.run_flag)) elif pp.run_flag == 'FORCE': outstring = ('PP_FILE OVERWRITE: %s overwrites destfile <%s>' % (sourcestring, destfilefullpath)) ut.putcomment(outstring, 0, ornament=False) pp.pp_log_fd.write('%s\n' % outstring) ut.write_text_file(source, destfilefullpath) elif pp.run_flag == 'INTERACTIVE': raise Exception('%s\nPP_FILE NO_OVERWRITE: run_flag is %s\n' % (outstring, pp.run_flag)) else: df1 = os.path.join(destdir, destfile) outstring = ('PP_FILE CREATE: destfile <%s> from %s' % (df1, sourcestring)) ut.putcomment(outstring, 1, ornament=False) pp.pp_log_fd.write('%s\n' % outstring) if pp.run_flag == 'DRY': pass elif pp.run_flag in ['FORCE', 'NORMAL', 'INTERACTIVE']: ut.write_text_file(source, df1) else: raise Exception('unknown run_flag: ' + pp.run_flag) ut.putcomment('write_bruker_search_path: end', 2) return destfile
def printarraylist(u_dc): arrays = u_dc['$ARRAY'] ut.putcomment('User variable arrays', 0) for key in sorted(arrays): print('%s:' % key), arrays[key]