read_raw_data.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Aug 13 18:01:27 2018
  4. @author: USER
  5. """
  6. # Codes are free to use. Do whatever you want
  7. from __future__ import absolute_import
  8. """Read raw data"""
  9. ####################### LIBRARY #############################
  10. # exceptions library
  11. from exceptions import (Illegal_Filename_Exception,
  12. Data_Format_Exception)
  13. # Python stdlib imports
  14. import datetime
  15. import re
  16. import os
  17. # data processing library
  18. import numpy as np
  19. import pandas as pd
  20. # pyrod library
  21. ####################### CONSTANT ############################
  22. # constant
  23. #regex of raw_data, parameters, sheet names
  24. RAW_DATA_DAT = re.compile('^\d\dL\.dat$')
  25. RAW_DATA_XLSX = re.compile('^\d\dL\.xlsx$')
  26. ####################### FUNCTIONS ###########################
  27. ######################## CLASSS #############################
  28. class initialization_rhkl(object):
  29. def __init__(self,
  30. raw_data = '00L.xlsx'):
  31. self.raw_data = raw_data
  32. self.path = os.path.abspath(os.path.dirname(raw_data)) + raw_data
  33. # check raw_data
  34. self.check_raw_data_dat = 1
  35. self.check_raw_data_xlsx = 1
  36. # check file names
  37. # raw_data and parameters
  38. def _check_filename(self):
  39. """legal names: xxL.xlsx or xxL.dat; parameters_xxx--.xlsx"""
  40. # check raw data, .dat or xlsx
  41. self.check_raw_data_dat = RAW_DATA_DAT.match(self.raw_data)
  42. self.check_raw_data_xlsx = RAW_DATA_XLSX.match(self.raw_data)
  43. if not (self.check_raw_data_dat or self.check_raw_data_xlsx):
  44. error = 'raw data name is illegal.appropriate file name: xxL.dat or xxL.xlsx'
  45. raise Illegal_Filename_Exception(error)
  46. def _read_rhkl(self, density = 100):
  47. """Read origin data.default reconstruct data density is 100"""
  48. try:
  49. # if data is origin data form from APS .dat
  50. if 'dat' in self.raw_data:
  51. # open od-origin data
  52. raw_data = open(self.path)
  53. qz = []
  54. ie = []
  55. # read in line form
  56. for line in raw_data:
  57. lst = line.split()
  58. # float data
  59. qz.append(float(lst[0]))
  60. ie.append(float(lst[1]))
  61. # if data is xlsx, maybe modulated
  62. elif 'xlsx' in self.raw_data:
  63. # read excel data as matrix
  64. rd = pd.read_excel(self.path).as_matrix()
  65. qz = rd[:,0].tolist()
  66. ie = rd[:,1].tolist()
  67. except Data_Format_Exception:
  68. print('Data format of raw data is illegal')
  69. # interpolant data density. ensure the integrality of bragg peak
  70. # qs--q start
  71. qs = qz[ 0]
  72. # qe--q end
  73. qe = qz[-1]
  74. # interpolant data with default intensity 100
  75. iq0 = 0.0
  76. iqs = round(qs*100)/100
  77. iqe = round(qe*100)/100
  78. iq= np.linspace(iq0,
  79. iqe,
  80. (iqe-iq0)/0.01+1)
  81. intensity = np.interp(iq,qz,ie)
  82. # the signal between iq0 and iqs is not detected
  83. intensity[0: int(iqs/0.01)] = 0
  84. return iq, intensity