Coverage for larch/xrmmap/asciifiles.py: 10%

110 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2024-10-16 21:04 +0000

1""" 

2utilities for reading files from raw scan folder 

3""" 

4import os 

5import sys 

6import numpy 

7from configparser import ConfigParser 

8 

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 

28 

29def readMasterFile(fname): 

30 return readASCII(fname, nskip=0, isnumeric=False) 

31 

32def readEnvironFile(fname): 

33 h, d = readASCII(fname, nskip=0, isnumeric=False) 

34 return h 

35 

36def read1DXRDFile(fname,metadata=True): 

37 return readASCII(fname, nskip=0, isnumeric=True) 

38 

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 

56 

57def readScanConfig(folder): 

58 sfile = Path(folder, 'Scan.ini') 

59 text = None 

60 if sfile.exists(): 

61 with open(sfile, 'r') as fh: 

62 text = fh.read() 

63 if text is None: 

64 raise IOError('No configuration file found: ', sfile.as_posix()) 

65 

66 cp = ConfigParser() 

67 cp.read_string(text) 

68 timestamp = os.stat(sfile).st_mtime 

69 scan = {'timestamp': timestamp} 

70 for key in cp.sections(): 

71 scan[key] = {} 

72 for attr in cp.options(key): 

73 scan[key][attr] = cp.get(key, attr) 

74 

75 # return scan, general, timestamp 

76 return scan 

77 

78def readROIFile(hfile, xrd=False): 

79 with open(hfile, 'r') as fh: 

80 text = fh.read() 

81 cp = ConfigParser() 

82 cp.read_string(text) 

83 output = [] 

84 

85 if xrd: 

86 for a in cp.options('xrd1d'): 

87 if a.lower().startswith('roi'): 

88 iroi = int(a[3:]) 

89 name,unit,dat = cp.get('xrd1d',a).split('|') 

90 lims = [float(i) for i in dat.split()] 

91 dat = [lims[0], lims[1]] 

92 output.append((iroi, name.strip(), unit.strip(), dat)) 

93 return sorted(output) 

94 

95 else: 

96 for a in cp.options('rois'): 

97 if a.lower().startswith('roi'): 

98 iroi = int(a[3:]) 

99 name, dat = cp.get('rois',a).split('|') 

100 lims = [int(i) for i in dat.split()] 

101 ndet = int(len(lims)/2) 

102 dat = [] 

103 for i in range(ndet): 

104 dat.append((lims[i*2], lims[i*2+1])) 

105 output.append((iroi, name.strip(), dat)) 

106 roidata = sorted(output) 

107 

108 calib = {} 

109 

110 caldat = cp.options('calibration') 

111 for attr in ('offset', 'slope', 'quad'): 

112 calib[attr] = [float(x) for x in cp.get('calibration', attr).split()] 

113 extra = {} 

114 ndet = len(calib['offset']) 

115 file_sections = cp.sections() 

116 for section in ('dxp', 'extra'): 

117 if section not in file_sections: 

118 continue 

119 for attr in cp.options(section): 

120 tmpdat = [x for x in cp.get(section, attr).split()] 

121 if len(tmpdat) == 2*ndet: 

122 tmpdat = ['%s %s' % (i, j) for i, j in zip(tmpdat[::2], tmpdat[1::2])] 

123 try: 

124 extra[attr] = [int(x) for x in tmpdat] 

125 except ValueError: 

126 try: 

127 extra[attr] = [float(x) for x in tmpdat] 

128 except ValueError: 

129 extra[attr] = tmpdat 

130 

131 

132 return roidata, calib, extra