Coverage for larch/xrd/xrd_files.py: 14%

21 statements  

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

1#!/usr/bin/env python 

2''' 

3Diffraction functions require for fitting and analyzing data. 

4 

5mkak 2017.02.06 (originally written spring 2016) 

6''' 

7 

8########################################################################## 

9# IMPORT PYTHON PACKAGES 

10 

11import numpy as np 

12 

13########################################################################## 

14# FUNCTIONS 

15 

16def xy_file_reader(xyfile): 

17 ''' 

18 Parses (x,y) data from xy text file. 

19 

20 options: 

21 char - chararacter separating columns in data file (e.g. ',') 

22 ''' 

23 

24 units = None 

25 x, y = [], [] 

26 with open(xyfile) as f: 

27 for line in f.readlines(): 

28 if '#' not in line: 

29 fields = line.split() 

30 x += [float(fields[0])] 

31 y += [float(fields[1])] 

32 else: 

33 for opt in ['2th_deg','q_A^-1']: 

34 if opt in line: units = opt 

35 

36 return np.array(x),np.array(y),units 

37 

38########################################################################## 

39def xy_file_writer(a,b,filename,char=None): 

40 

41 if char: 

42 str = '%s' + char + '%s\n' 

43 else: 

44 str = '%s %s\n' 

45 

46 with open(filename,'w') as f: 

47 for i,ai in a: 

48 f.write(str % (ai,b[i])) 

49 

50 return()