Coverage for /Users/Newville/Codes/xraylarch/larch/xrmmap/asciifiles.py: 11%
104 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-09 10:08 -0600
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-09 10:08 -0600
1"""
2utilities for reading files from raw scan folder
3"""
4import os
5import sys
6import numpy
7from configparser import ConfigParser
9def readASCII(fname, nskip=0, isnumeric=True):
10 dat, header = [], []
11 with open(fname,'r') as fh:
12 lines = fh.readlines()
13 for line in lines:
14 if line.startswith('#') or line.startswith(';'):
15 header.append(line[:-1])
16 continue
17 if nskip > 0:
18 nskip -= 1
19 header.append(line[:-1])
20 continue
21 if isnumeric:
22 dat.append([float(x) for x in line[:-1].split()])
23 else:
24 dat.append(line[:-1].split())
25 if isnumeric:
26 dat = numpy.array(dat)
27 return header, dat
29def readMasterFile(fname):
30 return readASCII(fname, nskip=0, isnumeric=False)
32def readEnvironFile(fname):
33 h, d = readASCII(fname, nskip=0, isnumeric=False)
34 return h
36def read1DXRDFile(fname,metadata=True):
37 return readASCII(fname, nskip=0, isnumeric=True)
39def parseEnviron(text):
40 """ split Environ data into desc, addr, val arrays """
41 env_desc, env_addr, env_vals = [], [], []
42 for eline in text:
43 eline = eline.replace('\t',' ').strip()
44 desc, val = [i.strip() for i in eline[1:].split('=')]
45 addr = ''
46 if '(' in desc:
47 n = desc.rfind('(')
48 addr = desc[n+1:-1]
49 if addr.endswith(')'):
50 addr = addr[:-1]
51 desc = desc[:n].rstrip()
52 env_vals.append(val)
53 env_desc.append(desc)
54 env_addr.append(addr)
55 return env_desc, env_addr, env_vals
57def readScanConfig(folder):
58 sfile = os.path.join(folder, 'Scan.ini')
59 if not os.path.exists(sfile):
60 raise IOError('No configuration file found')
62 cp = ConfigParser()
63 cp.read(sfile)
64 timestamp = os.stat(sfile).st_mtime
65 scan = {'timestamp': timestamp}
66 for key in cp.sections():
67 scan[key] = {}
68 for attr in cp.options(key):
69 scan[key][attr] = cp.get(key, attr)
71 # return scan, general, timestamp
72 return scan
74def readROIFile(hfile,xrd=False):
76 cp = ConfigParser()
77 cp.read(hfile)
78 output = []
80 if xrd:
81 for a in cp.options('xrd1d'):
82 if a.lower().startswith('roi'):
83 iroi = int(a[3:])
84 name,unit,dat = cp.get('xrd1d',a).split('|')
85 lims = [float(i) for i in dat.split()]
86 dat = [lims[0], lims[1]]
87 output.append((iroi, name.strip(), unit.strip(), dat))
88 return sorted(output)
90 else:
91 for a in cp.options('rois'):
92 if a.lower().startswith('roi'):
93 iroi = int(a[3:])
94 name, dat = cp.get('rois',a).split('|')
95 lims = [int(i) for i in dat.split()]
96 ndet = int(len(lims)/2)
97 dat = []
98 for i in range(ndet):
99 dat.append((lims[i*2], lims[i*2+1]))
100 output.append((iroi, name.strip(), dat))
101 roidata = sorted(output)
103 calib = {}
105 caldat = cp.options('calibration')
106 for attr in ('offset', 'slope', 'quad'):
107 calib[attr] = [float(x) for x in cp.get('calibration', attr).split()]
108 extra = {}
109 ndet = len(calib['offset'])
110 file_sections = cp.sections()
111 for section in ('dxp', 'extra'):
112 if section not in file_sections:
113 continue
114 for attr in cp.options(section):
115 tmpdat = [x for x in cp.get(section, attr).split()]
116 if len(tmpdat) == 2*ndet:
117 tmpdat = ['%s %s' % (i, j) for i, j in zip(tmpdat[::2], tmpdat[1::2])]
118 try:
119 extra[attr] = [int(x) for x in tmpdat]
120 except ValueError:
121 try:
122 extra[attr] = [float(x) for x in tmpdat]
123 except ValueError:
124 extra[attr] = tmpdat
127 return roidata, calib, extra