optical_constants.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # -*- coding: utf-8 -*-
  2. # File from Smuthi project https://gitlab.com/AmosEgel/smuthi/-/blob/master/smuthi/utility/optical_constants.py
  3. """Provide functionality to read optical constants in format provided by `refractiveindex.info <https://refractiveindex.info/>`_ website"""
  4. from scipy.interpolate import interp1d
  5. import numpy as np
  6. import yaml
  7. def read_refractive_index_from_yaml(filename, vacuum_wavelength, units="mkm", kind=1):
  8. """Read optical constants in format provided by refractiveindex.info website.
  9. Args:
  10. filename (str): path and file name for yaml data
  11. downloaded from refractiveindex.info
  12. vacuum_wavelength (float or np.array): wavelengths where refractive
  13. index data is needed
  14. units (str): units for wavelength. currently, microns ('mkm' or 'um')
  15. and nanometers ('nm') can be selected
  16. kind (int): order of interpolation
  17. Returns:
  18. A pair (or np.array of pairs) of wavelength and
  19. corresponding refractive index (complex)
  20. """
  21. if units == "nm":
  22. factor = 1000
  23. elif units in ("mkm", "um"):
  24. factor = 1
  25. else:
  26. raise NotImplementedError("Converting wavelength into '"+units
  27. +"' units for refractive index data"
  28. +" was not implemented.")
  29. with open(filename) as f:
  30. the_file = yaml.load(f)['DATA'][0]
  31. data_type = the_file['type']
  32. if data_type != 'tabulated nk':
  33. raise NotImplementedError("Input data type '"+data_type
  34. +"' available in file "+filename
  35. +" was not implemented.")
  36. data = the_file['data'].splitlines()
  37. data_split = []
  38. for wl in data:
  39. data_split.append(wl.split())
  40. data_num = []
  41. for wl in data_split:
  42. record = []
  43. for val in wl:
  44. record.append(float(val))
  45. data_num.append(record)
  46. data_np = np.array(data_num)
  47. data_wl = data_np[:,0]*factor
  48. index_re = data_np[:,1]
  49. index_im = data_np[:,2]
  50. f_re = interp1d(data_wl, index_re, kind=kind)
  51. f_im = interp1d(data_wl, index_im, kind=kind)
  52. data_out = np.transpose(np.vstack((vacuum_wavelength, f_re(vacuum_wavelength)+f_im(vacuum_wavelength)*1j)))
  53. if len(data_out) == 1:
  54. return data_out[0]
  55. return data_out